blob: 301c7b35c3d8d567c7265c333040e00e8716e117 [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.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner600fec32009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner00161a62008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner600fec32009-03-11 05:08:08 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000026#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000027#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000028#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000029#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000032#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000035#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000036#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000038#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000040#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
Chris Lattnercd3245a2006-12-19 22:41:21 +000043STATISTIC(NodesCombined , "Number of dag nodes combined");
44STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
45STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000046STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048namespace {
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000051 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000052
Jim Laskey07a27092006-10-18 19:08:31 +000053 static cl::opt<bool>
54 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
55 cl::desc("Include global information in alias analysis"));
56
Jim Laskeybc588b82006-10-05 15:07:25 +000057//------------------------------ DAGCombiner ---------------------------------//
58
Jim Laskey71382342006-10-07 23:37:56 +000059 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000060 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000061 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000064 bool LegalOperations;
65 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000068 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000069
Jim Laskeyc7c3f112006-10-16 20:52:31 +000070 // AA - Used for DAG load/store alias analysis.
71 AliasAnalysis &AA;
72
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 /// AddUsersToWorkList - When an instruction is simplified, add all users of
74 /// the instruction to the work lists because they might get more simplified
75 /// now.
76 ///
77 void AddUsersToWorkList(SDNode *N) {
78 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000080 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000081 }
82
Dan Gohman389079b2007-10-08 17:57:15 +000083 /// visit - call the node-specific routine that knows how to fold each
84 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000085 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000086
Chris Lattner24664722006-03-01 04:53:38 +000087 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000088 /// AddToWorkList - Add to the work list making sure it's instance is at the
89 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000090 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000092 WorkList.push_back(N);
93 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000094
Chris Lattnerf8dc0612008-02-03 06:49:24 +000095 /// removeFromWorkList - remove all instances of N from the worklist.
96 ///
97 void removeFromWorkList(SDNode *N) {
98 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
99 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000100 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000103 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Dan Gohman475871a2008-07-27 21:46:04 +0000105 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000106 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000107 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000108
Dan Gohman475871a2008-07-27 21:46:04 +0000109 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000110 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000111 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000112 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000113 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000114
115 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000123 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
124 return SimplifyDemandedBits(Op, Demanded);
125 }
126
Dan Gohman475871a2008-07-27 21:46:04 +0000127 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000128
Chris Lattner448f2192006-11-11 00:39:41 +0000129 bool CombineToPreIndexedLoadStore(SDNode *N);
130 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132
Dan Gohman389079b2007-10-08 17:57:15 +0000133 /// combine - call the node-specific routine that knows how to fold each
134 /// particular type of node. If that doesn't do anything, try the
135 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137
138 // Visitation implementation - Implement dag node combining for different
139 // node types. The semantics are as follows:
140 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000141 // SDValue.getNode() == 0 - No change was made
142 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
143 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000144 //
Dan Gohman475871a2008-07-27 21:46:04 +0000145 SDValue visitTokenFactor(SDNode *N);
146 SDValue visitMERGE_VALUES(SDNode *N);
147 SDValue visitADD(SDNode *N);
148 SDValue visitSUB(SDNode *N);
149 SDValue visitADDC(SDNode *N);
150 SDValue visitADDE(SDNode *N);
151 SDValue visitMUL(SDNode *N);
152 SDValue visitSDIV(SDNode *N);
153 SDValue visitUDIV(SDNode *N);
154 SDValue visitSREM(SDNode *N);
155 SDValue visitUREM(SDNode *N);
156 SDValue visitMULHU(SDNode *N);
157 SDValue visitMULHS(SDNode *N);
158 SDValue visitSMUL_LOHI(SDNode *N);
159 SDValue visitUMUL_LOHI(SDNode *N);
160 SDValue visitSDIVREM(SDNode *N);
161 SDValue visitUDIVREM(SDNode *N);
162 SDValue visitAND(SDNode *N);
163 SDValue visitOR(SDNode *N);
164 SDValue visitXOR(SDNode *N);
165 SDValue SimplifyVBinOp(SDNode *N);
166 SDValue visitSHL(SDNode *N);
167 SDValue visitSRA(SDNode *N);
168 SDValue visitSRL(SDNode *N);
169 SDValue visitCTLZ(SDNode *N);
170 SDValue visitCTTZ(SDNode *N);
171 SDValue visitCTPOP(SDNode *N);
172 SDValue visitSELECT(SDNode *N);
173 SDValue visitSELECT_CC(SDNode *N);
174 SDValue visitSETCC(SDNode *N);
175 SDValue visitSIGN_EXTEND(SDNode *N);
176 SDValue visitZERO_EXTEND(SDNode *N);
177 SDValue visitANY_EXTEND(SDNode *N);
178 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
179 SDValue visitTRUNCATE(SDNode *N);
180 SDValue visitBIT_CONVERT(SDNode *N);
181 SDValue visitBUILD_PAIR(SDNode *N);
182 SDValue visitFADD(SDNode *N);
183 SDValue visitFSUB(SDNode *N);
184 SDValue visitFMUL(SDNode *N);
185 SDValue visitFDIV(SDNode *N);
186 SDValue visitFREM(SDNode *N);
187 SDValue visitFCOPYSIGN(SDNode *N);
188 SDValue visitSINT_TO_FP(SDNode *N);
189 SDValue visitUINT_TO_FP(SDNode *N);
190 SDValue visitFP_TO_SINT(SDNode *N);
191 SDValue visitFP_TO_UINT(SDNode *N);
192 SDValue visitFP_ROUND(SDNode *N);
193 SDValue visitFP_ROUND_INREG(SDNode *N);
194 SDValue visitFP_EXTEND(SDNode *N);
195 SDValue visitFNEG(SDNode *N);
196 SDValue visitFABS(SDNode *N);
197 SDValue visitBRCOND(SDNode *N);
198 SDValue visitBR_CC(SDNode *N);
199 SDValue visitLOAD(SDNode *N);
200 SDValue visitSTORE(SDNode *N);
201 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
202 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
203 SDValue visitBUILD_VECTOR(SDNode *N);
204 SDValue visitCONCAT_VECTORS(SDNode *N);
205 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000206
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000208 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000209
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000211
Dan Gohman475871a2008-07-27 21:46:04 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
213 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000214 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000215 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
216 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000217 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000218 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000219 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000220 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000221 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000222 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
223 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000224 SDValue BuildSDIV(SDNode *N);
225 SDValue BuildUDIV(SDNode *N);
Bill Wendling317bd702009-01-30 21:14:50 +0000226 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000227 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000228 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000229
Dan Gohman475871a2008-07-27 21:46:04 +0000230 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000231
Jim Laskey6ff23e52006-10-04 16:53:27 +0000232 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
233 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
235 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000236
Jim Laskey096c22e2006-10-18 12:29:57 +0000237 /// isAlias - Return true if there is any possibility that the two addresses
238 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000239 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000240 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000241 SDValue Ptr2, int64_t Size2,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000242 const Value *SrcValue2, int SrcValueOffset2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000243
Jim Laskey7ca56af2006-10-11 13:47:09 +0000244 /// FindAliasInfo - Extracts the relevant alias information from the memory
245 /// node. Returns true if the operand was a load.
246 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue &Ptr, int64_t &Size,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000248 const Value *&SrcValue, int &SrcValueOffset) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000249
Jim Laskey279f0532006-09-25 16:29:54 +0000250 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000251 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000252 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000253
254 /// getShiftAmountTy - Returns a type large enough to hold any valid
255 /// shift amount - before type legalization these can be huge.
Owen Andersone50ed302009-08-10 22:56:29 +0000256 EVT getShiftAmountTy() {
Duncan Sands92abc622009-01-31 15:50:11 +0000257 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
258 }
259
Nate Begeman1d4d4142005-09-01 00:19:25 +0000260public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000261 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000262 : DAG(D),
263 TLI(D.getTargetLoweringInfo()),
Duncan Sands25cf2272008-11-24 14:53:14 +0000264 Level(Unrestricted),
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000265 OptLevel(OL),
Duncan Sands25cf2272008-11-24 14:53:14 +0000266 LegalOperations(false),
267 LegalTypes(false),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000268 AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000269
Nate Begeman1d4d4142005-09-01 00:19:25 +0000270 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000271 void Run(CombineLevel AtLevel);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000272 };
273}
274
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000275
276namespace {
277/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
278/// nodes from the worklist.
Scott Michelfdc40a02009-02-17 22:15:04 +0000279class VISIBILITY_HIDDEN WorkListRemover :
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000280 public SelectionDAG::DAGUpdateListener {
281 DAGCombiner &DC;
282public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000283 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Duncan Sandsedfcf592008-06-11 11:42:12 +0000285 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000286 DC.removeFromWorkList(N);
287 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000288
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000289 virtual void NodeUpdated(SDNode *N) {
290 // Ignore updates.
291 }
292};
293}
294
Chris Lattner24664722006-03-01 04:53:38 +0000295//===----------------------------------------------------------------------===//
296// TargetLowering::DAGCombinerInfo implementation
297//===----------------------------------------------------------------------===//
298
299void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
300 ((DAGCombiner*)DC)->AddToWorkList(N);
301}
302
Dan Gohman475871a2008-07-27 21:46:04 +0000303SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000304CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
305 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000306}
307
Dan Gohman475871a2008-07-27 21:46:04 +0000308SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000309CombineTo(SDNode *N, SDValue Res, bool AddTo) {
310 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000311}
312
313
Dan Gohman475871a2008-07-27 21:46:04 +0000314SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000315CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
316 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000317}
318
Dan Gohmane5af2d32009-01-29 01:59:02 +0000319void TargetLowering::DAGCombinerInfo::
320CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
321 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
322}
Chris Lattner24664722006-03-01 04:53:38 +0000323
Chris Lattner24664722006-03-01 04:53:38 +0000324//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000325// Helper Functions
326//===----------------------------------------------------------------------===//
327
328/// isNegatibleForFree - Return 1 if we can compute the negated form of the
329/// specified expression for the same cost as the expression itself, or 2 if we
330/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000331static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000332 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000333 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000335 return 0;
336
Chris Lattner29446522007-05-14 22:04:50 +0000337 // fneg is removable even if it has multiple uses.
338 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000339
Chris Lattner29446522007-05-14 22:04:50 +0000340 // Don't allow anything with multiple uses.
341 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000342
Chris Lattner3adf9512007-05-25 02:19:06 +0000343 // Don't recurse exponentially.
344 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000345
Chris Lattner29446522007-05-14 22:04:50 +0000346 switch (Op.getOpcode()) {
347 default: return false;
348 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000349 // Don't invert constant FP values after legalize. The negated constant
350 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000351 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000352 case ISD::FADD:
353 // FIXME: determine better conditions for this xform.
354 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000355
Bill Wendlingd34470c2009-01-30 23:10:18 +0000356 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000357 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000358 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000359 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sands25cf2272008-11-24 14:53:14 +0000360 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000361 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000362 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000363 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000364
Bill Wendlingd34470c2009-01-30 23:10:18 +0000365 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000366 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000367
Chris Lattner29446522007-05-14 22:04:50 +0000368 case ISD::FMUL:
369 case ISD::FDIV:
370 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000371
Bill Wendlingd34470c2009-01-30 23:10:18 +0000372 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sands25cf2272008-11-24 14:53:14 +0000373 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000374 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000375
Duncan Sands25cf2272008-11-24 14:53:14 +0000376 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000377
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FP_EXTEND:
379 case ISD::FP_ROUND:
380 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000381 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000382 }
383}
384
385/// GetNegatedExpression - If isNegatibleForFree returns true, this function
386/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000387static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000388 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000389 // fneg is removable even if it has multiple uses.
390 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000391
Chris Lattner29446522007-05-14 22:04:50 +0000392 // Don't allow anything with multiple uses.
393 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000394
Chris Lattner3adf9512007-05-25 02:19:06 +0000395 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000396 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000397 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000398 case ISD::ConstantFP: {
399 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
400 V.changeSign();
401 return DAG.getConstantFP(V, Op.getValueType());
402 }
Chris Lattner29446522007-05-14 22:04:50 +0000403 case ISD::FADD:
404 // FIXME: determine better conditions for this xform.
405 assert(UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000406
Bill Wendlingd34470c2009-01-30 23:10:18 +0000407 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000408 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000409 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000410 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000411 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000413 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000414 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000415 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000416 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000417 Op.getOperand(0));
418 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000419 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000420 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000421
Bill Wendlingd34470c2009-01-30 23:10:18 +0000422 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000423 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000424 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000425 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000426
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000428 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
429 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000430
Chris Lattner29446522007-05-14 22:04:50 +0000431 case ISD::FMUL:
432 case ISD::FDIV:
433 assert(!HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000434
Bill Wendlingd34470c2009-01-30 23:10:18 +0000435 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000436 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000437 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000438 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000439 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000440 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Bill Wendlingd34470c2009-01-30 23:10:18 +0000442 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000443 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000444 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000445 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000446 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Chris Lattner29446522007-05-14 22:04:50 +0000448 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000449 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000450 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000451 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000452 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000453 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000454 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000455 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000456 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000457 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000458 }
459}
Chris Lattner24664722006-03-01 04:53:38 +0000460
461
Nate Begeman4ebd8052005-09-01 23:24:04 +0000462// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
463// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000464// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000465// nodes based on the type of node we are checking. This simplifies life a
466// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000467static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
468 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 if (N.getOpcode() == ISD::SETCC) {
470 LHS = N.getOperand(0);
471 RHS = N.getOperand(1);
472 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000473 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000475 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476 N.getOperand(2).getOpcode() == ISD::Constant &&
477 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000478 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
480 LHS = N.getOperand(0);
481 RHS = N.getOperand(1);
482 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000484 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485 return false;
486}
487
Nate Begeman99801192005-09-07 23:25:52 +0000488// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
489// one use. If this is true, it allows the users to invert the operation for
490// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000491static bool isOneUseSetCC(SDValue N) {
492 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000493 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000494 return true;
495 return false;
496}
497
Bill Wendling35247c32009-01-30 00:45:56 +0000498SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
499 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000500 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
502 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000503 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000504 SDValue OpNode =
505 DAG.FoldConstantArithmetic(Opc, VT,
506 cast<ConstantSDNode>(N0.getOperand(1)),
507 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000508 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000510 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
511 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
512 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000513 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000514 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 }
516 }
Bill Wendling35247c32009-01-30 00:45:56 +0000517
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
519 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000520 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000521 SDValue OpNode =
522 DAG.FoldConstantArithmetic(Opc, VT,
523 cast<ConstantSDNode>(N1.getOperand(1)),
524 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000525 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000526 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000527 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000528 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000529 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000530 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000531 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000532 }
533 }
Bill Wendling35247c32009-01-30 00:45:56 +0000534
Dan Gohman475871a2008-07-27 21:46:04 +0000535 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000536}
537
Dan Gohman475871a2008-07-27 21:46:04 +0000538SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
539 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000540 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
541 ++NodesCombined;
542 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000543 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000544 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-01-21 15:17:51 +0000545 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
546 assert(N->getValueType(i) == To[i].getValueType() &&
547 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 WorkListRemover DeadNodes(*this);
549 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000550
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000551 if (AddTo) {
552 // Push the new nodes and any users onto the worklist
553 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000554 if (To[i].getNode()) {
555 AddToWorkList(To[i].getNode());
556 AddUsersToWorkList(To[i].getNode());
557 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558 }
559 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000560
Dan Gohmandbe664a2009-01-19 21:44:21 +0000561 // Finally, if the node is now dead, remove it from the graph. The node
562 // may not be dead if the replacement process recursively simplified to
563 // something else needing this node.
564 if (N->use_empty()) {
565 // Nodes can be reintroduced into the worklist. Make sure we do not
566 // process a node that has been replaced.
567 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000568
Dan Gohmandbe664a2009-01-19 21:44:21 +0000569 // Finally, since the node is now dead, remove it from the graph.
570 DAG.DeleteNode(N);
571 }
Dan Gohman475871a2008-07-27 21:46:04 +0000572 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000573}
574
Dan Gohmane5af2d32009-01-29 01:59:02 +0000575void
576DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
577 TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000578 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000579 // are deleted, make sure to remove them from our worklist.
580 WorkListRemover DeadNodes(*this);
581 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000582
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000583 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000584 AddToWorkList(TLO.New.getNode());
585 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000586
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000587 // Finally, if the node is now dead, remove it from the graph. The node
588 // may not be dead if the replacement process recursively simplified to
589 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000590 if (TLO.Old.getNode()->use_empty()) {
591 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000592
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000593 // If the operands of this node are only used by the node, they will now
594 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000595 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
596 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
597 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000598
Gabor Greifba36cb52008-08-28 21:40:38 +0000599 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000600 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000601}
602
603/// SimplifyDemandedBits - Check the specified integer node value to see if
604/// it can be simplified or if things it uses can be simplified by bit
605/// propagation. If so, return true.
606bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
607 TargetLowering::TargetLoweringOpt TLO(DAG);
608 APInt KnownZero, KnownOne;
609 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
610 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000611
Dan Gohmane5af2d32009-01-29 01:59:02 +0000612 // Revisit the node.
613 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Dan Gohmane5af2d32009-01-29 01:59:02 +0000615 // Replace the old value with the new one.
616 ++NodesCombined;
617 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
618 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
619 DOUT << '\n';
Scott Michelfdc40a02009-02-17 22:15:04 +0000620
Dan Gohmane5af2d32009-01-29 01:59:02 +0000621 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000622 return true;
623}
624
Chris Lattner29446522007-05-14 22:04:50 +0000625//===----------------------------------------------------------------------===//
626// Main DAG Combiner implementation
627//===----------------------------------------------------------------------===//
628
Duncan Sands25cf2272008-11-24 14:53:14 +0000629void DAGCombiner::Run(CombineLevel AtLevel) {
630 // set the instance variables, so that the various visit routines may use it.
631 Level = AtLevel;
632 LegalOperations = Level >= NoIllegalOperations;
633 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000634
Evan Cheng17a568b2008-08-29 22:21:44 +0000635 // Add all the dag nodes to the worklist.
636 WorkList.reserve(DAG.allnodes_size());
637 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
638 E = DAG.allnodes_end(); I != E; ++I)
639 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000640
Evan Cheng17a568b2008-08-29 22:21:44 +0000641 // Create a dummy node (which is not added to allnodes), that adds a reference
642 // to the root node, preventing it from being deleted, and tracking any
643 // changes of the root.
644 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000645
Jim Laskey26f7fa72006-10-17 19:33:52 +0000646 // The root of the dag may dangle to deleted nodes until the dag combiner is
647 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000648 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000649
Evan Cheng17a568b2008-08-29 22:21:44 +0000650 // while the worklist isn't empty, inspect the node on the end of it and
651 // try and combine it.
652 while (!WorkList.empty()) {
653 SDNode *N = WorkList.back();
654 WorkList.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +0000655
Evan Cheng17a568b2008-08-29 22:21:44 +0000656 // If N has no uses, it is dead. Make sure to revisit all N's operands once
657 // N is deleted from the DAG, since they too may now be dead or may have a
658 // reduced number of uses, allowing other xforms.
659 if (N->use_empty() && N != &Dummy) {
660 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
661 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000662
Evan Cheng17a568b2008-08-29 22:21:44 +0000663 DAG.DeleteNode(N);
664 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000666
Evan Cheng17a568b2008-08-29 22:21:44 +0000667 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Cheng17a568b2008-08-29 22:21:44 +0000669 if (RV.getNode() == 0)
670 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000671
Evan Cheng17a568b2008-08-29 22:21:44 +0000672 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +0000673
Evan Cheng17a568b2008-08-29 22:21:44 +0000674 // If we get back the same node we passed in, rather than a new node or
675 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +0000676 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +0000677 // mechanics for us, we have no work to do in this case.
678 if (RV.getNode() == N)
679 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000680
Evan Cheng17a568b2008-08-29 22:21:44 +0000681 assert(N->getOpcode() != ISD::DELETED_NODE &&
682 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
683 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000684
Evan Cheng17a568b2008-08-29 22:21:44 +0000685 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
686 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
687 DOUT << '\n';
688 WorkListRemover DeadNodes(*this);
689 if (N->getNumValues() == RV.getNode()->getNumValues())
690 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
691 else {
692 assert(N->getValueType(0) == RV.getValueType() &&
693 N->getNumValues() == 1 && "Type mismatch");
694 SDValue OpV = RV;
695 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
696 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000697
Evan Cheng17a568b2008-08-29 22:21:44 +0000698 // Push the new node and any users onto the worklist
699 AddToWorkList(RV.getNode());
700 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Evan Cheng17a568b2008-08-29 22:21:44 +0000702 // Add any uses of the old node to the worklist in case this node is the
703 // last one that uses them. They may become dead after this node is
704 // deleted.
705 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
706 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000707
Dan Gohmandbe664a2009-01-19 21:44:21 +0000708 // Finally, if the node is now dead, remove it from the graph. The node
709 // may not be dead if the replacement process recursively simplified to
710 // something else needing this node.
711 if (N->use_empty()) {
712 // Nodes can be reintroduced into the worklist. Make sure we do not
713 // process a node that has been replaced.
714 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000715
Dan Gohmandbe664a2009-01-19 21:44:21 +0000716 // Finally, since the node is now dead, remove it from the graph.
717 DAG.DeleteNode(N);
718 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000719 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000720
Chris Lattner95038592005-10-05 06:35:28 +0000721 // If the root changed (e.g. it was a dead load, update the root).
722 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000723}
724
Dan Gohman475871a2008-07-27 21:46:04 +0000725SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000726 switch(N->getOpcode()) {
727 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000728 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000729 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000730 case ISD::ADD: return visitADD(N);
731 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000732 case ISD::ADDC: return visitADDC(N);
733 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000734 case ISD::MUL: return visitMUL(N);
735 case ISD::SDIV: return visitSDIV(N);
736 case ISD::UDIV: return visitUDIV(N);
737 case ISD::SREM: return visitSREM(N);
738 case ISD::UREM: return visitUREM(N);
739 case ISD::MULHU: return visitMULHU(N);
740 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000741 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
742 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
743 case ISD::SDIVREM: return visitSDIVREM(N);
744 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 case ISD::AND: return visitAND(N);
746 case ISD::OR: return visitOR(N);
747 case ISD::XOR: return visitXOR(N);
748 case ISD::SHL: return visitSHL(N);
749 case ISD::SRA: return visitSRA(N);
750 case ISD::SRL: return visitSRL(N);
751 case ISD::CTLZ: return visitCTLZ(N);
752 case ISD::CTTZ: return visitCTTZ(N);
753 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000754 case ISD::SELECT: return visitSELECT(N);
755 case ISD::SELECT_CC: return visitSELECT_CC(N);
756 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000757 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
758 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000759 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000760 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
761 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000762 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000763 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000764 case ISD::FADD: return visitFADD(N);
765 case ISD::FSUB: return visitFSUB(N);
766 case ISD::FMUL: return visitFMUL(N);
767 case ISD::FDIV: return visitFDIV(N);
768 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000769 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
771 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
772 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
773 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
774 case ISD::FP_ROUND: return visitFP_ROUND(N);
775 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
776 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
777 case ISD::FNEG: return visitFNEG(N);
778 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000779 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000780 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000781 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000782 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000783 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000784 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000785 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
786 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000787 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788 }
Dan Gohman475871a2008-07-27 21:46:04 +0000789 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790}
791
Dan Gohman475871a2008-07-27 21:46:04 +0000792SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000793 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000794
795 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000796 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000797 assert(N->getOpcode() != ISD::DELETED_NODE &&
798 "Node was deleted but visit returned NULL!");
799
800 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
801 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
802
803 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +0000804 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +0000805 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000806
807 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
808 }
809 }
810
Scott Michelfdc40a02009-02-17 22:15:04 +0000811 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +0000812 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +0000813 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000814 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
815 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue N0 = N->getOperand(0);
817 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +0000818
Evan Cheng08b11732008-03-22 01:55:50 +0000819 // Constant operands are canonicalized to RHS.
820 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000821 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000822 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
823 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000824 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000825 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000826 }
827 }
828
Dan Gohman389079b2007-10-08 17:57:15 +0000829 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +0000830}
Dan Gohman389079b2007-10-08 17:57:15 +0000831
Chris Lattner6270f682006-10-08 22:57:01 +0000832/// getInputChainForNode - Given a node, return its input chain if it has one,
833/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000834static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000835 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000837 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000839 return N->getOperand(NumOps-1);
840 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +0000842 return N->getOperand(i);
843 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000844 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +0000845}
846
Dan Gohman475871a2008-07-27 21:46:04 +0000847SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000848 // If N has two operands, where one has an input chain equal to the other,
849 // the 'other' chain is redundant.
850 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000851 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000852 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000853 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000854 return N->getOperand(1);
855 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000856
Chris Lattnerc76d4412007-05-16 06:37:59 +0000857 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000858 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +0000859 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +0000860 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +0000861
Jim Laskey6ff23e52006-10-04 16:53:27 +0000862 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000863 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000864
Jim Laskey71382342006-10-07 23:37:56 +0000865 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000866 // encountered.
867 for (unsigned i = 0; i < TFs.size(); ++i) {
868 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +0000869
Jim Laskey6ff23e52006-10-04 16:53:27 +0000870 // Check each of the operands.
871 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +0000873
Jim Laskey6ff23e52006-10-04 16:53:27 +0000874 switch (Op.getOpcode()) {
875 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000876 // Entry tokens don't need to be added to the list. They are
877 // rededundant.
878 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000879 break;
Scott Michelfdc40a02009-02-17 22:15:04 +0000880
Jim Laskey6ff23e52006-10-04 16:53:27 +0000881 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000882 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000883 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000884 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000885 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000886 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000887 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000888 Changed = true;
889 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000890 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000891 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +0000892
Jim Laskey6ff23e52006-10-04 16:53:27 +0000893 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000894 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000895 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000896 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000897 else
898 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000899 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000900 }
901 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000902 }
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000905
906 // If we've change things around then replace token factor.
907 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000908 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000909 // The entry token is the only possible outcome.
910 Result = DAG.getEntryNode();
911 } else {
912 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +0000913 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000915 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000916
Jim Laskey274062c2006-10-13 23:32:28 +0000917 // Don't add users to work list.
918 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000919 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000920
Jim Laskey6ff23e52006-10-04 16:53:27 +0000921 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922}
923
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000924/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000925SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000926 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +0000927 // Replacing results may cause a different MERGE_VALUES to suddenly
928 // be CSE'd with N, and carry its uses with it. Iterate until no
929 // uses remain, to ensure that the node can be safely deleted.
930 do {
931 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
932 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
933 &DeadNodes);
934 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000935 removeFromWorkList(N);
936 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000937 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000938}
939
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000940static
Bill Wendlingd69c3142009-01-30 02:23:43 +0000941SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
942 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +0000943 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue N00 = N0.getOperand(0);
945 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000946 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +0000947
Gabor Greifba36cb52008-08-28 21:40:38 +0000948 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000949 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +0000950 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
951 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
952 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
953 N00.getOperand(0), N01),
954 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
955 N00.getOperand(1), N01));
956 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000957 }
Bill Wendlingd69c3142009-01-30 02:23:43 +0000958
Dan Gohman475871a2008-07-27 21:46:04 +0000959 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000960}
961
Dan Gohman475871a2008-07-27 21:46:04 +0000962SDValue DAGCombiner::visitADD(SDNode *N) {
963 SDValue N0 = N->getOperand(0);
964 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000965 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +0000967 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000968
969 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000971 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000972 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000973 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000974
Dan Gohman613e0d82007-07-03 14:03:57 +0000975 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000976 if (N0.getOpcode() == ISD::UNDEF)
977 return N0;
978 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000979 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000982 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000983 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000984 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +0000985 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000989 // fold (add Sym, c) -> Sym+c
990 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +0000991 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +0000992 GA->getOpcode() == ISD::GlobalAddress)
993 return DAG.getGlobalAddress(GA->getGlobal(), VT,
994 GA->getOffset() +
995 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000996 // fold ((c1-A)+c2) -> (c1+c2)-A
997 if (N1C && N0.getOpcode() == ISD::SUB)
998 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +0000999 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001000 DAG.getConstant(N1C->getAPIntValue()+
1001 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001002 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001003 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001004 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001006 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007 // fold ((0-A) + B) -> B-A
1008 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1009 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001010 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (A + (0-B)) -> A-B
1012 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1013 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001014 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001015 // fold (A+(B-A)) -> B
1016 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001018 // fold ((B-A)+A) -> B
1019 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1020 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001021 // fold (A+(B-(A+C))) to (B-C)
1022 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001023 N0 == N1.getOperand(1).getOperand(0))
1024 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001025 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001026 // fold (A+(B-(C+A))) to (B-C)
1027 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001028 N0 == N1.getOperand(1).getOperand(1))
1029 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001030 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001031 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001032 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1033 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001034 N0 == N1.getOperand(0).getOperand(1))
1035 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1036 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001037
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001038 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1039 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1040 SDValue N00 = N0.getOperand(0);
1041 SDValue N01 = N0.getOperand(1);
1042 SDValue N10 = N1.getOperand(0);
1043 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001044
1045 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1046 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1047 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1048 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001049 }
Chris Lattner947c2892006-03-13 06:51:27 +00001050
Dan Gohman475871a2008-07-27 21:46:04 +00001051 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1052 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
Chris Lattner947c2892006-03-13 06:51:27 +00001054 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001055 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001056 APInt LHSZero, LHSOne;
1057 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001058 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001059 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001060
Dan Gohman948d8ea2008-02-20 16:33:30 +00001061 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001062 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001063
Chris Lattner947c2892006-03-13 06:51:27 +00001064 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1065 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1066 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1067 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001068 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001069 }
1070 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001071
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001072 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001073 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001074 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001076 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001077 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001078 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001080 }
1081
Dan Gohman475871a2008-07-27 21:46:04 +00001082 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083}
1084
Dan Gohman475871a2008-07-27 21:46:04 +00001085SDValue DAGCombiner::visitADDC(SDNode *N) {
1086 SDValue N0 = N->getOperand(0);
1087 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001088 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1089 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001090 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001091
Chris Lattner91153682007-03-04 20:03:15 +00001092 // If the flag result is dead, turn this into an ADD.
1093 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-01-30 02:38:00 +00001094 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen874ae252009-06-02 03:12:52 +00001095 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner91153682007-03-04 20:03:15 +00001098 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001099 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001100 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001101
Chris Lattnerb6541762007-03-04 20:40:38 +00001102 // fold (addc x, 0) -> x + no carry out
1103 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001104 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001106
Dale Johannesen874ae252009-06-02 03:12:52 +00001107 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001108 APInt LHSZero, LHSOne;
1109 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001111 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001112
Dan Gohman948d8ea2008-02-20 16:33:30 +00001113 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001114 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001115
Chris Lattnerb6541762007-03-04 20:40:38 +00001116 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1117 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1118 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1119 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001120 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001121 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001124
Dan Gohman475871a2008-07-27 21:46:04 +00001125 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001126}
1127
Dan Gohman475871a2008-07-27 21:46:04 +00001128SDValue DAGCombiner::visitADDE(SDNode *N) {
1129 SDValue N0 = N->getOperand(0);
1130 SDValue N1 = N->getOperand(1);
1131 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001134
Chris Lattner91153682007-03-04 20:03:15 +00001135 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001136 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001137 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1138 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001139
Chris Lattnerb6541762007-03-04 20:40:38 +00001140 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001141 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1142 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001143
Dan Gohman475871a2008-07-27 21:46:04 +00001144 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001145}
1146
Dan Gohman475871a2008-07-27 21:46:04 +00001147SDValue DAGCombiner::visitSUB(SDNode *N) {
1148 SDValue N0 = N->getOperand(0);
1149 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001150 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001152 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001153
Dan Gohman7f321562007-06-25 16:23:39 +00001154 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001155 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001156 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001157 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001158 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001159
Chris Lattner854077d2005-10-17 01:07:11 +00001160 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001161 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001162 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001165 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001166 // fold (sub x, c) -> (add x, -c)
1167 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001168 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001169 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001171 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001174 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001175 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001176 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001177 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001178 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1179 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001180 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001181 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1182 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001183 // fold ((A+(C+B))-B) -> A+C
1184 if (N0.getOpcode() == ISD::ADD &&
1185 N0.getOperand(1).getOpcode() == ISD::ADD &&
1186 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001187 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1188 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001189 // fold ((A-(B-C))-C) -> A-B
1190 if (N0.getOpcode() == ISD::SUB &&
1191 N0.getOperand(1).getOpcode() == ISD::SUB &&
1192 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001193 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1194 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001195
Dan Gohman613e0d82007-07-03 14:03:57 +00001196 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001197 if (N0.getOpcode() == ISD::UNDEF)
1198 return N0;
1199 if (N1.getOpcode() == ISD::UNDEF)
1200 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001201
Dan Gohman6520e202008-10-18 02:06:02 +00001202 // If the relocation model supports it, consider symbol offsets.
1203 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001204 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001205 // fold (sub Sym, c) -> Sym-c
1206 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1207 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1208 GA->getOffset() -
1209 (uint64_t)N1C->getSExtValue());
1210 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1211 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1212 if (GA->getGlobal() == GB->getGlobal())
1213 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1214 VT);
1215 }
1216
Dan Gohman475871a2008-07-27 21:46:04 +00001217 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218}
1219
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDValue DAGCombiner::visitMUL(SDNode *N) {
1221 SDValue N0 = N->getOperand(0);
1222 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1224 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001225 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman7f321562007-06-25 16:23:39 +00001227 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001228 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001231 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001232
Dan Gohman613e0d82007-07-03 14:03:57 +00001233 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001234 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001235 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001237 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001238 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001239 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001240 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001241 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001244 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001246 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001247 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1248 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001250 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001251 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001252 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001253 getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001254 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001255 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1256 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001257 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001258 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001259 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1260 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001261 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner66b8bc32009-03-09 20:22:18 +00001262 DAG.getConstant(Log2Val, getShiftAmountTy())));
1263 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001264 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001265 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001266 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001267 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1268 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001270 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1271 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001272 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001273
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001274 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1275 // use.
1276 {
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001278 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1279 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001281 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001282 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001283 isa<ConstantSDNode>(N1.getOperand(1)) &&
1284 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001285 Sh = N1; Y = N0;
1286 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001287
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001289 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1290 Sh.getOperand(0), Y);
1291 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1292 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001293 }
1294 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001295
Chris Lattnera1deca32006-03-04 23:33:26 +00001296 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001297 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001298 isa<ConstantSDNode>(N0.getOperand(1)))
1299 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1300 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1301 N0.getOperand(0), N1),
1302 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1303 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001304
Nate Begemancd4d58c2006-02-03 06:46:56 +00001305 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001306 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001308 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001309
Dan Gohman475871a2008-07-27 21:46:04 +00001310 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311}
1312
Dan Gohman475871a2008-07-27 21:46:04 +00001313SDValue DAGCombiner::visitSDIV(SDNode *N) {
1314 SDValue N0 = N->getOperand(0);
1315 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001316 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001318 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319
Dan Gohman7f321562007-06-25 16:23:39 +00001320 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001321 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001322 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001328 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001329 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001330 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001331 return N0;
1332 // fold (sdiv X, -1) -> 0-X
1333 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001334 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1335 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001336 // If we know the sign bits of both operands are zero, strength reduce to a
1337 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001338 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001339 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001340 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1341 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001342 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001343 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001344 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michelfdc40a02009-02-17 22:15:04 +00001345 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman7810bfe2008-09-26 21:54:37 +00001346 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001347 // If dividing by powers of two is cheap, then don't perform the following
1348 // fold.
1349 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001351
Dan Gohman7810bfe2008-09-26 21:54:37 +00001352 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001353 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001354 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001355
Chris Lattner8f4880b2006-02-16 08:02:36 +00001356 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001357 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1358 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00001359 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001361
Chris Lattner8f4880b2006-02-16 08:02:36 +00001362 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001363 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1364 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands92abc622009-01-31 15:50:11 +00001365 getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001366 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 AddToWorkList(SRL.getNode());
1368 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001369 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands92abc622009-01-31 15:50:11 +00001370 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001371
Nate Begeman405e3ec2005-10-21 00:02:42 +00001372 // If we're dividing by a positive value, we're done. Otherwise, we must
1373 // negate the result.
1374 if (pow2 > 0)
1375 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001376
Gabor Greifba36cb52008-08-28 21:40:38 +00001377 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001378 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1379 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001380 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001381
Nate Begeman69575232005-10-20 02:15:44 +00001382 // if integer divide is expensive and we satisfy the requirements, emit an
1383 // alternate sequence.
Scott Michelfdc40a02009-02-17 22:15:04 +00001384 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001385 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001386 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001387 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001388 }
Dan Gohman7f321562007-06-25 16:23:39 +00001389
Dan Gohman613e0d82007-07-03 14:03:57 +00001390 // undef / X -> 0
1391 if (N0.getOpcode() == ISD::UNDEF)
1392 return DAG.getConstant(0, VT);
1393 // X / undef -> undef
1394 if (N1.getOpcode() == ISD::UNDEF)
1395 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001396
Dan Gohman475871a2008-07-27 21:46:04 +00001397 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398}
1399
Dan Gohman475871a2008-07-27 21:46:04 +00001400SDValue DAGCombiner::visitUDIV(SDNode *N) {
1401 SDValue N0 = N->getOperand(0);
1402 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001403 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001405 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001406
Dan Gohman7f321562007-06-25 16:23:39 +00001407 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001408 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001410 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001412
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001415 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001417 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001418 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001419 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001420 getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001421 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1422 if (N1.getOpcode() == ISD::SHL) {
1423 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001424 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001425 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001426 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1427 N1.getOperand(1),
1428 DAG.getConstant(SHC->getAPIntValue()
1429 .logBase2(),
1430 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001432 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001433 }
1434 }
1435 }
Nate Begeman69575232005-10-20 02:15:44 +00001436 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001437 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001440 }
Dan Gohman7f321562007-06-25 16:23:39 +00001441
Dan Gohman613e0d82007-07-03 14:03:57 +00001442 // undef / X -> 0
1443 if (N0.getOpcode() == ISD::UNDEF)
1444 return DAG.getConstant(0, VT);
1445 // X / undef -> undef
1446 if (N1.getOpcode() == ISD::UNDEF)
1447 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001448
Dan Gohman475871a2008-07-27 21:46:04 +00001449 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450}
1451
Dan Gohman475871a2008-07-27 21:46:04 +00001452SDValue DAGCombiner::visitSREM(SDNode *N) {
1453 SDValue N0 = N->getOperand(0);
1454 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001457 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001458
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001461 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001462 // If we know the sign bits of both operands are zero, strength reduce to a
1463 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001464 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001465 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001466 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001468
Dan Gohman77003042007-11-26 23:46:11 +00001469 // If X/C can be simplified by the division-by-constant logic, lower
1470 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001471 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001472 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001473 AddToWorkList(Div.getNode());
1474 SDValue OptimizedDiv = combine(Div.getNode());
1475 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001476 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1477 OptimizedDiv, N1);
1478 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001479 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001480 return Sub;
1481 }
Chris Lattner26d29902006-10-12 20:58:32 +00001482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001483
Dan Gohman613e0d82007-07-03 14:03:57 +00001484 // undef % X -> 0
1485 if (N0.getOpcode() == ISD::UNDEF)
1486 return DAG.getConstant(0, VT);
1487 // X % undef -> undef
1488 if (N1.getOpcode() == ISD::UNDEF)
1489 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001490
Dan Gohman475871a2008-07-27 21:46:04 +00001491 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492}
1493
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue DAGCombiner::visitUREM(SDNode *N) {
1495 SDValue N0 = N->getOperand(0);
1496 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001499 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001503 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001504 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001505 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001506 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001507 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001508 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1509 if (N1.getOpcode() == ISD::SHL) {
1510 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001511 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001513 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001515 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001516 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001517 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001518 }
1519 }
1520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
Dan Gohman77003042007-11-26 23:46:11 +00001522 // If X/C can be simplified by the division-by-constant logic, lower
1523 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001524 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001525 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001526 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 SDValue OptimizedDiv = combine(Div.getNode());
1528 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001529 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1530 OptimizedDiv, N1);
1531 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001532 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001533 return Sub;
1534 }
Chris Lattner26d29902006-10-12 20:58:32 +00001535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001536
Dan Gohman613e0d82007-07-03 14:03:57 +00001537 // undef % X -> 0
1538 if (N0.getOpcode() == ISD::UNDEF)
1539 return DAG.getConstant(0, VT);
1540 // X % undef -> undef
1541 if (N1.getOpcode() == ISD::UNDEF)
1542 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001543
Dan Gohman475871a2008-07-27 21:46:04 +00001544 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545}
1546
Dan Gohman475871a2008-07-27 21:46:04 +00001547SDValue DAGCombiner::visitMULHS(SDNode *N) {
1548 SDValue N0 = N->getOperand(0);
1549 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001555 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001557 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001558 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1559 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands92abc622009-01-31 15:50:11 +00001560 getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001561 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001562 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001563 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001564
Dan Gohman475871a2008-07-27 21:46:04 +00001565 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001566}
1567
Dan Gohman475871a2008-07-27 21:46:04 +00001568SDValue DAGCombiner::visitMULHU(SDNode *N) {
1569 SDValue N0 = N->getOperand(0);
1570 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001571 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001572 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001573
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001578 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001580 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001581 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001582 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001583
Dan Gohman475871a2008-07-27 21:46:04 +00001584 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585}
1586
Dan Gohman389079b2007-10-08 17:57:15 +00001587/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1588/// compute two values. LoOp and HiOp give the opcodes for the two computations
1589/// that are being performed. Return true if a simplification was made.
1590///
Scott Michelfdc40a02009-02-17 22:15:04 +00001591SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00001592 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001593 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001594 bool HiExists = N->hasAnyUseOfValue(1);
1595 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001596 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001597 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001598 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1599 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001600 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001601 }
1602
1603 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001604 bool LoExists = N->hasAnyUseOfValue(0);
1605 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001606 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001607 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001608 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1609 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001610 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001611 }
1612
Evan Cheng44711942007-11-08 09:25:29 +00001613 // If both halves are used, return as it is.
1614 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001615 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001616
1617 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001618 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001619 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1620 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001621 AddToWorkList(Lo.getNode());
1622 SDValue LoOpt = combine(Lo.getNode());
1623 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001624 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001625 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001626 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001627 }
1628
Evan Cheng44711942007-11-08 09:25:29 +00001629 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001630 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001631 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001632 AddToWorkList(Hi.getNode());
1633 SDValue HiOpt = combine(Hi.getNode());
1634 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001635 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001636 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001637 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001638 }
Bill Wendling826d1142009-01-30 03:08:40 +00001639
Dan Gohman475871a2008-07-27 21:46:04 +00001640 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001641}
1642
Dan Gohman475871a2008-07-27 21:46:04 +00001643SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1644 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001645 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001646
Dan Gohman475871a2008-07-27 21:46:04 +00001647 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001648}
1649
Dan Gohman475871a2008-07-27 21:46:04 +00001650SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1651 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001652 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001653
Dan Gohman475871a2008-07-27 21:46:04 +00001654 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001655}
1656
Dan Gohman475871a2008-07-27 21:46:04 +00001657SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1658 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001659 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Dan Gohman475871a2008-07-27 21:46:04 +00001661 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001662}
1663
Dan Gohman475871a2008-07-27 21:46:04 +00001664SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1665 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001666 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Dan Gohman475871a2008-07-27 21:46:04 +00001668 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001669}
1670
Chris Lattner35e5c142006-05-05 05:51:50 +00001671/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1672/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001673SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1674 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001675 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001676 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001677
Chris Lattner540121f2006-05-05 06:31:05 +00001678 // For each of OP in AND/OR/XOR:
1679 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1680 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1681 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman97121ba2009-04-08 00:15:30 +00001682 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Chris Lattner540121f2006-05-05 06:31:05 +00001683 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Dan Gohman97121ba2009-04-08 00:15:30 +00001684 N0.getOpcode() == ISD::SIGN_EXTEND ||
1685 (N0.getOpcode() == ISD::TRUNCATE &&
1686 !TLI.isTruncateFree(N0.getOperand(0).getValueType(), VT))) &&
Jakob Stoklund Olesen17421d82009-08-08 20:42:17 +00001687 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType() &&
1688 (!LegalOperations ||
1689 TLI.isOperationLegal(N->getOpcode(), N0.getOperand(0).getValueType()))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001690 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1691 N0.getOperand(0).getValueType(),
1692 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001694 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001696
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001697 // For each of OP in SHL/SRL/SRA/AND...
1698 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1699 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1700 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001701 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001702 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001703 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001704 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1705 N0.getOperand(0).getValueType(),
1706 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001708 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1709 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00001710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001711
Dan Gohman475871a2008-07-27 21:46:04 +00001712 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001713}
1714
Dan Gohman475871a2008-07-27 21:46:04 +00001715SDValue DAGCombiner::visitAND(SDNode *N) {
1716 SDValue N0 = N->getOperand(0);
1717 SDValue N1 = N->getOperand(1);
1718 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001719 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1720 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001721 EVT VT = N1.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001722 unsigned BitWidth = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001723
Dan Gohman7f321562007-06-25 16:23:39 +00001724 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001727 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Dan Gohman613e0d82007-07-03 14:03:57 +00001730 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001731 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001732 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001733 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001734 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001735 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001736 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001737 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00001738 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001739 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001740 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return N0;
1742 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001743 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001744 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001746 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00001747 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001748 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001749 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001750 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001751 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001753 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001755 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1756 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001757 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001758 APInt Mask = ~N1C->getAPIntValue();
1759 Mask.trunc(N0Op0.getValueSizeInBits());
1760 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00001761 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1762 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattner1ec05d12006-03-01 21:47:21 +00001764 // Replace uses of the AND with uses of the Zero extend node.
1765 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Chris Lattner3603cd62006-02-02 07:17:31 +00001767 // We actually want to replace all uses of the any_extend with the
1768 // zero_extend, to avoid duplicating things. This will later cause this
1769 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001770 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001771 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001772 }
1773 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001774 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1775 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1776 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1777 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001779 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001780 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00001781 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001782 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001783 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1784 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001785 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001786 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001787 }
Bill Wendling2627a882009-01-30 20:43:18 +00001788 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001789 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001790 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1791 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001793 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001794 }
Bill Wendling2627a882009-01-30 20:43:18 +00001795 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001796 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00001797 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1798 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001799 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001800 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001801 }
1802 }
1803 // canonicalize equivalent to ll == rl
1804 if (LL == RR && LR == RL) {
1805 Op1 = ISD::getSetCCSwappedOperands(Op1);
1806 std::swap(RL, RR);
1807 }
1808 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001809 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001810 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001811 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001812 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00001813 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1814 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001815 }
1816 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001817
Bill Wendling2627a882009-01-30 20:43:18 +00001818 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00001819 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001820 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001821 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001823
Nate Begemande996292006-02-03 22:24:05 +00001824 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1825 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SimplifyDemandedBits(SDValue(N, 0)))
1828 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001829 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001830 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001831 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001833 // If we zero all the possible extended bits, then we can turn this into
1834 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001835 unsigned BitWidth = N1.getValueSizeInBits();
1836 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001838 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001839 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001840 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1841 LN0->getChain(), LN0->getBasePtr(),
1842 LN0->getSrcValue(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001843 LN0->getSrcValueOffset(), EVT,
1844 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001845 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001847 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001848 }
1849 }
1850 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001851 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001852 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001853 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00001854 EVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001855 // If we zero all the possible extended bits, then we can turn this into
1856 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001857 unsigned BitWidth = N1.getValueSizeInBits();
1858 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001860 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001861 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001862 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1863 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001864 LN0->getBasePtr(), LN0->getSrcValue(),
1865 LN0->getSrcValueOffset(), EVT,
1866 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001867 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001868 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001869 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001870 }
1871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001873 // fold (and (load x), 255) -> (zextload x, i8)
1874 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001875 if (N1C && N0.getOpcode() == ISD::LOAD) {
1876 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1877 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001878 LN0->isUnindexed() && N0.hasOneUse() &&
1879 // Do not change the width of a volatile load.
1880 !LN0->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 EVT ExtVT = MVT::Other;
Duncan Sands8eab8a22008-06-09 11:32:28 +00001882 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1883 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
Owen Anderson23b9b192009-08-12 00:36:31 +00001884 ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
Duncan Sands8eab8a22008-06-09 11:32:28 +00001885
Owen Andersone50ed302009-08-10 22:56:29 +00001886 EVT LoadedVT = LN0->getMemoryVT();
Bill Wendling2627a882009-01-30 20:43:18 +00001887
Duncan Sandsad205a72008-06-16 08:14:38 +00001888 // Do not generate loads of non-round integer types since these can
1889 // be expensive (and would be wrong if the type is not byte sized).
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 if (ExtVT != MVT::Other && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
Owen Andersone50ed302009-08-10 22:56:29 +00001891 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
1892 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00001893
Evan Cheng466685d2006-10-09 20:57:25 +00001894 // For big endian targets, we need to add an offset to the pointer to
1895 // load the correct bytes. For little endian systems, we merely need to
1896 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001897 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
Owen Andersone50ed302009-08-10 22:56:29 +00001898 unsigned EVTStoreBytes = ExtVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001899 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001900 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue NewPtr = LN0->getBasePtr();
Bill Wendling2627a882009-01-30 20:43:18 +00001902
Duncan Sands0753fc12008-02-11 10:37:04 +00001903 if (TLI.isBigEndian()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00001904 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
Bill Wendling2627a882009-01-30 20:43:18 +00001905 NewPtr, DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001906 Alignment = MinAlign(Alignment, PtrOff);
1907 }
Bill Wendling2627a882009-01-30 20:43:18 +00001908
Gabor Greifba36cb52008-08-28 21:40:38 +00001909 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue Load =
Bill Wendling2627a882009-01-30 20:43:18 +00001911 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, LN0->getChain(),
1912 NewPtr, LN0->getSrcValue(), LN0->getSrcValueOffset(),
Owen Andersone50ed302009-08-10 22:56:29 +00001913 ExtVT, LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001914 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001915 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001916 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001917 }
Chris Lattner15045b62006-02-28 06:35:35 +00001918 }
1919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001920
Dan Gohman475871a2008-07-27 21:46:04 +00001921 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922}
1923
Dan Gohman475871a2008-07-27 21:46:04 +00001924SDValue DAGCombiner::visitOR(SDNode *N) {
1925 SDValue N0 = N->getOperand(0);
1926 SDValue N1 = N->getOperand(1);
1927 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001928 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001930 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001931
Dan Gohman7f321562007-06-25 16:23:39 +00001932 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001933 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001935 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Dan Gohman613e0d82007-07-03 14:03:57 +00001938 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001939 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman5cbd37e2009-08-06 09:18:59 +00001940 return DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001943 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001944 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001945 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00001946 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001947 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001948 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001951 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001952 return N1;
1953 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001954 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001956 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00001957 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001958 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001959 return ROR;
1960 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001961 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001962 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001963 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling09025642009-01-30 20:59:34 +00001964 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
1965 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
1966 N0.getOperand(0), N1),
1967 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00001968 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001969 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1970 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1971 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1972 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00001973
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001974 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001975 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00001976 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
1977 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001978 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001979 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00001980 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
1981 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001982 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00001983 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001984 }
Bill Wendling09025642009-01-30 20:59:34 +00001985 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
1986 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001987 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001988 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00001989 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
1990 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00001992 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001993 }
1994 }
1995 // canonicalize equivalent to ll == rl
1996 if (LL == RR && LR == RL) {
1997 Op1 = ISD::getSetCCSwappedOperands(Op1);
1998 std::swap(RL, RR);
1999 }
2000 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002002 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002003 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002004 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002005 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2006 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002007 }
2008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Bill Wendling09025642009-01-30 20:59:34 +00002010 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002011 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002014 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002015
Bill Wendling09025642009-01-30 20:59:34 +00002016 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002017 if (N0.getOpcode() == ISD::AND &&
2018 N1.getOpcode() == ISD::AND &&
2019 N0.getOperand(1).getOpcode() == ISD::Constant &&
2020 N1.getOperand(1).getOpcode() == ISD::Constant &&
2021 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002022 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002023 // We can only do this xform if we know that bits from X that are set in C2
2024 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002025 const APInt &LHSMask =
2026 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2027 const APInt &RHSMask =
2028 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Dan Gohmanea859be2007-06-22 14:59:07 +00002030 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2031 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002032 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2033 N0.getOperand(0), N1.getOperand(0));
2034 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2035 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002036 }
2037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Chris Lattner516b9622006-09-14 20:50:57 +00002039 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002040 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002041 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002042
Dan Gohman475871a2008-07-27 21:46:04 +00002043 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044}
2045
Chris Lattner516b9622006-09-14 20:50:57 +00002046/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002047static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002048 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002049 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002050 Mask = Op.getOperand(1);
2051 Op = Op.getOperand(0);
2052 } else {
2053 return false;
2054 }
2055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002056
Chris Lattner516b9622006-09-14 20:50:57 +00002057 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2058 Shift = Op;
2059 return true;
2060 }
Bill Wendling09025642009-01-30 20:59:34 +00002061
Scott Michelfdc40a02009-02-17 22:15:04 +00002062 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002063}
2064
Chris Lattner516b9622006-09-14 20:50:57 +00002065// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2066// idioms for rotate, and if the target supports rotation instructions, generate
2067// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002068SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002069 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002070 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002071 if (!TLI.isTypeLegal(VT)) return 0;
2072
2073 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002074 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2075 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002076 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002077
Chris Lattner516b9622006-09-14 20:50:57 +00002078 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002079 SDValue LHSShift; // The shift.
2080 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002081 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2082 return 0; // Not part of a rotate.
2083
Dan Gohman475871a2008-07-27 21:46:04 +00002084 SDValue RHSShift; // The shift.
2085 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002086 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2087 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner516b9622006-09-14 20:50:57 +00002089 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2090 return 0; // Not shifting the same value.
2091
2092 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2093 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002094
Chris Lattner516b9622006-09-14 20:50:57 +00002095 // Canonicalize shl to left side in a shl/srl pair.
2096 if (RHSShift.getOpcode() == ISD::SHL) {
2097 std::swap(LHS, RHS);
2098 std::swap(LHSShift, RHSShift);
2099 std::swap(LHSMask , RHSMask );
2100 }
2101
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002103 SDValue LHSShiftArg = LHSShift.getOperand(0);
2104 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2105 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002106
2107 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2108 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002109 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2110 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002111 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2112 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002113 if ((LShVal + RShVal) != OpSizeInBits)
2114 return 0;
2115
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002117 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002118 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002119 else
Bill Wendling317bd702009-01-30 21:14:50 +00002120 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner516b9622006-09-14 20:50:57 +00002122 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002123 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002124 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002125
Gabor Greifba36cb52008-08-28 21:40:38 +00002126 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002127 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2128 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002129 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002130 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002131 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2132 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Bill Wendling317bd702009-01-30 21:14:50 +00002135 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Gabor Greifba36cb52008-08-28 21:40:38 +00002138 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002140
Chris Lattner516b9622006-09-14 20:50:57 +00002141 // If there is a mask here, and we have a variable shift, we can't be sure
2142 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002143 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002144 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Chris Lattner516b9622006-09-14 20:50:57 +00002146 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2147 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002148 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2149 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002150 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002151 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002152 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002153 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002154 return DAG.getNode(ISD::ROTL, DL, VT,
2155 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002156 else
Bill Wendling317bd702009-01-30 21:14:50 +00002157 return DAG.getNode(ISD::ROTR, DL, VT,
2158 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002159 }
Chris Lattner516b9622006-09-14 20:50:57 +00002160 }
2161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002162
Chris Lattner516b9622006-09-14 20:50:57 +00002163 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2164 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002165 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2166 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002168 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002169 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002170 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002171 return DAG.getNode(ISD::ROTR, DL, VT,
2172 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002173 else
Bill Wendling317bd702009-01-30 21:14:50 +00002174 return DAG.getNode(ISD::ROTL, DL, VT,
2175 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002176 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002177 }
2178 }
2179
Dan Gohman74feef22008-10-17 01:23:35 +00002180 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002181 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2182 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002183 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2184 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002185 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2186 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002187 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2188 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2190 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002191 if (RExtOp0.getOpcode() == ISD::SUB &&
2192 RExtOp0.getOperand(1) == LExtOp0) {
2193 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002194 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002195 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002196 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002197 if (ConstantSDNode *SUBC =
2198 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002199 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002200 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2201 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002202 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002203 }
2204 }
2205 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2206 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002207 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002208 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002209 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002210 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002211 if (ConstantSDNode *SUBC =
2212 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002213 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002214 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2215 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002216 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002217 }
2218 }
Chris Lattner516b9622006-09-14 20:50:57 +00002219 }
2220 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Chris Lattner516b9622006-09-14 20:50:57 +00002222 return 0;
2223}
2224
Dan Gohman475871a2008-07-27 21:46:04 +00002225SDValue DAGCombiner::visitXOR(SDNode *N) {
2226 SDValue N0 = N->getOperand(0);
2227 SDValue N1 = N->getOperand(1);
2228 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002229 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2230 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002232
Dan Gohman7f321562007-06-25 16:23:39 +00002233 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002238
Evan Cheng26471c42008-03-25 20:08:07 +00002239 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2240 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2241 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002242 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002243 if (N0.getOpcode() == ISD::UNDEF)
2244 return N0;
2245 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002246 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002247 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002248 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002249 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002250 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002251 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002252 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002254 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002255 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002256 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002257 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002258 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002259 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002260
Nate Begeman1d4d4142005-09-01 00:19:25 +00002261 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002262 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002264 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2265 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002266
Duncan Sands25cf2272008-11-24 14:53:14 +00002267 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002268 switch (N0.getOpcode()) {
2269 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002270 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00002271 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002272 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002273 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002274 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002275 N0.getOperand(3), NotCC);
2276 }
2277 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002279
Chris Lattner61c5ff42007-09-10 21:39:07 +00002280 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002281 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002282 N0.getNode()->hasOneUse() &&
2283 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002286 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002287 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002288 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Bill Wendling317bd702009-01-30 21:14:50 +00002291 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002293 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002294 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002295 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2296 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002297 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2298 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002299 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002300 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 }
2302 }
Bill Wendling317bd702009-01-30 21:14:50 +00002303 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00002304 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00002305 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002306 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002307 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2308 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002309 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2310 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002311 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002312 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 }
2314 }
Bill Wendling317bd702009-01-30 21:14:50 +00002315 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00002316 if (N1C && N0.getOpcode() == ISD::XOR) {
2317 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2318 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2319 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00002320 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2321 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002322 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002323 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002324 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2325 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002326 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002327 }
2328 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002329 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002330 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002331 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002332 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002333 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002334 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2335 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Chenga87008d2009-02-25 22:49:59 +00002336 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2337 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002338 }
2339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Chris Lattner35e5c142006-05-05 05:51:50 +00002341 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2342 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002343 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002344 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Chris Lattner3e104b12006-04-08 04:15:24 +00002347 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002348 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002349 SimplifyDemandedBits(SDValue(N, 0)))
2350 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002351
Dan Gohman475871a2008-07-27 21:46:04 +00002352 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353}
2354
Chris Lattnere70da202007-12-06 07:33:36 +00002355/// visitShiftByConstant - Handle transforms common to the three shifts, when
2356/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002357SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002359 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Chris Lattnere70da202007-12-06 07:33:36 +00002361 // We want to pull some binops through shifts, so that we have (and (shift))
2362 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2363 // thing happens with address calculations, so it's important to canonicalize
2364 // it.
2365 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Chris Lattnere70da202007-12-06 07:33:36 +00002367 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002368 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002369 case ISD::OR:
2370 case ISD::XOR:
2371 HighBitSet = false; // We can only transform sra if the high bit is clear.
2372 break;
2373 case ISD::AND:
2374 HighBitSet = true; // We can only transform sra if the high bit is set.
2375 break;
2376 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00002377 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002378 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002379 HighBitSet = false; // We can only transform sra if the high bit is clear.
2380 break;
2381 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002382
Chris Lattnere70da202007-12-06 07:33:36 +00002383 // We require the RHS of the binop to be a constant as well.
2384 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002385 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002386
2387 // FIXME: disable this unless the input to the binop is a shift by a constant.
2388 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002389 //
Bill Wendling88103372009-01-30 21:37:17 +00002390 // void foo(int *X, int i) { X[i & 1235] = 1; }
2391 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002392 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002393 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002394 BinOpLHSVal->getOpcode() != ISD::SRA &&
2395 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2396 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002397 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002398
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Bill Wendling88103372009-01-30 21:37:17 +00002401 // If this is a signed shift right, and the high bit is modified by the
2402 // logical operation, do not perform the transformation. The highBitSet
2403 // boolean indicates the value of the high bit of the constant which would
2404 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002405 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002406 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2407 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002408 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002409 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002410
Chris Lattnere70da202007-12-06 07:33:36 +00002411 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002412 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2413 N->getValueType(0),
2414 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002415
2416 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002417 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2418 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002419
2420 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002421 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002422}
2423
Dan Gohman475871a2008-07-27 21:46:04 +00002424SDValue DAGCombiner::visitSHL(SDNode *N) {
2425 SDValue N0 = N->getOperand(0);
2426 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002427 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2428 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002429 EVT VT = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002430 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002431
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002434 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002436 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002437 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002439 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002440 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002442 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002445 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002448 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002449 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002450 N1.getOperand(0).getOpcode() == ISD::AND &&
2451 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002452 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002453 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002454 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002455 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002456 APInt TruncC = N101C->getAPIntValue();
2457 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002458 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002459 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2460 DAG.getNode(ISD::TRUNCATE,
2461 N->getDebugLoc(),
2462 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002463 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002464 }
2465 }
2466
Dan Gohman475871a2008-07-27 21:46:04 +00002467 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2468 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002469
2470 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002471 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002473 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2474 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002476 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002477 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 }
Bill Wendling88103372009-01-30 21:37:17 +00002480 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2481 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002482 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002484 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00002485 if (c1 < VT.getSizeInBits()) {
2486 uint64_t c2 = N1C->getZExtValue();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002487 SDValue HiBitsMask =
2488 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2489 VT.getSizeInBits() - c1),
2490 VT);
Evan Chengd101a722009-07-21 05:40:15 +00002491 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2492 N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002493 HiBitsMask);
Evan Chengd101a722009-07-21 05:40:15 +00002494 if (c2 > c1)
2495 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2496 DAG.getConstant(c2-c1, N1.getValueType()));
2497 else
2498 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2499 DAG.getConstant(c1-c2, N1.getValueType()));
2500 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501 }
Bill Wendling88103372009-01-30 21:37:17 +00002502 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002503 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2504 SDValue HiBitsMask =
2505 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2506 VT.getSizeInBits() -
2507 N1C->getZExtValue()),
2508 VT);
Bill Wendling88103372009-01-30 21:37:17 +00002509 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002510 HiBitsMask);
2511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002512
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002513 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514}
2515
Dan Gohman475871a2008-07-27 21:46:04 +00002516SDValue DAGCombiner::visitSRA(SDNode *N) {
2517 SDValue N0 = N->getOperand(0);
2518 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002519 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2520 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002522
Bill Wendling88103372009-01-30 21:37:17 +00002523 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002524 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002525 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002526 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002528 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002530 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002531 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002532 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002533 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00002534 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002535 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002536 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002538 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2539 // sext_inreg.
2540 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002541 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Owen Anderson23b9b192009-08-12 00:36:31 +00002542 EVT EVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002543 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002544 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
2545 N0.getOperand(0), DAG.getValueType(EVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002546 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002547
Bill Wendling88103372009-01-30 21:37:17 +00002548 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002549 if (N1C && N0.getOpcode() == ISD::SRA) {
2550 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002551 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Bill Wendling88103372009-01-30 21:37:17 +00002553 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002554 DAG.getConstant(Sum, N1C->getValueType(0)));
2555 }
2556 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002557
Bill Wendling88103372009-01-30 21:37:17 +00002558 // fold (sra (shl X, m), (sub result_size, n))
2559 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00002560 // result_size - n != m.
2561 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00002562 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002563 if (N0.getOpcode() == ISD::SHL) {
2564 // Get the two constanst of the shifts, CN0 = m, CN = n.
2565 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2566 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002567 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 unsigned VTValSize = VT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00002569 EVT TruncVT =
Owen Anderson23b9b192009-08-12 00:36:31 +00002570 EVT::getIntegerVT(*DAG.getContext(), VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002571 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00002572 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002573
Scott Michelfdc40a02009-02-17 22:15:04 +00002574 // If the shift is not a no-op (in which case this should be just a sign
2575 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002576 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002577 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00002578 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002579 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2580 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002581 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002582
Duncan Sands92abc622009-01-31 15:50:11 +00002583 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002584 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2585 N0.getOperand(0), Amt);
2586 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2587 Shift);
2588 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2589 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002590 }
2591 }
2592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002593
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002594 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002595 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002596 N1.getOperand(0).getOpcode() == ISD::AND &&
2597 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002598 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002599 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002600 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002601 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002602 APInt TruncC = N101C->getAPIntValue();
2603 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002604 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002605 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002606 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002607 DAG.getNode(ISD::TRUNCATE,
2608 N->getDebugLoc(),
2609 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002610 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002611 }
2612 }
2613
Scott Michelfdc40a02009-02-17 22:15:04 +00002614 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002615 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2616 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002617
2618
Nate Begeman1d4d4142005-09-01 00:19:25 +00002619 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002620 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002621 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002622
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002623 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002624}
2625
Dan Gohman475871a2008-07-27 21:46:04 +00002626SDValue DAGCombiner::visitSRL(SDNode *N) {
2627 SDValue N0 = N->getOperand(0);
2628 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002629 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2630 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002631 EVT VT = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002632 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002633
Nate Begeman1d4d4142005-09-01 00:19:25 +00002634 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002635 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002636 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002638 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002639 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002641 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002642 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002643 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002644 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002645 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002646 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002647 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002648 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002649 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002650
Bill Wendling88103372009-01-30 21:37:17 +00002651 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002653 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002654 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2655 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002657 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002658 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002659 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002660 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002661
Chris Lattner06afe072006-05-05 22:53:17 +00002662 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2663 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2664 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00002665 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002666 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00002667 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00002668
Bill Wendling88103372009-01-30 21:37:17 +00002669 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2670 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002671 AddToWorkList(SmallShift.getNode());
Bill Wendling88103372009-01-30 21:37:17 +00002672 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
Chris Lattner06afe072006-05-05 22:53:17 +00002673 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002674
Chris Lattner3657ffe2006-10-12 20:23:19 +00002675 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2676 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00002677 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002678 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00002679 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00002680 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002681
Chris Lattner350bec02006-04-02 06:11:11 +00002682 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00002683 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002685 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002686 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002687 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00002688
Chris Lattner350bec02006-04-02 06:11:11 +00002689 // If any of the input bits are KnownOne, then the input couldn't be all
2690 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002691 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002692
Chris Lattner350bec02006-04-02 06:11:11 +00002693 // If all of the bits input the to ctlz node are known to be zero, then
2694 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002695 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002696 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002697
Chris Lattner350bec02006-04-02 06:11:11 +00002698 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00002699 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00002700 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00002701 // could be set on input to the CTLZ node. If this bit is set, the SRL
2702 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2703 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002704 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00002706
Chris Lattner350bec02006-04-02 06:11:11 +00002707 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00002708 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands92abc622009-01-31 15:50:11 +00002709 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002710 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002711 }
Bill Wendling88103372009-01-30 21:37:17 +00002712
2713 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
2714 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00002715 }
2716 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002717
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002718 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002719 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002720 N1.getOperand(0).getOpcode() == ISD::AND &&
2721 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002722 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002723 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002724 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002725 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002726 APInt TruncC = N101C->getAPIntValue();
2727 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002728 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002729 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002730 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002731 DAG.getNode(ISD::TRUNCATE,
2732 N->getDebugLoc(),
2733 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002734 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002735 }
2736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002737
Chris Lattner61a4c072007-04-18 03:06:49 +00002738 // fold operands of srl based on knowledge that the low bits are not
2739 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002740 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2741 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002742
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002743 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002744}
2745
Dan Gohman475871a2008-07-27 21:46:04 +00002746SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2747 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002748 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002749
2750 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002751 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002752 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002753 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002754}
2755
Dan Gohman475871a2008-07-27 21:46:04 +00002756SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2757 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002758 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002759
Nate Begeman1d4d4142005-09-01 00:19:25 +00002760 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002761 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002762 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002763 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764}
2765
Dan Gohman475871a2008-07-27 21:46:04 +00002766SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2767 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002769
Nate Begeman1d4d4142005-09-01 00:19:25 +00002770 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002771 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002772 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002773 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774}
2775
Dan Gohman475871a2008-07-27 21:46:04 +00002776SDValue DAGCombiner::visitSELECT(SDNode *N) {
2777 SDValue N0 = N->getOperand(0);
2778 SDValue N1 = N->getOperand(1);
2779 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2782 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00002783 EVT VT = N->getValueType(0);
2784 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002785
Bill Wendling34584e62009-01-30 22:02:18 +00002786 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002787 if (N1 == N2)
2788 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002789 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002790 if (N0C && !N0C->isNullValue())
2791 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002792 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00002793 if (N0C && N0C->isNullValue())
2794 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00002795 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00002797 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2798 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00002799 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00002801 (VT0.isInteger() &&
2802 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002803 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002804 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00002805 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00002806 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
2807 N0, DAG.getConstant(1, VT0));
2808 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
2809 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002810 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002811 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00002812 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
2813 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00002814 }
Bill Wendling34584e62009-01-30 22:02:18 +00002815 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002816 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00002817 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00002818 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00002819 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002820 }
Bill Wendling34584e62009-01-30 22:02:18 +00002821 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002822 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002823 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00002824 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00002825 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002826 }
Bill Wendling34584e62009-01-30 22:02:18 +00002827 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00002829 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
2830 // fold (select X, X, Y) -> (or X, Y)
2831 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002832 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00002833 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2834 // fold (select X, Y, X) -> (and X, Y)
2835 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00002837 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002838
Chris Lattner40c62d52005-10-18 06:04:22 +00002839 // If we can fold this based on the true/false value, do so.
2840 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002841 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002842
Nate Begeman44728a72005-09-19 22:34:01 +00002843 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002844 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002845 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00002846 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00002847 // having to say they don't support SELECT_CC on every type the DAG knows
2848 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00002849 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00002850 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00002851 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
2852 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00002853 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00002854 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002855 }
Bill Wendling34584e62009-01-30 22:02:18 +00002856
Dan Gohman475871a2008-07-27 21:46:04 +00002857 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002858}
2859
Dan Gohman475871a2008-07-27 21:46:04 +00002860SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2861 SDValue N0 = N->getOperand(0);
2862 SDValue N1 = N->getOperand(1);
2863 SDValue N2 = N->getOperand(2);
2864 SDValue N3 = N->getOperand(3);
2865 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002866 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002867
Nate Begeman44728a72005-09-19 22:34:01 +00002868 // fold select_cc lhs, rhs, x, x, cc -> x
2869 if (N2 == N3)
2870 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002871
Chris Lattner5f42a242006-09-20 06:19:26 +00002872 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002873 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002874 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002875 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002876
Gabor Greifba36cb52008-08-28 21:40:38 +00002877 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002878 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002879 return N2; // cond always true -> true val
2880 else
2881 return N3; // cond always false -> false val
2882 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002883
Chris Lattner5f42a242006-09-20 06:19:26 +00002884 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002885 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00002886 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
2887 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00002888 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00002889
Chris Lattner40c62d52005-10-18 06:04:22 +00002890 // If we can fold this based on the true/false value, do so.
2891 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002892 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00002893
Nate Begeman44728a72005-09-19 22:34:01 +00002894 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00002895 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002896}
2897
Dan Gohman475871a2008-07-27 21:46:04 +00002898SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002899 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002900 cast<CondCodeSDNode>(N->getOperand(2))->get(),
2901 N->getDebugLoc());
Nate Begeman452d7be2005-09-16 00:54:12 +00002902}
2903
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002904// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00002905// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002906// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00002907// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002908static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002909 unsigned ExtOpc,
2910 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002911 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002912 bool HasCopyToRegUses = false;
2913 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002914 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2915 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002916 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002917 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002918 if (User == N)
2919 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00002920 if (UI.getUse().getResNo() != N0.getResNo())
2921 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002922 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00002923 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002924 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2925 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2926 // Sign bits will be lost after a zext.
2927 return false;
2928 bool Add = false;
2929 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002930 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002931 if (UseOp == N0)
2932 continue;
2933 if (!isa<ConstantSDNode>(UseOp))
2934 return false;
2935 Add = true;
2936 }
2937 if (Add)
2938 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00002939 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002940 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00002941 // If truncates aren't free and there are users we can't
2942 // extend, it isn't worthwhile.
2943 if (!isTruncFree)
2944 return false;
2945 // Remember if this value is live-out.
2946 if (User->getOpcode() == ISD::CopyToReg)
2947 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 }
2949
2950 if (HasCopyToRegUses) {
2951 bool BothLiveOut = false;
2952 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2953 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00002954 SDUse &Use = UI.getUse();
2955 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
2956 BothLiveOut = true;
2957 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002958 }
2959 }
2960 if (BothLiveOut)
2961 // Both unextended and extended values are live out. There had better be
2962 // good a reason for the transformation.
2963 return ExtendNodes.size();
2964 }
2965 return true;
2966}
2967
Dan Gohman475871a2008-07-27 21:46:04 +00002968SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2969 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002970 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002971
Nate Begeman1d4d4142005-09-01 00:19:25 +00002972 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002973 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00002974 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002975
Nate Begeman1d4d4142005-09-01 00:19:25 +00002976 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002977 // fold (sext (aext x)) -> (sext x)
2978 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00002979 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
2980 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002981
Chris Lattner22558872007-02-26 03:13:59 +00002982 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002983 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2984 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002985 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2986 if (NarrowLoad.getNode()) {
2987 if (NarrowLoad.getNode() != N0.getNode())
2988 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanc7b34442009-04-27 02:00:55 +00002989 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00002990 }
Evan Chengc88138f2007-03-22 01:54:19 +00002991
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002992 // See if the value being truncated is already sign extended. If so, just
2993 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002994 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002995 unsigned OpBits = Op.getValueType().getSizeInBits();
2996 unsigned MidBits = N0.getValueType().getSizeInBits();
2997 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002998 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00002999
Chris Lattner22558872007-02-26 03:13:59 +00003000 if (OpBits == DestBits) {
3001 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3002 // bits, it is already ready.
3003 if (NumSignBits > DestBits-MidBits)
3004 return Op;
3005 } else if (OpBits < DestBits) {
3006 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3007 // bits, just sext from i32.
3008 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003009 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003010 } else {
3011 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3012 // bits, just truncate to i32.
3013 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003014 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003016
Chris Lattner22558872007-02-26 03:13:59 +00003017 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003018 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3019 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003020 if (Op.getValueType().bitsLT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003021 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003022 else if (Op.getValueType().bitsGT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003023 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3024 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Chris Lattner22558872007-02-26 03:13:59 +00003025 DAG.getValueType(N0.getValueType()));
3026 }
Chris Lattner6007b842006-09-21 06:00:20 +00003027 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003028
Evan Cheng110dec22005-12-14 02:19:23 +00003029 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003030 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003031 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003032 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003033 bool DoXform = true;
3034 SmallVector<SDNode*, 4> SetCCs;
3035 if (!N0.hasOneUse())
3036 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3037 if (DoXform) {
3038 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003039 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3040 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003041 LN0->getBasePtr(), LN0->getSrcValue(),
3042 LN0->getSrcValueOffset(),
3043 N0.getValueType(),
3044 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003045 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003046 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3047 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003048 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003049
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003050 // Extend SetCC uses if necessary.
3051 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3052 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003054
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003055 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003056 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003057 if (SOp == Trunc)
3058 Ops.push_back(ExtLoad);
3059 else
Dan Gohman57fc82d2009-04-09 03:51:29 +00003060 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3061 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003062 }
3063
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003064 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003065 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003066 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003067 &Ops[0], Ops.size()));
3068 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003069
Dan Gohman475871a2008-07-27 21:46:04 +00003070 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003071 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003072 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003073
3074 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3075 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003076 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3077 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003078 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003079 EVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003080 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003081 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003082 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3083 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003084 LN0->getBasePtr(), LN0->getSrcValue(),
3085 LN0->getSrcValueOffset(), EVT,
3086 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003087 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003088 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003089 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3090 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003091 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003092 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003093 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003095
Chris Lattner20a35c32007-04-11 05:32:27 +00003096 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00003097 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3098 if (VT.isVector() &&
3099 // We know that the # elements of the results is the same as the
3100 // # elements of the compare (and the # elements of the compare result
3101 // for that matter). Check to see that they are the same size. If so,
3102 // we know that the element size of the sext'd result matches the
3103 // element size of the compare operands.
3104 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3105
3106 // Only do this before legalize for now.
3107 !LegalOperations) {
3108 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3109 N0.getOperand(1),
3110 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3111 }
3112
3113 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003114 SDValue NegOne =
3115 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003116 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003117 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003118 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003119 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003120 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003121 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00003122
3123
Scott Michelfdc40a02009-02-17 22:15:04 +00003124
Dan Gohman8f0ad582008-04-28 16:58:24 +00003125 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003126 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003127 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003128 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003129
Dan Gohman475871a2008-07-27 21:46:04 +00003130 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003131}
3132
Dan Gohman475871a2008-07-27 21:46:04 +00003133SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3134 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003135 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003136
Nate Begeman1d4d4142005-09-01 00:19:25 +00003137 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003138 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003139 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003140 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003141 // fold (zext (aext x)) -> (zext x)
3142 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003143 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3144 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003145
Evan Chengc88138f2007-03-22 01:54:19 +00003146 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3147 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003148 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3150 if (NarrowLoad.getNode()) {
3151 if (NarrowLoad.getNode() != N0.getNode())
3152 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003153 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003154 }
Evan Chengc88138f2007-03-22 01:54:19 +00003155 }
3156
Chris Lattner6007b842006-09-21 06:00:20 +00003157 // fold (zext (truncate x)) -> (and x, mask)
3158 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003159 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003161 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003162 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003163 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003164 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003165 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003166 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), N0.getValueType());
Chris Lattner6007b842006-09-21 06:00:20 +00003167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Dan Gohman97121ba2009-04-08 00:15:30 +00003169 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3170 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00003171 if (N0.getOpcode() == ISD::AND &&
3172 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003173 N0.getOperand(1).getOpcode() == ISD::Constant &&
3174 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3175 N0.getValueType()) ||
3176 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003178 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003179 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003180 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003181 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003182 }
Dan Gohman220a8232008-03-03 23:51:38 +00003183 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003184 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003185 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3186 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003188
Evan Cheng110dec22005-12-14 02:19:23 +00003189 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003191 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003192 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003193 bool DoXform = true;
3194 SmallVector<SDNode*, 4> SetCCs;
3195 if (!N0.hasOneUse())
3196 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3197 if (DoXform) {
3198 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003199 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3200 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003201 LN0->getBasePtr(), LN0->getSrcValue(),
3202 LN0->getSrcValueOffset(),
3203 N0.getValueType(),
3204 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003205 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003206 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3207 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003208 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003209
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003210 // Extend SetCC uses if necessary.
3211 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3212 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003213 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003214
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003215 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003216 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003217 if (SOp == Trunc)
3218 Ops.push_back(ExtLoad);
3219 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00003220 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3221 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003222 }
3223
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003224 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003225 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003226 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003227 &Ops[0], Ops.size()));
3228 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003229
Dan Gohman475871a2008-07-27 21:46:04 +00003230 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003231 }
Evan Cheng110dec22005-12-14 02:19:23 +00003232 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003233
3234 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3235 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003236 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3237 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003238 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003239 EVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003240 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003241 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003242 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3243 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003244 LN0->getBasePtr(), LN0->getSrcValue(),
3245 LN0->getSrcValueOffset(), EVT,
3246 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003247 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003248 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003249 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3250 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003251 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003252 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003253 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003255
Chris Lattner20a35c32007-04-11 05:32:27 +00003256 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3257 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003258 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003259 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00003260 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003261 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003262 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003263 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003264
Dan Gohman475871a2008-07-27 21:46:04 +00003265 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003266}
3267
Dan Gohman475871a2008-07-27 21:46:04 +00003268SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3269 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003270 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003271
Chris Lattner5ffc0662006-05-05 05:58:59 +00003272 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003273 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003274 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003275 // fold (aext (aext x)) -> (aext x)
3276 // fold (aext (zext x)) -> (zext x)
3277 // fold (aext (sext x)) -> (sext x)
3278 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3279 N0.getOpcode() == ISD::ZERO_EXTEND ||
3280 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003281 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003282
Evan Chengc88138f2007-03-22 01:54:19 +00003283 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3284 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3285 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003286 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3287 if (NarrowLoad.getNode()) {
3288 if (NarrowLoad.getNode() != N0.getNode())
3289 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003290 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003291 }
Evan Chengc88138f2007-03-22 01:54:19 +00003292 }
3293
Chris Lattner84750582006-09-20 06:29:17 +00003294 // fold (aext (truncate x))
3295 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003297 if (TruncOp.getValueType() == VT)
3298 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003299 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003300 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3301 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003303
Dan Gohman97121ba2009-04-08 00:15:30 +00003304 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3305 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00003306 if (N0.getOpcode() == ISD::AND &&
3307 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003308 N0.getOperand(1).getOpcode() == ISD::Constant &&
3309 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3310 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003312 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003313 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003314 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003315 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003316 }
Dan Gohman220a8232008-03-03 23:51:38 +00003317 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003318 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003319 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3320 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003322
Chris Lattner5ffc0662006-05-05 05:58:59 +00003323 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman57fc82d2009-04-09 03:51:29 +00003324 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003325 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003326 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003327 bool DoXform = true;
3328 SmallVector<SDNode*, 4> SetCCs;
3329 if (!N0.hasOneUse())
3330 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3331 if (DoXform) {
3332 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3333 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3334 LN0->getChain(),
3335 LN0->getBasePtr(), LN0->getSrcValue(),
3336 LN0->getSrcValueOffset(),
3337 N0.getValueType(),
3338 LN0->isVolatile(), LN0->getAlignment());
3339 CombineTo(N, ExtLoad);
3340 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3341 N0.getValueType(), ExtLoad);
3342 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3343
3344 // Extend SetCC uses if necessary.
3345 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3346 SDNode *SetCC = SetCCs[i];
3347 SmallVector<SDValue, 4> Ops;
3348
3349 for (unsigned j = 0; j != 2; ++j) {
3350 SDValue SOp = SetCC->getOperand(j);
3351 if (SOp == Trunc)
3352 Ops.push_back(ExtLoad);
3353 else
3354 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3355 N->getDebugLoc(), VT, SOp));
3356 }
3357
3358 Ops.push_back(SetCC->getOperand(2));
3359 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3360 SetCC->getValueType(0),
3361 &Ops[0], Ops.size()));
3362 }
3363
3364 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3365 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00003366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003367
Chris Lattner5ffc0662006-05-05 05:58:59 +00003368 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3369 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3370 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003371 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003372 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003373 N0.hasOneUse()) {
3374 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003375 EVT EVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003376 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3377 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003378 LN0->getSrcValue(),
3379 LN0->getSrcValueOffset(), EVT,
3380 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003381 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003382 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003383 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3384 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003385 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003386 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003388
Chris Lattner20a35c32007-04-11 05:32:27 +00003389 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3390 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003391 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003392 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003393 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003394 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003395 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003396 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003398
Dan Gohman475871a2008-07-27 21:46:04 +00003399 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003400}
3401
Chris Lattner2b4c2792007-10-13 06:35:54 +00003402/// GetDemandedBits - See if the specified operand can be simplified with the
3403/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003404/// simpler operand, otherwise return a null SDValue.
3405SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003406 switch (V.getOpcode()) {
3407 default: break;
3408 case ISD::OR:
3409 case ISD::XOR:
3410 // If the LHS or RHS don't contribute bits to the or, drop them.
3411 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3412 return V.getOperand(1);
3413 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3414 return V.getOperand(0);
3415 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003416 case ISD::SRL:
3417 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003418 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003419 break;
3420 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3421 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003422 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00003423
Dan Gohmancc91d632009-01-03 19:22:06 +00003424 // Watch out for shift count overflow though.
3425 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003426 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00003428 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003429 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00003430 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00003431 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003432 }
Dan Gohman475871a2008-07-27 21:46:04 +00003433 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003434}
3435
Evan Chengc88138f2007-03-22 01:54:19 +00003436/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3437/// bits and then truncated to a narrower type and where N is a multiple
3438/// of number of bits of the narrower type, transform it to a narrower load
3439/// from address + N / num of bits of new type. If the result is to be
3440/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003441SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003442 unsigned Opc = N->getOpcode();
3443 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003444 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003445 EVT VT = N->getValueType(0);
3446 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003447
Dan Gohman7f8613e2008-08-14 20:04:46 +00003448 // This transformation isn't valid for vector loads.
3449 if (VT.isVector())
3450 return SDValue();
3451
Evan Chenge177e302007-03-23 22:13:36 +00003452 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3453 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003454 if (Opc == ISD::SIGN_EXTEND_INREG) {
3455 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00003456 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3457 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003458 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003459 }
3460
Owen Andersone50ed302009-08-10 22:56:29 +00003461 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003462 unsigned ShAmt = 0;
Eli Friedman5aba5c02009-08-23 00:14:19 +00003463 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003464 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003465 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003466 // Is the shift amount a multiple of size of VT?
3467 if ((ShAmt & (EVTBits-1)) == 0) {
3468 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00003469 // Is the load width a multiple of size of VT?
3470 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00003471 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003472 }
3473 }
3474 }
3475
Duncan Sandsad205a72008-06-16 08:14:38 +00003476 // Do not generate loads of non-round integer types since these can
3477 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersone50ed302009-08-10 22:56:29 +00003478 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003479 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003480 // Do not change the width of a volatile load.
3481 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003482 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003483 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00003484
Evan Chengdae54ce2007-03-24 00:02:43 +00003485 // For big endian targets, we need to adjust the offset to the pointer to
3486 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003487 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003488 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003489 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003490 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3491 }
Bill Wendling8509c902009-01-30 22:33:24 +00003492
Evan Chengdae54ce2007-03-24 00:02:43 +00003493 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003494 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendling9729c5a2009-01-31 03:12:48 +00003495 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling8509c902009-01-30 22:33:24 +00003496 PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003497 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003498 AddToWorkList(NewPtr.getNode());
Bill Wendling8509c902009-01-30 22:33:24 +00003499
Dan Gohman475871a2008-07-27 21:46:04 +00003500 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling8509c902009-01-30 22:33:24 +00003501 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003502 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003503 LN0->isVolatile(), NewAlign)
Bill Wendling8509c902009-01-30 22:33:24 +00003504 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003505 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Owen Andersone50ed302009-08-10 22:56:29 +00003506 ExtVT, LN0->isVolatile(), NewAlign);
Bill Wendling8509c902009-01-30 22:33:24 +00003507
Dan Gohman764fd0c2009-01-21 15:17:51 +00003508 // Replace the old load's chain with the new load's chain.
3509 WorkListRemover DeadNodes(*this);
3510 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3511 &DeadNodes);
Bill Wendling8509c902009-01-30 22:33:24 +00003512
Dan Gohman764fd0c2009-01-21 15:17:51 +00003513 // Return the new loaded value.
3514 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003515 }
3516
Dan Gohman475871a2008-07-27 21:46:04 +00003517 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003518}
3519
Dan Gohman475871a2008-07-27 21:46:04 +00003520SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3521 SDValue N0 = N->getOperand(0);
3522 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003523 EVT VT = N->getValueType(0);
3524 EVT EVT = cast<VTSDNode>(N1)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 unsigned VTBits = VT.getSizeInBits();
3526 unsigned EVTBits = EVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003527
Nate Begeman1d4d4142005-09-01 00:19:25 +00003528 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003529 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00003530 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003531
Chris Lattner541a24f2006-05-06 22:43:44 +00003532 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003534 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003535
Nate Begeman646d7e22005-09-02 21:18:40 +00003536 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3537 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003538 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00003539 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3540 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003541 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003542
Dan Gohman75dcf082008-07-31 00:50:31 +00003543 // fold (sext_in_reg (sext x)) -> (sext x)
3544 // fold (sext_in_reg (aext x)) -> (sext x)
3545 // if x is small enough.
3546 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3547 SDValue N00 = N0.getOperand(0);
3548 if (N00.getValueType().getSizeInBits() < EVTBits)
Bill Wendling8509c902009-01-30 22:33:24 +00003549 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00003550 }
3551
Chris Lattner95a5e052007-04-17 19:03:21 +00003552 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003553 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003554 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003555
Chris Lattner95a5e052007-04-17 19:03:21 +00003556 // fold operands of sext_in_reg based on knowledge that the top bits are not
3557 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003558 if (SimplifyDemandedBits(SDValue(N, 0)))
3559 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003560
Evan Chengc88138f2007-03-22 01:54:19 +00003561 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3562 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003563 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003564 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003565 return NarrowLoad;
3566
Bill Wendling8509c902009-01-30 22:33:24 +00003567 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
3568 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00003569 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3570 if (N0.getOpcode() == ISD::SRL) {
3571 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003572 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003573 // We can turn this into an SRA iff the input to the SRL is already sign
3574 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003575 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003576 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00003577 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
3578 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00003579 }
3580 }
Evan Chengc88138f2007-03-22 01:54:19 +00003581
Nate Begemanded49632005-10-13 03:11:28 +00003582 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00003583 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003584 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003585 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003586 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003587 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003588 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003589 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3590 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003591 LN0->getBasePtr(), LN0->getSrcValue(),
3592 LN0->getSrcValueOffset(), EVT,
3593 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003594 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003595 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003596 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003597 }
3598 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003599 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003600 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003601 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003602 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003603 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003604 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003605 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3606 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003607 LN0->getBasePtr(), LN0->getSrcValue(),
3608 LN0->getSrcValueOffset(), EVT,
3609 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003610 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003611 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003612 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003613 }
Dan Gohman475871a2008-07-27 21:46:04 +00003614 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003615}
3616
Dan Gohman475871a2008-07-27 21:46:04 +00003617SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3618 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003619 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003620
3621 // noop truncate
3622 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003623 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003624 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003625 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00003626 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003627 // fold (truncate (truncate x)) -> (truncate x)
3628 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00003629 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003630 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003631 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3632 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003633 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003634 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00003635 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
3636 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003637 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003638 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00003639 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003640 else
3641 // if the source and dest are the same type, we can drop both the extend
3642 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003643 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003644 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003645
Chris Lattner2b4c2792007-10-13 06:35:54 +00003646 // See if we can simplify the input to this truncate through knowledge that
3647 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3648 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003649 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003650 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003651 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003652 if (Shorter.getNode())
Bill Wendling67a67682009-01-30 22:44:24 +00003653 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattner2b4c2792007-10-13 06:35:54 +00003654
Nate Begeman3df4d522005-10-12 20:40:40 +00003655 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003656 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003657 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003658}
3659
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003660static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003661 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003662 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003663 return Elt.getNode();
3664 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003665}
3666
3667/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00003668/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00003669SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003670 assert(N->getOpcode() == ISD::BUILD_PAIR);
3671
Nate Begemanabc01992009-06-05 21:37:30 +00003672 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
3673 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
3674 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003675 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00003676 EVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003677 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendling67a67682009-01-30 22:44:24 +00003678
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003679 if (ISD::isNON_EXTLoad(LD2) &&
3680 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003681 // If both are volatile this would reduce the number of volatile loads.
3682 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00003683 !LD1->isVolatile() &&
3684 !LD2->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003685 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Nate Begemanabc01992009-06-05 21:37:30 +00003686 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003687 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00003688 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00003689
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003690 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003691 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00003692 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
3693 LD1->getBasePtr(), LD1->getSrcValue(),
3694 LD1->getSrcValueOffset(), false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003695 }
Bill Wendling67a67682009-01-30 22:44:24 +00003696
Dan Gohman475871a2008-07-27 21:46:04 +00003697 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003698}
3699
Dan Gohman475871a2008-07-27 21:46:04 +00003700SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3701 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003702 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003703
Dan Gohman7f321562007-06-25 16:23:39 +00003704 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3705 // Only do this before legalize, since afterward the target may be depending
3706 // on the bitconvert.
3707 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003708 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003709 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003710 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003711 bool isSimple = true;
3712 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3713 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3714 N0.getOperand(i).getOpcode() != ISD::Constant &&
3715 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003716 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00003717 break;
3718 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003719
Owen Andersone50ed302009-08-10 22:56:29 +00003720 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003721 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003722 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00003723 if (isSimple)
Gabor Greifba36cb52008-08-28 21:40:38 +00003724 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003725 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003726
Dan Gohman3dd168d2008-09-05 01:58:21 +00003727 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003728 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling67a67682009-01-30 22:44:24 +00003729 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00003730 if (Res.getNode() != N) {
3731 if (!LegalOperations ||
3732 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
3733 return Res;
3734
3735 // Folding it resulted in an illegal node, and it's too late to
3736 // do that. Clean up the old node and forego the transformation.
3737 // Ideally this won't happen very often, because instcombine
3738 // and the earlier dagcombine runs (where illegal nodes are
3739 // permitted) should have folded most of them already.
3740 DAG.DeleteNode(Res.getNode());
3741 }
Chris Lattner94683772005-12-23 05:30:37 +00003742 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003743
Bill Wendling67a67682009-01-30 22:44:24 +00003744 // (conv (conv x, t1), t2) -> (conv x, t2)
3745 if (N0.getOpcode() == ISD::BIT_CONVERT)
3746 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
3747 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003748
Chris Lattner57104102005-12-23 05:44:41 +00003749 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003750 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003751 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003752 // Do not change the width of a volatile load.
3753 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003754 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003755 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003756 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00003757 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00003758 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00003759
Evan Cheng59d5b682007-05-07 21:27:48 +00003760 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00003761 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
3762 LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003763 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3764 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003765 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003766 CombineTo(N0.getNode(),
Bill Wendling67a67682009-01-30 22:44:24 +00003767 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
3768 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003769 Load.getValue(1));
3770 return Load;
3771 }
Chris Lattner57104102005-12-23 05:44:41 +00003772 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003773
Bill Wendling67a67682009-01-30 22:44:24 +00003774 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
3775 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00003776 // This often reduces constant pool loads.
3777 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003778 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling67a67682009-01-30 22:44:24 +00003779 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
3780 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003781 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00003782
Duncan Sands83ec4b62008-06-06 12:08:01 +00003783 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003784 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00003785 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3786 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003787 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00003788 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3789 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003790 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003791
Bill Wendling67a67682009-01-30 22:44:24 +00003792 // fold (bitconvert (fcopysign cst, x)) ->
3793 // (or (and (bitconvert x), sign), (and cst, (not sign)))
3794 // Note that we don't handle (copysign x, cst) because this can always be
3795 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003796 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003797 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003798 VT.isInteger() && !VT.isVector()) {
3799 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00003800 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Duncan Sands25cf2272008-11-24 14:53:14 +00003801 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003802 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003803 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00003804 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003805
Duncan Sands25cf2272008-11-24 14:53:14 +00003806 // If X has a different width than the result/lhs, sext it or truncate it.
3807 unsigned VTWidth = VT.getSizeInBits();
3808 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003809 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00003810 AddToWorkList(X.getNode());
3811 } else if (OrigXWidth > VTWidth) {
3812 // To get the sign bit in the right place, we have to shift it right
3813 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00003814 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003815 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00003816 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3817 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00003818 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00003819 AddToWorkList(X.getNode());
3820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003821
Duncan Sands25cf2272008-11-24 14:53:14 +00003822 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00003823 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00003824 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00003825 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003826
Bill Wendling9729c5a2009-01-31 03:12:48 +00003827 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00003828 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003829 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00003830 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00003831 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003832
Bill Wendling67a67682009-01-30 22:44:24 +00003833 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00003834 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003835 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003836
Scott Michelfdc40a02009-02-17 22:15:04 +00003837 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003838 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003839 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3840 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003841 return CombineLD;
3842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003843
Dan Gohman475871a2008-07-27 21:46:04 +00003844 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003845}
3846
Dan Gohman475871a2008-07-27 21:46:04 +00003847SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003849 return CombineConsecutiveLoads(N, VT);
3850}
3851
Dan Gohman7f321562007-06-25 16:23:39 +00003852/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00003853/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00003854/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003855SDValue DAGCombiner::
Owen Andersone50ed302009-08-10 22:56:29 +00003856ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
3857 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003858
Chris Lattner6258fb22006-04-02 02:53:43 +00003859 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003860 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003861
Duncan Sands83ec4b62008-06-06 12:08:01 +00003862 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3863 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003864
Chris Lattner6258fb22006-04-02 02:53:43 +00003865 // If this is a conversion of N elements of one type to N elements of another
3866 // type, convert each element. This handles FP<->INT cases.
3867 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003868 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003869 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00003870 SDValue Op = BV->getOperand(i);
3871 // If the vector element type is not legal, the BUILD_VECTOR operands
3872 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00003873 if (Op.getValueType() != SrcEltVT)
3874 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003875 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00003876 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00003877 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003878 }
Owen Anderson23b9b192009-08-12 00:36:31 +00003879 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003880 BV->getValueType(0).getVectorNumElements());
Evan Chenga87008d2009-02-25 22:49:59 +00003881 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3882 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003883 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003884
Chris Lattner6258fb22006-04-02 02:53:43 +00003885 // Otherwise, we're growing or shrinking the elements. To avoid having to
3886 // handle annoying details of growing/shrinking FP values, we convert them to
3887 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003888 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003889 // Convert the input float vector to a int vector where the elements are the
3890 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00003891 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00003892 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003893 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003894 SrcEltVT = IntVT;
3895 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003896
Chris Lattner6258fb22006-04-02 02:53:43 +00003897 // Now we know the input is an integer vector. If the output is a FP type,
3898 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00003901 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003902 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003903
Chris Lattner6258fb22006-04-02 02:53:43 +00003904 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003905 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Chris Lattner6258fb22006-04-02 02:53:43 +00003908 // Okay, we know the src/dst types are both integers of differing types.
3909 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003910 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003911 if (SrcBitSize < DstBitSize) {
3912 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00003913
Dan Gohman475871a2008-07-27 21:46:04 +00003914 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003915 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003916 i += NumInputsPerOutput) {
3917 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003918 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003919 bool EltIsUndef = true;
3920 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3921 // Shift the previously computed bits over.
3922 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003923 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003924 if (Op.getOpcode() == ISD::UNDEF) continue;
3925 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00003926
Bob Wilsonb1303d02009-04-13 22:05:19 +00003927 NewBits |= (APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
3928 zextOrTrunc(SrcBitSize).zext(DstBitSize));
Chris Lattner6258fb22006-04-02 02:53:43 +00003929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003930
Chris Lattner6258fb22006-04-02 02:53:43 +00003931 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00003932 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00003933 else
3934 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3935 }
3936
Owen Anderson23b9b192009-08-12 00:36:31 +00003937 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00003938 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3939 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003940 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003941
Chris Lattner6258fb22006-04-02 02:53:43 +00003942 // Finally, this must be the case where we are shrinking elements: each input
3943 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003944 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003945 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00003946 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
3947 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00003949
Dan Gohman7f321562007-06-25 16:23:39 +00003950 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003951 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3952 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00003953 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00003954 continue;
3955 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00003956
Bob Wilsonb1303d02009-04-13 22:05:19 +00003957 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
3958 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00003959
Chris Lattner6258fb22006-04-02 02:53:43 +00003960 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003961 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003962 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003963 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003964 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00003965 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
3966 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003967 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003968 }
3969
3970 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003971 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003972 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3973 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00003974
Evan Chenga87008d2009-02-25 22:49:59 +00003975 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3976 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003977}
3978
Dan Gohman475871a2008-07-27 21:46:04 +00003979SDValue DAGCombiner::visitFADD(SDNode *N) {
3980 SDValue N0 = N->getOperand(0);
3981 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003982 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3983 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003984 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003985
Dan Gohman7f321562007-06-25 16:23:39 +00003986 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003987 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003988 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003989 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003991
Bill Wendlingb0162f52009-01-30 22:53:48 +00003992 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00003994 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003995 // canonicalize constant to RHS
3996 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00003997 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
3998 // fold (fadd A, 0) -> A
Dan Gohman760f86f2009-01-22 21:58:43 +00003999 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4000 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004001 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004002 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004003 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004004 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00004005 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sands25cf2272008-11-24 14:53:14 +00004006 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004007 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00004008 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004009
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004010 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4011 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004012 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004013 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004014 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4015 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004016
Dan Gohman475871a2008-07-27 21:46:04 +00004017 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004018}
4019
Dan Gohman475871a2008-07-27 21:46:04 +00004020SDValue DAGCombiner::visitFSUB(SDNode *N) {
4021 SDValue N0 = N->getOperand(0);
4022 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004023 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4024 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004025 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004026
Dan Gohman7f321562007-06-25 16:23:39 +00004027 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004028 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004029 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004030 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004031 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004032
Nate Begemana0e221d2005-10-18 00:28:13 +00004033 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004035 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004036 // fold (fsub A, 0) -> A
Dan Gohmana90c8e62009-01-23 19:10:37 +00004037 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4038 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004039 // fold (fsub 0, B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004040 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004041 if (isNegatibleForFree(N1, LegalOperations))
4042 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00004043 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004044 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00004045 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004046 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004047 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004048 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004049 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004050
Dan Gohman475871a2008-07-27 21:46:04 +00004051 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004052}
4053
Dan Gohman475871a2008-07-27 21:46:04 +00004054SDValue DAGCombiner::visitFMUL(SDNode *N) {
4055 SDValue N0 = N->getOperand(0);
4056 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004057 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4058 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004059 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004060
Dan Gohman7f321562007-06-25 16:23:39 +00004061 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004062 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004063 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004064 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004065 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004066
Nate Begeman11af4ea2005-10-17 20:40:11 +00004067 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004069 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004070 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004071 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004072 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4073 // fold (fmul A, 0) -> 0
Dan Gohman760f86f2009-01-22 21:58:43 +00004074 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4075 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00004076 // fold (fmul A, 0) -> 0, vector edition.
4077 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4078 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004079 // fold (fmul X, 2.0) -> (fadd X, X)
4080 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004081 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00004082 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00004083 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004084 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004085 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004086
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004087 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004088 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4089 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004090 // Both can be negated for free, check to see if at least one is cheaper
4091 // negated.
4092 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004093 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004094 GetNegatedExpression(N0, DAG, LegalOperations),
4095 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004096 }
4097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004098
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004099 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4100 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004101 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004102 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00004103 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00004104 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004105
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004107}
4108
Dan Gohman475871a2008-07-27 21:46:04 +00004109SDValue DAGCombiner::visitFDIV(SDNode *N) {
4110 SDValue N0 = N->getOperand(0);
4111 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004112 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4113 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004114 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004115
Dan Gohman7f321562007-06-25 16:23:39 +00004116 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004117 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004118 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004119 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004121
Nate Begemana148d982006-01-18 22:35:16 +00004122 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004124 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004125
4126
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004127 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004128 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4129 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004130 // Both can be negated for free, check to see if at least one is cheaper
4131 // negated.
4132 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00004133 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004134 GetNegatedExpression(N0, DAG, LegalOperations),
4135 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004136 }
4137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Dan Gohman475871a2008-07-27 21:46:04 +00004139 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004140}
4141
Dan Gohman475871a2008-07-27 21:46:04 +00004142SDValue DAGCombiner::visitFREM(SDNode *N) {
4143 SDValue N0 = N->getOperand(0);
4144 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004145 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4146 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004147 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004148
Nate Begemana148d982006-01-18 22:35:16 +00004149 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004151 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004152
Dan Gohman475871a2008-07-27 21:46:04 +00004153 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004154}
4155
Dan Gohman475871a2008-07-27 21:46:04 +00004156SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4157 SDValue N0 = N->getOperand(0);
4158 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004159 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4160 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004161 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004162
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004164 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004165
Chris Lattner12d83032006-03-05 05:30:57 +00004166 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004167 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004168 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4169 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004170 if (!V.isNegative()) {
4171 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004172 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004173 } else {
4174 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004175 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004176 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00004177 }
Chris Lattner12d83032006-03-05 05:30:57 +00004178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004179
Chris Lattner12d83032006-03-05 05:30:57 +00004180 // copysign(fabs(x), y) -> copysign(x, y)
4181 // copysign(fneg(x), y) -> copysign(x, y)
4182 // copysign(copysign(x,z), y) -> copysign(x, y)
4183 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4184 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004185 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4186 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00004187
4188 // copysign(x, abs(y)) -> abs(x)
4189 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004190 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004191
Chris Lattner12d83032006-03-05 05:30:57 +00004192 // copysign(x, copysign(y,z)) -> copysign(x, z)
4193 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004194 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4195 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004196
Chris Lattner12d83032006-03-05 05:30:57 +00004197 // copysign(x, fp_extend(y)) -> copysign(x, y)
4198 // copysign(x, fp_round(y)) -> copysign(x, y)
4199 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004200 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4201 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004202
Dan Gohman475871a2008-07-27 21:46:04 +00004203 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004204}
4205
Dan Gohman475871a2008-07-27 21:46:04 +00004206SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4207 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004208 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004209 EVT VT = N->getValueType(0);
4210 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00004211
Nate Begeman1d4d4142005-09-01 00:19:25 +00004212 // fold (sint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004214 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004215
Chris Lattnercda88752008-06-26 00:16:49 +00004216 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4217 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004218 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4219 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004220 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004221 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004222 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004223 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004224
Dan Gohman475871a2008-07-27 21:46:04 +00004225 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004226}
4227
Dan Gohman475871a2008-07-27 21:46:04 +00004228SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4229 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004230 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004231 EVT VT = N->getValueType(0);
4232 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004233
Nate Begeman1d4d4142005-09-01 00:19:25 +00004234 // fold (uint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004236 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004237
Chris Lattnercda88752008-06-26 00:16:49 +00004238 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4239 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004240 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4241 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004242 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004243 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004244 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004245 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004246
Dan Gohman475871a2008-07-27 21:46:04 +00004247 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004248}
4249
Dan Gohman475871a2008-07-27 21:46:04 +00004250SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4251 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004252 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004253 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004254
Nate Begeman1d4d4142005-09-01 00:19:25 +00004255 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004256 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004257 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4258
Dan Gohman475871a2008-07-27 21:46:04 +00004259 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004260}
4261
Dan Gohman475871a2008-07-27 21:46:04 +00004262SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4263 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004264 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004265 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004266
Nate Begeman1d4d4142005-09-01 00:19:25 +00004267 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004269 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4270
Dan Gohman475871a2008-07-27 21:46:04 +00004271 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004272}
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4275 SDValue N0 = N->getOperand(0);
4276 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004277 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004278 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004279
Nate Begeman1d4d4142005-09-01 00:19:25 +00004280 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004282 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004283
Chris Lattner79dbea52006-03-13 06:26:26 +00004284 // fold (fp_round (fp_extend x)) -> x
4285 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4286 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004287
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004288 // fold (fp_round (fp_round x)) -> (fp_round x)
4289 if (N0.getOpcode() == ISD::FP_ROUND) {
4290 // This is a value preserving truncation if both round's are.
4291 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004292 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00004293 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004294 DAG.getIntPtrConstant(IsTrunc));
4295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004296
Chris Lattner79dbea52006-03-13 06:26:26 +00004297 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004298 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00004299 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4300 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004302 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4303 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00004304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004305
Dan Gohman475871a2008-07-27 21:46:04 +00004306 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004307}
4308
Dan Gohman475871a2008-07-27 21:46:04 +00004309SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4310 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004311 EVT VT = N->getValueType(0);
4312 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004313 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004314
Nate Begeman1d4d4142005-09-01 00:19:25 +00004315 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004316 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004317 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004318 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004319 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004320
Dan Gohman475871a2008-07-27 21:46:04 +00004321 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004322}
4323
Dan Gohman475871a2008-07-27 21:46:04 +00004324SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4325 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004326 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004327 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004328
Chris Lattner5938bef2007-12-29 06:55:23 +00004329 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00004330 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004331 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004332 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004333
Nate Begeman1d4d4142005-09-01 00:19:25 +00004334 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004335 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004336 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004337
4338 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4339 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004340 if (N0.getOpcode() == ISD::FP_ROUND
4341 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004342 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004343 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004344 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004345 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4346 In, N0.getOperand(1));
4347 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00004348 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004349
Chris Lattner0bd48932008-01-17 07:00:52 +00004350 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004351 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004352 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004353 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004354 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004355 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4356 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004357 LN0->getBasePtr(), LN0->getSrcValue(),
4358 LN0->getSrcValueOffset(),
4359 N0.getValueType(),
4360 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004361 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004362 CombineTo(N0.getNode(),
4363 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4364 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004365 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004366 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004367 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004368
Dan Gohman475871a2008-07-27 21:46:04 +00004369 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004370}
4371
Dan Gohman475871a2008-07-27 21:46:04 +00004372SDValue DAGCombiner::visitFNEG(SDNode *N) {
4373 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004374
Duncan Sands25cf2272008-11-24 14:53:14 +00004375 if (isNegatibleForFree(N0, LegalOperations))
4376 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004377
Chris Lattner3bd39d42008-01-27 17:42:27 +00004378 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4379 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004380 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004381 N0.getOperand(0).getValueType().isInteger() &&
4382 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004384 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004385 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004386 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4387 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 AddToWorkList(Int.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004389 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4390 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004391 }
4392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004393
Dan Gohman475871a2008-07-27 21:46:04 +00004394 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004395}
4396
Dan Gohman475871a2008-07-27 21:46:04 +00004397SDValue DAGCombiner::visitFABS(SDNode *N) {
4398 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004399 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004400 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004401
Nate Begeman1d4d4142005-09-01 00:19:25 +00004402 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004403 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004404 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004405 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004406 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004407 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004408 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004409 // fold (fabs (fcopysign x, y)) -> (fabs x)
4410 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004411 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004412
Chris Lattner3bd39d42008-01-27 17:42:27 +00004413 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4414 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004415 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004416 N0.getOperand(0).getValueType().isInteger() &&
4417 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004418 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004419 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004420 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004421 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004422 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004423 AddToWorkList(Int.getNode());
Bill Wendlingc0debad2009-01-30 23:27:35 +00004424 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4425 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004426 }
4427 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004428
Dan Gohman475871a2008-07-27 21:46:04 +00004429 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004430}
4431
Dan Gohman475871a2008-07-27 21:46:04 +00004432SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4433 SDValue Chain = N->getOperand(0);
4434 SDValue N1 = N->getOperand(1);
4435 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004436 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Nate Begeman44728a72005-09-19 22:34:01 +00004438 // never taken branch, fold to chain
4439 if (N1C && N1C->isNullValue())
4440 return Chain;
4441 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004442 if (N1C && N1C->getAPIntValue() == 1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004443 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004444 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4445 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00004446 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004447 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4448 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004449 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004450 N1.getOperand(0), N1.getOperand(1), N2);
4451 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004452
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004453 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4454 // Match this pattern so that we can generate simpler code:
4455 //
4456 // %a = ...
4457 // %b = and i32 %a, 2
4458 // %c = srl i32 %b, 1
4459 // brcond i32 %c ...
4460 //
4461 // into
4462 //
4463 // %a = ...
4464 // %b = and %a, 2
4465 // %c = setcc eq %b, 0
4466 // brcond %c ...
4467 //
4468 // This applies only when the AND constant value has one bit set and the
4469 // SRL constant is equal to the log2 of the AND constant. The back-end is
4470 // smart enough to convert the result into a TEST/JMP sequence.
4471 SDValue Op0 = N1.getOperand(0);
4472 SDValue Op1 = N1.getOperand(1);
4473
4474 if (Op0.getOpcode() == ISD::AND &&
4475 Op0.hasOneUse() &&
4476 Op1.getOpcode() == ISD::Constant) {
4477 SDValue AndOp0 = Op0.getOperand(0);
4478 SDValue AndOp1 = Op0.getOperand(1);
4479
4480 if (AndOp1.getOpcode() == ISD::Constant) {
4481 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4482
4483 if (AndConst.isPowerOf2() &&
4484 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4485 SDValue SetCC =
4486 DAG.getSetCC(N->getDebugLoc(),
4487 TLI.getSetCCResultType(Op0.getValueType()),
4488 Op0, DAG.getConstant(0, Op0.getValueType()),
4489 ISD::SETNE);
4490
4491 // Replace the uses of SRL with SETCC
4492 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
4493 removeFromWorkList(N1.getNode());
4494 DAG.DeleteNode(N1.getNode());
4495 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00004496 MVT::Other, Chain, SetCC, N2);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004497 }
4498 }
4499 }
4500 }
4501
Dan Gohman475871a2008-07-27 21:46:04 +00004502 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004503}
4504
Chris Lattner3ea0b472005-10-05 06:47:48 +00004505// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4506//
Dan Gohman475871a2008-07-27 21:46:04 +00004507SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004508 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004509 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004510
Duncan Sands8eab8a22008-06-09 11:32:28 +00004511 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004512 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004513 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
4514 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004515 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004516
Gabor Greifba36cb52008-08-28 21:40:38 +00004517 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004518
4519 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004520 if (SCCC && !SCCC->isNullValue())
Owen Anderson825b72b2009-08-11 20:47:22 +00004521 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004522 N->getOperand(0), N->getOperand(4));
Nate Begemane17daeb2005-10-05 21:43:42 +00004523 // fold br_cc false, dest -> unconditional fall through
4524 if (SCCC && SCCC->isNullValue())
4525 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004526
Nate Begemane17daeb2005-10-05 21:43:42 +00004527 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004528 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00004529 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004530 N->getOperand(0), Simp.getOperand(2),
4531 Simp.getOperand(0), Simp.getOperand(1),
4532 N->getOperand(4));
4533
Dan Gohman475871a2008-07-27 21:46:04 +00004534 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004535}
4536
Duncan Sandsec87aa82008-06-15 20:12:31 +00004537/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4538/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004539/// and it has other uses besides the load / store. After the
4540/// transformation, the new indexed load / store has effectively folded
4541/// the add / subtract in and all of its other uses are redirected to the
4542/// new load / store.
4543bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004544 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004545 return false;
4546
4547 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004548 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00004549 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004550 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004551 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004552 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004553 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004554 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004555 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4556 return false;
4557 Ptr = LD->getBasePtr();
4558 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004559 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004560 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004561 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004562 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4563 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4564 return false;
4565 Ptr = ST->getBasePtr();
4566 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004567 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00004568 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004569 }
Chris Lattner448f2192006-11-11 00:39:41 +00004570
Chris Lattner9f1794e2006-11-11 00:56:29 +00004571 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4572 // out. There is no reason to make this a preinc/predec.
4573 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004574 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004575 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004576
Chris Lattner9f1794e2006-11-11 00:56:29 +00004577 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004578 SDValue BasePtr;
4579 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004580 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4581 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4582 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004583 // Don't create a indexed load / store with zero offset.
4584 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004585 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004586 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004587
Chris Lattner41e53fd2006-11-11 01:00:15 +00004588 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004589 // 1) The new base ptr is a frame index.
4590 // 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 +00004591 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004592 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004593 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004594 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004595
Chris Lattner41e53fd2006-11-11 01:00:15 +00004596 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4597 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00004598 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00004599 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004600
Chris Lattner41e53fd2006-11-11 01:00:15 +00004601 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004602 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004604 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004605 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004606 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004607
Evan Chengc843abe2007-05-24 02:35:39 +00004608 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004609 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004610 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4611 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004612 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004613 if (Use == N)
4614 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004615 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004616 return false;
4617
4618 if (!((Use->getOpcode() == ISD::LOAD &&
4619 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004620 (Use->getOpcode() == ISD::STORE &&
4621 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004622 RealUse = true;
4623 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004624
Chris Lattner9f1794e2006-11-11 00:56:29 +00004625 if (!RealUse)
4626 return false;
4627
Dan Gohman475871a2008-07-27 21:46:04 +00004628 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004629 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004630 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4631 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004632 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00004633 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4634 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004635 ++PreIndexedNodes;
4636 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004637 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004638 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004639 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004640 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004641 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004642 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004643 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004644 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004645 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004646 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004647 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004648 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004649 }
4650
Chris Lattner9f1794e2006-11-11 00:56:29 +00004651 // Finally, since the node is now dead, remove it from the graph.
4652 DAG.DeleteNode(N);
4653
4654 // Replace the uses of Ptr with uses of the updated base value.
4655 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004656 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004657 removeFromWorkList(Ptr.getNode());
4658 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004659
4660 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004661}
4662
Duncan Sandsec87aa82008-06-15 20:12:31 +00004663/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004664/// add / sub of the base pointer node into a post-indexed load / store.
4665/// The transformation folded the add / subtract into the new indexed
4666/// load / store effectively and all of its uses are redirected to the
4667/// new load / store.
4668bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004669 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004670 return false;
4671
4672 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00004674 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004675 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004676 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004677 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004678 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004679 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4680 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4681 return false;
4682 Ptr = LD->getBasePtr();
4683 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004684 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004685 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004686 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004687 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4688 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4689 return false;
4690 Ptr = ST->getBasePtr();
4691 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004692 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00004693 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00004694 }
Chris Lattner448f2192006-11-11 00:39:41 +00004695
Gabor Greifba36cb52008-08-28 21:40:38 +00004696 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004697 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004698
Gabor Greifba36cb52008-08-28 21:40:38 +00004699 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4700 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004701 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004702 if (Op == N ||
4703 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4704 continue;
4705
Dan Gohman475871a2008-07-27 21:46:04 +00004706 SDValue BasePtr;
4707 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004708 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4709 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4710 if (Ptr == Offset)
4711 std::swap(BasePtr, Offset);
4712 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004713 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004714 // Don't create a indexed load / store with zero offset.
4715 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004716 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004717 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004718
Chris Lattner9f1794e2006-11-11 00:56:29 +00004719 // Try turning it into a post-indexed load / store except when
4720 // 1) All uses are load / store ops that use it as base ptr.
4721 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4722 // nor a successor of N. Otherwise, if Op is folded that would
4723 // create a cycle.
4724
Evan Chengcaab1292009-05-06 18:25:01 +00004725 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
4726 continue;
4727
Chris Lattner9f1794e2006-11-11 00:56:29 +00004728 // Check for #1.
4729 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004730 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4731 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004732 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004733 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004734 continue;
4735
Chris Lattner9f1794e2006-11-11 00:56:29 +00004736 // If all the uses are load / store addresses, then don't do the
4737 // transformation.
4738 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4739 bool RealUse = false;
4740 for (SDNode::use_iterator III = Use->use_begin(),
4741 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004742 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004743 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004744 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004745 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004746 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004747 RealUse = true;
4748 }
Chris Lattner448f2192006-11-11 00:39:41 +00004749
Chris Lattner9f1794e2006-11-11 00:56:29 +00004750 if (!RealUse) {
4751 TryNext = true;
4752 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004753 }
4754 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004755 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004756
Chris Lattner9f1794e2006-11-11 00:56:29 +00004757 if (TryNext)
4758 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004759
Chris Lattner9f1794e2006-11-11 00:56:29 +00004760 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004761 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00004763 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4764 BasePtr, Offset, AM)
4765 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4766 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004767 ++PostIndexedNodes;
4768 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004769 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004770 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004771 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004772 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004773 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004774 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004775 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004776 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004777 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004778 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004779 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004780 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004781 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004782
Chris Lattner9f1794e2006-11-11 00:56:29 +00004783 // Finally, since the node is now dead, remove it from the graph.
4784 DAG.DeleteNode(N);
4785
4786 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004787 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004788 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004789 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004790 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004791 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004792 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004793 }
4794 }
4795 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004796
Chris Lattner448f2192006-11-11 00:39:41 +00004797 return false;
4798}
4799
Chris Lattner00161a62008-01-25 07:20:16 +00004800/// InferAlignment - If we can infer some alignment information from this
4801/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004802static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004803 // If this is a direct reference to a stack slot, use information about the
4804 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004805 int FrameIdx = 1 << 31;
4806 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004807 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004808 FrameIdx = FI->getIndex();
Scott Michelfdc40a02009-02-17 22:15:04 +00004809 } else if (Ptr.getOpcode() == ISD::ADD &&
Chris Lattner1329cb82008-01-26 19:45:50 +00004810 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4811 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4812 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4813 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004814 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004815
Chris Lattner1329cb82008-01-26 19:45:50 +00004816 if (FrameIdx != (1 << 31)) {
4817 // FIXME: Handle FI+CST.
4818 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4819 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004820 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004821
4822 // The alignment of the frame index can be determined from its offset from
4823 // the incoming frame position. If the frame object is at offset 32 and
4824 // the stack is guaranteed to be 16-byte aligned, then we know that the
4825 // object is 16-byte aligned.
4826 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4827 unsigned Align = MinAlign(ObjectOffset, StackAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00004828
Chris Lattner1329cb82008-01-26 19:45:50 +00004829 // Finally, the frame object itself may have a known alignment. Factor
4830 // the alignment + offset into a new alignment. For example, if we know
4831 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4832 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4833 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
Scott Michelfdc40a02009-02-17 22:15:04 +00004834 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
Chris Lattner1329cb82008-01-26 19:45:50 +00004835 FrameOffset);
4836 return std::max(Align, FIInfoAlign);
4837 }
4838 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004839
Chris Lattner00161a62008-01-25 07:20:16 +00004840 return 0;
4841}
Chris Lattner448f2192006-11-11 00:39:41 +00004842
Dan Gohman475871a2008-07-27 21:46:04 +00004843SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004844 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue Chain = LD->getChain();
4846 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00004847
Chris Lattner00161a62008-01-25 07:20:16 +00004848 // Try to infer better alignment information than the load already has.
Bill Wendling98a366d2009-04-29 23:29:43 +00004849 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004850 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4851 if (Align > LD->getAlignment())
Bill Wendlingc0debad2009-01-30 23:27:35 +00004852 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
4853 LD->getValueType(0),
Chris Lattner00161a62008-01-25 07:20:16 +00004854 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004855 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004856 LD->isVolatile(), Align);
4857 }
4858 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004859
4860 // If load is not volatile and there are no uses of the loaded value (and
4861 // the updated indexed value in case of indexed loads), change uses of the
4862 // chain value into uses of the chain input (i.e. delete the dead load).
4863 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00004865 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004866 if (N->hasNUsesOfValue(0, 0)) {
4867 // It's not safe to use the two value CombineTo variant here. e.g.
4868 // v1, chain2 = load chain1, loc
4869 // v2, chain3 = load chain2, loc
4870 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004871 // Now we replace use of chain2 with chain1. This makes the second load
4872 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004873 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004875 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004876 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004877 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00004878
Chris Lattner125991a2008-01-24 07:57:06 +00004879 if (N->use_empty()) {
4880 removeFromWorkList(N);
4881 DAG.DeleteNode(N);
4882 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004883
Dan Gohman475871a2008-07-27 21:46:04 +00004884 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004885 }
Evan Cheng498f5592007-05-01 08:53:39 +00004886 } else {
4887 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00004888 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Evan Cheng498f5592007-05-01 08:53:39 +00004889 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00004890 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004891 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004892 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004893 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004894 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004895 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4896 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00004897 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004898 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004899 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004900 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004901 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004902 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004903 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004904 }
4905 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004906
Chris Lattner01a22022005-10-10 22:04:48 +00004907 // If this load is directly stored, replace the load value with the stored
4908 // value.
4909 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004910 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004911 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4912 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004913 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004914 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4915 if (PrevST->getBasePtr() == Ptr &&
4916 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004917 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004918 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004920
Jim Laskey7ca56af2006-10-11 13:47:09 +00004921 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004922 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004923 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004924
Jim Laskey6ff23e52006-10-04 16:53:27 +00004925 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004926 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004928
Jim Laskey279f0532006-09-25 16:29:54 +00004929 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004930 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00004931 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
4932 BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004933 LD->getSrcValue(), LD->getSrcValueOffset(),
4934 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004935 } else {
Bill Wendlingc0debad2009-01-30 23:27:35 +00004936 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004937 LD->getValueType(0),
4938 BetterChain, Ptr, LD->getSrcValue(),
4939 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004940 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004941 LD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004942 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004943 }
Jim Laskey279f0532006-09-25 16:29:54 +00004944
Jim Laskey6ff23e52006-10-04 16:53:27 +00004945 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00004946 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00004947 MVT::Other, Chain, ReplLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004948
Jim Laskey274062c2006-10-13 23:32:28 +00004949 // Replace uses with load result and token factor. Don't add users
4950 // to work list.
4951 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004952 }
4953 }
4954
Evan Cheng7fc033a2006-11-03 03:06:21 +00004955 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004956 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004958
Dan Gohman475871a2008-07-27 21:46:04 +00004959 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004960}
4961
Evan Cheng8b944d32009-05-28 00:35:15 +00004962
4963/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
4964/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
4965/// of the loaded bits, try narrowing the load and store if it would end up
4966/// being a win for performance or code size.
4967SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
4968 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00004969 if (ST->isVolatile())
4970 return SDValue();
4971
Evan Cheng8b944d32009-05-28 00:35:15 +00004972 SDValue Chain = ST->getChain();
4973 SDValue Value = ST->getValue();
4974 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00004975 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00004976
4977 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00004978 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004979
4980 unsigned Opc = Value.getOpcode();
4981 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
4982 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00004983 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004984
4985 SDValue N0 = Value.getOperand(0);
4986 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
4987 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengcdcecc02009-05-28 18:41:02 +00004988 if (LD->getBasePtr() != Ptr)
4989 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004990
4991 // Find the type to narrow it the load / op / store to.
4992 SDValue N1 = Value.getOperand(1);
4993 unsigned BitWidth = N1.getValueSizeInBits();
4994 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
4995 if (Opc == ISD::AND)
4996 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00004997 if (Imm == 0 || Imm.isAllOnesValue())
4998 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00004999 unsigned ShAmt = Imm.countTrailingZeros();
5000 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5001 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00005002 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005003 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00005004 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00005005 TLI.isNarrowingProfitable(VT, NewVT))) {
5006 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00005007 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005008 }
Evan Chengcdcecc02009-05-28 18:41:02 +00005009 if (NewBW >= BitWidth)
5010 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005011
5012 // If the lsb changed does not start at the type bitwidth boundary,
5013 // start at the previous one.
5014 if (ShAmt % NewBW)
5015 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5016 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5017 if ((Imm & Mask) == Imm) {
5018 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5019 if (Opc == ISD::AND)
5020 NewImm ^= APInt::getAllOnesValue(NewBW);
5021 uint64_t PtrOff = ShAmt / 8;
5022 // For big endian targets, we need to adjust the offset to the pointer to
5023 // load the correct bytes.
5024 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00005025 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00005026
5027 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Evan Chengcdcecc02009-05-28 18:41:02 +00005028 if (NewAlign <
Owen Anderson23b9b192009-08-12 00:36:31 +00005029 TLI.getTargetData()->getABITypeAlignment(NewVT.getTypeForEVT(*DAG.getContext())))
Evan Chengcdcecc02009-05-28 18:41:02 +00005030 return SDValue();
5031
Evan Cheng8b944d32009-05-28 00:35:15 +00005032 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5033 Ptr.getValueType(), Ptr,
5034 DAG.getConstant(PtrOff, Ptr.getValueType()));
5035 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5036 LD->getChain(), NewPtr,
5037 LD->getSrcValue(), LD->getSrcValueOffset(),
5038 LD->isVolatile(), NewAlign);
5039 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5040 DAG.getConstant(NewImm, NewVT));
5041 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5042 NewVal, NewPtr,
5043 ST->getSrcValue(), ST->getSrcValueOffset(),
Evan Chengcdcecc02009-05-28 18:41:02 +00005044 false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005045
5046 AddToWorkList(NewPtr.getNode());
5047 AddToWorkList(NewLD.getNode());
5048 AddToWorkList(NewVal.getNode());
5049 WorkListRemover DeadNodes(*this);
5050 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5051 &DeadNodes);
5052 ++OpsNarrowed;
5053 return NewST;
5054 }
5055 }
5056
Evan Chengcdcecc02009-05-28 18:41:02 +00005057 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005058}
5059
Dan Gohman475871a2008-07-27 21:46:04 +00005060SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005061 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005062 SDValue Chain = ST->getChain();
5063 SDValue Value = ST->getValue();
5064 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005065
Chris Lattner00161a62008-01-25 07:20:16 +00005066 // Try to infer better alignment information than the store already has.
Bill Wendling98a366d2009-04-29 23:29:43 +00005067 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00005068 if (unsigned Align = InferAlignment(Ptr, DAG)) {
5069 if (Align > ST->getAlignment())
Bill Wendlingc144a572009-01-30 23:36:47 +00005070 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5071 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005072 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00005073 ST->isVolatile(), Align);
5074 }
5075 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005076
Evan Cheng59d5b682007-05-07 21:27:48 +00005077 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00005078 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00005079 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005080 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00005081 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00005082 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00005083 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005084 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005085 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005086 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005087 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00005088 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5089 Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00005090 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00005091 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005092
Nate Begeman2cbba892006-12-11 02:23:46 +00005093 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00005094 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005095 // NOTE: If the original store is volatile, this transform must not increase
5096 // the number of stores. For example, on x86-32 an f64 can be stored in one
5097 // processor operation but an i64 (which is not legal) requires two. So the
5098 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00005099 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00005100 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005102 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 case MVT::f80: // We don't do this for these yet.
5104 case MVT::f128:
5105 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00005106 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 case MVT::f32:
5108 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005109 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00005111 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00005112 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00005113 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5114 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005115 ST->getSrcValueOffset(), ST->isVolatile(),
5116 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00005117 }
5118 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005119 case MVT::f64:
5120 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005121 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00005123 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00005124 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00005125 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5126 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005127 ST->getSrcValueOffset(), ST->isVolatile(),
5128 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005129 } else if (!ST->isVolatile() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005130 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00005131 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00005132 // argument passing. Since this is so common, custom legalize the
5133 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00005134 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5136 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00005137 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00005138
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005139 int SVOffset = ST->getSrcValueOffset();
5140 unsigned Alignment = ST->getAlignment();
5141 bool isVolatile = ST->isVolatile();
5142
Bill Wendlingc144a572009-01-30 23:36:47 +00005143 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5144 Ptr, ST->getSrcValue(),
5145 ST->getSrcValueOffset(),
5146 isVolatile, ST->getAlignment());
5147 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00005148 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005149 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00005150 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00005151 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5152 Ptr, ST->getSrcValue(),
5153 SVOffset, isVolatile, Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00005155 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00005156 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005157
Chris Lattner62be1a72006-12-12 04:16:14 +00005158 break;
Evan Cheng25ece662006-12-11 17:25:19 +00005159 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005160 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005161 }
5162
Scott Michelfdc40a02009-02-17 22:15:04 +00005163 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005164 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005165 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005166
Jim Laskey6ff23e52006-10-04 16:53:27 +00005167 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005168 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00005169 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00005170 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005171 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005172 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005173 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005174 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00005175 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005176 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00005177 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005178 ST->getSrcValue(), ST->getSrcValueOffset(),
5179 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005181
Jim Laskey279f0532006-09-25 16:29:54 +00005182 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00005183 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00005185
Jim Laskey274062c2006-10-13 23:32:28 +00005186 // Don't add users to work list.
5187 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005188 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00005189 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Evan Cheng33dbedc2006-11-05 09:31:14 +00005191 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005192 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005193 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00005194
Chris Lattner3c872852007-12-29 06:26:16 +00005195 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00005196 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005197 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005198 // See if we can simplify the input to this truncstore with knowledge that
5199 // only the low bits are being used. For example:
5200 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00005201 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005202 GetDemandedBits(Value,
Bill Wendlingc144a572009-01-30 23:36:47 +00005203 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5204 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005205 AddToWorkList(Value.getNode());
5206 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00005207 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5208 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005209 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00005210 ST->isVolatile(), ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00005211
Chris Lattnere33544c2007-10-13 06:58:48 +00005212 // Otherwise, see if we can simplify the operation with
5213 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00005214 if (SimplifyDemandedBits(Value,
5215 APInt::getLowBitsSet(
5216 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005217 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00005218 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00005219 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005220
Chris Lattner3c872852007-12-29 06:26:16 +00005221 // If this is a load followed by a store to the same location, then the store
5222 // is dead/noop.
5223 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005224 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005225 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00005226 // There can't be any side effects between the load and store, such as
5227 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00005228 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00005229 // The store is dead, remove it.
5230 return Chain;
5231 }
5232 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005233
Chris Lattnerddf89562008-01-17 19:59:44 +00005234 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5235 // truncating store. We can do this even if this is already a truncstore.
5236 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00005237 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005238 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005239 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005240 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5241 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005242 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00005243 ST->isVolatile(), ST->getAlignment());
5244 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005245
Evan Cheng8b944d32009-05-28 00:35:15 +00005246 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00005247}
5248
Dan Gohman475871a2008-07-27 21:46:04 +00005249SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5250 SDValue InVec = N->getOperand(0);
5251 SDValue InVal = N->getOperand(1);
5252 SDValue EltNo = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005253
Chris Lattnerca242442006-03-19 01:27:56 +00005254 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5255 // vector with the inserted element.
5256 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005257 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005258 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5259 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005260 if (Elt < Ops.size())
5261 Ops[Elt] = InVal;
Evan Chenga87008d2009-02-25 22:49:59 +00005262 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5263 InVec.getValueType(), &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005264 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005265 // If the invec is an UNDEF and if EltNo is a constant, create a new
5266 // BUILD_VECTOR with undef elements and the inserted element.
5267 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5268 isa<ConstantSDNode>(EltNo)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005269 EVT VT = InVec.getValueType();
5270 EVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005271 unsigned NElts = VT.getVectorNumElements();
5272 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005273
Nate Begeman9008ca62009-04-27 18:41:29 +00005274 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5275 if (Elt < Ops.size())
5276 Ops[Elt] = InVal;
5277 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5278 InVec.getValueType(), &Ops[0], Ops.size());
5279 }
Dan Gohman475871a2008-07-27 21:46:04 +00005280 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005281}
5282
Dan Gohman475871a2008-07-27 21:46:04 +00005283SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005284 // (vextract (scalar_to_vector val, 0) -> val
5285 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005286
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005287 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5288 // If the operand is wider than the vector element type then it is implicitly
5289 // truncated. Make that explicit here.
Owen Andersone50ed302009-08-10 22:56:29 +00005290 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005291 SDValue InOp = InVec.getOperand(0);
5292 if (InOp.getValueType() != EltVT)
5293 return DAG.getNode(ISD::TRUNCATE, InVec.getDebugLoc(), EltVT, InOp);
5294 return InOp;
5295 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005296
5297 // Perform only after legalization to ensure build_vector / vector_shuffle
5298 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005299 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005300
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005301 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5302 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5303 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005304 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005305
Evan Cheng513da432007-10-06 08:19:55 +00005306 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005307 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005308 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005309 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00005310 EVT VT = InVec.getValueType();
5311 EVT ExtVT = VT.getVectorElementType();
5312 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00005313
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005314 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005315 EVT BCVT = InVec.getOperand(0).getValueType();
5316 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005317 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005318 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5319 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005320 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005321 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005322 NewLoad = true;
5323 }
Evan Cheng513da432007-10-06 08:19:55 +00005324
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005325 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00005326 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00005327 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005328 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00005329 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00005330 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00005331 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005332 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00005333 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005334 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5335 // =>
5336 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00005337
Mon P Wanga60b5232008-12-11 00:26:16 +00005338 // If the bit convert changed the number of elements, it is unsafe
5339 // to examine the mask.
5340 if (BCNumEltsChanged)
5341 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00005342
5343 // Select the input vector, guarding against out of range extract vector.
5344 unsigned NumElems = VT.getVectorNumElements();
5345 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5346 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5347
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005348 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5349 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00005350 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005351 LN0 = cast<LoadSDNode>(InVec);
Nate Begeman5a5ca152009-04-29 05:20:52 +00005352 Elt = (Idx < (int)NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00005353 }
5354 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005355
Duncan Sandsec87aa82008-06-15 20:12:31 +00005356 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00005357 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005358
5359 unsigned Align = LN0->getAlignment();
5360 if (NewLoad) {
5361 // Check the resultant load doesn't need a higher alignment than the
5362 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00005363 unsigned NewAlign =
Owen Anderson23b9b192009-08-12 00:36:31 +00005364 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00005365
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005366 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005367 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00005368
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005369 Align = NewAlign;
5370 }
5371
Dan Gohman475871a2008-07-27 21:46:04 +00005372 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005373 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005375 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005376 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005377 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00005378 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005379 DAG.getConstant(PtrOff, PtrType));
5380 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005381
5382 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005383 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5384 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005385 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005386
Dan Gohman475871a2008-07-27 21:46:04 +00005387 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005388}
Evan Cheng513da432007-10-06 08:19:55 +00005389
Dan Gohman475871a2008-07-27 21:46:04 +00005390SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005391 unsigned NumInScalars = N->getNumOperands();
Owen Andersone50ed302009-08-10 22:56:29 +00005392 EVT VT = N->getValueType(0);
5393 EVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00005394
Dan Gohman7f321562007-06-25 16:23:39 +00005395 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5396 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5397 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005398 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005399 for (unsigned i = 0; i != NumInScalars; ++i) {
5400 // Ignore undef inputs.
5401 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Dan Gohman7f321562007-06-25 16:23:39 +00005403 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005404 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005405 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005406 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005407 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005408 break;
5409 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005410
Dan Gohman7f321562007-06-25 16:23:39 +00005411 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005412 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005414 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005415 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005416 break;
5417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005418
Chris Lattnerd7648c82006-03-28 20:28:38 +00005419 // Otherwise, remember this. We allow up to two distinct input vectors.
5420 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5421 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005422
Gabor Greifba36cb52008-08-28 21:40:38 +00005423 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005424 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005425 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005426 VecIn2 = ExtractedFromVec;
5427 } else {
5428 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005429 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005430 break;
5431 }
5432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005433
Chris Lattnerd7648c82006-03-28 20:28:38 +00005434 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005435 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005436 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005437 for (unsigned i = 0; i != NumInScalars; ++i) {
5438 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005439 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005440 continue;
5441 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005442
Rafael Espindola15684b22009-04-24 12:40:33 +00005443 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00005444 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00005445 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005446 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00005447 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
5448 if (ExtIndex > VT.getVectorNumElements())
5449 return SDValue();
5450
5451 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005452 continue;
5453 }
5454
5455 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00005456 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00005457 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005459
Chris Lattnerd7648c82006-03-28 20:28:38 +00005460 // Add count and size info.
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 if (!TLI.isTypeLegal(VT) && LegalTypes)
Duncan Sands25cf2272008-11-24 14:53:14 +00005462 return SDValue();
5463
Dan Gohman7f321562007-06-25 16:23:39 +00005464 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005466 Ops[0] = VecIn1;
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5468 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005470
Dan Gohman475871a2008-07-27 21:46:04 +00005471 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005472}
5473
Dan Gohman475871a2008-07-27 21:46:04 +00005474SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005475 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5476 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5477 // inputs come from at most two distinct vectors, turn this into a shuffle
5478 // node.
5479
5480 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00005481 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00005482 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005483
Dan Gohman475871a2008-07-27 21:46:04 +00005484 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005485}
5486
Dan Gohman475871a2008-07-27 21:46:04 +00005487SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005488 return SDValue();
5489
Owen Andersone50ed302009-08-10 22:56:29 +00005490 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005492
Mon P Wangaeb06d22008-11-10 04:46:22 +00005493 SDValue N0 = N->getOperand(0);
5494 SDValue N1 = N->getOperand(1);
5495
5496 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5497 "Vector shuffle must be normalized in DAG");
5498
Nate Begeman9008ca62009-04-27 18:41:29 +00005499 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Evan Chenge7bec0d2006-07-20 22:44:41 +00005500
Evan Cheng917ec982006-07-21 08:25:53 +00005501 // If it is a splat, check if the argument vector is a build_vector with
5502 // all scalar elements the same.
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005504 SDNode *V = N0.getNode();
Nate Begeman9008ca62009-04-27 18:41:29 +00005505
Evan Cheng917ec982006-07-21 08:25:53 +00005506
Dan Gohman7f321562007-06-25 16:23:39 +00005507 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005508 // not the number of vector elements, look through it. Be careful not to
5509 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005510 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005511 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005512 if (ConvInput.getValueType().isVector() &&
5513 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005514 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005515 }
5516
Dan Gohman7f321562007-06-25 16:23:39 +00005517 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5518 unsigned NumElems = V->getNumOperands();
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Evan Cheng917ec982006-07-21 08:25:53 +00005520 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005522 bool AllSame = true;
5523 for (unsigned i = 0; i != NumElems; ++i) {
5524 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5525 Base = V->getOperand(i);
5526 break;
5527 }
5528 }
5529 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005530 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005531 return N0;
5532 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005533 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005534 AllSame = false;
5535 break;
5536 }
5537 }
5538 // Splat of <x, x, x, x>, return <x, x, x, x>
5539 if (AllSame)
5540 return N0;
5541 }
5542 }
5543 }
Dan Gohman475871a2008-07-27 21:46:04 +00005544 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005545}
5546
Evan Cheng44f1f092006-04-20 08:56:16 +00005547/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005548/// an AND to a vector_shuffle with the destination vector and a zero vector.
5549/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005550/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005551SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005552 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005553 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005554 SDValue LHS = N->getOperand(0);
5555 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005556 if (N->getOpcode() == ISD::AND) {
5557 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005558 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005559 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005560 SmallVector<int, 8> Indices;
5561 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00005562 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005564 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005565 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005566 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00005567 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005568 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00005569 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005570 else
Dan Gohman475871a2008-07-27 21:46:04 +00005571 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005572 }
5573
5574 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00005575 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005576 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005577 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005578
Dan Gohman7f321562007-06-25 16:23:39 +00005579 // Return the new VECTOR_SHUFFLE node.
Owen Andersone50ed302009-08-10 22:56:29 +00005580 EVT EVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
5582 DAG.getConstant(0, EVT));
5583 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5584 RVT, &ZeroOps[0], ZeroOps.size());
5585 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
5586 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
5587 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00005588 }
5589 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005590
Dan Gohman475871a2008-07-27 21:46:04 +00005591 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005592}
5593
Dan Gohman7f321562007-06-25 16:23:39 +00005594/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005595SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005596 // After legalize, the target may be depending on adds and other
5597 // binary ops to provide legal ways to construct constants or other
5598 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005599 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005600
Owen Andersone50ed302009-08-10 22:56:29 +00005601 EVT VT = N->getValueType(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005602 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005603
Owen Andersone50ed302009-08-10 22:56:29 +00005604 EVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005605 SDValue LHS = N->getOperand(0);
5606 SDValue RHS = N->getOperand(1);
5607 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005608 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005609
Dan Gohman7f321562007-06-25 16:23:39 +00005610 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005611 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00005612 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00005613 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005614 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005615 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005616 SDValue LHSOp = LHS.getOperand(i);
5617 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005618 // If these two elements can't be folded, bail out.
5619 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5620 LHSOp.getOpcode() != ISD::Constant &&
5621 LHSOp.getOpcode() != ISD::ConstantFP) ||
5622 (RHSOp.getOpcode() != ISD::UNDEF &&
5623 RHSOp.getOpcode() != ISD::Constant &&
5624 RHSOp.getOpcode() != ISD::ConstantFP))
5625 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005626
Evan Cheng7b336a82006-05-31 06:08:35 +00005627 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005628 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5629 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005630 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005631 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005632 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005633 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005634 break;
5635 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005636
Bill Wendling9729c5a2009-01-31 03:12:48 +00005637 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005638 EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005639 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005640 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5641 Ops.back().getOpcode() == ISD::Constant ||
5642 Ops.back().getOpcode() == ISD::ConstantFP) &&
5643 "Scalar binop didn't fold!");
5644 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005645
Dan Gohman7f321562007-06-25 16:23:39 +00005646 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005647 EVT VT = LHS.getValueType();
Evan Chenga87008d2009-02-25 22:49:59 +00005648 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
5649 &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005650 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005652
Dan Gohman475871a2008-07-27 21:46:04 +00005653 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005654}
5655
Bill Wendling836ca7d2009-01-30 23:59:18 +00005656SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
5657 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005658 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005659
Bill Wendling836ca7d2009-01-30 23:59:18 +00005660 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005661 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005662
Nate Begemanf845b452005-10-08 00:29:44 +00005663 // If we got a simplified select_cc node back from SimplifySelectCC, then
5664 // break it down into a new SETCC node, and a new SELECT node, and then return
5665 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005666 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005667 // Check to see if we got a select_cc back (to turn into setcc/select).
5668 // Otherwise, just return whatever node we got back, like fabs.
5669 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005670 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
5671 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005672 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005673 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005674 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005675 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
5676 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00005677 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005678
Nate Begemanf845b452005-10-08 00:29:44 +00005679 return SCC;
5680 }
Dan Gohman475871a2008-07-27 21:46:04 +00005681 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005682}
5683
Chris Lattner40c62d52005-10-18 06:04:22 +00005684/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5685/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005686/// select. Callers of this should assume that TheSelect is deleted if this
5687/// returns true. As such, they should return the appropriate thing (e.g. the
5688/// node) back to the top-level of the DAG combiner loop to avoid it being
5689/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00005690bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00005691 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005692
Chris Lattner40c62d52005-10-18 06:04:22 +00005693 // If this is a select from two identical things, try to pull the operation
5694 // through the select.
5695 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005696 // If this is a load and the token chain is identical, replace the select
5697 // of two loads with a load through a select of the address to load from.
5698 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5699 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005700 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005701 // Do not let this transformation reduce the number of volatile loads.
5702 !cast<LoadSDNode>(LHS)->isVolatile() &&
5703 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005704 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005705 LHS.getOperand(0) == RHS.getOperand(0)) {
5706 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5707 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5708
5709 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005710 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005711 // FIXME: this conflates two src values, discarding one. This is not
5712 // the right thing to do, but nothing uses srcvalues now. When they do,
5713 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005714 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005715 if (TheSelect->getOpcode() == ISD::SELECT) {
5716 // Check that the condition doesn't reach either load. If so, folding
5717 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005718 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5719 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005720 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
5721 LLD->getBasePtr().getValueType(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005722 TheSelect->getOperand(0), LLD->getBasePtr(),
5723 RLD->getBasePtr());
5724 }
5725 } else {
5726 // Check that the condition doesn't reach either load. If so, folding
5727 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005728 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5729 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5730 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5731 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005732 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
5733 LLD->getBasePtr().getValueType(),
5734 TheSelect->getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005735 TheSelect->getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005736 LLD->getBasePtr(), RLD->getBasePtr(),
5737 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005738 }
Evan Cheng466685d2006-10-09 20:57:25 +00005739 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005740
Gabor Greifba36cb52008-08-28 21:40:38 +00005741 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005742 SDValue Load;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005743 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
5744 Load = DAG.getLoad(TheSelect->getValueType(0),
5745 TheSelect->getDebugLoc(),
5746 LLD->getChain(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005747 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005748 LLD->getSrcValueOffset(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005749 LLD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005750 LLD->getAlignment());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005751 } else {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005752 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005753 TheSelect->getDebugLoc(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005754 TheSelect->getValueType(0),
5755 LLD->getChain(), Addr, LLD->getSrcValue(),
5756 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005757 LLD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005758 LLD->isVolatile(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005759 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005760 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005761
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005762 // Users of the select now use the result of the load.
5763 CombineTo(TheSelect, Load);
Scott Michelfdc40a02009-02-17 22:15:04 +00005764
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005765 // Users of the old loads now use the new load's chain. We know the
5766 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005767 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5768 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005769 return true;
5770 }
Evan Chengc5484282006-10-04 00:56:09 +00005771 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005772 }
5773 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005774
Chris Lattner40c62d52005-10-18 06:04:22 +00005775 return false;
5776}
5777
Chris Lattner600fec32009-03-11 05:08:08 +00005778/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
5779/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00005780SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00005781 SDValue N2, SDValue N3,
5782 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00005783 // (x ? y : y) -> y.
5784 if (N2 == N3) return N2;
5785
Owen Andersone50ed302009-08-10 22:56:29 +00005786 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005787 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5788 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5789 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005790
5791 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005792 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005793 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005794 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5795 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005796
5797 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005798 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005799 return N2;
5800 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005801 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005802 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00005803
Nate Begemanf845b452005-10-08 00:29:44 +00005804 // Check to see if we can simplify the select into an fabs node
5805 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5806 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005807 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005808 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5809 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5810 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5811 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00005812 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005813
Nate Begemanf845b452005-10-08 00:29:44 +00005814 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5815 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5816 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5817 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00005818 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00005819 }
5820 }
Chris Lattner600fec32009-03-11 05:08:08 +00005821
5822 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
5823 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
5824 // in it. This is a win when the constant is not otherwise available because
5825 // it replaces two constant pool loads with one. We only do this if the FP
5826 // type is known to be legal, because if it isn't, then we are before legalize
5827 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00005828 // messing with soft float) and if the ConstantFP is not legal, because if
5829 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00005830 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
5831 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
5832 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00005833 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
5834 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00005835 // If both constants have multiple uses, then we won't need to do an
5836 // extra load, they are likely around in registers for other users.
5837 (TV->hasOneUse() || FV->hasOneUse())) {
5838 Constant *Elts[] = {
5839 const_cast<ConstantFP*>(FV->getConstantFPValue()),
5840 const_cast<ConstantFP*>(TV->getConstantFPValue())
5841 };
5842 const Type *FPTy = Elts[0]->getType();
5843 const TargetData &TD = *TLI.getTargetData();
5844
5845 // Create a ConstantArray of the two constants.
Owen Andersondebcb012009-07-29 22:17:13 +00005846 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner600fec32009-03-11 05:08:08 +00005847 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
5848 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00005849 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00005850
5851 // Get the offsets to the 0 and 1 element of the array so that we can
5852 // select between them.
5853 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00005854 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00005855 SDValue One = DAG.getIntPtrConstant(EltSize);
5856
5857 SDValue Cond = DAG.getSetCC(DL,
5858 TLI.getSetCCResultType(N0.getValueType()),
5859 N0, N1, CC);
5860 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
5861 Cond, One, Zero);
5862 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
5863 CstOffset);
5864 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
5865 PseudoSourceValue::getConstantPool(), 0, false,
5866 Alignment);
5867
5868 }
5869 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005870
Nate Begemanf845b452005-10-08 00:29:44 +00005871 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00005872 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00005873 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005874 N0.getValueType().isInteger() &&
5875 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005876 (N1C->isNullValue() || // (a < 0) ? b : 0
5877 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00005878 EVT XType = N0.getValueType();
5879 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005880 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005881 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005882 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005883 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5884 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005885 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands92abc622009-01-31 15:50:11 +00005886 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005887 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005888 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005889 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005890
Duncan Sands8e4eb092008-06-08 20:54:56 +00005891 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005892 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005893 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005894 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005895
5896 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00005897 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005898
Bill Wendling9729c5a2009-01-31 03:12:48 +00005899 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005900 XType, N0,
5901 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005902 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005903 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00005904
Duncan Sands8e4eb092008-06-08 20:54:56 +00005905 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005906 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005907 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005908 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005909
5910 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00005911 }
5912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005913
Nate Begeman07ed4172005-10-10 21:26:48 +00005914 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005915 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005916 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005917
Chris Lattner1eba01e2007-04-11 06:50:51 +00005918 // If the caller doesn't want us to simplify this into a zext of a compare,
5919 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005920 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005921 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005922
Nate Begeman07ed4172005-10-10 21:26:48 +00005923 // Get a SetCC of the condition
5924 // FIXME: Should probably make sure that setcc is legal if we ever have a
5925 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005926 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005927 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005928 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005929 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005930 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005931 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00005932 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00005933 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00005934 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005935 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005936 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00005937 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00005938 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00005939 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005940 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005941
Gabor Greifba36cb52008-08-28 21:40:38 +00005942 AddToWorkList(SCC.getNode());
5943 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005944
Dan Gohman002e5d02008-03-13 22:13:53 +00005945 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005946 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00005947
Nate Begeman07ed4172005-10-10 21:26:48 +00005948 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00005949 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005950 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00005951 getShiftAmountTy()));
Nate Begeman07ed4172005-10-10 21:26:48 +00005952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005953
Nate Begemanf845b452005-10-08 00:29:44 +00005954 // Check to see if this is the equivalent of setcc
5955 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5956 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005957 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005958 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005959 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005960 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005961 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005962 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00005963 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00005964 return Res;
5965 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005966
Bill Wendling836ca7d2009-01-30 23:59:18 +00005967 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00005968 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005969 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005970 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005971 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005972 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005973 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands92abc622009-01-31 15:50:11 +00005974 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005975 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005976 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00005977 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00005978 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
5979 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00005980 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00005981 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005982 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005983 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005984 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005985 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00005986 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00005987 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005988 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00005989 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00005990 getShiftAmountTy()));
Bill Wendling836ca7d2009-01-30 23:59:18 +00005991 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00005992 }
5993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005994
Nate Begemanf845b452005-10-08 00:29:44 +00005995 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5996 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5997 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005998 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005999 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006000 EVT XType = N0.getValueType();
Bill Wendling9729c5a2009-01-31 03:12:48 +00006001 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006002 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006003 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006004 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006005 N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006006 AddToWorkList(Shift.getNode());
6007 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006008 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00006009 }
6010 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6011 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6012 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6013 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6014 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersone50ed302009-08-10 22:56:29 +00006015 EVT XType = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006016 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006017 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006018 N0,
6019 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006020 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006021 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006022 XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006023 AddToWorkList(Shift.getNode());
6024 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006025 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00006026 }
6027 }
6028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006029
Dan Gohman475871a2008-07-27 21:46:04 +00006030 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006031}
6032
Evan Chengfa1eb272007-02-08 22:13:59 +00006033/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00006034SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00006035 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006036 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006037 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00006038 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006039 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +00006040}
6041
Nate Begeman69575232005-10-20 02:15:44 +00006042/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6043/// return a DAG expression to select that will generate the same value by
6044/// multiplying by a magic number. See:
6045/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006046SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006047 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006048 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006049
Andrew Lenharth232c9102006-06-12 16:07:18 +00006050 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006051 ii != ee; ++ii)
6052 AddToWorkList(*ii);
6053 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006054}
6055
6056/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6057/// return a DAG expression to select that will generate the same value by
6058/// multiplying by a magic number. See:
6059/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006060SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006061 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006062 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00006063
Andrew Lenharth232c9102006-06-12 16:07:18 +00006064 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006065 ii != ee; ++ii)
6066 AddToWorkList(*ii);
6067 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006068}
6069
Jim Laskey71382342006-10-07 23:37:56 +00006070/// FindBaseOffset - Return true if base is known not to alias with anything
6071/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00006072static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00006073 // Assume it is a primitive operation.
6074 Base = Ptr; Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006075
Jim Laskey71382342006-10-07 23:37:56 +00006076 // If it's an adding a simple constant then integrate the offset.
6077 if (Base.getOpcode() == ISD::ADD) {
6078 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6079 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006080 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00006081 }
6082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006083
Jim Laskey71382342006-10-07 23:37:56 +00006084 // If it's any of the following then it can't alias with anything but itself.
6085 return isa<FrameIndexSDNode>(Base) ||
6086 isa<ConstantPoolSDNode>(Base) ||
6087 isa<GlobalAddressSDNode>(Base);
6088}
6089
6090/// isAlias - Return true if there is any possibility that the two addresses
6091/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00006092bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00006093 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue Ptr2, int64_t Size2,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006095 const Value *SrcValue2, int SrcValueOffset2) const {
Jim Laskey71382342006-10-07 23:37:56 +00006096 // If they are the same then they must be aliases.
6097 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006098
Jim Laskey71382342006-10-07 23:37:56 +00006099 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00006100 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00006101 int64_t Offset1, Offset2;
6102 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
6103 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006104
Jim Laskey71382342006-10-07 23:37:56 +00006105 // If they have a same base address then...
Bill Wendling836ca7d2009-01-30 23:59:18 +00006106 if (Base1 == Base2)
Jim Laskey71382342006-10-07 23:37:56 +00006107 // Check to see if the addresses overlap.
Bill Wendling836ca7d2009-01-30 23:59:18 +00006108 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006109
Jim Laskey096c22e2006-10-18 12:29:57 +00006110 // If we know both bases then they can't alias.
6111 if (KnownBase1 && KnownBase2) return false;
6112
Jim Laskey07a27092006-10-18 19:08:31 +00006113 if (CombinerGlobalAA) {
6114 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00006115 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6116 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6117 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00006118 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00006119 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00006120 if (AAResult == AliasAnalysis::NoAlias)
6121 return false;
6122 }
Jim Laskey096c22e2006-10-18 12:29:57 +00006123
6124 // Otherwise we have to assume they alias.
6125 return true;
Jim Laskey71382342006-10-07 23:37:56 +00006126}
6127
6128/// FindAliasInfo - Extracts the relevant alias information from the memory
6129/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00006130bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00006131 SDValue &Ptr, int64_t &Size,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006132 const Value *&SrcValue, int &SrcValueOffset) const {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006133 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6134 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006135 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006136 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006137 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00006138 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006139 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006140 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006141 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006142 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006143 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00006144 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006145 llvm_unreachable("FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00006146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006147
Jim Laskey71382342006-10-07 23:37:56 +00006148 return false;
6149}
6150
Jim Laskey6ff23e52006-10-04 16:53:27 +00006151/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6152/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006153void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6154 SmallVector<SDValue, 8> &Aliases) {
6155 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00006156 std::set<SDNode *> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00006157
Jim Laskey279f0532006-09-25 16:29:54 +00006158 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00006159 SDValue Ptr;
Daniel Dunbar8c562e22009-05-18 16:43:04 +00006160 int64_t Size = 0;
6161 const Value *SrcValue = 0;
6162 int SrcValueOffset = 0;
Jim Laskey096c22e2006-10-18 12:29:57 +00006163 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00006164
Jim Laskey6ff23e52006-10-04 16:53:27 +00006165 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00006166 Chains.push_back(OriginalChain);
Scott Michelfdc40a02009-02-17 22:15:04 +00006167
Jim Laskeybc588b82006-10-05 15:07:25 +00006168 // Look at each chain and determine if it is an alias. If so, add it to the
6169 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00006170 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00006171 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006172 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00006173 Chains.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +00006174
Jim Laskeybc588b82006-10-05 15:07:25 +00006175 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00006176 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
6177 Visited.insert(Chain.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006178
Jim Laskeybc588b82006-10-05 15:07:25 +00006179 switch (Chain.getOpcode()) {
6180 case ISD::EntryToken:
6181 // Entry token is ideal chain operand, but handled in FindBetterChain.
6182 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006183
Jim Laskeybc588b82006-10-05 15:07:25 +00006184 case ISD::LOAD:
6185 case ISD::STORE: {
6186 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006187 SDValue OpPtr;
Daniel Dunbar8c562e22009-05-18 16:43:04 +00006188 int64_t OpSize = 0;
6189 const Value *OpSrcValue = 0;
6190 int OpSrcValueOffset = 0;
Gabor Greifba36cb52008-08-28 21:40:38 +00006191 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00006192 OpSrcValue, OpSrcValueOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00006193
Jim Laskeybc588b82006-10-05 15:07:25 +00006194 // If chain is alias then stop here.
6195 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00006196 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
6197 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00006198 Aliases.push_back(Chain);
6199 } else {
6200 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00006201 Chains.push_back(Chain.getOperand(0));
Jim Laskeybc588b82006-10-05 15:07:25 +00006202 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00006203 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00006204 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006205 break;
6206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006207
Jim Laskeybc588b82006-10-05 15:07:25 +00006208 case ISD::TokenFactor:
6209 // We have to check each of the operands of the token factor, so we queue
6210 // then up. Adding the operands to the queue (stack) in reverse order
6211 // maintains the original order and increases the likelihood that getNode
6212 // will find a matching token factor (CSE.)
6213 for (unsigned n = Chain.getNumOperands(); n;)
6214 Chains.push_back(Chain.getOperand(--n));
6215 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00006216 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00006217 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006218
Jim Laskeybc588b82006-10-05 15:07:25 +00006219 default:
6220 // For all other instructions we will just have to take what we can get.
6221 Aliases.push_back(Chain);
6222 break;
Jim Laskey279f0532006-09-25 16:29:54 +00006223 }
6224 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00006225}
6226
6227/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6228/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00006229SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6230 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00006231
Jim Laskey6ff23e52006-10-04 16:53:27 +00006232 // Accumulate all the aliases to this node.
6233 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00006234
Jim Laskey6ff23e52006-10-04 16:53:27 +00006235 if (Aliases.size() == 0) {
6236 // If no operands then chain to entry token.
6237 return DAG.getEntryNode();
6238 } else if (Aliases.size() == 1) {
6239 // If a single operand then chain to it. We don't need to revisit it.
6240 return Aliases[0];
6241 }
6242
6243 // Construct a custom tailored token factor.
Owen Anderson825b72b2009-08-11 20:47:22 +00006244 SDValue NewChain = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006245 &Aliases[0], Aliases.size());
Jim Laskey6ff23e52006-10-04 16:53:27 +00006246
6247 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00006248 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006249
Jim Laskey6ff23e52006-10-04 16:53:27 +00006250 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00006251}
6252
Nate Begeman1d4d4142005-09-01 00:19:25 +00006253// SelectionDAG::Combine - This is the entry point for the file.
6254//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006255void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00006256 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00006257 /// run - This is the main entry point to this class.
6258 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006259 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006260}