blob: 0823e0a71c1c3bebca2c9565cfe28ebeffd85bae [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000032using namespace llvm;
33
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NodesCombined , "Number of dag nodes combined");
35STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
36STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
37
Nate Begeman1d4d4142005-09-01 00:19:25 +000038namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000039#ifndef NDEBUG
40 static cl::opt<bool>
41 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
42 cl::desc("Pop up a window to show dags before the first "
43 "dag combine pass"));
44 static cl::opt<bool>
45 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
46 cl::desc("Pop up a window to show dags before the second "
47 "dag combine pass"));
48#else
49 static const bool ViewDAGCombine1 = false;
50 static const bool ViewDAGCombine2 = false;
51#endif
52
Jim Laskey71382342006-10-07 23:37:56 +000053 static cl::opt<bool>
54 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000055 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000056
Jim Laskey07a27092006-10-18 19:08:31 +000057 static cl::opt<bool>
58 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
59 cl::desc("Include global information in alias analysis"));
60
Jim Laskeybc588b82006-10-05 15:07:25 +000061//------------------------------ DAGCombiner ---------------------------------//
62
Jim Laskey71382342006-10-07 23:37:56 +000063 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
Jim Laskeyc7c3f112006-10-16 20:52:31 +000071 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 }
83
Dan Gohman389079b2007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
86 SDOperand visit(SDNode *N);
87
Chris Lattner24664722006-03-01 04:53:38 +000088 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000089 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000091 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000092 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000093 WorkList.push_back(N);
94 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000095
Chris Lattnerf8dc0612008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
104 bool AddTo = true);
105
Jim Laskey274062c2006-10-13 23:32:28 +0000106 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
107 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000108 }
109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
111 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000112 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000113 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000114 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000115
Chris Lattner24664722006-03-01 04:53:38 +0000116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000121 bool SimplifyDemandedBits(SDOperand Op, uint64_t Demanded = ~0ULL);
Chris Lattner87514ca2005-10-10 22:31:19 +0000122
Chris Lattner448f2192006-11-11 00:39:41 +0000123 bool CombineToPreIndexedLoadStore(SDNode *N);
124 bool CombineToPostIndexedLoadStore(SDNode *N);
125
126
Dan Gohman389079b2007-10-08 17:57:15 +0000127 /// combine - call the node-specific routine that knows how to fold each
128 /// particular type of node. If that doesn't do anything, try the
129 /// target-specific DAG combines.
130 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000131
132 // Visitation implementation - Implement dag node combining for different
133 // node types. The semantics are as follows:
134 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000135 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000136 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000137 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000138 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000139 SDOperand visitTokenFactor(SDNode *N);
140 SDOperand visitADD(SDNode *N);
141 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000142 SDOperand visitADDC(SDNode *N);
143 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000144 SDOperand visitMUL(SDNode *N);
145 SDOperand visitSDIV(SDNode *N);
146 SDOperand visitUDIV(SDNode *N);
147 SDOperand visitSREM(SDNode *N);
148 SDOperand visitUREM(SDNode *N);
149 SDOperand visitMULHU(SDNode *N);
150 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000151 SDOperand visitSMUL_LOHI(SDNode *N);
152 SDOperand visitUMUL_LOHI(SDNode *N);
153 SDOperand visitSDIVREM(SDNode *N);
154 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000155 SDOperand visitAND(SDNode *N);
156 SDOperand visitOR(SDNode *N);
157 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000158 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000159 SDOperand visitSHL(SDNode *N);
160 SDOperand visitSRA(SDNode *N);
161 SDOperand visitSRL(SDNode *N);
162 SDOperand visitCTLZ(SDNode *N);
163 SDOperand visitCTTZ(SDNode *N);
164 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000165 SDOperand visitSELECT(SDNode *N);
166 SDOperand visitSELECT_CC(SDNode *N);
167 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000168 SDOperand visitSIGN_EXTEND(SDNode *N);
169 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000170 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000171 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
172 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000173 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000174 SDOperand visitFADD(SDNode *N);
175 SDOperand visitFSUB(SDNode *N);
176 SDOperand visitFMUL(SDNode *N);
177 SDOperand visitFDIV(SDNode *N);
178 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000179 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000180 SDOperand visitSINT_TO_FP(SDNode *N);
181 SDOperand visitUINT_TO_FP(SDNode *N);
182 SDOperand visitFP_TO_SINT(SDNode *N);
183 SDOperand visitFP_TO_UINT(SDNode *N);
184 SDOperand visitFP_ROUND(SDNode *N);
185 SDOperand visitFP_ROUND_INREG(SDNode *N);
186 SDOperand visitFP_EXTEND(SDNode *N);
187 SDOperand visitFNEG(SDNode *N);
188 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000189 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000190 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000191 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000192 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000193 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000194 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000195 SDOperand visitBUILD_VECTOR(SDNode *N);
196 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000197 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000198
Evan Cheng44f1f092006-04-20 08:56:16 +0000199 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000200 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
201
Chris Lattnere70da202007-12-06 07:33:36 +0000202 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
203
Chris Lattner40c62d52005-10-18 06:04:22 +0000204 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000205 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000206 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
207 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000208 SDOperand N3, ISD::CondCode CC,
209 bool NotExtCompare = false);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000210 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000211 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000212 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
213 unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000214 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000215 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000216 SDOperand BuildUDIV(SDNode *N);
217 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000218 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000219
Chris Lattner2b4c2792007-10-13 06:35:54 +0000220 SDOperand GetDemandedBits(SDOperand V, uint64_t Mask);
221
Jim Laskey6ff23e52006-10-04 16:53:27 +0000222 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
223 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000224 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000225 SmallVector<SDOperand, 8> &Aliases);
226
Jim Laskey096c22e2006-10-18 12:29:57 +0000227 /// isAlias - Return true if there is any possibility that the two addresses
228 /// overlap.
229 bool isAlias(SDOperand Ptr1, int64_t Size1,
230 const Value *SrcValue1, int SrcValueOffset1,
231 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000232 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000233
Jim Laskey7ca56af2006-10-11 13:47:09 +0000234 /// FindAliasInfo - Extracts the relevant alias information from the memory
235 /// node. Returns true if the operand was a load.
236 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000237 SDOperand &Ptr, int64_t &Size,
238 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000239
Jim Laskey279f0532006-09-25 16:29:54 +0000240 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000241 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000242 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
243
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000245 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
246 : DAG(D),
247 TLI(D.getTargetLoweringInfo()),
248 AfterLegalize(false),
249 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000250
251 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000252 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000253 };
254}
255
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000256
257namespace {
258/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
259/// nodes from the worklist.
260class VISIBILITY_HIDDEN WorkListRemover :
261 public SelectionDAG::DAGUpdateListener {
262 DAGCombiner &DC;
263public:
264 WorkListRemover(DAGCombiner &dc) : DC(dc) {}
265
266 virtual void NodeDeleted(SDNode *N) {
267 printf("remove from WL: %p\n", (void*)N);
268 DC.removeFromWorkList(N);
269 }
270
271 virtual void NodeUpdated(SDNode *N) {
272 // Ignore updates.
273 }
274};
275}
276
Chris Lattner24664722006-03-01 04:53:38 +0000277//===----------------------------------------------------------------------===//
278// TargetLowering::DAGCombinerInfo implementation
279//===----------------------------------------------------------------------===//
280
281void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
282 ((DAGCombiner*)DC)->AddToWorkList(N);
283}
284
285SDOperand TargetLowering::DAGCombinerInfo::
286CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000287 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000288}
289
290SDOperand TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDOperand Res) {
292 return ((DAGCombiner*)DC)->CombineTo(N, Res);
293}
294
295
296SDOperand TargetLowering::DAGCombinerInfo::
297CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
298 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
299}
300
301
Chris Lattner24664722006-03-01 04:53:38 +0000302//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000303// Helper Functions
304//===----------------------------------------------------------------------===//
305
306/// isNegatibleForFree - Return 1 if we can compute the negated form of the
307/// specified expression for the same cost as the expression itself, or 2 if we
308/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000309static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000310 // No compile time optimizations on this type.
311 if (Op.getValueType() == MVT::ppcf128)
312 return 0;
313
Chris Lattner29446522007-05-14 22:04:50 +0000314 // fneg is removable even if it has multiple uses.
315 if (Op.getOpcode() == ISD::FNEG) return 2;
316
317 // Don't allow anything with multiple uses.
318 if (!Op.hasOneUse()) return 0;
319
Chris Lattner3adf9512007-05-25 02:19:06 +0000320 // Don't recurse exponentially.
321 if (Depth > 6) return 0;
322
Chris Lattner29446522007-05-14 22:04:50 +0000323 switch (Op.getOpcode()) {
324 default: return false;
325 case ISD::ConstantFP:
326 return 1;
327 case ISD::FADD:
328 // FIXME: determine better conditions for this xform.
329 if (!UnsafeFPMath) return 0;
330
331 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000332 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000333 return V;
334 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000335 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000336 case ISD::FSUB:
337 // We can't turn -(A-B) into B-A when we honor signed zeros.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A-B) -> B-A
341 return 1;
342
343 case ISD::FMUL:
344 case ISD::FDIV:
345 if (HonorSignDependentRoundingFPMath()) return 0;
346
347 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000348 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000349 return V;
350
Chris Lattner501fee72007-05-23 07:35:22 +0000351 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000352
353 case ISD::FP_EXTEND:
354 case ISD::FP_ROUND:
355 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000356 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000357 }
358}
359
360/// GetNegatedExpression - If isNegatibleForFree returns true, this function
361/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000362static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
363 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000364 // fneg is removable even if it has multiple uses.
365 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
366
367 // Don't allow anything with multiple uses.
368 assert(Op.hasOneUse() && "Unknown reuse!");
369
Chris Lattner3adf9512007-05-25 02:19:06 +0000370 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000371 switch (Op.getOpcode()) {
372 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000373 case ISD::ConstantFP: {
374 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
375 V.changeSign();
376 return DAG.getConstantFP(V, Op.getValueType());
377 }
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FADD:
379 // FIXME: determine better conditions for this xform.
380 assert(UnsafeFPMath);
381
382 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000383 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000384 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000385 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000386 Op.getOperand(1));
387 // -(A+B) -> -B - A
388 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000389 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000390 Op.getOperand(0));
391 case ISD::FSUB:
392 // We can't turn -(A-B) into B-A when we honor signed zeros.
393 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000394
395 // -(0-B) -> B
396 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000397 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000398 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000399
400 // -(A-B) -> B-A
401 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
402 Op.getOperand(0));
403
404 case ISD::FMUL:
405 case ISD::FDIV:
406 assert(!HonorSignDependentRoundingFPMath());
407
408 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000409 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000410 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000411 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
413
414 // -(X*Y) -> X * -Y
415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
416 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000417 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000418
419 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000420 case ISD::FSIN:
421 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000422 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000423 case ISD::FP_ROUND:
424 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
425 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
426 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000427 }
428}
Chris Lattner24664722006-03-01 04:53:38 +0000429
430
Nate Begeman4ebd8052005-09-01 23:24:04 +0000431// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
432// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000433// Also, set the incoming LHS, RHS, and CC references to the appropriate
434// nodes based on the type of node we are checking. This simplifies life a
435// bit for the callers.
436static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
437 SDOperand &CC) {
438 if (N.getOpcode() == ISD::SETCC) {
439 LHS = N.getOperand(0);
440 RHS = N.getOperand(1);
441 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000442 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000443 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444 if (N.getOpcode() == ISD::SELECT_CC &&
445 N.getOperand(2).getOpcode() == ISD::Constant &&
446 N.getOperand(3).getOpcode() == ISD::Constant &&
447 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
449 LHS = N.getOperand(0);
450 RHS = N.getOperand(1);
451 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000452 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454 return false;
455}
456
Nate Begeman99801192005-09-07 23:25:52 +0000457// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
458// one use. If this is true, it allows the users to invert the operation for
459// free when it is profitable to do so.
460static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000463 return true;
464 return false;
465}
466
Nate Begemancd4d58c2006-02-03 06:46:56 +0000467SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
468 MVT::ValueType VT = N0.getValueType();
469 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
470 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
471 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
472 if (isa<ConstantSDNode>(N1)) {
473 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000474 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000475 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
476 } else if (N0.hasOneUse()) {
477 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000478 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000479 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
480 }
481 }
482 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
483 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
484 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
485 if (isa<ConstantSDNode>(N0)) {
486 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000487 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000488 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
489 } else if (N1.hasOneUse()) {
490 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000491 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
493 }
494 }
495 return SDOperand();
496}
497
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000498SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
499 bool AddTo) {
500 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
501 ++NodesCombined;
502 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
503 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
504 DOUT << " and " << NumTo-1 << " other values\n";
505 WorkListRemover DeadNodes(*this);
506 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
507
508 if (AddTo) {
509 // Push the new nodes and any users onto the worklist
510 for (unsigned i = 0, e = NumTo; i != e; ++i) {
511 AddToWorkList(To[i].Val);
512 AddUsersToWorkList(To[i].Val);
513 }
514 }
515
516 // Nodes can be reintroduced into the worklist. Make sure we do not
517 // process a node that has been replaced.
518 removeFromWorkList(N);
519
520 // Finally, since the node is now dead, remove it from the graph.
521 DAG.DeleteNode(N);
522 return SDOperand(N, 0);
523}
524
525/// SimplifyDemandedBits - Check the specified integer node value to see if
526/// it can be simplified or if things it uses can be simplified by bit
527/// propagation. If so, return true.
528bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, uint64_t Demanded) {
529 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
530 uint64_t KnownZero, KnownOne;
531 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
532 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
533 return false;
534
535 // Revisit the node.
536 AddToWorkList(Op.Val);
537
538 // Replace the old value with the new one.
539 ++NodesCombined;
540 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
541 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
542 DOUT << '\n';
543
544 // Replace all uses. If any nodes become isomorphic to other nodes and
545 // are deleted, make sure to remove them from our worklist.
546 WorkListRemover DeadNodes(*this);
547 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
548
549 // Push the new node and any (possibly new) users onto the worklist.
550 AddToWorkList(TLO.New.Val);
551 AddUsersToWorkList(TLO.New.Val);
552
553 // Finally, if the node is now dead, remove it from the graph. The node
554 // may not be dead if the replacement process recursively simplified to
555 // something else needing this node.
556 if (TLO.Old.Val->use_empty()) {
557 removeFromWorkList(TLO.Old.Val);
558
559 // If the operands of this node are only used by the node, they will now
560 // be dead. Make sure to visit them first to delete dead nodes early.
561 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
562 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
563 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
564
565 DAG.DeleteNode(TLO.Old.Val);
566 }
567 return true;
568}
569
Chris Lattner29446522007-05-14 22:04:50 +0000570//===----------------------------------------------------------------------===//
571// Main DAG Combiner implementation
572//===----------------------------------------------------------------------===//
573
Nate Begeman4ebd8052005-09-01 23:24:04 +0000574void DAGCombiner::Run(bool RunningAfterLegalize) {
575 // set the instance variable, so that the various visit routines may use it.
576 AfterLegalize = RunningAfterLegalize;
577
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000579 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
580 E = DAG.allnodes_end(); I != E; ++I)
581 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582
Chris Lattner95038592005-10-05 06:35:28 +0000583 // Create a dummy node (which is not added to allnodes), that adds a reference
584 // to the root node, preventing it from being deleted, and tracking any
585 // changes of the root.
586 HandleSDNode Dummy(DAG.getRoot());
587
Jim Laskey26f7fa72006-10-17 19:33:52 +0000588 // The root of the dag may dangle to deleted nodes until the dag combiner is
589 // done. Set it to null to avoid confusion.
590 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000591
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 // while the worklist isn't empty, inspect the node on the end of it and
593 // try and combine it.
594 while (!WorkList.empty()) {
595 SDNode *N = WorkList.back();
596 WorkList.pop_back();
597
598 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000599 // N is deleted from the DAG, since they too may now be dead or may have a
600 // reduced number of uses, allowing other xforms.
601 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000603 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604
Chris Lattner95038592005-10-05 06:35:28 +0000605 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 continue;
607 }
608
Dan Gohman389079b2007-10-08 17:57:15 +0000609 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000610
Chris Lattner50d8e492008-01-25 23:34:24 +0000611 if (RV.Val == 0)
612 continue;
613
614 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000615
Chris Lattner50d8e492008-01-25 23:34:24 +0000616 // If we get back the same node we passed in, rather than a new node or
617 // zero, we know that the node must have defined multiple values and
618 // CombineTo was used. Since CombineTo takes care of the worklist
619 // mechanics for us, we have no work to do in this case.
620 if (RV.Val == N)
621 continue;
622
623 assert(N->getOpcode() != ISD::DELETED_NODE &&
624 RV.Val->getOpcode() != ISD::DELETED_NODE &&
625 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000626
Chris Lattner50d8e492008-01-25 23:34:24 +0000627 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
628 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
629 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000630 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000631 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000632 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000633 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000634 assert(N->getValueType(0) == RV.getValueType() &&
635 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000636 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000637 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000639
640 // Push the new node and any users onto the worklist
641 AddToWorkList(RV.Val);
642 AddUsersToWorkList(RV.Val);
643
644 // Add any uses of the old node to the worklist in case this node is the
645 // last one that uses them. They may become dead after this node is
646 // deleted.
647 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
648 AddToWorkList(N->getOperand(i).Val);
649
650 // Nodes can be reintroduced into the worklist. Make sure we do not
651 // process a node that has been replaced.
652 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000653
654 // Finally, since the node is now dead, remove it from the graph.
655 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000656 }
Chris Lattner95038592005-10-05 06:35:28 +0000657
658 // If the root changed (e.g. it was a dead load, update the root).
659 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000660}
661
Nate Begeman83e75ec2005-09-06 04:43:02 +0000662SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 switch(N->getOpcode()) {
664 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000665 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000666 case ISD::ADD: return visitADD(N);
667 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000668 case ISD::ADDC: return visitADDC(N);
669 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 case ISD::MUL: return visitMUL(N);
671 case ISD::SDIV: return visitSDIV(N);
672 case ISD::UDIV: return visitUDIV(N);
673 case ISD::SREM: return visitSREM(N);
674 case ISD::UREM: return visitUREM(N);
675 case ISD::MULHU: return visitMULHU(N);
676 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000677 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
678 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
679 case ISD::SDIVREM: return visitSDIVREM(N);
680 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000681 case ISD::AND: return visitAND(N);
682 case ISD::OR: return visitOR(N);
683 case ISD::XOR: return visitXOR(N);
684 case ISD::SHL: return visitSHL(N);
685 case ISD::SRA: return visitSRA(N);
686 case ISD::SRL: return visitSRL(N);
687 case ISD::CTLZ: return visitCTLZ(N);
688 case ISD::CTTZ: return visitCTTZ(N);
689 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000690 case ISD::SELECT: return visitSELECT(N);
691 case ISD::SELECT_CC: return visitSELECT_CC(N);
692 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
694 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000695 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000696 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
697 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000698 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000699 case ISD::FADD: return visitFADD(N);
700 case ISD::FSUB: return visitFSUB(N);
701 case ISD::FMUL: return visitFMUL(N);
702 case ISD::FDIV: return visitFDIV(N);
703 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000704 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
706 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
707 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
708 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
709 case ISD::FP_ROUND: return visitFP_ROUND(N);
710 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
711 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
712 case ISD::FNEG: return visitFNEG(N);
713 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000714 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000715 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000716 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000717 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000718 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000719 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000720 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
721 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000722 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000723 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000725}
726
Dan Gohman389079b2007-10-08 17:57:15 +0000727SDOperand DAGCombiner::combine(SDNode *N) {
728
729 SDOperand RV = visit(N);
730
731 // If nothing happened, try a target-specific DAG combine.
732 if (RV.Val == 0) {
733 assert(N->getOpcode() != ISD::DELETED_NODE &&
734 "Node was deleted but visit returned NULL!");
735
736 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
737 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
738
739 // Expose the DAG combiner to the target combiner impls.
740 TargetLowering::DAGCombinerInfo
741 DagCombineInfo(DAG, !AfterLegalize, false, this);
742
743 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
744 }
745 }
746
747 return RV;
748}
749
Chris Lattner6270f682006-10-08 22:57:01 +0000750/// getInputChainForNode - Given a node, return its input chain if it has one,
751/// otherwise return a null sd operand.
752static SDOperand getInputChainForNode(SDNode *N) {
753 if (unsigned NumOps = N->getNumOperands()) {
754 if (N->getOperand(0).getValueType() == MVT::Other)
755 return N->getOperand(0);
756 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
757 return N->getOperand(NumOps-1);
758 for (unsigned i = 1; i < NumOps-1; ++i)
759 if (N->getOperand(i).getValueType() == MVT::Other)
760 return N->getOperand(i);
761 }
762 return SDOperand(0, 0);
763}
764
Nate Begeman83e75ec2005-09-06 04:43:02 +0000765SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000766 // If N has two operands, where one has an input chain equal to the other,
767 // the 'other' chain is redundant.
768 if (N->getNumOperands() == 2) {
769 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
770 return N->getOperand(0);
771 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
772 return N->getOperand(1);
773 }
774
Chris Lattnerc76d4412007-05-16 06:37:59 +0000775 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
776 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
777 SmallPtrSet<SDNode*, 16> SeenOps;
778 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000779
780 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000781 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000782
Jim Laskey71382342006-10-07 23:37:56 +0000783 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000784 // encountered.
785 for (unsigned i = 0; i < TFs.size(); ++i) {
786 SDNode *TF = TFs[i];
787
Jim Laskey6ff23e52006-10-04 16:53:27 +0000788 // Check each of the operands.
789 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
790 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000791
Jim Laskey6ff23e52006-10-04 16:53:27 +0000792 switch (Op.getOpcode()) {
793 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000794 // Entry tokens don't need to be added to the list. They are
795 // rededundant.
796 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000797 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000798
Jim Laskey6ff23e52006-10-04 16:53:27 +0000799 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000800 if ((CombinerAA || Op.hasOneUse()) &&
801 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000802 // Queue up for processing.
803 TFs.push_back(Op.Val);
804 // Clean up in case the token factor is removed.
805 AddToWorkList(Op.Val);
806 Changed = true;
807 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000808 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000809 // Fall thru
810
811 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000812 // Only add if it isn't already in the list.
813 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000814 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000815 else
816 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000817 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000818 }
819 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000820 }
821
822 SDOperand Result;
823
824 // If we've change things around then replace token factor.
825 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000826 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 // The entry token is the only possible outcome.
828 Result = DAG.getEntryNode();
829 } else {
830 // New and improved token factor.
831 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000832 }
Jim Laskey274062c2006-10-13 23:32:28 +0000833
834 // Don't add users to work list.
835 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000836 }
Jim Laskey279f0532006-09-25 16:29:54 +0000837
Jim Laskey6ff23e52006-10-04 16:53:27 +0000838 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839}
840
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000841static
842SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
843 MVT::ValueType VT = N0.getValueType();
844 SDOperand N00 = N0.getOperand(0);
845 SDOperand N01 = N0.getOperand(1);
846 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
847 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
848 isa<ConstantSDNode>(N00.getOperand(1))) {
849 N0 = DAG.getNode(ISD::ADD, VT,
850 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
851 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
852 return DAG.getNode(ISD::ADD, VT, N0, N1);
853 }
854 return SDOperand();
855}
856
Evan Chengb13cdbd2007-06-21 07:39:16 +0000857static
858SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
859 SelectionDAG &DAG) {
860 MVT::ValueType VT = N->getValueType(0);
861 unsigned Opc = N->getOpcode();
862 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
863 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
864 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
865 ISD::CondCode CC = ISD::SETCC_INVALID;
866 if (isSlctCC)
867 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
868 else {
869 SDOperand CCOp = Slct.getOperand(0);
870 if (CCOp.getOpcode() == ISD::SETCC)
871 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
872 }
873
874 bool DoXform = false;
875 bool InvCC = false;
876 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
877 "Bad input!");
878 if (LHS.getOpcode() == ISD::Constant &&
879 cast<ConstantSDNode>(LHS)->isNullValue())
880 DoXform = true;
881 else if (CC != ISD::SETCC_INVALID &&
882 RHS.getOpcode() == ISD::Constant &&
883 cast<ConstantSDNode>(RHS)->isNullValue()) {
884 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000885 SDOperand Op0 = Slct.getOperand(0);
886 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
887 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000888 CC = ISD::getSetCCInverse(CC, isInt);
889 DoXform = true;
890 InvCC = true;
891 }
892
893 if (DoXform) {
894 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
895 if (isSlctCC)
896 return DAG.getSelectCC(OtherOp, Result,
897 Slct.getOperand(0), Slct.getOperand(1), CC);
898 SDOperand CCOp = Slct.getOperand(0);
899 if (InvCC)
900 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
901 CCOp.getOperand(1), CC);
902 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
903 }
904 return SDOperand();
905}
906
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908 SDOperand N0 = N->getOperand(0);
909 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000910 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
911 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000912 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000913
914 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000915 if (MVT::isVector(VT)) {
916 SDOperand FoldedVOp = SimplifyVBinOp(N);
917 if (FoldedVOp.Val) return FoldedVOp;
918 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919
Dan Gohman613e0d82007-07-03 14:03:57 +0000920 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000921 if (N0.getOpcode() == ISD::UNDEF)
922 return N0;
923 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000924 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000927 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000928 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000929 if (N0C && !N1C)
930 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000934 // fold ((c1-A)+c2) -> (c1+c2)-A
935 if (N1C && N0.getOpcode() == ISD::SUB)
936 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
937 return DAG.getNode(ISD::SUB, VT,
938 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
939 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000940 // reassociate add
941 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
942 if (RADD.Val != 0)
943 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 // fold ((0-A) + B) -> B-A
945 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
946 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000947 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000948 // fold (A + (0-B)) -> A-B
949 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
950 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000951 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000952 // fold (A+(B-A)) -> B
953 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000955
Evan Cheng860771d2006-03-01 01:09:54 +0000956 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000957 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000958
959 // fold (a+b) -> (a|b) iff a and b share no bits.
960 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
961 uint64_t LHSZero, LHSOne;
962 uint64_t RHSZero, RHSOne;
963 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000964 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000965 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000966 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000967
968 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
969 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
970 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
971 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
972 return DAG.getNode(ISD::OR, VT, N0, N1);
973 }
974 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000975
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000976 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
977 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
978 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
979 if (Result.Val) return Result;
980 }
981 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
982 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
983 if (Result.Val) return Result;
984 }
985
Evan Chengb13cdbd2007-06-21 07:39:16 +0000986 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
987 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
988 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
989 if (Result.Val) return Result;
990 }
991 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
992 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
993 if (Result.Val) return Result;
994 }
995
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997}
998
Chris Lattner91153682007-03-04 20:03:15 +0000999SDOperand DAGCombiner::visitADDC(SDNode *N) {
1000 SDOperand N0 = N->getOperand(0);
1001 SDOperand N1 = N->getOperand(1);
1002 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1003 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1004 MVT::ValueType VT = N0.getValueType();
1005
1006 // If the flag result is dead, turn this into an ADD.
1007 if (N->hasNUsesOfValue(0, 1))
1008 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001009 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001010
1011 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001012 if (N0C && !N1C) {
1013 SDOperand Ops[] = { N1, N0 };
1014 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1015 }
Chris Lattner91153682007-03-04 20:03:15 +00001016
Chris Lattnerb6541762007-03-04 20:40:38 +00001017 // fold (addc x, 0) -> x + no carry out
1018 if (N1C && N1C->isNullValue())
1019 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1020
1021 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
1022 uint64_t LHSZero, LHSOne;
1023 uint64_t RHSZero, RHSOne;
1024 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001025 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001026 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001027 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001028
1029 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1030 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1031 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1032 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1033 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1034 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1035 }
Chris Lattner91153682007-03-04 20:03:15 +00001036
1037 return SDOperand();
1038}
1039
1040SDOperand DAGCombiner::visitADDE(SDNode *N) {
1041 SDOperand N0 = N->getOperand(0);
1042 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001043 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001044 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1045 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001046 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001047
1048 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001049 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001050 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001051 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1052 }
Chris Lattner91153682007-03-04 20:03:15 +00001053
Chris Lattnerb6541762007-03-04 20:40:38 +00001054 // fold (adde x, y, false) -> (addc x, y)
1055 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1056 SDOperand Ops[] = { N1, N0 };
1057 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1058 }
Chris Lattner91153682007-03-04 20:03:15 +00001059
1060 return SDOperand();
1061}
1062
1063
1064
Nate Begeman83e75ec2005-09-06 04:43:02 +00001065SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001066 SDOperand N0 = N->getOperand(0);
1067 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001068 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1069 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001070 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001071
Dan Gohman7f321562007-06-25 16:23:39 +00001072 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001073 if (MVT::isVector(VT)) {
1074 SDOperand FoldedVOp = SimplifyVBinOp(N);
1075 if (FoldedVOp.Val) return FoldedVOp;
1076 }
Dan Gohman7f321562007-06-25 16:23:39 +00001077
Chris Lattner854077d2005-10-17 01:07:11 +00001078 // fold (sub x, x) -> 0
1079 if (N0 == N1)
1080 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001082 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001083 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001084 // fold (sub x, c) -> (add x, -c)
1085 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001086 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001088 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001089 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001091 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001092 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001093 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1094 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1095 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1096 if (Result.Val) return Result;
1097 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001098 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001099 if (N0.getOpcode() == ISD::UNDEF)
1100 return N0;
1101 if (N1.getOpcode() == ISD::UNDEF)
1102 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001103
Nate Begeman83e75ec2005-09-06 04:43:02 +00001104 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105}
1106
Nate Begeman83e75ec2005-09-06 04:43:02 +00001107SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001108 SDOperand N0 = N->getOperand(0);
1109 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1111 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001112 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113
Dan Gohman7f321562007-06-25 16:23:39 +00001114 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001115 if (MVT::isVector(VT)) {
1116 SDOperand FoldedVOp = SimplifyVBinOp(N);
1117 if (FoldedVOp.Val) return FoldedVOp;
1118 }
Dan Gohman7f321562007-06-25 16:23:39 +00001119
Dan Gohman613e0d82007-07-03 14:03:57 +00001120 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001121 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001122 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001124 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001125 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001126 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001127 if (N0C && !N1C)
1128 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001131 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001133 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001134 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001137 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001139 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001140 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1141 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1142 // FIXME: If the input is something that is easily negated (e.g. a
1143 // single-use add), we should put the negate there.
1144 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1145 DAG.getNode(ISD::SHL, VT, N0,
1146 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1147 TLI.getShiftAmountTy())));
1148 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001149
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001150 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1151 if (N1C && N0.getOpcode() == ISD::SHL &&
1152 isa<ConstantSDNode>(N0.getOperand(1))) {
1153 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001154 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001155 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1156 }
1157
1158 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1159 // use.
1160 {
1161 SDOperand Sh(0,0), Y(0,0);
1162 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1163 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1164 N0.Val->hasOneUse()) {
1165 Sh = N0; Y = N1;
1166 } else if (N1.getOpcode() == ISD::SHL &&
1167 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1168 Sh = N1; Y = N0;
1169 }
1170 if (Sh.Val) {
1171 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1172 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1173 }
1174 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001175 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1176 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1177 isa<ConstantSDNode>(N0.getOperand(1))) {
1178 return DAG.getNode(ISD::ADD, VT,
1179 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1180 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1181 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001182
Nate Begemancd4d58c2006-02-03 06:46:56 +00001183 // reassociate mul
1184 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1185 if (RMUL.Val != 0)
1186 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001187
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189}
1190
Nate Begeman83e75ec2005-09-06 04:43:02 +00001191SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 SDOperand N0 = N->getOperand(0);
1193 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1195 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001196 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001197
Dan Gohman7f321562007-06-25 16:23:39 +00001198 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001199 if (MVT::isVector(VT)) {
1200 SDOperand FoldedVOp = SimplifyVBinOp(N);
1201 if (FoldedVOp.Val) return FoldedVOp;
1202 }
Dan Gohman7f321562007-06-25 16:23:39 +00001203
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001205 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001206 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001207 // fold (sdiv X, 1) -> X
1208 if (N1C && N1C->getSignExtended() == 1LL)
1209 return N0;
1210 // fold (sdiv X, -1) -> 0-X
1211 if (N1C && N1C->isAllOnesValue())
1212 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001213 // If we know the sign bits of both operands are zero, strength reduce to a
1214 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001215 if (!MVT::isVector(VT)) {
1216 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1217 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1218 DAG.MaskedValueIsZero(N0, SignBit))
1219 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1220 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001221 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001222 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001223 (isPowerOf2_64(N1C->getSignExtended()) ||
1224 isPowerOf2_64(-N1C->getSignExtended()))) {
1225 // If dividing by powers of two is cheap, then don't perform the following
1226 // fold.
1227 if (TLI.isPow2DivCheap())
1228 return SDOperand();
1229 int64_t pow2 = N1C->getSignExtended();
1230 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001231 unsigned lg2 = Log2_64(abs2);
1232 // Splat the sign bit into the register
1233 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001234 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1235 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001236 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001237 // Add (N0 < 0) ? abs2 - 1 : 0;
1238 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1239 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001240 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001241 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001242 AddToWorkList(SRL.Val);
1243 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001244 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1245 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001246 // If we're dividing by a positive value, we're done. Otherwise, we must
1247 // negate the result.
1248 if (pow2 > 0)
1249 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001250 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001251 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1252 }
Nate Begeman69575232005-10-20 02:15:44 +00001253 // if integer divide is expensive and we satisfy the requirements, emit an
1254 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001255 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001256 !TLI.isIntDivCheap()) {
1257 SDOperand Op = BuildSDIV(N);
1258 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001259 }
Dan Gohman7f321562007-06-25 16:23:39 +00001260
Dan Gohman613e0d82007-07-03 14:03:57 +00001261 // undef / X -> 0
1262 if (N0.getOpcode() == ISD::UNDEF)
1263 return DAG.getConstant(0, VT);
1264 // X / undef -> undef
1265 if (N1.getOpcode() == ISD::UNDEF)
1266 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001267
Nate Begeman83e75ec2005-09-06 04:43:02 +00001268 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269}
1270
Nate Begeman83e75ec2005-09-06 04:43:02 +00001271SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272 SDOperand N0 = N->getOperand(0);
1273 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001276 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277
Dan Gohman7f321562007-06-25 16:23:39 +00001278 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001279 if (MVT::isVector(VT)) {
1280 SDOperand FoldedVOp = SimplifyVBinOp(N);
1281 if (FoldedVOp.Val) return FoldedVOp;
1282 }
Dan Gohman7f321562007-06-25 16:23:39 +00001283
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001285 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001286 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001288 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001289 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001290 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001292 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1293 if (N1.getOpcode() == ISD::SHL) {
1294 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1295 if (isPowerOf2_64(SHC->getValue())) {
1296 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001297 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1298 DAG.getConstant(Log2_64(SHC->getValue()),
1299 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001300 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001301 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001302 }
1303 }
1304 }
Nate Begeman69575232005-10-20 02:15:44 +00001305 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001306 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1307 SDOperand Op = BuildUDIV(N);
1308 if (Op.Val) return Op;
1309 }
Dan Gohman7f321562007-06-25 16:23:39 +00001310
Dan Gohman613e0d82007-07-03 14:03:57 +00001311 // undef / X -> 0
1312 if (N0.getOpcode() == ISD::UNDEF)
1313 return DAG.getConstant(0, VT);
1314 // X / undef -> undef
1315 if (N1.getOpcode() == ISD::UNDEF)
1316 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001317
Nate Begeman83e75ec2005-09-06 04:43:02 +00001318 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319}
1320
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 SDOperand N0 = N->getOperand(0);
1323 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1325 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001326 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327
1328 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001329 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001330 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001331 // If we know the sign bits of both operands are zero, strength reduce to a
1332 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001333 if (!MVT::isVector(VT)) {
1334 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1335 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1336 DAG.MaskedValueIsZero(N0, SignBit))
1337 return DAG.getNode(ISD::UREM, VT, N0, N1);
1338 }
Chris Lattner26d29902006-10-12 20:58:32 +00001339
Dan Gohman77003042007-11-26 23:46:11 +00001340 // If X/C can be simplified by the division-by-constant logic, lower
1341 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001342 if (N1C && !N1C->isNullValue()) {
1343 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001344 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001345 SDOperand OptimizedDiv = combine(Div.Val);
1346 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1347 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1348 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1349 AddToWorkList(Mul.Val);
1350 return Sub;
1351 }
Chris Lattner26d29902006-10-12 20:58:32 +00001352 }
1353
Dan Gohman613e0d82007-07-03 14:03:57 +00001354 // undef % X -> 0
1355 if (N0.getOpcode() == ISD::UNDEF)
1356 return DAG.getConstant(0, VT);
1357 // X % undef -> undef
1358 if (N1.getOpcode() == ISD::UNDEF)
1359 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001360
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362}
1363
Nate Begeman83e75ec2005-09-06 04:43:02 +00001364SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365 SDOperand N0 = N->getOperand(0);
1366 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001367 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1368 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001369 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370
1371 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001373 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001374 // fold (urem x, pow2) -> (and x, pow2-1)
1375 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001376 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001377 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1378 if (N1.getOpcode() == ISD::SHL) {
1379 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1380 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001381 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001382 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001383 return DAG.getNode(ISD::AND, VT, N0, Add);
1384 }
1385 }
1386 }
Chris Lattner26d29902006-10-12 20:58:32 +00001387
Dan Gohman77003042007-11-26 23:46:11 +00001388 // If X/C can be simplified by the division-by-constant logic, lower
1389 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001390 if (N1C && !N1C->isNullValue()) {
1391 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001392 SDOperand OptimizedDiv = combine(Div.Val);
1393 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1394 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1395 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1396 AddToWorkList(Mul.Val);
1397 return Sub;
1398 }
Chris Lattner26d29902006-10-12 20:58:32 +00001399 }
1400
Dan Gohman613e0d82007-07-03 14:03:57 +00001401 // undef % X -> 0
1402 if (N0.getOpcode() == ISD::UNDEF)
1403 return DAG.getConstant(0, VT);
1404 // X % undef -> undef
1405 if (N1.getOpcode() == ISD::UNDEF)
1406 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001407
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409}
1410
Nate Begeman83e75ec2005-09-06 04:43:02 +00001411SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 SDOperand N0 = N->getOperand(0);
1413 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001415 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416
1417 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001419 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001421 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1423 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001424 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001425 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001426 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001427 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001428
Nate Begeman83e75ec2005-09-06 04:43:02 +00001429 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430}
1431
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433 SDOperand N0 = N->getOperand(0);
1434 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001435 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001436 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001437
1438 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001439 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001443 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001444 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001445 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001446 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001447
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449}
1450
Dan Gohman389079b2007-10-08 17:57:15 +00001451/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1452/// compute two values. LoOp and HiOp give the opcodes for the two computations
1453/// that are being performed. Return true if a simplification was made.
1454///
Chris Lattner5eee4272008-01-26 01:09:19 +00001455SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1456 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001457 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001458 bool HiExists = N->hasAnyUseOfValue(1);
1459 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001460 (!AfterLegalize ||
1461 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001462 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1463 N->getNumOperands());
1464 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001465 }
1466
1467 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001468 bool LoExists = N->hasAnyUseOfValue(0);
1469 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001470 (!AfterLegalize ||
1471 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001472 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1473 N->getNumOperands());
1474 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001475 }
1476
Evan Cheng44711942007-11-08 09:25:29 +00001477 // If both halves are used, return as it is.
1478 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001479 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001480
1481 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001482 if (LoExists) {
1483 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1484 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001485 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001486 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001487 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1488 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1489 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001490 }
1491
Evan Cheng44711942007-11-08 09:25:29 +00001492 if (HiExists) {
1493 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1494 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001495 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001496 SDOperand HiOpt = combine(Hi.Val);
1497 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001498 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1499 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001500 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001501 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001502}
1503
1504SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001505 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1506 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001507
1508 return SDOperand();
1509}
1510
1511SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001512 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1513 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001514
1515 return SDOperand();
1516}
1517
1518SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001519 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1520 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001521
1522 return SDOperand();
1523}
1524
1525SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001526 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1527 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001528
1529 return SDOperand();
1530}
1531
Chris Lattner35e5c142006-05-05 05:51:50 +00001532/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1533/// two operands of the same opcode, try to simplify it.
1534SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1535 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1536 MVT::ValueType VT = N0.getValueType();
1537 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1538
Chris Lattner540121f2006-05-05 06:31:05 +00001539 // For each of OP in AND/OR/XOR:
1540 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1541 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1542 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001543 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001544 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001545 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001546 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1547 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1548 N0.getOperand(0).getValueType(),
1549 N0.getOperand(0), N1.getOperand(0));
1550 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001551 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001552 }
1553
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001554 // For each of OP in SHL/SRL/SRA/AND...
1555 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1556 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1557 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001558 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001559 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001560 N0.getOperand(1) == N1.getOperand(1)) {
1561 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1562 N0.getOperand(0).getValueType(),
1563 N0.getOperand(0), N1.getOperand(0));
1564 AddToWorkList(ORNode.Val);
1565 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1566 }
1567
1568 return SDOperand();
1569}
1570
Nate Begeman83e75ec2005-09-06 04:43:02 +00001571SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572 SDOperand N0 = N->getOperand(0);
1573 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001574 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1576 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 MVT::ValueType VT = N1.getValueType();
1578
Dan Gohman7f321562007-06-25 16:23:39 +00001579 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001580 if (MVT::isVector(VT)) {
1581 SDOperand FoldedVOp = SimplifyVBinOp(N);
1582 if (FoldedVOp.Val) return FoldedVOp;
1583 }
Dan Gohman7f321562007-06-25 16:23:39 +00001584
Dan Gohman613e0d82007-07-03 14:03:57 +00001585 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001586 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001587 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001589 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001590 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001591 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001592 if (N0C && !N1C)
1593 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001595 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001596 return N0;
1597 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001598 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001599 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001600 // reassociate and
1601 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1602 if (RAND.Val != 0)
1603 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001605 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001607 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001609 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1610 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001611 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001612 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001613 ~N1C->getValue() & InMask)) {
1614 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1615 N0.getOperand(0));
1616
1617 // Replace uses of the AND with uses of the Zero extend node.
1618 CombineTo(N, Zext);
1619
Chris Lattner3603cd62006-02-02 07:17:31 +00001620 // We actually want to replace all uses of the any_extend with the
1621 // zero_extend, to avoid duplicating things. This will later cause this
1622 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001623 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001624 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001625 }
1626 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001627 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1628 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1629 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1630 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1631
1632 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1633 MVT::isInteger(LL.getValueType())) {
1634 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1635 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1636 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001637 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001638 return DAG.getSetCC(VT, ORNode, LR, Op1);
1639 }
1640 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1641 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1642 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001643 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001644 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1645 }
1646 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1647 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1648 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001649 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001650 return DAG.getSetCC(VT, ORNode, LR, Op1);
1651 }
1652 }
1653 // canonicalize equivalent to ll == rl
1654 if (LL == RR && LR == RL) {
1655 Op1 = ISD::getSetCCSwappedOperands(Op1);
1656 std::swap(RL, RR);
1657 }
1658 if (LL == RL && LR == RR) {
1659 bool isInteger = MVT::isInteger(LL.getValueType());
1660 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1661 if (Result != ISD::SETCC_INVALID)
1662 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1663 }
1664 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001665
1666 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1667 if (N0.getOpcode() == N1.getOpcode()) {
1668 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1669 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001670 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001671
Nate Begemande996292006-02-03 22:24:05 +00001672 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1673 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001674 if (!MVT::isVector(VT) &&
1675 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001676 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001677 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001678 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001679 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001680 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001681 // If we zero all the possible extended bits, then we can turn this into
1682 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001683 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001684 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001685 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1686 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001687 LN0->getSrcValueOffset(), EVT,
1688 LN0->isVolatile(),
1689 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001690 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001691 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001692 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001693 }
1694 }
1695 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001696 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1697 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001698 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001699 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001700 // If we zero all the possible extended bits, then we can turn this into
1701 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001702 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001703 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001704 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1705 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001706 LN0->getSrcValueOffset(), EVT,
1707 LN0->isVolatile(),
1708 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001709 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001710 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001711 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001712 }
1713 }
Chris Lattner15045b62006-02-28 06:35:35 +00001714
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001715 // fold (and (load x), 255) -> (zextload x, i8)
1716 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001717 if (N1C && N0.getOpcode() == ISD::LOAD) {
1718 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1719 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001720 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001721 MVT::ValueType EVT, LoadedVT;
1722 if (N1C->getValue() == 255)
1723 EVT = MVT::i8;
1724 else if (N1C->getValue() == 65535)
1725 EVT = MVT::i16;
1726 else if (N1C->getValue() == ~0U)
1727 EVT = MVT::i32;
1728 else
1729 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001730
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001731 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001732 if (EVT != MVT::Other && LoadedVT > EVT &&
1733 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1734 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1735 // For big endian targets, we need to add an offset to the pointer to
1736 // load the correct bytes. For little endian systems, we merely need to
1737 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001738 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1739 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1740 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001741 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001742 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sandsdc846502007-10-28 12:59:45 +00001743 if (!TLI.isLittleEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001744 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1745 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001746 Alignment = MinAlign(Alignment, PtrOff);
1747 }
Evan Cheng466685d2006-10-09 20:57:25 +00001748 AddToWorkList(NewPtr.Val);
1749 SDOperand Load =
1750 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001751 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001752 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001753 AddToWorkList(N);
1754 CombineTo(N0.Val, Load, Load.getValue(1));
1755 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1756 }
Chris Lattner15045b62006-02-28 06:35:35 +00001757 }
1758 }
1759
Nate Begeman83e75ec2005-09-06 04:43:02 +00001760 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761}
1762
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 SDOperand N0 = N->getOperand(0);
1765 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001766 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001767 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001769 MVT::ValueType VT = N1.getValueType();
1770 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771
Dan Gohman7f321562007-06-25 16:23:39 +00001772 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001773 if (MVT::isVector(VT)) {
1774 SDOperand FoldedVOp = SimplifyVBinOp(N);
1775 if (FoldedVOp.Val) return FoldedVOp;
1776 }
Dan Gohman7f321562007-06-25 16:23:39 +00001777
Dan Gohman613e0d82007-07-03 14:03:57 +00001778 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001779 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001780 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001782 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001783 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001784 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001785 if (N0C && !N1C)
1786 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001788 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001789 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001791 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001792 return N1;
1793 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001794 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001795 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001796 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001797 // reassociate or
1798 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1799 if (ROR.Val != 0)
1800 return ROR;
1801 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1802 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001803 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001804 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1805 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1806 N1),
1807 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001808 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001809 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1810 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1811 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1812 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1813
1814 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1815 MVT::isInteger(LL.getValueType())) {
1816 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1817 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1818 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1819 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1820 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001821 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 return DAG.getSetCC(VT, ORNode, LR, Op1);
1823 }
1824 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1825 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1826 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1827 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1828 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001829 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001830 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1831 }
1832 }
1833 // canonicalize equivalent to ll == rl
1834 if (LL == RR && LR == RL) {
1835 Op1 = ISD::getSetCCSwappedOperands(Op1);
1836 std::swap(RL, RR);
1837 }
1838 if (LL == RL && LR == RR) {
1839 bool isInteger = MVT::isInteger(LL.getValueType());
1840 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1841 if (Result != ISD::SETCC_INVALID)
1842 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1843 }
1844 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001845
1846 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1847 if (N0.getOpcode() == N1.getOpcode()) {
1848 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1849 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001850 }
Chris Lattner516b9622006-09-14 20:50:57 +00001851
Chris Lattner1ec72732006-09-14 21:11:37 +00001852 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1853 if (N0.getOpcode() == ISD::AND &&
1854 N1.getOpcode() == ISD::AND &&
1855 N0.getOperand(1).getOpcode() == ISD::Constant &&
1856 N1.getOperand(1).getOpcode() == ISD::Constant &&
1857 // Don't increase # computations.
1858 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1859 // We can only do this xform if we know that bits from X that are set in C2
1860 // but not in C1 are already zero. Likewise for Y.
1861 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1862 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1863
Dan Gohmanea859be2007-06-22 14:59:07 +00001864 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1865 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001866 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1867 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1868 }
1869 }
1870
1871
Chris Lattner516b9622006-09-14 20:50:57 +00001872 // See if this is some rotate idiom.
1873 if (SDNode *Rot = MatchRotate(N0, N1))
1874 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001875
Nate Begeman83e75ec2005-09-06 04:43:02 +00001876 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001877}
1878
Chris Lattner516b9622006-09-14 20:50:57 +00001879
1880/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1881static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1882 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001883 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001884 Mask = Op.getOperand(1);
1885 Op = Op.getOperand(0);
1886 } else {
1887 return false;
1888 }
1889 }
1890
1891 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1892 Shift = Op;
1893 return true;
1894 }
1895 return false;
1896}
1897
1898
1899// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1900// idioms for rotate, and if the target supports rotation instructions, generate
1901// a rot[lr].
1902SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1903 // Must be a legal type. Expanded an promoted things won't work with rotates.
1904 MVT::ValueType VT = LHS.getValueType();
1905 if (!TLI.isTypeLegal(VT)) return 0;
1906
1907 // The target must have at least one rotate flavor.
1908 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1909 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1910 if (!HasROTL && !HasROTR) return 0;
1911
1912 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1913 SDOperand LHSShift; // The shift.
1914 SDOperand LHSMask; // AND value if any.
1915 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1916 return 0; // Not part of a rotate.
1917
1918 SDOperand RHSShift; // The shift.
1919 SDOperand RHSMask; // AND value if any.
1920 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1921 return 0; // Not part of a rotate.
1922
1923 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1924 return 0; // Not shifting the same value.
1925
1926 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1927 return 0; // Shifts must disagree.
1928
1929 // Canonicalize shl to left side in a shl/srl pair.
1930 if (RHSShift.getOpcode() == ISD::SHL) {
1931 std::swap(LHS, RHS);
1932 std::swap(LHSShift, RHSShift);
1933 std::swap(LHSMask , RHSMask );
1934 }
1935
1936 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001937 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1938 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1939 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001940
1941 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1942 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001943 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1944 RHSShiftAmt.getOpcode() == ISD::Constant) {
1945 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1946 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001947 if ((LShVal + RShVal) != OpSizeInBits)
1948 return 0;
1949
1950 SDOperand Rot;
1951 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001952 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001953 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001954 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001955
1956 // If there is an AND of either shifted operand, apply it to the result.
1957 if (LHSMask.Val || RHSMask.Val) {
1958 uint64_t Mask = MVT::getIntVTBitMask(VT);
1959
1960 if (LHSMask.Val) {
1961 uint64_t RHSBits = (1ULL << LShVal)-1;
1962 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1963 }
1964 if (RHSMask.Val) {
1965 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1966 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1967 }
1968
1969 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1970 }
1971
1972 return Rot.Val;
1973 }
1974
1975 // If there is a mask here, and we have a variable shift, we can't be sure
1976 // that we're masking out the right stuff.
1977 if (LHSMask.Val || RHSMask.Val)
1978 return 0;
1979
1980 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1981 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001982 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1983 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001984 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001986 if (SUBC->getValue() == OpSizeInBits)
1987 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001988 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001989 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001990 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001991 }
1992 }
1993
1994 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1995 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001996 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1997 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001998 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001999 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002000 if (SUBC->getValue() == OpSizeInBits)
2001 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002002 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002003 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002004 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2005 }
2006 }
2007
2008 // Look for sign/zext/any-extended cases:
2009 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2010 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2011 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2012 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2013 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2014 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2015 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2016 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2017 if (RExtOp0.getOpcode() == ISD::SUB &&
2018 RExtOp0.getOperand(1) == LExtOp0) {
2019 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2020 // (rotr x, y)
2021 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2022 // (rotl x, (sub 32, y))
2023 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2024 if (SUBC->getValue() == OpSizeInBits) {
2025 if (HasROTL)
2026 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2027 else
2028 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2029 }
2030 }
2031 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2032 RExtOp0 == LExtOp0.getOperand(1)) {
2033 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2034 // (rotl x, y)
2035 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2036 // (rotr x, (sub 32, y))
2037 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2038 if (SUBC->getValue() == OpSizeInBits) {
2039 if (HasROTL)
2040 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2041 else
2042 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2043 }
2044 }
Chris Lattner516b9622006-09-14 20:50:57 +00002045 }
2046 }
2047
2048 return 0;
2049}
2050
2051
Nate Begeman83e75ec2005-09-06 04:43:02 +00002052SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002053 SDOperand N0 = N->getOperand(0);
2054 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002055 SDOperand LHS, RHS, CC;
2056 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2057 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002058 MVT::ValueType VT = N0.getValueType();
2059
Dan Gohman7f321562007-06-25 16:23:39 +00002060 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002061 if (MVT::isVector(VT)) {
2062 SDOperand FoldedVOp = SimplifyVBinOp(N);
2063 if (FoldedVOp.Val) return FoldedVOp;
2064 }
Dan Gohman7f321562007-06-25 16:23:39 +00002065
Dan Gohman613e0d82007-07-03 14:03:57 +00002066 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002067 if (N0.getOpcode() == ISD::UNDEF)
2068 return N0;
2069 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002070 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002073 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002074 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002075 if (N0C && !N1C)
2076 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002078 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002079 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002080 // reassociate xor
2081 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2082 if (RXOR.Val != 0)
2083 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002085 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2086 bool isInt = MVT::isInteger(LHS.getValueType());
2087 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2088 isInt);
2089 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002090 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002091 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002092 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002093 assert(0 && "Unhandled SetCC Equivalent!");
2094 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002096 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2097 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2098 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2099 SDOperand V = N0.getOperand(0);
2100 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002101 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002102 AddToWorkList(V.Val);
2103 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2104 }
2105
Nate Begeman99801192005-09-07 23:25:52 +00002106 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002107 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002108 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002110 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2111 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2113 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002114 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002115 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116 }
2117 }
Nate Begeman99801192005-09-07 23:25:52 +00002118 // fold !(x or y) -> (!x and !y) iff x or y are constants
2119 if (N1C && N1C->isAllOnesValue() &&
2120 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002121 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002122 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2123 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2125 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002126 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002127 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128 }
2129 }
Nate Begeman223df222005-09-08 20:18:10 +00002130 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2131 if (N1C && N0.getOpcode() == ISD::XOR) {
2132 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2133 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2134 if (N00C)
2135 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2136 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2137 if (N01C)
2138 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2139 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2140 }
2141 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002142 if (N0 == N1) {
2143 if (!MVT::isVector(VT)) {
2144 return DAG.getConstant(0, VT);
2145 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2146 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002147 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002148 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002149 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002150 }
2151 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002152
2153 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2154 if (N0.getOpcode() == N1.getOpcode()) {
2155 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2156 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002157 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002158
Chris Lattner3e104b12006-04-08 04:15:24 +00002159 // Simplify the expression using non-local knowledge.
2160 if (!MVT::isVector(VT) &&
2161 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002162 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002163
Nate Begeman83e75ec2005-09-06 04:43:02 +00002164 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165}
2166
Chris Lattnere70da202007-12-06 07:33:36 +00002167/// visitShiftByConstant - Handle transforms common to the three shifts, when
2168/// the shift amount is a constant.
2169SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2170 SDNode *LHS = N->getOperand(0).Val;
2171 if (!LHS->hasOneUse()) return SDOperand();
2172
2173 // We want to pull some binops through shifts, so that we have (and (shift))
2174 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2175 // thing happens with address calculations, so it's important to canonicalize
2176 // it.
2177 bool HighBitSet = false; // Can we transform this if the high bit is set?
2178
2179 switch (LHS->getOpcode()) {
2180 default: return SDOperand();
2181 case ISD::OR:
2182 case ISD::XOR:
2183 HighBitSet = false; // We can only transform sra if the high bit is clear.
2184 break;
2185 case ISD::AND:
2186 HighBitSet = true; // We can only transform sra if the high bit is set.
2187 break;
2188 case ISD::ADD:
2189 if (N->getOpcode() != ISD::SHL)
2190 return SDOperand(); // only shl(add) not sr[al](add).
2191 HighBitSet = false; // We can only transform sra if the high bit is clear.
2192 break;
2193 }
2194
2195 // We require the RHS of the binop to be a constant as well.
2196 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2197 if (!BinOpCst) return SDOperand();
2198
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002199
2200 // FIXME: disable this for unless the input to the binop is a shift by a
2201 // constant. If it is not a shift, it pessimizes some common cases like:
2202 //
2203 //void foo(int *X, int i) { X[i & 1235] = 1; }
2204 //int bar(int *X, int i) { return X[i & 255]; }
2205 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2206 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2207 BinOpLHSVal->getOpcode() != ISD::SRA &&
2208 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2209 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2210 return SDOperand();
2211
Chris Lattnere70da202007-12-06 07:33:36 +00002212 MVT::ValueType VT = N->getValueType(0);
2213
2214 // If this is a signed shift right, and the high bit is modified
2215 // by the logical operation, do not perform the transformation.
2216 // The highBitSet boolean indicates the value of the high bit of
2217 // the constant which would cause it to be modified for this
2218 // operation.
2219 if (N->getOpcode() == ISD::SRA) {
2220 uint64_t BinOpRHSSign = BinOpCst->getValue() >> MVT::getSizeInBits(VT)-1;
2221 if ((bool)BinOpRHSSign != HighBitSet)
2222 return SDOperand();
2223 }
2224
2225 // Fold the constants, shifting the binop RHS by the shift amount.
2226 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2227 LHS->getOperand(1), N->getOperand(1));
2228
2229 // Create the new shift.
2230 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2231 N->getOperand(1));
2232
2233 // Create the new binop.
2234 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2235}
2236
2237
Nate Begeman83e75ec2005-09-06 04:43:02 +00002238SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002239 SDOperand N0 = N->getOperand(0);
2240 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002241 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2242 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002243 MVT::ValueType VT = N0.getValueType();
2244 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2245
2246 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002247 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002248 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002249 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002250 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002251 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002252 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002253 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002254 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002255 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002256 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002257 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002258 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002259 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002260 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002261 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002262 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002263 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002264 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002265 N0.getOperand(1).getOpcode() == ISD::Constant) {
2266 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002267 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002268 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002269 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002271 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002272 }
2273 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2274 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002275 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002276 N0.getOperand(1).getOpcode() == ISD::Constant) {
2277 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002278 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2280 DAG.getConstant(~0ULL << c1, VT));
2281 if (c2 > c1)
2282 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002283 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002285 return DAG.getNode(ISD::SRL, VT, Mask,
2286 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 }
2288 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002289 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002290 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002291 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002292
2293 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294}
2295
Nate Begeman83e75ec2005-09-06 04:43:02 +00002296SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002297 SDOperand N0 = N->getOperand(0);
2298 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2300 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302
2303 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002304 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002305 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002306 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002307 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002308 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002311 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002313 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002314 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002316 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002317 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002318 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2319 // sext_inreg.
2320 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2321 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2322 MVT::ValueType EVT;
2323 switch (LowBits) {
2324 default: EVT = MVT::Other; break;
2325 case 1: EVT = MVT::i1; break;
2326 case 8: EVT = MVT::i8; break;
2327 case 16: EVT = MVT::i16; break;
2328 case 32: EVT = MVT::i32; break;
2329 }
2330 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2331 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2332 DAG.getValueType(EVT));
2333 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002334
2335 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2336 if (N1C && N0.getOpcode() == ISD::SRA) {
2337 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2338 unsigned Sum = N1C->getValue() + C1->getValue();
2339 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2340 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2341 DAG.getConstant(Sum, N1C->getValueType(0)));
2342 }
2343 }
2344
Chris Lattnera8504462006-05-08 20:51:54 +00002345 // Simplify, based on bits shifted out of the LHS.
2346 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2347 return SDOperand(N, 0);
2348
2349
Nate Begeman1d4d4142005-09-01 00:19:25 +00002350 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002351 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002352 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002353
2354 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002355}
2356
Nate Begeman83e75ec2005-09-06 04:43:02 +00002357SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002358 SDOperand N0 = N->getOperand(0);
2359 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002360 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2361 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 MVT::ValueType VT = N0.getValueType();
2363 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2364
2365 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002367 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002368 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002369 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002370 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002372 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002375 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002376 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002377 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002378 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002379 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002380
Nate Begeman1d4d4142005-09-01 00:19:25 +00002381 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002382 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 N0.getOperand(1).getOpcode() == ISD::Constant) {
2384 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002385 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002386 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002387 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002388 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002390 }
Chris Lattner350bec02006-04-02 06:11:11 +00002391
Chris Lattner06afe072006-05-05 22:53:17 +00002392 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2393 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2394 // Shifting in all undef bits?
2395 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2396 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2397 return DAG.getNode(ISD::UNDEF, VT);
2398
2399 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2400 AddToWorkList(SmallShift.Val);
2401 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2402 }
2403
Chris Lattner3657ffe2006-10-12 20:23:19 +00002404 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2405 // bit, which is unmodified by sra.
2406 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2407 if (N0.getOpcode() == ISD::SRA)
2408 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2409 }
2410
Chris Lattner350bec02006-04-02 06:11:11 +00002411 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2412 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2413 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2414 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002415 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002416
2417 // If any of the input bits are KnownOne, then the input couldn't be all
2418 // zeros, thus the result of the srl will always be zero.
2419 if (KnownOne) return DAG.getConstant(0, VT);
2420
2421 // If all of the bits input the to ctlz node are known to be zero, then
2422 // the result of the ctlz is "32" and the result of the shift is one.
2423 uint64_t UnknownBits = ~KnownZero & Mask;
2424 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2425
2426 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2427 if ((UnknownBits & (UnknownBits-1)) == 0) {
2428 // Okay, we know that only that the single bit specified by UnknownBits
2429 // could be set on input to the CTLZ node. If this bit is set, the SRL
2430 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2431 // to an SRL,XOR pair, which is likely to simplify more.
2432 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2433 SDOperand Op = N0.getOperand(0);
2434 if (ShAmt) {
2435 Op = DAG.getNode(ISD::SRL, VT, Op,
2436 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2437 AddToWorkList(Op.Val);
2438 }
2439 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2440 }
2441 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002442
2443 // fold operands of srl based on knowledge that the low bits are not
2444 // demanded.
2445 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2446 return SDOperand(N, 0);
2447
Chris Lattnere70da202007-12-06 07:33:36 +00002448 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449}
2450
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002453 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454
2455 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002456 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002457 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459}
2460
Nate Begeman83e75ec2005-09-06 04:43:02 +00002461SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002463 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464
2465 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002466 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002467 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469}
2470
Nate Begeman83e75ec2005-09-06 04:43:02 +00002471SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002473 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002474
2475 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002476 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002477 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479}
2480
Nate Begeman452d7beb2005-09-16 00:54:12 +00002481SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2482 SDOperand N0 = N->getOperand(0);
2483 SDOperand N1 = N->getOperand(1);
2484 SDOperand N2 = N->getOperand(2);
2485 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2486 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2487 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2488 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002489 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002490
Nate Begeman452d7beb2005-09-16 00:54:12 +00002491 // fold select C, X, X -> X
2492 if (N1 == N2)
2493 return N1;
2494 // fold select true, X, Y -> X
2495 if (N0C && !N0C->isNullValue())
2496 return N1;
2497 // fold select false, X, Y -> Y
2498 if (N0C && N0C->isNullValue())
2499 return N2;
2500 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002501 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002502 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002503 // fold select C, 0, 1 -> ~C
2504 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2505 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2506 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2507 if (VT == VT0)
2508 return XORNode;
2509 AddToWorkList(XORNode.Val);
2510 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2511 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2512 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2513 }
Nate Begeman452d7beb2005-09-16 00:54:12 +00002514 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002515 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2516 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002517 AddToWorkList(XORNode.Val);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002518 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2519 }
2520 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002521 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2522 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002523 AddToWorkList(XORNode.Val);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002524 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2525 }
2526 // fold select C, X, 0 -> C & X
2527 // FIXME: this should check for C type == X type, not i1?
2528 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2529 return DAG.getNode(ISD::AND, VT, N0, N1);
2530 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2531 if (MVT::i1 == VT && N0 == N1)
2532 return DAG.getNode(ISD::OR, VT, N0, N2);
2533 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2534 if (MVT::i1 == VT && N0 == N2)
2535 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002536
Chris Lattner40c62d52005-10-18 06:04:22 +00002537 // If we can fold this based on the true/false value, do so.
2538 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002539 return SDOperand(N, 0); // Don't revisit N.
2540
Nate Begeman44728a72005-09-19 22:34:01 +00002541 // fold selects based on a setcc into other things, such as min/max/abs
2542 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002543 // FIXME:
2544 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2545 // having to say they don't support SELECT_CC on every type the DAG knows
2546 // about, since there is no way to mark an opcode illegal at all value types
2547 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2548 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2549 N1, N2, N0.getOperand(2));
2550 else
2551 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002552 return SDOperand();
2553}
2554
2555SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002556 SDOperand N0 = N->getOperand(0);
2557 SDOperand N1 = N->getOperand(1);
2558 SDOperand N2 = N->getOperand(2);
2559 SDOperand N3 = N->getOperand(3);
2560 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002561 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2562
Nate Begeman44728a72005-09-19 22:34:01 +00002563 // fold select_cc lhs, rhs, x, x, cc -> x
2564 if (N2 == N3)
2565 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002566
Chris Lattner5f42a242006-09-20 06:19:26 +00002567 // Determine if the condition we're dealing with is constant
2568 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002569 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002570
2571 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2572 if (SCCC->getValue())
2573 return N2; // cond always true -> true val
2574 else
2575 return N3; // cond always false -> false val
2576 }
2577
2578 // Fold to a simpler select_cc
2579 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2580 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2581 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2582 SCC.getOperand(2));
2583
Chris Lattner40c62d52005-10-18 06:04:22 +00002584 // If we can fold this based on the true/false value, do so.
2585 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002586 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002587
Nate Begeman44728a72005-09-19 22:34:01 +00002588 // fold select_cc into other things, such as min/max/abs
2589 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002590}
2591
2592SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2593 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2594 cast<CondCodeSDNode>(N->getOperand(2))->get());
2595}
2596
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002597// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2598// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2599// transformation. Returns true if extension are possible and the above
2600// mentioned transformation is profitable.
2601static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2602 unsigned ExtOpc,
2603 SmallVector<SDNode*, 4> &ExtendNodes,
2604 TargetLowering &TLI) {
2605 bool HasCopyToRegUses = false;
2606 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2607 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2608 UI != UE; ++UI) {
2609 SDNode *User = *UI;
2610 if (User == N)
2611 continue;
2612 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2613 if (User->getOpcode() == ISD::SETCC) {
2614 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2615 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2616 // Sign bits will be lost after a zext.
2617 return false;
2618 bool Add = false;
2619 for (unsigned i = 0; i != 2; ++i) {
2620 SDOperand UseOp = User->getOperand(i);
2621 if (UseOp == N0)
2622 continue;
2623 if (!isa<ConstantSDNode>(UseOp))
2624 return false;
2625 Add = true;
2626 }
2627 if (Add)
2628 ExtendNodes.push_back(User);
2629 } else {
2630 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2631 SDOperand UseOp = User->getOperand(i);
2632 if (UseOp == N0) {
2633 // If truncate from extended type to original load type is free
2634 // on this target, then it's ok to extend a CopyToReg.
2635 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2636 HasCopyToRegUses = true;
2637 else
2638 return false;
2639 }
2640 }
2641 }
2642 }
2643
2644 if (HasCopyToRegUses) {
2645 bool BothLiveOut = false;
2646 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2647 UI != UE; ++UI) {
2648 SDNode *User = *UI;
2649 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2650 SDOperand UseOp = User->getOperand(i);
2651 if (UseOp.Val == N && UseOp.ResNo == 0) {
2652 BothLiveOut = true;
2653 break;
2654 }
2655 }
2656 }
2657 if (BothLiveOut)
2658 // Both unextended and extended values are live out. There had better be
2659 // good a reason for the transformation.
2660 return ExtendNodes.size();
2661 }
2662 return true;
2663}
2664
Nate Begeman83e75ec2005-09-06 04:43:02 +00002665SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002666 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002667 MVT::ValueType VT = N->getValueType(0);
2668
Nate Begeman1d4d4142005-09-01 00:19:25 +00002669 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002670 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002671 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002672
Nate Begeman1d4d4142005-09-01 00:19:25 +00002673 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002674 // fold (sext (aext x)) -> (sext x)
2675 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002676 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002677
Evan Chengc88138f2007-03-22 01:54:19 +00002678 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2679 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002680 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002681 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002682 if (NarrowLoad.Val) {
2683 if (NarrowLoad.Val != N0.Val)
2684 CombineTo(N0.Val, NarrowLoad);
2685 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2686 }
Evan Chengc88138f2007-03-22 01:54:19 +00002687 }
2688
2689 // See if the value being truncated is already sign extended. If so, just
2690 // eliminate the trunc/sext pair.
2691 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002692 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002693 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2694 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2695 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002696 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002697
2698 if (OpBits == DestBits) {
2699 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2700 // bits, it is already ready.
2701 if (NumSignBits > DestBits-MidBits)
2702 return Op;
2703 } else if (OpBits < DestBits) {
2704 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2705 // bits, just sext from i32.
2706 if (NumSignBits > OpBits-MidBits)
2707 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2708 } else {
2709 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2710 // bits, just truncate to i32.
2711 if (NumSignBits > OpBits-MidBits)
2712 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002713 }
Chris Lattner22558872007-02-26 03:13:59 +00002714
2715 // fold (sext (truncate x)) -> (sextinreg x).
2716 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2717 N0.getValueType())) {
2718 if (Op.getValueType() < VT)
2719 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2720 else if (Op.getValueType() > VT)
2721 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2722 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2723 DAG.getValueType(N0.getValueType()));
2724 }
Chris Lattner6007b842006-09-21 06:00:20 +00002725 }
Chris Lattner310b5782006-05-06 23:06:26 +00002726
Evan Cheng110dec22005-12-14 02:19:23 +00002727 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002728 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002729 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002730 bool DoXform = true;
2731 SmallVector<SDNode*, 4> SetCCs;
2732 if (!N0.hasOneUse())
2733 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2734 if (DoXform) {
2735 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2736 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2737 LN0->getBasePtr(), LN0->getSrcValue(),
2738 LN0->getSrcValueOffset(),
2739 N0.getValueType(),
2740 LN0->isVolatile(),
2741 LN0->getAlignment());
2742 CombineTo(N, ExtLoad);
2743 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2744 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2745 // Extend SetCC uses if necessary.
2746 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2747 SDNode *SetCC = SetCCs[i];
2748 SmallVector<SDOperand, 4> Ops;
2749 for (unsigned j = 0; j != 2; ++j) {
2750 SDOperand SOp = SetCC->getOperand(j);
2751 if (SOp == Trunc)
2752 Ops.push_back(ExtLoad);
2753 else
2754 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2755 }
2756 Ops.push_back(SetCC->getOperand(2));
2757 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2758 &Ops[0], Ops.size()));
2759 }
2760 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2761 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002762 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002763
2764 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2765 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002766 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2767 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002768 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002769 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002770 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2771 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2772 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002773 LN0->getSrcValueOffset(), EVT,
2774 LN0->isVolatile(),
2775 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002776 CombineTo(N, ExtLoad);
2777 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2778 ExtLoad.getValue(1));
2779 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2780 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002781 }
2782
Chris Lattner20a35c32007-04-11 05:32:27 +00002783 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2784 if (N0.getOpcode() == ISD::SETCC) {
2785 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002786 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2787 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2788 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2789 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002790 }
2791
Nate Begeman83e75ec2005-09-06 04:43:02 +00002792 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002793}
2794
Nate Begeman83e75ec2005-09-06 04:43:02 +00002795SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002796 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002797 MVT::ValueType VT = N->getValueType(0);
2798
Nate Begeman1d4d4142005-09-01 00:19:25 +00002799 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002800 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002801 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002802 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002803 // fold (zext (aext x)) -> (zext x)
2804 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002805 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002806
Evan Chengc88138f2007-03-22 01:54:19 +00002807 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2808 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002809 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002810 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002811 if (NarrowLoad.Val) {
2812 if (NarrowLoad.Val != N0.Val)
2813 CombineTo(N0.Val, NarrowLoad);
2814 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2815 }
Evan Chengc88138f2007-03-22 01:54:19 +00002816 }
2817
Chris Lattner6007b842006-09-21 06:00:20 +00002818 // fold (zext (truncate x)) -> (and x, mask)
2819 if (N0.getOpcode() == ISD::TRUNCATE &&
2820 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2821 SDOperand Op = N0.getOperand(0);
2822 if (Op.getValueType() < VT) {
2823 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2824 } else if (Op.getValueType() > VT) {
2825 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2826 }
2827 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2828 }
2829
Chris Lattner111c2282006-09-21 06:14:31 +00002830 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2831 if (N0.getOpcode() == ISD::AND &&
2832 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2833 N0.getOperand(1).getOpcode() == ISD::Constant) {
2834 SDOperand X = N0.getOperand(0).getOperand(0);
2835 if (X.getValueType() < VT) {
2836 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2837 } else if (X.getValueType() > VT) {
2838 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2839 }
2840 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2841 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2842 }
2843
Evan Cheng110dec22005-12-14 02:19:23 +00002844 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002845 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002846 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002847 bool DoXform = true;
2848 SmallVector<SDNode*, 4> SetCCs;
2849 if (!N0.hasOneUse())
2850 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2851 if (DoXform) {
2852 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2853 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2854 LN0->getBasePtr(), LN0->getSrcValue(),
2855 LN0->getSrcValueOffset(),
2856 N0.getValueType(),
2857 LN0->isVolatile(),
2858 LN0->getAlignment());
2859 CombineTo(N, ExtLoad);
2860 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2861 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2862 // Extend SetCC uses if necessary.
2863 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2864 SDNode *SetCC = SetCCs[i];
2865 SmallVector<SDOperand, 4> Ops;
2866 for (unsigned j = 0; j != 2; ++j) {
2867 SDOperand SOp = SetCC->getOperand(j);
2868 if (SOp == Trunc)
2869 Ops.push_back(ExtLoad);
2870 else
Evan Chengde1631b2007-10-30 20:11:21 +00002871 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002872 }
2873 Ops.push_back(SetCC->getOperand(2));
2874 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2875 &Ops[0], Ops.size()));
2876 }
2877 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2878 }
Evan Cheng110dec22005-12-14 02:19:23 +00002879 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002880
2881 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2882 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002883 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2884 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002885 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002886 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002887 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2888 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002889 LN0->getSrcValueOffset(), EVT,
2890 LN0->isVolatile(),
2891 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002892 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002893 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2894 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002895 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002896 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002897
2898 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2899 if (N0.getOpcode() == ISD::SETCC) {
2900 SDOperand SCC =
2901 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2902 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002903 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2904 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002905 }
2906
Nate Begeman83e75ec2005-09-06 04:43:02 +00002907 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002908}
2909
Chris Lattner5ffc0662006-05-05 05:58:59 +00002910SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2911 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002912 MVT::ValueType VT = N->getValueType(0);
2913
2914 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002915 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002916 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2917 // fold (aext (aext x)) -> (aext x)
2918 // fold (aext (zext x)) -> (zext x)
2919 // fold (aext (sext x)) -> (sext x)
2920 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2921 N0.getOpcode() == ISD::ZERO_EXTEND ||
2922 N0.getOpcode() == ISD::SIGN_EXTEND)
2923 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2924
Evan Chengc88138f2007-03-22 01:54:19 +00002925 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2926 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2927 if (N0.getOpcode() == ISD::TRUNCATE) {
2928 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002929 if (NarrowLoad.Val) {
2930 if (NarrowLoad.Val != N0.Val)
2931 CombineTo(N0.Val, NarrowLoad);
2932 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2933 }
Evan Chengc88138f2007-03-22 01:54:19 +00002934 }
2935
Chris Lattner84750582006-09-20 06:29:17 +00002936 // fold (aext (truncate x))
2937 if (N0.getOpcode() == ISD::TRUNCATE) {
2938 SDOperand TruncOp = N0.getOperand(0);
2939 if (TruncOp.getValueType() == VT)
2940 return TruncOp; // x iff x size == zext size.
2941 if (TruncOp.getValueType() > VT)
2942 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2943 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2944 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002945
2946 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2947 if (N0.getOpcode() == ISD::AND &&
2948 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2949 N0.getOperand(1).getOpcode() == ISD::Constant) {
2950 SDOperand X = N0.getOperand(0).getOperand(0);
2951 if (X.getValueType() < VT) {
2952 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2953 } else if (X.getValueType() > VT) {
2954 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2955 }
2956 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2957 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2958 }
2959
Chris Lattner5ffc0662006-05-05 05:58:59 +00002960 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002961 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002962 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002963 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2964 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2965 LN0->getBasePtr(), LN0->getSrcValue(),
2966 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002967 N0.getValueType(),
2968 LN0->isVolatile(),
2969 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002970 CombineTo(N, ExtLoad);
2971 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2972 ExtLoad.getValue(1));
2973 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2974 }
2975
2976 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2977 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2978 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002979 if (N0.getOpcode() == ISD::LOAD &&
2980 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002981 N0.hasOneUse()) {
2982 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002983 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002984 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2985 LN0->getChain(), LN0->getBasePtr(),
2986 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002987 LN0->getSrcValueOffset(), EVT,
2988 LN0->isVolatile(),
2989 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002990 CombineTo(N, ExtLoad);
2991 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2992 ExtLoad.getValue(1));
2993 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2994 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002995
2996 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2997 if (N0.getOpcode() == ISD::SETCC) {
2998 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002999 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3000 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003001 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003002 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003003 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003004 }
3005
Chris Lattner5ffc0662006-05-05 05:58:59 +00003006 return SDOperand();
3007}
3008
Chris Lattner2b4c2792007-10-13 06:35:54 +00003009/// GetDemandedBits - See if the specified operand can be simplified with the
3010/// knowledge that only the bits specified by Mask are used. If so, return the
3011/// simpler operand, otherwise return a null SDOperand.
3012SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
3013 switch (V.getOpcode()) {
3014 default: break;
3015 case ISD::OR:
3016 case ISD::XOR:
3017 // If the LHS or RHS don't contribute bits to the or, drop them.
3018 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3019 return V.getOperand(1);
3020 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3021 return V.getOperand(0);
3022 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003023 case ISD::SRL:
3024 // Only look at single-use SRLs.
3025 if (!V.Val->hasOneUse())
3026 break;
3027 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3028 // See if we can recursively simplify the LHS.
3029 unsigned Amt = RHSC->getValue();
3030 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
3031 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
3032 if (SimplifyLHS.Val) {
3033 return DAG.getNode(ISD::SRL, V.getValueType(),
3034 SimplifyLHS, V.getOperand(1));
3035 }
3036 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003037 }
3038 return SDOperand();
3039}
3040
Evan Chengc88138f2007-03-22 01:54:19 +00003041/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3042/// bits and then truncated to a narrower type and where N is a multiple
3043/// of number of bits of the narrower type, transform it to a narrower load
3044/// from address + N / num of bits of new type. If the result is to be
3045/// extended, also fold the extension to form a extending load.
3046SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3047 unsigned Opc = N->getOpcode();
3048 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3049 SDOperand N0 = N->getOperand(0);
3050 MVT::ValueType VT = N->getValueType(0);
3051 MVT::ValueType EVT = N->getValueType(0);
3052
Evan Chenge177e302007-03-23 22:13:36 +00003053 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3054 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003055 if (Opc == ISD::SIGN_EXTEND_INREG) {
3056 ExtType = ISD::SEXTLOAD;
3057 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003058 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3059 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003060 }
3061
3062 unsigned EVTBits = MVT::getSizeInBits(EVT);
3063 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003064 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003065 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3066 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3067 ShAmt = N01->getValue();
3068 // Is the shift amount a multiple of size of VT?
3069 if ((ShAmt & (EVTBits-1)) == 0) {
3070 N0 = N0.getOperand(0);
3071 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3072 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003073 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003074 }
3075 }
3076 }
3077
3078 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3079 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3080 // zero extended form: by shrinking the load, we lose track of the fact
3081 // that it is already zero extended.
3082 // FIXME: This should be reevaluated.
3083 VT != MVT::i1) {
3084 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3085 "Cannot truncate to larger type!");
3086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3087 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003088 // For big endian targets, we need to adjust the offset to the pointer to
3089 // load the correct bytes.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003090 if (!TLI.isLittleEndian()) {
3091 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3092 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3093 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3094 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003095 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003096 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003097 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3098 DAG.getConstant(PtrOff, PtrType));
3099 AddToWorkList(NewPtr.Val);
3100 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3101 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003102 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003103 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003104 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003105 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003106 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003107 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003108 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003109 WorkListRemover DeadNodes(*this);
3110 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3111 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003112 CombineTo(N->getOperand(0).Val, Load);
3113 } else
3114 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003115 if (ShAmt) {
3116 if (Opc == ISD::SIGN_EXTEND_INREG)
3117 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3118 else
3119 return DAG.getNode(Opc, VT, Load);
3120 }
Evan Chengc88138f2007-03-22 01:54:19 +00003121 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3122 }
3123
3124 return SDOperand();
3125}
3126
Chris Lattner5ffc0662006-05-05 05:58:59 +00003127
Nate Begeman83e75ec2005-09-06 04:43:02 +00003128SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003129 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003130 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003131 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003132 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00003133 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003134
Nate Begeman1d4d4142005-09-01 00:19:25 +00003135 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003136 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003137 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003138
Chris Lattner541a24f2006-05-06 22:43:44 +00003139 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003140 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003141 return N0;
3142
Nate Begeman646d7e22005-09-02 21:18:40 +00003143 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3144 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3145 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003146 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003147 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003148
Chris Lattner95a5e052007-04-17 19:03:21 +00003149 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00003150 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00003151 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003152
Chris Lattner95a5e052007-04-17 19:03:21 +00003153 // fold operands of sext_in_reg based on knowledge that the top bits are not
3154 // demanded.
3155 if (SimplifyDemandedBits(SDOperand(N, 0)))
3156 return SDOperand(N, 0);
3157
Evan Chengc88138f2007-03-22 01:54:19 +00003158 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3159 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3160 SDOperand NarrowLoad = ReduceLoadWidth(N);
3161 if (NarrowLoad.Val)
3162 return NarrowLoad;
3163
Chris Lattner4b37e872006-05-08 21:18:59 +00003164 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3165 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3166 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3167 if (N0.getOpcode() == ISD::SRL) {
3168 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3169 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3170 // We can turn this into an SRA iff the input to the SRL is already sign
3171 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003172 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003173 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3174 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3175 }
3176 }
Evan Chengc88138f2007-03-22 01:54:19 +00003177
Nate Begemanded49632005-10-13 03:11:28 +00003178 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003179 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003180 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003181 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003182 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003183 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3184 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3185 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003186 LN0->getSrcValueOffset(), EVT,
3187 LN0->isVolatile(),
3188 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003189 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003190 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003191 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003192 }
3193 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003194 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3195 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003196 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003197 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003198 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3199 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3200 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003201 LN0->getSrcValueOffset(), EVT,
3202 LN0->isVolatile(),
3203 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003204 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003205 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003206 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003207 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003208 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003209}
3210
Nate Begeman83e75ec2005-09-06 04:43:02 +00003211SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003212 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003213 MVT::ValueType VT = N->getValueType(0);
3214
3215 // noop truncate
3216 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003217 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003218 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003219 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003220 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003221 // fold (truncate (truncate x)) -> (truncate x)
3222 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003223 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003224 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003225 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3226 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003227 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003228 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003229 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003230 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003231 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003232 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003233 else
3234 // if the source and dest are the same type, we can drop both the extend
3235 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003236 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003237 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003238
Chris Lattner2b4c2792007-10-13 06:35:54 +00003239 // See if we can simplify the input to this truncate through knowledge that
3240 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3241 // -> trunc y
3242 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3243 if (Shorter.Val)
3244 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3245
Nate Begeman3df4d522005-10-12 20:40:40 +00003246 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003247 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003248 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003249}
3250
Chris Lattner94683772005-12-23 05:30:37 +00003251SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3252 SDOperand N0 = N->getOperand(0);
3253 MVT::ValueType VT = N->getValueType(0);
3254
Dan Gohman7f321562007-06-25 16:23:39 +00003255 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3256 // Only do this before legalize, since afterward the target may be depending
3257 // on the bitconvert.
3258 // First check to see if this is all constant.
3259 if (!AfterLegalize &&
3260 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3261 MVT::isVector(VT)) {
3262 bool isSimple = true;
3263 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3264 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3265 N0.getOperand(i).getOpcode() != ISD::Constant &&
3266 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3267 isSimple = false;
3268 break;
3269 }
3270
3271 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3272 assert(!MVT::isVector(DestEltVT) &&
3273 "Element type of vector ValueType must not be vector!");
3274 if (isSimple) {
3275 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3276 }
3277 }
3278
Chris Lattner94683772005-12-23 05:30:37 +00003279 // If the input is a constant, let getNode() fold it.
3280 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3281 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3282 if (Res.Val != N) return Res;
3283 }
3284
Chris Lattnerc8547d82005-12-23 05:37:50 +00003285 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3286 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003287
Chris Lattner57104102005-12-23 05:44:41 +00003288 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003289 // If the resultant load doesn't need a higher alignment than the original!
3290 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003291 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003292 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003293 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003294 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003295 unsigned OrigAlign = LN0->getAlignment();
3296 if (Align <= OrigAlign) {
3297 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3298 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003299 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003300 AddToWorkList(N);
3301 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3302 Load.getValue(1));
3303 return Load;
3304 }
Chris Lattner57104102005-12-23 05:44:41 +00003305 }
3306
Chris Lattner3bd39d42008-01-27 17:42:27 +00003307 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3308 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3309 // This often reduces constant pool loads.
3310 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3311 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3312 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3313 AddToWorkList(NewConv.Val);
3314
3315 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3316 if (N0.getOpcode() == ISD::FNEG)
3317 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3318 assert(N0.getOpcode() == ISD::FABS);
3319 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3320 }
3321
3322 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3323 // Note that we don't handle copysign(x,cst) because this can always be folded
3324 // to an fneg or fabs.
3325 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003326 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3327 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003328 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3329 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3330 N0.getOperand(1));
3331 AddToWorkList(X.Val);
3332
3333 // If X has a different width than the result/lhs, sext it or truncate it.
3334 unsigned VTWidth = MVT::getSizeInBits(VT);
3335 if (OrigXWidth < VTWidth) {
3336 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3337 AddToWorkList(X.Val);
3338 } else if (OrigXWidth > VTWidth) {
3339 // To get the sign bit in the right place, we have to shift it right
3340 // before truncating.
3341 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3342 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3343 AddToWorkList(X.Val);
3344 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3345 AddToWorkList(X.Val);
3346 }
3347
3348 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3349 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3350 AddToWorkList(X.Val);
3351
3352 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3353 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3354 AddToWorkList(Cst.Val);
3355
3356 return DAG.getNode(ISD::OR, VT, X, Cst);
3357 }
3358
Chris Lattner94683772005-12-23 05:30:37 +00003359 return SDOperand();
3360}
3361
Dan Gohman7f321562007-06-25 16:23:39 +00003362/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003363/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3364/// destination element value type.
3365SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003366ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003367 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3368
3369 // If this is already the right type, we're done.
3370 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3371
3372 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3373 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3374
3375 // If this is a conversion of N elements of one type to N elements of another
3376 // type, convert each element. This handles FP<->INT cases.
3377 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003378 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003379 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003380 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003381 AddToWorkList(Ops.back().Val);
3382 }
Dan Gohman7f321562007-06-25 16:23:39 +00003383 MVT::ValueType VT =
3384 MVT::getVectorType(DstEltVT,
3385 MVT::getVectorNumElements(BV->getValueType(0)));
3386 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003387 }
3388
3389 // Otherwise, we're growing or shrinking the elements. To avoid having to
3390 // handle annoying details of growing/shrinking FP values, we convert them to
3391 // int first.
3392 if (MVT::isFloatingPoint(SrcEltVT)) {
3393 // Convert the input float vector to a int vector where the elements are the
3394 // same sizes.
3395 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3396 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003397 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003398 SrcEltVT = IntVT;
3399 }
3400
3401 // Now we know the input is an integer vector. If the output is a FP type,
3402 // convert to integer first, then to FP of the right size.
3403 if (MVT::isFloatingPoint(DstEltVT)) {
3404 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3405 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003406 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003407
3408 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003409 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003410 }
3411
3412 // Okay, we know the src/dst types are both integers of differing types.
3413 // Handling growing first.
3414 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3415 if (SrcBitSize < DstBitSize) {
3416 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3417
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003418 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003419 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003420 i += NumInputsPerOutput) {
3421 bool isLE = TLI.isLittleEndian();
3422 uint64_t NewBits = 0;
3423 bool EltIsUndef = true;
3424 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3425 // Shift the previously computed bits over.
3426 NewBits <<= SrcBitSize;
3427 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3428 if (Op.getOpcode() == ISD::UNDEF) continue;
3429 EltIsUndef = false;
3430
3431 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3432 }
3433
3434 if (EltIsUndef)
3435 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3436 else
3437 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3438 }
3439
Dan Gohman7f321562007-06-25 16:23:39 +00003440 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3441 Ops.size());
3442 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003443 }
3444
3445 // Finally, this must be the case where we are shrinking elements: each input
3446 // turns into multiple outputs.
3447 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003448 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003449 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003450 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3451 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3452 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3453 continue;
3454 }
3455 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3456
3457 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3458 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3459 OpVal >>= DstBitSize;
3460 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3461 }
3462
3463 // For big endian targets, swap the order of the pieces of each element.
3464 if (!TLI.isLittleEndian())
3465 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3466 }
Dan Gohman7f321562007-06-25 16:23:39 +00003467 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3468 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003469}
3470
3471
3472
Chris Lattner01b3d732005-09-28 22:28:18 +00003473SDOperand DAGCombiner::visitFADD(SDNode *N) {
3474 SDOperand N0 = N->getOperand(0);
3475 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003476 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3477 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003478 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003479
Dan Gohman7f321562007-06-25 16:23:39 +00003480 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003481 if (MVT::isVector(VT)) {
3482 SDOperand FoldedVOp = SimplifyVBinOp(N);
3483 if (FoldedVOp.Val) return FoldedVOp;
3484 }
Dan Gohman7f321562007-06-25 16:23:39 +00003485
Nate Begemana0e221d2005-10-18 00:28:13 +00003486 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003487 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003488 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003489 // canonicalize constant to RHS
3490 if (N0CFP && !N1CFP)
3491 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003492 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003493 if (isNegatibleForFree(N1) == 2)
3494 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003495 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003496 if (isNegatibleForFree(N0) == 2)
3497 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003498
3499 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3500 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3501 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3502 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3503 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3504
Chris Lattner01b3d732005-09-28 22:28:18 +00003505 return SDOperand();
3506}
3507
3508SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3509 SDOperand N0 = N->getOperand(0);
3510 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003511 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3512 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003513 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003514
Dan Gohman7f321562007-06-25 16:23:39 +00003515 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003516 if (MVT::isVector(VT)) {
3517 SDOperand FoldedVOp = SimplifyVBinOp(N);
3518 if (FoldedVOp.Val) return FoldedVOp;
3519 }
Dan Gohman7f321562007-06-25 16:23:39 +00003520
Nate Begemana0e221d2005-10-18 00:28:13 +00003521 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003522 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003523 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003524 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003525 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003526 if (isNegatibleForFree(N1))
3527 return GetNegatedExpression(N1, DAG);
3528 return DAG.getNode(ISD::FNEG, VT, N1);
3529 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003530 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003531 if (isNegatibleForFree(N1))
3532 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3533
Chris Lattner01b3d732005-09-28 22:28:18 +00003534 return SDOperand();
3535}
3536
3537SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3538 SDOperand N0 = N->getOperand(0);
3539 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003540 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3541 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003542 MVT::ValueType VT = N->getValueType(0);
3543
Dan Gohman7f321562007-06-25 16:23:39 +00003544 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003545 if (MVT::isVector(VT)) {
3546 SDOperand FoldedVOp = SimplifyVBinOp(N);
3547 if (FoldedVOp.Val) return FoldedVOp;
3548 }
Dan Gohman7f321562007-06-25 16:23:39 +00003549
Nate Begeman11af4ea2005-10-17 20:40:11 +00003550 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003551 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003552 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003553 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003554 if (N0CFP && !N1CFP)
3555 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003556 // fold (fmul X, 2.0) -> (fadd X, X)
3557 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3558 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003559 // fold (fmul X, -1.0) -> (fneg X)
3560 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3561 return DAG.getNode(ISD::FNEG, VT, N0);
3562
3563 // -X * -Y -> X*Y
3564 if (char LHSNeg = isNegatibleForFree(N0)) {
3565 if (char RHSNeg = isNegatibleForFree(N1)) {
3566 // Both can be negated for free, check to see if at least one is cheaper
3567 // negated.
3568 if (LHSNeg == 2 || RHSNeg == 2)
3569 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3570 GetNegatedExpression(N1, DAG));
3571 }
3572 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003573
3574 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3575 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3576 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3577 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3578 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3579
Chris Lattner01b3d732005-09-28 22:28:18 +00003580 return SDOperand();
3581}
3582
3583SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3584 SDOperand N0 = N->getOperand(0);
3585 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003586 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3587 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003588 MVT::ValueType VT = N->getValueType(0);
3589
Dan Gohman7f321562007-06-25 16:23:39 +00003590 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003591 if (MVT::isVector(VT)) {
3592 SDOperand FoldedVOp = SimplifyVBinOp(N);
3593 if (FoldedVOp.Val) return FoldedVOp;
3594 }
Dan Gohman7f321562007-06-25 16:23:39 +00003595
Nate Begemana148d982006-01-18 22:35:16 +00003596 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003597 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003598 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003599
3600
3601 // -X / -Y -> X*Y
3602 if (char LHSNeg = isNegatibleForFree(N0)) {
3603 if (char RHSNeg = isNegatibleForFree(N1)) {
3604 // Both can be negated for free, check to see if at least one is cheaper
3605 // negated.
3606 if (LHSNeg == 2 || RHSNeg == 2)
3607 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3608 GetNegatedExpression(N1, DAG));
3609 }
3610 }
3611
Chris Lattner01b3d732005-09-28 22:28:18 +00003612 return SDOperand();
3613}
3614
3615SDOperand DAGCombiner::visitFREM(SDNode *N) {
3616 SDOperand N0 = N->getOperand(0);
3617 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003618 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3619 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003620 MVT::ValueType VT = N->getValueType(0);
3621
Nate Begemana148d982006-01-18 22:35:16 +00003622 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003623 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003624 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003625
Chris Lattner01b3d732005-09-28 22:28:18 +00003626 return SDOperand();
3627}
3628
Chris Lattner12d83032006-03-05 05:30:57 +00003629SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3630 SDOperand N0 = N->getOperand(0);
3631 SDOperand N1 = N->getOperand(1);
3632 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3633 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3634 MVT::ValueType VT = N->getValueType(0);
3635
Dale Johannesendb44bf82007-10-16 23:38:29 +00003636 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003637 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3638
3639 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003640 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003641 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3642 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003643 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003644 return DAG.getNode(ISD::FABS, VT, N0);
3645 else
3646 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3647 }
3648
3649 // copysign(fabs(x), y) -> copysign(x, y)
3650 // copysign(fneg(x), y) -> copysign(x, y)
3651 // copysign(copysign(x,z), y) -> copysign(x, y)
3652 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3653 N0.getOpcode() == ISD::FCOPYSIGN)
3654 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3655
3656 // copysign(x, abs(y)) -> abs(x)
3657 if (N1.getOpcode() == ISD::FABS)
3658 return DAG.getNode(ISD::FABS, VT, N0);
3659
3660 // copysign(x, copysign(y,z)) -> copysign(x, z)
3661 if (N1.getOpcode() == ISD::FCOPYSIGN)
3662 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3663
3664 // copysign(x, fp_extend(y)) -> copysign(x, y)
3665 // copysign(x, fp_round(y)) -> copysign(x, y)
3666 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3667 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3668
3669 return SDOperand();
3670}
3671
3672
Chris Lattner01b3d732005-09-28 22:28:18 +00003673
Nate Begeman83e75ec2005-09-06 04:43:02 +00003674SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003675 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003677 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003678
3679 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003680 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003681 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003683}
3684
Nate Begeman83e75ec2005-09-06 04:43:02 +00003685SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003686 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003687 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003688 MVT::ValueType VT = N->getValueType(0);
3689
Nate Begeman1d4d4142005-09-01 00:19:25 +00003690 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003691 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003692 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003693 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003694}
3695
Nate Begeman83e75ec2005-09-06 04:43:02 +00003696SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003697 SDOperand N0 = N->getOperand(0);
3698 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3699 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003700
3701 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003702 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003703 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003704 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003705}
3706
Nate Begeman83e75ec2005-09-06 04:43:02 +00003707SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003708 SDOperand N0 = N->getOperand(0);
3709 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3710 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003711
3712 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003713 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003714 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003715 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003716}
3717
Nate Begeman83e75ec2005-09-06 04:43:02 +00003718SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003719 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003720 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003721 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3722 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003723
3724 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003725 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003726 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003727
3728 // fold (fp_round (fp_extend x)) -> x
3729 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3730 return N0.getOperand(0);
3731
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003732 // fold (fp_round (fp_round x)) -> (fp_round x)
3733 if (N0.getOpcode() == ISD::FP_ROUND) {
3734 // This is a value preserving truncation if both round's are.
3735 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3736 N0.Val->getConstantOperandVal(1) == 1;
3737 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3738 DAG.getIntPtrConstant(IsTrunc));
3739 }
3740
Chris Lattner79dbea52006-03-13 06:26:26 +00003741 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3742 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003743 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003744 AddToWorkList(Tmp.Val);
3745 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3746 }
3747
Nate Begeman83e75ec2005-09-06 04:43:02 +00003748 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003749}
3750
Nate Begeman83e75ec2005-09-06 04:43:02 +00003751SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003752 SDOperand N0 = N->getOperand(0);
3753 MVT::ValueType VT = N->getValueType(0);
3754 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003755 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003756
Nate Begeman1d4d4142005-09-01 00:19:25 +00003757 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003758 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003759 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003760 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003761 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003763}
3764
Nate Begeman83e75ec2005-09-06 04:43:02 +00003765SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003766 SDOperand N0 = N->getOperand(0);
3767 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3768 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003769
Chris Lattner5938bef2007-12-29 06:55:23 +00003770 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3771 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3772 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003773
Nate Begeman1d4d4142005-09-01 00:19:25 +00003774 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003775 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003776 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003777
3778 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3779 // value of X.
3780 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3781 SDOperand In = N0.getOperand(0);
3782 if (In.getValueType() == VT) return In;
3783 if (VT < In.getValueType())
3784 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3785 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3786 }
3787
3788 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003789 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003790 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003791 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3792 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3793 LN0->getBasePtr(), LN0->getSrcValue(),
3794 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003795 N0.getValueType(),
3796 LN0->isVolatile(),
3797 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003798 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003799 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3800 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003801 ExtLoad.getValue(1));
3802 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3803 }
3804
3805
Nate Begeman83e75ec2005-09-06 04:43:02 +00003806 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807}
3808
Nate Begeman83e75ec2005-09-06 04:43:02 +00003809SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003810 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003811
Dan Gohman23ff1822007-07-02 15:48:56 +00003812 if (isNegatibleForFree(N0))
3813 return GetNegatedExpression(N0, DAG);
3814
Chris Lattner3bd39d42008-01-27 17:42:27 +00003815 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3816 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003817 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3818 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3819 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003820 SDOperand Int = N0.getOperand(0);
3821 MVT::ValueType IntVT = Int.getValueType();
3822 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3823 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3824 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3825 AddToWorkList(Int.Val);
3826 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3827 }
3828 }
3829
Nate Begeman83e75ec2005-09-06 04:43:02 +00003830 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003831}
3832
Nate Begeman83e75ec2005-09-06 04:43:02 +00003833SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003834 SDOperand N0 = N->getOperand(0);
3835 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3836 MVT::ValueType VT = N->getValueType(0);
3837
Nate Begeman1d4d4142005-09-01 00:19:25 +00003838 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003839 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003840 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003841 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003842 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003843 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003844 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003845 // fold (fabs (fcopysign x, y)) -> (fabs x)
3846 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3847 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3848
Chris Lattner3bd39d42008-01-27 17:42:27 +00003849 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3850 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003851 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3852 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3853 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003854 SDOperand Int = N0.getOperand(0);
3855 MVT::ValueType IntVT = Int.getValueType();
3856 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3857 Int = DAG.getNode(ISD::AND, IntVT, Int,
3858 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3859 AddToWorkList(Int.Val);
3860 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3861 }
3862 }
3863
Nate Begeman83e75ec2005-09-06 04:43:02 +00003864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003865}
3866
Nate Begeman44728a72005-09-19 22:34:01 +00003867SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3868 SDOperand Chain = N->getOperand(0);
3869 SDOperand N1 = N->getOperand(1);
3870 SDOperand N2 = N->getOperand(2);
3871 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3872
3873 // never taken branch, fold to chain
3874 if (N1C && N1C->isNullValue())
3875 return Chain;
3876 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003877 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003878 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003879 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3880 // on the target.
3881 if (N1.getOpcode() == ISD::SETCC &&
3882 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3883 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3884 N1.getOperand(0), N1.getOperand(1), N2);
3885 }
Nate Begeman44728a72005-09-19 22:34:01 +00003886 return SDOperand();
3887}
3888
Chris Lattner3ea0b472005-10-05 06:47:48 +00003889// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3890//
Nate Begeman44728a72005-09-19 22:34:01 +00003891SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003892 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3893 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3894
3895 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003896 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003897 if (Simp.Val) AddToWorkList(Simp.Val);
3898
Nate Begemane17daeb2005-10-05 21:43:42 +00003899 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3900
3901 // fold br_cc true, dest -> br dest (unconditional branch)
3902 if (SCCC && SCCC->getValue())
3903 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3904 N->getOperand(4));
3905 // fold br_cc false, dest -> unconditional fall through
3906 if (SCCC && SCCC->isNullValue())
3907 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003908
Nate Begemane17daeb2005-10-05 21:43:42 +00003909 // fold to a simpler setcc
3910 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3911 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3912 Simp.getOperand(2), Simp.getOperand(0),
3913 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003914 return SDOperand();
3915}
3916
Chris Lattner448f2192006-11-11 00:39:41 +00003917
3918/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3919/// pre-indexed load / store when the base pointer is a add or subtract
3920/// and it has other uses besides the load / store. After the
3921/// transformation, the new indexed load / store has effectively folded
3922/// the add / subtract in and all of its other uses are redirected to the
3923/// new load / store.
3924bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3925 if (!AfterLegalize)
3926 return false;
3927
3928 bool isLoad = true;
3929 SDOperand Ptr;
3930 MVT::ValueType VT;
3931 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003932 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003933 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003934 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003935 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003936 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3937 return false;
3938 Ptr = LD->getBasePtr();
3939 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003940 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003941 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003942 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003943 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3944 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3945 return false;
3946 Ptr = ST->getBasePtr();
3947 isLoad = false;
3948 } else
3949 return false;
3950
Chris Lattner9f1794e2006-11-11 00:56:29 +00003951 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3952 // out. There is no reason to make this a preinc/predec.
3953 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3954 Ptr.Val->hasOneUse())
3955 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003956
Chris Lattner9f1794e2006-11-11 00:56:29 +00003957 // Ask the target to do addressing mode selection.
3958 SDOperand BasePtr;
3959 SDOperand Offset;
3960 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3961 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3962 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003963 // Don't create a indexed load / store with zero offset.
3964 if (isa<ConstantSDNode>(Offset) &&
3965 cast<ConstantSDNode>(Offset)->getValue() == 0)
3966 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003967
Chris Lattner41e53fd2006-11-11 01:00:15 +00003968 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003969 // 1) The new base ptr is a frame index.
3970 // 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 +00003971 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003972 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003973 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003974 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003975
Chris Lattner41e53fd2006-11-11 01:00:15 +00003976 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3977 // (plus the implicit offset) to a register to preinc anyway.
3978 if (isa<FrameIndexSDNode>(BasePtr))
3979 return false;
3980
3981 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003982 if (!isLoad) {
3983 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003984 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003985 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003986 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003987
Evan Chengc843abe2007-05-24 02:35:39 +00003988 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003989 bool RealUse = false;
3990 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3991 E = Ptr.Val->use_end(); I != E; ++I) {
3992 SDNode *Use = *I;
3993 if (Use == N)
3994 continue;
3995 if (Use->isPredecessor(N))
3996 return false;
3997
3998 if (!((Use->getOpcode() == ISD::LOAD &&
3999 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
4000 (Use->getOpcode() == ISD::STORE) &&
4001 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
4002 RealUse = true;
4003 }
4004 if (!RealUse)
4005 return false;
4006
4007 SDOperand Result;
4008 if (isLoad)
4009 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4010 else
4011 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4012 ++PreIndexedNodes;
4013 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004014 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004015 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4016 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004017 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004018 if (isLoad) {
4019 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004020 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004021 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004022 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004023 } else {
4024 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004025 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004026 }
4027
Chris Lattner9f1794e2006-11-11 00:56:29 +00004028 // Finally, since the node is now dead, remove it from the graph.
4029 DAG.DeleteNode(N);
4030
4031 // Replace the uses of Ptr with uses of the updated base value.
4032 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004033 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004034 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004035 DAG.DeleteNode(Ptr.Val);
4036
4037 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004038}
4039
4040/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4041/// add / sub of the base pointer node into a post-indexed load / store.
4042/// The transformation folded the add / subtract into the new indexed
4043/// load / store effectively and all of its uses are redirected to the
4044/// new load / store.
4045bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4046 if (!AfterLegalize)
4047 return false;
4048
4049 bool isLoad = true;
4050 SDOperand Ptr;
4051 MVT::ValueType VT;
4052 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004053 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004054 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004055 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004056 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4057 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4058 return false;
4059 Ptr = LD->getBasePtr();
4060 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004061 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004062 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004063 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004064 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4065 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4066 return false;
4067 Ptr = ST->getBasePtr();
4068 isLoad = false;
4069 } else
4070 return false;
4071
Evan Chengcc470212006-11-16 00:08:20 +00004072 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004073 return false;
4074
4075 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4076 E = Ptr.Val->use_end(); I != E; ++I) {
4077 SDNode *Op = *I;
4078 if (Op == N ||
4079 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4080 continue;
4081
4082 SDOperand BasePtr;
4083 SDOperand Offset;
4084 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4085 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4086 if (Ptr == Offset)
4087 std::swap(BasePtr, Offset);
4088 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004089 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004090 // Don't create a indexed load / store with zero offset.
4091 if (isa<ConstantSDNode>(Offset) &&
4092 cast<ConstantSDNode>(Offset)->getValue() == 0)
4093 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004094
Chris Lattner9f1794e2006-11-11 00:56:29 +00004095 // Try turning it into a post-indexed load / store except when
4096 // 1) All uses are load / store ops that use it as base ptr.
4097 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4098 // nor a successor of N. Otherwise, if Op is folded that would
4099 // create a cycle.
4100
4101 // Check for #1.
4102 bool TryNext = false;
4103 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4104 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4105 SDNode *Use = *II;
4106 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004107 continue;
4108
Chris Lattner9f1794e2006-11-11 00:56:29 +00004109 // If all the uses are load / store addresses, then don't do the
4110 // transformation.
4111 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4112 bool RealUse = false;
4113 for (SDNode::use_iterator III = Use->use_begin(),
4114 EEE = Use->use_end(); III != EEE; ++III) {
4115 SDNode *UseUse = *III;
4116 if (!((UseUse->getOpcode() == ISD::LOAD &&
4117 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
4118 (UseUse->getOpcode() == ISD::STORE) &&
4119 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
4120 RealUse = true;
4121 }
Chris Lattner448f2192006-11-11 00:39:41 +00004122
Chris Lattner9f1794e2006-11-11 00:56:29 +00004123 if (!RealUse) {
4124 TryNext = true;
4125 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004126 }
4127 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004128 }
4129 if (TryNext)
4130 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004131
Chris Lattner9f1794e2006-11-11 00:56:29 +00004132 // Check for #2
4133 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4134 SDOperand Result = isLoad
4135 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4136 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4137 ++PostIndexedNodes;
4138 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004139 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004140 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4141 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004142 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004143 if (isLoad) {
4144 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004145 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004146 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004147 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004148 } else {
4149 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004150 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004151 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004152
Chris Lattner9f1794e2006-11-11 00:56:29 +00004153 // Finally, since the node is now dead, remove it from the graph.
4154 DAG.DeleteNode(N);
4155
4156 // Replace the uses of Use with uses of the updated base value.
4157 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4158 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004159 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004160 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004161 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004163 }
4164 }
4165 }
4166 return false;
4167}
4168
Chris Lattner00161a62008-01-25 07:20:16 +00004169/// InferAlignment - If we can infer some alignment information from this
4170/// pointer, return it.
4171static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4172 // If this is a direct reference to a stack slot, use information about the
4173 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004174 int FrameIdx = 1 << 31;
4175 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004176 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004177 FrameIdx = FI->getIndex();
4178 } else if (Ptr.getOpcode() == ISD::ADD &&
4179 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4180 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4181 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4182 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004183 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004184
4185 if (FrameIdx != (1 << 31)) {
4186 // FIXME: Handle FI+CST.
4187 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4188 if (MFI.isFixedObjectIndex(FrameIdx)) {
4189 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4190
4191 // The alignment of the frame index can be determined from its offset from
4192 // the incoming frame position. If the frame object is at offset 32 and
4193 // the stack is guaranteed to be 16-byte aligned, then we know that the
4194 // object is 16-byte aligned.
4195 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4196 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4197
4198 // Finally, the frame object itself may have a known alignment. Factor
4199 // the alignment + offset into a new alignment. For example, if we know
4200 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4201 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4202 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4203 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4204 FrameOffset);
4205 return std::max(Align, FIInfoAlign);
4206 }
4207 }
Chris Lattner00161a62008-01-25 07:20:16 +00004208
4209 return 0;
4210}
Chris Lattner448f2192006-11-11 00:39:41 +00004211
Chris Lattner01a22022005-10-10 22:04:48 +00004212SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004213 LoadSDNode *LD = cast<LoadSDNode>(N);
4214 SDOperand Chain = LD->getChain();
4215 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004216
4217 // Try to infer better alignment information than the load already has.
4218 if (LD->isUnindexed()) {
4219 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4220 if (Align > LD->getAlignment())
4221 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4222 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004223 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004224 LD->isVolatile(), Align);
4225 }
4226 }
4227
Evan Cheng45a7ca92007-05-01 00:38:21 +00004228
4229 // If load is not volatile and there are no uses of the loaded value (and
4230 // the updated indexed value in case of indexed loads), change uses of the
4231 // chain value into uses of the chain input (i.e. delete the dead load).
4232 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004233 if (N->getValueType(1) == MVT::Other) {
4234 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004235 if (N->hasNUsesOfValue(0, 0)) {
4236 // It's not safe to use the two value CombineTo variant here. e.g.
4237 // v1, chain2 = load chain1, loc
4238 // v2, chain3 = load chain2, loc
4239 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004240 // Now we replace use of chain2 with chain1. This makes the second load
4241 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004242 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004243 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4244 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004245 WorkListRemover DeadNodes(*this);
4246 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004247 if (N->use_empty()) {
4248 removeFromWorkList(N);
4249 DAG.DeleteNode(N);
4250 }
Evan Cheng02c42852008-01-16 23:11:54 +00004251 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4252 }
Evan Cheng498f5592007-05-01 08:53:39 +00004253 } else {
4254 // Indexed loads.
4255 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4256 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004257 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4258 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4259 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4260 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004261 WorkListRemover DeadNodes(*this);
4262 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004263 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004264 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004265 &DeadNodes);
4266 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004267 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004268 DAG.DeleteNode(N);
4269 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004270 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004271 }
4272 }
Chris Lattner01a22022005-10-10 22:04:48 +00004273
4274 // If this load is directly stored, replace the load value with the stored
4275 // value.
4276 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004277 // TODO: Handle TRUNCSTORE/LOADEXT
4278 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004279 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4280 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4281 if (PrevST->getBasePtr() == Ptr &&
4282 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004283 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004284 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004285 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004286
Jim Laskey7ca56af2006-10-11 13:47:09 +00004287 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004288 // Walk up chain skipping non-aliasing memory nodes.
4289 SDOperand BetterChain = FindBetterChain(N, Chain);
4290
Jim Laskey6ff23e52006-10-04 16:53:27 +00004291 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004292 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004293 SDOperand ReplLoad;
4294
Jim Laskey279f0532006-09-25 16:29:54 +00004295 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004296 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4297 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004298 LD->getSrcValue(), LD->getSrcValueOffset(),
4299 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004300 } else {
4301 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4302 LD->getValueType(0),
4303 BetterChain, Ptr, LD->getSrcValue(),
4304 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004305 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004306 LD->isVolatile(),
4307 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004308 }
Jim Laskey279f0532006-09-25 16:29:54 +00004309
Jim Laskey6ff23e52006-10-04 16:53:27 +00004310 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004311 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4312 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004313
Jim Laskey274062c2006-10-13 23:32:28 +00004314 // Replace uses with load result and token factor. Don't add users
4315 // to work list.
4316 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004317 }
4318 }
4319
Evan Cheng7fc033a2006-11-03 03:06:21 +00004320 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004321 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004322 return SDOperand(N, 0);
4323
Chris Lattner01a22022005-10-10 22:04:48 +00004324 return SDOperand();
4325}
4326
Chris Lattner07649d92008-01-08 23:08:06 +00004327
Chris Lattner87514ca2005-10-10 22:31:19 +00004328SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004329 StoreSDNode *ST = cast<StoreSDNode>(N);
4330 SDOperand Chain = ST->getChain();
4331 SDOperand Value = ST->getValue();
4332 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004333
Chris Lattner00161a62008-01-25 07:20:16 +00004334 // Try to infer better alignment information than the store already has.
4335 if (ST->isUnindexed()) {
4336 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4337 if (Align > ST->getAlignment())
4338 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004339 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004340 ST->isVolatile(), Align);
4341 }
4342 }
4343
Evan Cheng59d5b682007-05-07 21:27:48 +00004344 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004345 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004346 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004347 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004348 unsigned Align = ST->getAlignment();
4349 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4350 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004351 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004352 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004353 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004354 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004355 }
4356
Nate Begeman2cbba892006-12-11 02:23:46 +00004357 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004358 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004359 if (Value.getOpcode() != ISD::TargetConstantFP) {
4360 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004361 switch (CFP->getValueType(0)) {
4362 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004363 case MVT::f80: // We don't do this for these yet.
4364 case MVT::f128:
4365 case MVT::ppcf128:
4366 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004367 case MVT::f32:
4368 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004369 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4370 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004371 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004372 ST->getSrcValueOffset(), ST->isVolatile(),
4373 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004374 }
4375 break;
4376 case MVT::f64:
4377 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004378 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4379 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004380 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004381 ST->getSrcValueOffset(), ST->isVolatile(),
4382 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004383 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004384 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004385 // argument passing. Since this is so common, custom legalize the
4386 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004387 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004388 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4389 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
4390 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
4391
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004392 int SVOffset = ST->getSrcValueOffset();
4393 unsigned Alignment = ST->getAlignment();
4394 bool isVolatile = ST->isVolatile();
4395
Chris Lattner62be1a72006-12-12 04:16:14 +00004396 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004397 ST->getSrcValueOffset(),
4398 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004399 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4400 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004401 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004402 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004403 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004404 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004405 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4406 }
4407 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004408 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004409 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004410 }
4411
Jim Laskey279f0532006-09-25 16:29:54 +00004412 if (CombinerAA) {
4413 // Walk up chain skipping non-aliasing memory nodes.
4414 SDOperand BetterChain = FindBetterChain(N, Chain);
4415
Jim Laskey6ff23e52006-10-04 16:53:27 +00004416 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004417 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004418 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004419 SDOperand ReplStore;
4420 if (ST->isTruncatingStore()) {
4421 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004422 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004423 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004424 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004425 } else {
4426 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004427 ST->getSrcValue(), ST->getSrcValueOffset(),
4428 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004429 }
4430
Jim Laskey279f0532006-09-25 16:29:54 +00004431 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004432 SDOperand Token =
4433 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4434
4435 // Don't add users to work list.
4436 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004437 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004438 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004439
Evan Cheng33dbedc2006-11-05 09:31:14 +00004440 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004441 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004442 return SDOperand(N, 0);
4443
Chris Lattner3c872852007-12-29 06:26:16 +00004444 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004445 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004446 MVT::isInteger(Value.getValueType())) {
4447 // See if we can simplify the input to this truncstore with knowledge that
4448 // only the low bits are being used. For example:
4449 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4450 SDOperand Shorter =
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004451 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getMemoryVT()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004452 AddToWorkList(Value.Val);
4453 if (Shorter.Val)
4454 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004455 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004456 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004457
4458 // Otherwise, see if we can simplify the operation with
4459 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004460 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getMemoryVT())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004461 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004462 }
4463
Chris Lattner3c872852007-12-29 06:26:16 +00004464 // If this is a load followed by a store to the same location, then the store
4465 // is dead/noop.
4466 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004467 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004468 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004469 // There can't be any side effects between the load and store, such as
4470 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004471 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004472 // The store is dead, remove it.
4473 return Chain;
4474 }
4475 }
4476
Chris Lattnerddf89562008-01-17 19:59:44 +00004477 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4478 // truncating store. We can do this even if this is already a truncstore.
4479 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4480 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4481 Value.Val->hasOneUse() && ST->isUnindexed() &&
4482 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004483 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004484 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004485 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004486 ST->isVolatile(), ST->getAlignment());
4487 }
4488
Chris Lattner87514ca2005-10-10 22:31:19 +00004489 return SDOperand();
4490}
4491
Chris Lattnerca242442006-03-19 01:27:56 +00004492SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4493 SDOperand InVec = N->getOperand(0);
4494 SDOperand InVal = N->getOperand(1);
4495 SDOperand EltNo = N->getOperand(2);
4496
4497 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4498 // vector with the inserted element.
4499 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4500 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004501 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004502 if (Elt < Ops.size())
4503 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004504 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4505 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004506 }
4507
4508 return SDOperand();
4509}
4510
Evan Cheng513da432007-10-06 08:19:55 +00004511SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4512 SDOperand InVec = N->getOperand(0);
4513 SDOperand EltNo = N->getOperand(1);
4514
4515 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4516 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4517 if (isa<ConstantSDNode>(EltNo)) {
4518 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4519 bool NewLoad = false;
4520 if (Elt == 0) {
4521 MVT::ValueType VT = InVec.getValueType();
4522 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4523 MVT::ValueType LVT = EVT;
4524 unsigned NumElts = MVT::getVectorNumElements(VT);
4525 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4526 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004527 if (!MVT::isVector(BCVT) ||
4528 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004529 return SDOperand();
4530 InVec = InVec.getOperand(0);
4531 EVT = MVT::getVectorElementType(BCVT);
4532 NewLoad = true;
4533 }
4534 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4535 InVec.getOperand(0).getValueType() == EVT &&
4536 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4537 InVec.getOperand(0).hasOneUse()) {
4538 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4539 unsigned Align = LN0->getAlignment();
4540 if (NewLoad) {
4541 // Check the resultant load doesn't need a higher alignment than the
4542 // original load.
4543 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4544 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4545 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4546 return SDOperand();
4547 Align = NewAlign;
4548 }
4549
4550 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4551 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4552 LN0->isVolatile(), Align);
4553 }
4554 }
4555 }
4556 return SDOperand();
4557}
4558
4559
Dan Gohman7f321562007-06-25 16:23:39 +00004560SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4561 unsigned NumInScalars = N->getNumOperands();
4562 MVT::ValueType VT = N->getValueType(0);
4563 unsigned NumElts = MVT::getVectorNumElements(VT);
4564 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004565
Dan Gohman7f321562007-06-25 16:23:39 +00004566 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4567 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4568 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004569 SDOperand VecIn1, VecIn2;
4570 for (unsigned i = 0; i != NumInScalars; ++i) {
4571 // Ignore undef inputs.
4572 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4573
Dan Gohman7f321562007-06-25 16:23:39 +00004574 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004575 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004576 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004577 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4578 VecIn1 = VecIn2 = SDOperand(0, 0);
4579 break;
4580 }
4581
Dan Gohman7f321562007-06-25 16:23:39 +00004582 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004583 // we can't make a shuffle.
4584 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004585 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004586 VecIn1 = VecIn2 = SDOperand(0, 0);
4587 break;
4588 }
4589
4590 // Otherwise, remember this. We allow up to two distinct input vectors.
4591 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4592 continue;
4593
4594 if (VecIn1.Val == 0) {
4595 VecIn1 = ExtractedFromVec;
4596 } else if (VecIn2.Val == 0) {
4597 VecIn2 = ExtractedFromVec;
4598 } else {
4599 // Too many inputs.
4600 VecIn1 = VecIn2 = SDOperand(0, 0);
4601 break;
4602 }
4603 }
4604
4605 // If everything is good, we can make a shuffle operation.
4606 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004607 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004608 for (unsigned i = 0; i != NumInScalars; ++i) {
4609 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004610 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004611 continue;
4612 }
4613
4614 SDOperand Extract = N->getOperand(i);
4615
4616 // If extracting from the first vector, just use the index directly.
4617 if (Extract.getOperand(0) == VecIn1) {
4618 BuildVecIndices.push_back(Extract.getOperand(1));
4619 continue;
4620 }
4621
4622 // Otherwise, use InIdx + VecSize
4623 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004624 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004625 }
4626
4627 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004628 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004629
Dan Gohman7f321562007-06-25 16:23:39 +00004630 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004631 SDOperand Ops[5];
4632 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004633 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004634 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004635 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004636 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004637 std::vector<SDOperand> UnOps(NumInScalars,
4638 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004639 EltType));
4640 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004641 &UnOps[0], UnOps.size());
4642 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004643 }
Dan Gohman7f321562007-06-25 16:23:39 +00004644 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004645 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004646 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004647 }
4648
4649 return SDOperand();
4650}
4651
Dan Gohman7f321562007-06-25 16:23:39 +00004652SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4653 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4654 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4655 // inputs come from at most two distinct vectors, turn this into a shuffle
4656 // node.
4657
4658 // If we only have one input vector, we don't need to do any concatenation.
4659 if (N->getNumOperands() == 1) {
4660 return N->getOperand(0);
4661 }
4662
4663 return SDOperand();
4664}
4665
Chris Lattner66445d32006-03-28 22:11:53 +00004666SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004667 SDOperand ShufMask = N->getOperand(2);
4668 unsigned NumElts = ShufMask.getNumOperands();
4669
4670 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4671 bool isIdentity = true;
4672 for (unsigned i = 0; i != NumElts; ++i) {
4673 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4674 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4675 isIdentity = false;
4676 break;
4677 }
4678 }
4679 if (isIdentity) return N->getOperand(0);
4680
4681 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4682 isIdentity = true;
4683 for (unsigned i = 0; i != NumElts; ++i) {
4684 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4685 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4686 isIdentity = false;
4687 break;
4688 }
4689 }
4690 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004691
4692 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4693 // needed at all.
4694 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004695 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004696 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004697 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004698 for (unsigned i = 0; i != NumElts; ++i)
4699 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4700 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4701 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004702 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004703 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004704 BaseIdx = Idx;
4705 } else {
4706 if (BaseIdx != Idx)
4707 isSplat = false;
4708 if (VecNum != V) {
4709 isUnary = false;
4710 break;
4711 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004712 }
4713 }
4714
4715 SDOperand N0 = N->getOperand(0);
4716 SDOperand N1 = N->getOperand(1);
4717 // Normalize unary shuffle so the RHS is undef.
4718 if (isUnary && VecNum == 1)
4719 std::swap(N0, N1);
4720
Evan Cheng917ec982006-07-21 08:25:53 +00004721 // If it is a splat, check if the argument vector is a build_vector with
4722 // all scalar elements the same.
4723 if (isSplat) {
4724 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004725
Dan Gohman7f321562007-06-25 16:23:39 +00004726 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004727 // not the number of vector elements, look through it. Be careful not to
4728 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004729 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004730 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004731 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004732 V = ConvInput.Val;
4733 }
4734
Dan Gohman7f321562007-06-25 16:23:39 +00004735 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4736 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004737 if (NumElems > BaseIdx) {
4738 SDOperand Base;
4739 bool AllSame = true;
4740 for (unsigned i = 0; i != NumElems; ++i) {
4741 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4742 Base = V->getOperand(i);
4743 break;
4744 }
4745 }
4746 // Splat of <u, u, u, u>, return <u, u, u, u>
4747 if (!Base.Val)
4748 return N0;
4749 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004750 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004751 AllSame = false;
4752 break;
4753 }
4754 }
4755 // Splat of <x, x, x, x>, return <x, x, x, x>
4756 if (AllSame)
4757 return N0;
4758 }
4759 }
4760 }
4761
Evan Chenge7bec0d2006-07-20 22:44:41 +00004762 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4763 // into an undef.
4764 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004765 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4766 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004767 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004768 for (unsigned i = 0; i != NumElts; ++i) {
4769 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4770 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4771 MappedOps.push_back(ShufMask.getOperand(i));
4772 } else {
4773 unsigned NewIdx =
4774 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4775 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4776 }
4777 }
Dan Gohman7f321562007-06-25 16:23:39 +00004778 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004779 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004780 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004781 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4782 N0,
4783 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4784 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004785 }
Dan Gohman7f321562007-06-25 16:23:39 +00004786
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004787 return SDOperand();
4788}
4789
Evan Cheng44f1f092006-04-20 08:56:16 +00004790/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004791/// an AND to a vector_shuffle with the destination vector and a zero vector.
4792/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004793/// vector_shuffle V, Zero, <0, 4, 2, 4>
4794SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4795 SDOperand LHS = N->getOperand(0);
4796 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004797 if (N->getOpcode() == ISD::AND) {
4798 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004799 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004800 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004801 std::vector<SDOperand> IdxOps;
4802 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004803 unsigned NumElts = NumOps;
4804 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004805 for (unsigned i = 0; i != NumElts; ++i) {
4806 SDOperand Elt = RHS.getOperand(i);
4807 if (!isa<ConstantSDNode>(Elt))
4808 return SDOperand();
4809 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4810 IdxOps.push_back(DAG.getConstant(i, EVT));
4811 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4812 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4813 else
4814 return SDOperand();
4815 }
4816
4817 // Let's see if the target supports this vector_shuffle.
4818 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4819 return SDOperand();
4820
Dan Gohman7f321562007-06-25 16:23:39 +00004821 // Return the new VECTOR_SHUFFLE node.
4822 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004823 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004824 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004825 Ops.push_back(LHS);
4826 AddToWorkList(LHS.Val);
4827 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004828 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004829 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004830 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004831 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004832 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004833 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004834 if (VT != LHS.getValueType()) {
4835 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004836 }
4837 return Result;
4838 }
4839 }
4840 return SDOperand();
4841}
4842
Dan Gohman7f321562007-06-25 16:23:39 +00004843/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4844SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4845 // After legalize, the target may be depending on adds and other
4846 // binary ops to provide legal ways to construct constants or other
4847 // things. Simplifying them may result in a loss of legality.
4848 if (AfterLegalize) return SDOperand();
4849
4850 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004851 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004852
4853 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004854 SDOperand LHS = N->getOperand(0);
4855 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004856 SDOperand Shuffle = XformToShuffleWithZero(N);
4857 if (Shuffle.Val) return Shuffle;
4858
Dan Gohman7f321562007-06-25 16:23:39 +00004859 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004860 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004861 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4862 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004863 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004864 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004865 SDOperand LHSOp = LHS.getOperand(i);
4866 SDOperand RHSOp = RHS.getOperand(i);
4867 // If these two elements can't be folded, bail out.
4868 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4869 LHSOp.getOpcode() != ISD::Constant &&
4870 LHSOp.getOpcode() != ISD::ConstantFP) ||
4871 (RHSOp.getOpcode() != ISD::UNDEF &&
4872 RHSOp.getOpcode() != ISD::Constant &&
4873 RHSOp.getOpcode() != ISD::ConstantFP))
4874 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004875 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004876 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4877 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004878 if ((RHSOp.getOpcode() == ISD::Constant &&
4879 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4880 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004881 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004882 break;
4883 }
Dan Gohman7f321562007-06-25 16:23:39 +00004884 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004885 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004886 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4887 Ops.back().getOpcode() == ISD::Constant ||
4888 Ops.back().getOpcode() == ISD::ConstantFP) &&
4889 "Scalar binop didn't fold!");
4890 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004891
Dan Gohman7f321562007-06-25 16:23:39 +00004892 if (Ops.size() == LHS.getNumOperands()) {
4893 MVT::ValueType VT = LHS.getValueType();
4894 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004895 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004896 }
4897
4898 return SDOperand();
4899}
4900
Nate Begeman44728a72005-09-19 22:34:01 +00004901SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004902 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4903
4904 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4905 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4906 // If we got a simplified select_cc node back from SimplifySelectCC, then
4907 // break it down into a new SETCC node, and a new SELECT node, and then return
4908 // the SELECT node, since we were called with a SELECT node.
4909 if (SCC.Val) {
4910 // Check to see if we got a select_cc back (to turn into setcc/select).
4911 // Otherwise, just return whatever node we got back, like fabs.
4912 if (SCC.getOpcode() == ISD::SELECT_CC) {
4913 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4914 SCC.getOperand(0), SCC.getOperand(1),
4915 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004916 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004917 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4918 SCC.getOperand(3), SETCC);
4919 }
4920 return SCC;
4921 }
Nate Begeman44728a72005-09-19 22:34:01 +00004922 return SDOperand();
4923}
4924
Chris Lattner40c62d52005-10-18 06:04:22 +00004925/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4926/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004927/// select. Callers of this should assume that TheSelect is deleted if this
4928/// returns true. As such, they should return the appropriate thing (e.g. the
4929/// node) back to the top-level of the DAG combiner loop to avoid it being
4930/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004931///
4932bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4933 SDOperand RHS) {
4934
4935 // If this is a select from two identical things, try to pull the operation
4936 // through the select.
4937 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004938 // If this is a load and the token chain is identical, replace the select
4939 // of two loads with a load through a select of the address to load from.
4940 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4941 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004942 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004943 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004944 LHS.getOperand(0) == RHS.getOperand(0)) {
4945 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4946 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4947
4948 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004949 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004950 // FIXME: this conflates two src values, discarding one. This is not
4951 // the right thing to do, but nothing uses srcvalues now. When they do,
4952 // turn SrcValue into a list of locations.
4953 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004954 if (TheSelect->getOpcode() == ISD::SELECT) {
4955 // Check that the condition doesn't reach either load. If so, folding
4956 // this will induce a cycle into the DAG.
4957 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4958 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4959 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4960 TheSelect->getOperand(0), LLD->getBasePtr(),
4961 RLD->getBasePtr());
4962 }
4963 } else {
4964 // Check that the condition doesn't reach either load. If so, folding
4965 // this will induce a cycle into the DAG.
4966 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4967 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4968 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4969 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4970 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004971 TheSelect->getOperand(0),
4972 TheSelect->getOperand(1),
4973 LLD->getBasePtr(), RLD->getBasePtr(),
4974 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004975 }
Evan Cheng466685d2006-10-09 20:57:25 +00004976 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004977
4978 if (Addr.Val) {
4979 SDOperand Load;
4980 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4981 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4982 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004983 LLD->getSrcValueOffset(),
4984 LLD->isVolatile(),
4985 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004986 else {
4987 Load = DAG.getExtLoad(LLD->getExtensionType(),
4988 TheSelect->getValueType(0),
4989 LLD->getChain(), Addr, LLD->getSrcValue(),
4990 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004991 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004992 LLD->isVolatile(),
4993 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004994 }
4995 // Users of the select now use the result of the load.
4996 CombineTo(TheSelect, Load);
4997
4998 // Users of the old loads now use the new load's chain. We know the
4999 // old-load value is dead now.
5000 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5001 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5002 return true;
5003 }
Evan Chengc5484282006-10-04 00:56:09 +00005004 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005005 }
5006 }
5007
5008 return false;
5009}
5010
Nate Begeman44728a72005-09-19 22:34:01 +00005011SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5012 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005013 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005014
5015 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005016 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5017 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5018 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5019
5020 // Determine if the condition we're dealing with is constant
5021 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005022 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005023 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5024
5025 // fold select_cc true, x, y -> x
5026 if (SCCC && SCCC->getValue())
5027 return N2;
5028 // fold select_cc false, x, y -> y
5029 if (SCCC && SCCC->getValue() == 0)
5030 return N3;
5031
5032 // Check to see if we can simplify the select into an fabs node
5033 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5034 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005035 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005036 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5037 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5038 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5039 N2 == N3.getOperand(0))
5040 return DAG.getNode(ISD::FABS, VT, N0);
5041
5042 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5043 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5044 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5045 N2.getOperand(0) == N3)
5046 return DAG.getNode(ISD::FABS, VT, N3);
5047 }
5048 }
5049
5050 // Check to see if we can perform the "gzip trick", transforming
5051 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005052 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005053 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005054 MVT::isInteger(N2.getValueType()) &&
5055 (N1C->isNullValue() || // (a < 0) ? b : 0
5056 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005057 MVT::ValueType XType = N0.getValueType();
5058 MVT::ValueType AType = N2.getValueType();
5059 if (XType >= AType) {
5060 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005061 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005062 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5063 unsigned ShCtV = Log2_64(N2C->getValue());
5064 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5065 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5066 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005067 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005068 if (XType > AType) {
5069 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005070 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005071 }
5072 return DAG.getNode(ISD::AND, AType, Shift, N2);
5073 }
5074 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5075 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5076 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005077 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005078 if (XType > AType) {
5079 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005080 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005081 }
5082 return DAG.getNode(ISD::AND, AType, Shift, N2);
5083 }
5084 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005085
5086 // fold select C, 16, 0 -> shl C, 4
5087 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5088 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005089
5090 // If the caller doesn't want us to simplify this into a zext of a compare,
5091 // don't do it.
5092 if (NotExtCompare && N2C->getValue() == 1)
5093 return SDOperand();
5094
Nate Begeman07ed4172005-10-10 21:26:48 +00005095 // Get a SetCC of the condition
5096 // FIXME: Should probably make sure that setcc is legal if we ever have a
5097 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005098 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005099 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005100 if (AfterLegalize) {
5101 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005102 if (N2.getValueType() < SCC.getValueType())
5103 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5104 else
5105 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005106 } else {
5107 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005108 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005109 }
Chris Lattner5750df92006-03-01 04:03:14 +00005110 AddToWorkList(SCC.Val);
5111 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005112
5113 if (N2C->getValue() == 1)
5114 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005115 // shl setcc result by log2 n2c
5116 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5117 DAG.getConstant(Log2_64(N2C->getValue()),
5118 TLI.getShiftAmountTy()));
5119 }
5120
Nate Begemanf845b452005-10-08 00:29:44 +00005121 // Check to see if this is the equivalent of setcc
5122 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5123 // otherwise, go ahead with the folds.
5124 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5125 MVT::ValueType XType = N0.getValueType();
5126 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5127 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5128 if (Res.getValueType() != VT)
5129 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5130 return Res;
5131 }
5132
5133 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5134 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5135 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5136 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5137 return DAG.getNode(ISD::SRL, XType, Ctlz,
5138 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5139 TLI.getShiftAmountTy()));
5140 }
5141 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5142 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5143 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5144 N0);
5145 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5146 DAG.getConstant(~0ULL, XType));
5147 return DAG.getNode(ISD::SRL, XType,
5148 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5149 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5150 TLI.getShiftAmountTy()));
5151 }
5152 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5153 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5154 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5155 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5156 TLI.getShiftAmountTy()));
5157 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5158 }
5159 }
5160
5161 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5162 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5163 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005164 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5165 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5166 MVT::ValueType XType = N0.getValueType();
5167 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5168 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5169 TLI.getShiftAmountTy()));
5170 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5171 AddToWorkList(Shift.Val);
5172 AddToWorkList(Add.Val);
5173 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5174 }
5175 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5176 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5177 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5178 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5179 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005180 MVT::ValueType XType = N0.getValueType();
5181 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5182 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5183 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005184 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005185 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005186 AddToWorkList(Shift.Val);
5187 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005188 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5189 }
5190 }
5191 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005192
Nate Begeman44728a72005-09-19 22:34:01 +00005193 return SDOperand();
5194}
5195
Evan Chengfa1eb272007-02-08 22:13:59 +00005196/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7beb2005-09-16 00:54:12 +00005197SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005198 SDOperand N1, ISD::CondCode Cond,
5199 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005200 TargetLowering::DAGCombinerInfo
5201 DagCombineInfo(DAG, !AfterLegalize, false, this);
5202 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7beb2005-09-16 00:54:12 +00005203}
5204
Nate Begeman69575232005-10-20 02:15:44 +00005205/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5206/// return a DAG expression to select that will generate the same value by
5207/// multiplying by a magic number. See:
5208/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5209SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005210 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005211 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5212
Andrew Lenharth232c9102006-06-12 16:07:18 +00005213 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005214 ii != ee; ++ii)
5215 AddToWorkList(*ii);
5216 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005217}
5218
5219/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5220/// return a DAG expression to select that will generate the same value by
5221/// multiplying by a magic number. See:
5222/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5223SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005224 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005225 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005226
Andrew Lenharth232c9102006-06-12 16:07:18 +00005227 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005228 ii != ee; ++ii)
5229 AddToWorkList(*ii);
5230 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005231}
5232
Jim Laskey71382342006-10-07 23:37:56 +00005233/// FindBaseOffset - Return true if base is known not to alias with anything
5234/// but itself. Provides base object and offset as results.
5235static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5236 // Assume it is a primitive operation.
5237 Base = Ptr; Offset = 0;
5238
5239 // If it's an adding a simple constant then integrate the offset.
5240 if (Base.getOpcode() == ISD::ADD) {
5241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5242 Base = Base.getOperand(0);
5243 Offset += C->getValue();
5244 }
5245 }
5246
5247 // If it's any of the following then it can't alias with anything but itself.
5248 return isa<FrameIndexSDNode>(Base) ||
5249 isa<ConstantPoolSDNode>(Base) ||
5250 isa<GlobalAddressSDNode>(Base);
5251}
5252
5253/// isAlias - Return true if there is any possibility that the two addresses
5254/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005255bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5256 const Value *SrcValue1, int SrcValueOffset1,
5257 SDOperand Ptr2, int64_t Size2,
5258 const Value *SrcValue2, int SrcValueOffset2)
5259{
Jim Laskey71382342006-10-07 23:37:56 +00005260 // If they are the same then they must be aliases.
5261 if (Ptr1 == Ptr2) return true;
5262
5263 // Gather base node and offset information.
5264 SDOperand Base1, Base2;
5265 int64_t Offset1, Offset2;
5266 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5267 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5268
5269 // If they have a same base address then...
5270 if (Base1 == Base2) {
5271 // Check to see if the addresses overlap.
5272 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5273 }
5274
Jim Laskey096c22e2006-10-18 12:29:57 +00005275 // If we know both bases then they can't alias.
5276 if (KnownBase1 && KnownBase2) return false;
5277
Jim Laskey07a27092006-10-18 19:08:31 +00005278 if (CombinerGlobalAA) {
5279 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005280 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5281 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5282 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005283 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005284 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005285 if (AAResult == AliasAnalysis::NoAlias)
5286 return false;
5287 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005288
5289 // Otherwise we have to assume they alias.
5290 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005291}
5292
5293/// FindAliasInfo - Extracts the relevant alias information from the memory
5294/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005295bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005296 SDOperand &Ptr, int64_t &Size,
5297 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005298 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5299 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005300 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005301 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005302 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005303 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005304 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005305 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005306 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005307 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005308 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005309 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005310 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005311 }
5312
5313 return false;
5314}
5315
Jim Laskey6ff23e52006-10-04 16:53:27 +00005316/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5317/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005318void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005319 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005320 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005321 std::set<SDNode *> Visited; // Visited node set.
5322
Jim Laskey279f0532006-09-25 16:29:54 +00005323 // Get alias information for node.
5324 SDOperand Ptr;
5325 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005326 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005327 int SrcValueOffset;
5328 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005329
Jim Laskey6ff23e52006-10-04 16:53:27 +00005330 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005331 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005332
Jim Laskeybc588b82006-10-05 15:07:25 +00005333 // Look at each chain and determine if it is an alias. If so, add it to the
5334 // aliases list. If not, then continue up the chain looking for the next
5335 // candidate.
5336 while (!Chains.empty()) {
5337 SDOperand Chain = Chains.back();
5338 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005339
Jim Laskeybc588b82006-10-05 15:07:25 +00005340 // Don't bother if we've been before.
5341 if (Visited.find(Chain.Val) != Visited.end()) continue;
5342 Visited.insert(Chain.Val);
5343
5344 switch (Chain.getOpcode()) {
5345 case ISD::EntryToken:
5346 // Entry token is ideal chain operand, but handled in FindBetterChain.
5347 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005348
Jim Laskeybc588b82006-10-05 15:07:25 +00005349 case ISD::LOAD:
5350 case ISD::STORE: {
5351 // Get alias information for Chain.
5352 SDOperand OpPtr;
5353 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005354 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005355 int OpSrcValueOffset;
5356 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5357 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005358
5359 // If chain is alias then stop here.
5360 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005361 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5362 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005363 Aliases.push_back(Chain);
5364 } else {
5365 // Look further up the chain.
5366 Chains.push_back(Chain.getOperand(0));
5367 // Clean up old chain.
5368 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005369 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005370 break;
5371 }
5372
5373 case ISD::TokenFactor:
5374 // We have to check each of the operands of the token factor, so we queue
5375 // then up. Adding the operands to the queue (stack) in reverse order
5376 // maintains the original order and increases the likelihood that getNode
5377 // will find a matching token factor (CSE.)
5378 for (unsigned n = Chain.getNumOperands(); n;)
5379 Chains.push_back(Chain.getOperand(--n));
5380 // Eliminate the token factor if we can.
5381 AddToWorkList(Chain.Val);
5382 break;
5383
5384 default:
5385 // For all other instructions we will just have to take what we can get.
5386 Aliases.push_back(Chain);
5387 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005388 }
5389 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005390}
5391
5392/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5393/// for a better chain (aliasing node.)
5394SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5395 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005396
Jim Laskey6ff23e52006-10-04 16:53:27 +00005397 // Accumulate all the aliases to this node.
5398 GatherAllAliases(N, OldChain, Aliases);
5399
5400 if (Aliases.size() == 0) {
5401 // If no operands then chain to entry token.
5402 return DAG.getEntryNode();
5403 } else if (Aliases.size() == 1) {
5404 // If a single operand then chain to it. We don't need to revisit it.
5405 return Aliases[0];
5406 }
5407
5408 // Construct a custom tailored token factor.
5409 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5410 &Aliases[0], Aliases.size());
5411
5412 // Make sure the old chain gets cleaned up.
5413 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5414
5415 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005416}
5417
Nate Begeman1d4d4142005-09-01 00:19:25 +00005418// SelectionDAG::Combine - This is the entry point for the file.
5419//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005420void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005421 if (!RunningAfterLegalize && ViewDAGCombine1)
5422 viewGraph();
5423 if (RunningAfterLegalize && ViewDAGCombine2)
5424 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005425 /// run - This is the main entry point to this class.
5426 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005427 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005428}