blob: ce5e8cbfc79f73db6d90f592e6ee4924b115035f [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"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000034#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000035#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000037#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000038#include "llvm/Support/raw_ostream.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040using namespace llvm;
41
Chris Lattnercd3245a2006-12-19 22:41:21 +000042STATISTIC(NodesCombined , "Number of dag nodes combined");
43STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
44STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000045STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046
Nate Begeman1d4d4142005-09-01 00:19:25 +000047namespace {
Jim Laskey71382342006-10-07 23:37:56 +000048 static cl::opt<bool>
49 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000050 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000051
Jim Laskey07a27092006-10-18 19:08:31 +000052 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
Jim Laskeybc588b82006-10-05 15:07:25 +000056//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000059 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000065
66 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000067 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000068
Jim Laskeyc7c3f112006-10-16 20:52:31 +000069 // AA - Used for DAG load/store alias analysis.
70 AliasAnalysis &AA;
71
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 /// AddUsersToWorkList - When an instruction is simplified, add all users of
73 /// the instruction to the work lists because they might get more simplified
74 /// now.
75 ///
76 void AddUsersToWorkList(SDNode *N) {
77 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000078 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000079 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000080 }
81
Dan Gohman389079b2007-10-08 17:57:15 +000082 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000085
Chris Lattner24664722006-03-01 04:53:38 +000086 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000087 /// AddToWorkList - Add to the work list making sure it's instance is at the
88 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000089 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000090 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000091 WorkList.push_back(N);
92 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000093
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 /// removeFromWorkList - remove all instances of N from the worklist.
95 ///
96 void removeFromWorkList(SDNode *N) {
97 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
98 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000099 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000100
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000102 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Dan Gohman475871a2008-07-27 21:46:04 +0000104 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000105 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000106 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000107
Dan Gohman475871a2008-07-27 21:46:04 +0000108 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000109 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000110 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000111 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000112 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000113
114 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000122 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
123 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000124 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
Evan Cheng95c57ea2010-04-24 04:43:44 +0000132 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
133 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
134 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
135 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000136 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000137 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000138 SDValue PromoteExtend(SDValue Op);
139 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Dan Gohman389079b2007-10-08 17:57:15 +0000141 /// combine - call the node-specific routine that knows how to fold each
142 /// particular type of node. If that doesn't do anything, try the
143 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000144 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000145
146 // Visitation implementation - Implement dag node combining for different
147 // node types. The semantics are as follows:
148 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000149 // SDValue.getNode() == 0 - No change was made
150 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
151 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000152 //
Dan Gohman475871a2008-07-27 21:46:04 +0000153 SDValue visitTokenFactor(SDNode *N);
154 SDValue visitMERGE_VALUES(SDNode *N);
155 SDValue visitADD(SDNode *N);
156 SDValue visitSUB(SDNode *N);
157 SDValue visitADDC(SDNode *N);
158 SDValue visitADDE(SDNode *N);
159 SDValue visitMUL(SDNode *N);
160 SDValue visitSDIV(SDNode *N);
161 SDValue visitUDIV(SDNode *N);
162 SDValue visitSREM(SDNode *N);
163 SDValue visitUREM(SDNode *N);
164 SDValue visitMULHU(SDNode *N);
165 SDValue visitMULHS(SDNode *N);
166 SDValue visitSMUL_LOHI(SDNode *N);
167 SDValue visitUMUL_LOHI(SDNode *N);
168 SDValue visitSDIVREM(SDNode *N);
169 SDValue visitUDIVREM(SDNode *N);
170 SDValue visitAND(SDNode *N);
171 SDValue visitOR(SDNode *N);
172 SDValue visitXOR(SDNode *N);
173 SDValue SimplifyVBinOp(SDNode *N);
174 SDValue visitSHL(SDNode *N);
175 SDValue visitSRA(SDNode *N);
176 SDValue visitSRL(SDNode *N);
177 SDValue visitCTLZ(SDNode *N);
178 SDValue visitCTTZ(SDNode *N);
179 SDValue visitCTPOP(SDNode *N);
180 SDValue visitSELECT(SDNode *N);
181 SDValue visitSELECT_CC(SDNode *N);
182 SDValue visitSETCC(SDNode *N);
183 SDValue visitSIGN_EXTEND(SDNode *N);
184 SDValue visitZERO_EXTEND(SDNode *N);
185 SDValue visitANY_EXTEND(SDNode *N);
186 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
187 SDValue visitTRUNCATE(SDNode *N);
188 SDValue visitBIT_CONVERT(SDNode *N);
189 SDValue visitBUILD_PAIR(SDNode *N);
190 SDValue visitFADD(SDNode *N);
191 SDValue visitFSUB(SDNode *N);
192 SDValue visitFMUL(SDNode *N);
193 SDValue visitFDIV(SDNode *N);
194 SDValue visitFREM(SDNode *N);
195 SDValue visitFCOPYSIGN(SDNode *N);
196 SDValue visitSINT_TO_FP(SDNode *N);
197 SDValue visitUINT_TO_FP(SDNode *N);
198 SDValue visitFP_TO_SINT(SDNode *N);
199 SDValue visitFP_TO_UINT(SDNode *N);
200 SDValue visitFP_ROUND(SDNode *N);
201 SDValue visitFP_ROUND_INREG(SDNode *N);
202 SDValue visitFP_EXTEND(SDNode *N);
203 SDValue visitFNEG(SDNode *N);
204 SDValue visitFABS(SDNode *N);
205 SDValue visitBRCOND(SDNode *N);
206 SDValue visitBR_CC(SDNode *N);
207 SDValue visitLOAD(SDNode *N);
208 SDValue visitSTORE(SDNode *N);
209 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
210 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
211 SDValue visitBUILD_VECTOR(SDNode *N);
212 SDValue visitCONCAT_VECTORS(SDNode *N);
213 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000216 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000217
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000219
Dan Gohman475871a2008-07-27 21:46:04 +0000220 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
221 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000222 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000223 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
224 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000225 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000226 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000227 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000228 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000229 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000230 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
231 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000232 SDValue BuildSDIV(SDNode *N);
233 SDValue BuildUDIV(SDNode *N);
Bill Wendling317bd702009-01-30 21:14:50 +0000234 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000236 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000237
Dan Gohman475871a2008-07-27 21:46:04 +0000238 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
Jim Laskey6ff23e52006-10-04 16:53:27 +0000240 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
241 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000242 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
243 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000244
Jim Laskey096c22e2006-10-18 12:29:57 +0000245 /// isAlias - Return true if there is any possibility that the two addresses
246 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000247 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000248 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000249 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000251 const Value *SrcValue2, int SrcValueOffset2,
252 unsigned SrcValueAlign2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Jim Laskey7ca56af2006-10-11 13:47:09 +0000254 /// FindAliasInfo - Extracts the relevant alias information from the memory
255 /// node. Returns true if the operand was a load.
256 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000257 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000258 const Value *&SrcValue, int &SrcValueOffset,
259 unsigned &SrcValueAlignment) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000260
Jim Laskey279f0532006-09-25 16:29:54 +0000261 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000262 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000264
Chris Lattner2392ae72010-04-15 04:48:01 +0000265 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000266 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Chris Lattner2392ae72010-04-15 04:48:01 +0000267 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(Unrestricted),
268 OptLevel(OL), LegalOperations(false), LegalTypes(false), 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);
Chris Lattner2392ae72010-04-15 04:48:01 +0000272
273 SelectionDAG &getDAG() const { return DAG; }
274
275 /// getShiftAmountTy - Returns a type large enough to hold any valid
276 /// shift amount - before type legalization these can be huge.
277 EVT getShiftAmountTy() {
278 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
279 }
280
281 /// isTypeLegal - This method returns true if we are running before type
282 /// legalization or if the specified VT is legal.
283 bool isTypeLegal(const EVT &VT) {
284 if (!LegalTypes) return true;
285 return TLI.isTypeLegal(VT);
286 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000287 };
288}
289
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000290
291namespace {
292/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
293/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000294class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000295 DAGCombiner &DC;
296public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000297 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Duncan Sandsedfcf592008-06-11 11:42:12 +0000299 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000300 DC.removeFromWorkList(N);
301 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000302
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000303 virtual void NodeUpdated(SDNode *N) {
304 // Ignore updates.
305 }
306};
307}
308
Chris Lattner24664722006-03-01 04:53:38 +0000309//===----------------------------------------------------------------------===//
310// TargetLowering::DAGCombinerInfo implementation
311//===----------------------------------------------------------------------===//
312
313void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
314 ((DAGCombiner*)DC)->AddToWorkList(N);
315}
316
Dan Gohman475871a2008-07-27 21:46:04 +0000317SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000318CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
319 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000320}
321
Dan Gohman475871a2008-07-27 21:46:04 +0000322SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000323CombineTo(SDNode *N, SDValue Res, bool AddTo) {
324 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000325}
326
327
Dan Gohman475871a2008-07-27 21:46:04 +0000328SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000329CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
330 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000331}
332
Dan Gohmane5af2d32009-01-29 01:59:02 +0000333void TargetLowering::DAGCombinerInfo::
334CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
335 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
336}
Chris Lattner24664722006-03-01 04:53:38 +0000337
Chris Lattner24664722006-03-01 04:53:38 +0000338//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000339// Helper Functions
340//===----------------------------------------------------------------------===//
341
342/// isNegatibleForFree - Return 1 if we can compute the negated form of the
343/// specified expression for the same cost as the expression itself, or 2 if we
344/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000345static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000346 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000347 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000349 return 0;
350
Chris Lattner29446522007-05-14 22:04:50 +0000351 // fneg is removable even if it has multiple uses.
352 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000353
Chris Lattner29446522007-05-14 22:04:50 +0000354 // Don't allow anything with multiple uses.
355 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Chris Lattner3adf9512007-05-25 02:19:06 +0000357 // Don't recurse exponentially.
358 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Chris Lattner29446522007-05-14 22:04:50 +0000360 switch (Op.getOpcode()) {
361 default: return false;
362 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000363 // Don't invert constant FP values after legalize. The negated constant
364 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000365 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000366 case ISD::FADD:
367 // FIXME: determine better conditions for this xform.
368 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000369
Bill Wendlingd34470c2009-01-30 23:10:18 +0000370 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000371 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000372 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000373 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sands25cf2272008-11-24 14:53:14 +0000374 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000375 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000376 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000377 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000378
Bill Wendlingd34470c2009-01-30 23:10:18 +0000379 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000380 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Chris Lattner29446522007-05-14 22:04:50 +0000382 case ISD::FMUL:
383 case ISD::FDIV:
384 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000385
Bill Wendlingd34470c2009-01-30 23:10:18 +0000386 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sands25cf2272008-11-24 14:53:14 +0000387 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000388 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000389
Duncan Sands25cf2272008-11-24 14:53:14 +0000390 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000391
Chris Lattner29446522007-05-14 22:04:50 +0000392 case ISD::FP_EXTEND:
393 case ISD::FP_ROUND:
394 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000395 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000396 }
397}
398
399/// GetNegatedExpression - If isNegatibleForFree returns true, this function
400/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000401static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000402 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000403 // fneg is removable even if it has multiple uses.
404 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000405
Chris Lattner29446522007-05-14 22:04:50 +0000406 // Don't allow anything with multiple uses.
407 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000408
Chris Lattner3adf9512007-05-25 02:19:06 +0000409 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000410 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000411 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000412 case ISD::ConstantFP: {
413 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
414 V.changeSign();
415 return DAG.getConstantFP(V, Op.getValueType());
416 }
Chris Lattner29446522007-05-14 22:04:50 +0000417 case ISD::FADD:
418 // FIXME: determine better conditions for this xform.
419 assert(UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000420
Bill Wendlingd34470c2009-01-30 23:10:18 +0000421 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000422 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000423 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000425 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000428 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000431 Op.getOperand(0));
432 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000433 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000434 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000435
Bill Wendlingd34470c2009-01-30 23:10:18 +0000436 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000437 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000438 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000439 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000440
Bill Wendlingd34470c2009-01-30 23:10:18 +0000441 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000442 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
443 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Chris Lattner29446522007-05-14 22:04:50 +0000445 case ISD::FMUL:
446 case ISD::FDIV:
447 assert(!HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Bill Wendlingd34470c2009-01-30 23:10:18 +0000449 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000450 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000451 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000453 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000454 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000455
Bill Wendlingd34470c2009-01-30 23:10:18 +0000456 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000457 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000458 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000459 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000460 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000461
Chris Lattner29446522007-05-14 22:04:50 +0000462 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000463 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000464 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000465 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000466 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000467 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000468 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000469 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000470 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000471 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000472 }
473}
Chris Lattner24664722006-03-01 04:53:38 +0000474
475
Nate Begeman4ebd8052005-09-01 23:24:04 +0000476// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
477// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000478// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000479// nodes based on the type of node we are checking. This simplifies life a
480// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000481static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
482 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N.getOpcode() == ISD::SETCC) {
484 LHS = N.getOperand(0);
485 RHS = N.getOperand(1);
486 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000487 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000489 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 N.getOperand(2).getOpcode() == ISD::Constant &&
491 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000492 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
494 LHS = N.getOperand(0);
495 RHS = N.getOperand(1);
496 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000497 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 return false;
500}
501
Nate Begeman99801192005-09-07 23:25:52 +0000502// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
503// one use. If this is true, it allows the users to invert the operation for
504// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000505static bool isOneUseSetCC(SDValue N) {
506 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000507 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000508 return true;
509 return false;
510}
511
Bill Wendling35247c32009-01-30 00:45:56 +0000512SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
513 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000514 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
516 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000517 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000518 SDValue OpNode =
519 DAG.FoldConstantArithmetic(Opc, VT,
520 cast<ConstantSDNode>(N0.getOperand(1)),
521 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000522 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000523 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000524 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
525 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
526 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000527 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000528 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000529 }
530 }
Bill Wendling35247c32009-01-30 00:45:56 +0000531
Nate Begemancd4d58c2006-02-03 06:46:56 +0000532 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
533 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000534 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000535 SDValue OpNode =
536 DAG.FoldConstantArithmetic(Opc, VT,
537 cast<ConstantSDNode>(N1.getOperand(1)),
538 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000539 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000540 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000541 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000542 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000543 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000544 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000545 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000546 }
547 }
Bill Wendling35247c32009-01-30 00:45:56 +0000548
Dan Gohman475871a2008-07-27 21:46:04 +0000549 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000550}
551
Dan Gohman475871a2008-07-27 21:46:04 +0000552SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
553 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000554 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
555 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000556 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000557 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000558 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000559 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000560 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000561 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000562 assert((!To[i].getNode() ||
563 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000564 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565 WorkListRemover DeadNodes(*this);
566 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000567
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000568 if (AddTo) {
569 // Push the new nodes and any users onto the worklist
570 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000571 if (To[i].getNode()) {
572 AddToWorkList(To[i].getNode());
573 AddUsersToWorkList(To[i].getNode());
574 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000575 }
576 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000577
Dan Gohmandbe664a2009-01-19 21:44:21 +0000578 // Finally, if the node is now dead, remove it from the graph. The node
579 // may not be dead if the replacement process recursively simplified to
580 // something else needing this node.
581 if (N->use_empty()) {
582 // Nodes can be reintroduced into the worklist. Make sure we do not
583 // process a node that has been replaced.
584 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000585
Dan Gohmandbe664a2009-01-19 21:44:21 +0000586 // Finally, since the node is now dead, remove it from the graph.
587 DAG.DeleteNode(N);
588 }
Dan Gohman475871a2008-07-27 21:46:04 +0000589 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000590}
591
Evan Chenge5b51ac2010-04-17 06:13:15 +0000592void DAGCombiner::
593CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000594 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000595 // are deleted, make sure to remove them from our worklist.
596 WorkListRemover DeadNodes(*this);
597 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000598
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000599 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000600 AddToWorkList(TLO.New.getNode());
601 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000602
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000603 // Finally, if the node is now dead, remove it from the graph. The node
604 // may not be dead if the replacement process recursively simplified to
605 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000606 if (TLO.Old.getNode()->use_empty()) {
607 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000608
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000609 // If the operands of this node are only used by the node, they will now
610 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000611 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
612 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
613 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Gabor Greifba36cb52008-08-28 21:40:38 +0000615 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000616 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000617}
618
619/// SimplifyDemandedBits - Check the specified integer node value to see if
620/// it can be simplified or if things it uses can be simplified by bit
621/// propagation. If so, return true.
622bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000623 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000624 APInt KnownZero, KnownOne;
625 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
626 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000627
Dan Gohmane5af2d32009-01-29 01:59:02 +0000628 // Revisit the node.
629 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000630
Dan Gohmane5af2d32009-01-29 01:59:02 +0000631 // Replace the old value with the new one.
632 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000633 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000634 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000635 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000636 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000637 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000638
Dan Gohmane5af2d32009-01-29 01:59:02 +0000639 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 return true;
641}
642
Evan Cheng95c57ea2010-04-24 04:43:44 +0000643void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
644 DebugLoc dl = Load->getDebugLoc();
645 EVT VT = Load->getValueType(0);
646 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000647
Evan Cheng95c57ea2010-04-24 04:43:44 +0000648 DEBUG(dbgs() << "\nReplacing.9 ";
649 Load->dump(&DAG);
650 dbgs() << "\nWith: ";
651 Trunc.getNode()->dump(&DAG);
652 dbgs() << '\n');
653 WorkListRemover DeadNodes(*this);
654 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc, &DeadNodes);
655 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1),
656 &DeadNodes);
657 removeFromWorkList(Load);
658 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000659 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000660}
661
662SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
663 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000664 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000665 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000666 EVT MemVT = LD->getMemoryVT();
667 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
668 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD : ISD::EXTLOAD)
669 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000670 Replace = true;
Evan Cheng4c26e932010-04-19 19:29:22 +0000671 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000672 LD->getChain(), LD->getBasePtr(),
673 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengac7eae52010-04-27 19:48:13 +0000674 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000675 LD->isNonTemporal(), LD->getAlignment());
676 }
677
Evan Cheng4c26e932010-04-19 19:29:22 +0000678 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000679 switch (Opc) {
680 default: break;
681 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000682 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000683 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000684 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000685 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000686 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000687 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000688 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000689 case ISD::Constant: {
690 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000691 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000692 return DAG.getNode(ExtOpc, dl, PVT, Op);
693 }
694 }
695
696 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000697 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000698 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000699}
700
Evan Cheng95c57ea2010-04-24 04:43:44 +0000701SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000702 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
703 return SDValue();
704 EVT OldVT = Op.getValueType();
705 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000706 bool Replace = false;
707 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
708 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000709 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000710 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000711
712 if (Replace)
713 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
714 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000715 DAG.getValueType(OldVT));
716}
717
Evan Cheng95c57ea2010-04-24 04:43:44 +0000718SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000719 EVT OldVT = Op.getValueType();
720 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000721 bool Replace = false;
722 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
723 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000724 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000725 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000726
727 if (Replace)
728 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
729 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000730}
731
Evan Cheng64b7bf72010-04-16 06:14:10 +0000732/// PromoteIntBinOp - Promote the specified integer binary operation if the
733/// target indicates it is beneficial. e.g. On x86, it's usually better to
734/// promote i16 operations to i32 since i16 instructions are longer.
735SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
736 if (!LegalOperations)
737 return SDValue();
738
739 EVT VT = Op.getValueType();
740 if (VT.isVector() || !VT.isInteger())
741 return SDValue();
742
Evan Chenge5b51ac2010-04-17 06:13:15 +0000743 // If operation type is 'undesirable', e.g. i16 on x86, consider
744 // promoting it.
745 unsigned Opc = Op.getOpcode();
746 if (TLI.isTypeDesirableForOp(Opc, VT))
747 return SDValue();
748
Evan Cheng64b7bf72010-04-16 06:14:10 +0000749 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000750 // Consult target whether it is a good idea to promote this operation and
751 // what's the right type to promote it to.
752 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000753 assert(PVT != VT && "Don't know what type to promote to!");
754
Evan Cheng95c57ea2010-04-24 04:43:44 +0000755 bool Replace0 = false;
756 SDValue N0 = Op.getOperand(0);
757 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
758 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000759 return SDValue();
760
Evan Cheng95c57ea2010-04-24 04:43:44 +0000761 bool Replace1 = false;
762 SDValue N1 = Op.getOperand(1);
763 SDValue NN1 = PromoteOperand(N1, PVT, Replace1);
764 if (NN1.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000765 return SDValue();
766
Evan Cheng95c57ea2010-04-24 04:43:44 +0000767 AddToWorkList(NN0.getNode());
768 AddToWorkList(NN1.getNode());
769
770 if (Replace0)
771 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
772 if (Replace1)
773 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000774
Evan Chengac7eae52010-04-27 19:48:13 +0000775 DEBUG(dbgs() << "\nPromoting ";
776 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000777 DebugLoc dl = Op.getDebugLoc();
778 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000779 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000780 }
781 return SDValue();
782}
783
784/// PromoteIntShiftOp - Promote the specified integer shift operation if the
785/// target indicates it is beneficial. e.g. On x86, it's usually better to
786/// promote i16 operations to i32 since i16 instructions are longer.
787SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
788 if (!LegalOperations)
789 return SDValue();
790
791 EVT VT = Op.getValueType();
792 if (VT.isVector() || !VT.isInteger())
793 return SDValue();
794
795 // If operation type is 'undesirable', e.g. i16 on x86, consider
796 // promoting it.
797 unsigned Opc = Op.getOpcode();
798 if (TLI.isTypeDesirableForOp(Opc, VT))
799 return SDValue();
800
801 EVT PVT = VT;
802 // Consult target whether it is a good idea to promote this operation and
803 // what's the right type to promote it to.
804 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
805 assert(PVT != VT && "Don't know what type to promote to!");
806
Evan Cheng95c57ea2010-04-24 04:43:44 +0000807 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000808 SDValue N0 = Op.getOperand(0);
809 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000810 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000811 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000812 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000813 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000814 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000815 if (N0.getNode() == 0)
816 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000817
Evan Chenge5b51ac2010-04-17 06:13:15 +0000818 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000819 if (Replace)
820 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000821
Evan Chengac7eae52010-04-27 19:48:13 +0000822 DEBUG(dbgs() << "\nPromoting ";
823 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000824 DebugLoc dl = Op.getDebugLoc();
825 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000826 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000827 }
828 return SDValue();
829}
830
Evan Cheng4c26e932010-04-19 19:29:22 +0000831SDValue DAGCombiner::PromoteExtend(SDValue Op) {
832 if (!LegalOperations)
833 return SDValue();
834
835 EVT VT = Op.getValueType();
836 if (VT.isVector() || !VT.isInteger())
837 return SDValue();
838
839 // If operation type is 'undesirable', e.g. i16 on x86, consider
840 // promoting it.
841 unsigned Opc = Op.getOpcode();
842 if (TLI.isTypeDesirableForOp(Opc, VT))
843 return SDValue();
844
845 EVT PVT = VT;
846 // Consult target whether it is a good idea to promote this operation and
847 // what's the right type to promote it to.
848 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
849 assert(PVT != VT && "Don't know what type to promote to!");
850 // fold (aext (aext x)) -> (aext x)
851 // fold (aext (zext x)) -> (zext x)
852 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000853 DEBUG(dbgs() << "\nPromoting ";
854 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000855 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
856 }
857 return SDValue();
858}
859
860bool DAGCombiner::PromoteLoad(SDValue Op) {
861 if (!LegalOperations)
862 return false;
863
864 EVT VT = Op.getValueType();
865 if (VT.isVector() || !VT.isInteger())
866 return false;
867
868 // If operation type is 'undesirable', e.g. i16 on x86, consider
869 // promoting it.
870 unsigned Opc = Op.getOpcode();
871 if (TLI.isTypeDesirableForOp(Opc, VT))
872 return false;
873
874 EVT PVT = VT;
875 // Consult target whether it is a good idea to promote this operation and
876 // what's the right type to promote it to.
877 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
878 assert(PVT != VT && "Don't know what type to promote to!");
879
880 DebugLoc dl = Op.getDebugLoc();
881 SDNode *N = Op.getNode();
882 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000883 EVT MemVT = LD->getMemoryVT();
884 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
885 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD : ISD::EXTLOAD)
886 : LD->getExtensionType();
Evan Cheng4c26e932010-04-19 19:29:22 +0000887 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
888 LD->getChain(), LD->getBasePtr(),
889 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengac7eae52010-04-27 19:48:13 +0000890 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000891 LD->isNonTemporal(), LD->getAlignment());
892 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
893
Evan Cheng95c57ea2010-04-24 04:43:44 +0000894 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000895 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000896 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000897 Result.getNode()->dump(&DAG);
898 dbgs() << '\n');
899 WorkListRemover DeadNodes(*this);
900 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result, &DeadNodes);
901 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1), &DeadNodes);
902 removeFromWorkList(N);
903 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000904 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000905 return true;
906 }
907 return false;
908}
909
Evan Chenge5b51ac2010-04-17 06:13:15 +0000910
Chris Lattner29446522007-05-14 22:04:50 +0000911//===----------------------------------------------------------------------===//
912// Main DAG Combiner implementation
913//===----------------------------------------------------------------------===//
914
Duncan Sands25cf2272008-11-24 14:53:14 +0000915void DAGCombiner::Run(CombineLevel AtLevel) {
916 // set the instance variables, so that the various visit routines may use it.
917 Level = AtLevel;
918 LegalOperations = Level >= NoIllegalOperations;
919 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000920
Evan Cheng17a568b2008-08-29 22:21:44 +0000921 // Add all the dag nodes to the worklist.
922 WorkList.reserve(DAG.allnodes_size());
923 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
924 E = DAG.allnodes_end(); I != E; ++I)
925 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000926
Evan Cheng17a568b2008-08-29 22:21:44 +0000927 // Create a dummy node (which is not added to allnodes), that adds a reference
928 // to the root node, preventing it from being deleted, and tracking any
929 // changes of the root.
930 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000931
Jim Laskey26f7fa72006-10-17 19:33:52 +0000932 // The root of the dag may dangle to deleted nodes until the dag combiner is
933 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000934 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000935
Evan Cheng17a568b2008-08-29 22:21:44 +0000936 // while the worklist isn't empty, inspect the node on the end of it and
937 // try and combine it.
938 while (!WorkList.empty()) {
939 SDNode *N = WorkList.back();
940 WorkList.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +0000941
Evan Cheng17a568b2008-08-29 22:21:44 +0000942 // If N has no uses, it is dead. Make sure to revisit all N's operands once
943 // N is deleted from the DAG, since they too may now be dead or may have a
944 // reduced number of uses, allowing other xforms.
945 if (N->use_empty() && N != &Dummy) {
946 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
947 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000948
Evan Cheng17a568b2008-08-29 22:21:44 +0000949 DAG.DeleteNode(N);
950 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Evan Cheng17a568b2008-08-29 22:21:44 +0000953 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000954
Evan Cheng17a568b2008-08-29 22:21:44 +0000955 if (RV.getNode() == 0)
956 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000957
Evan Cheng17a568b2008-08-29 22:21:44 +0000958 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +0000959
Evan Cheng17a568b2008-08-29 22:21:44 +0000960 // If we get back the same node we passed in, rather than a new node or
961 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +0000962 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +0000963 // mechanics for us, we have no work to do in this case.
964 if (RV.getNode() == N)
965 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
Evan Cheng17a568b2008-08-29 22:21:44 +0000967 assert(N->getOpcode() != ISD::DELETED_NODE &&
968 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
969 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000970
David Greenef1090292010-01-05 01:25:00 +0000971 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000972 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000973 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000974 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000975 dbgs() << '\n');
Evan Cheng17a568b2008-08-29 22:21:44 +0000976 WorkListRemover DeadNodes(*this);
977 if (N->getNumValues() == RV.getNode()->getNumValues())
978 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
979 else {
980 assert(N->getValueType(0) == RV.getValueType() &&
981 N->getNumValues() == 1 && "Type mismatch");
982 SDValue OpV = RV;
983 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
984 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
Evan Cheng17a568b2008-08-29 22:21:44 +0000986 // Push the new node and any users onto the worklist
987 AddToWorkList(RV.getNode());
988 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000989
Evan Cheng17a568b2008-08-29 22:21:44 +0000990 // Add any uses of the old node to the worklist in case this node is the
991 // last one that uses them. They may become dead after this node is
992 // deleted.
993 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
994 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000995
Dan Gohmandbe664a2009-01-19 21:44:21 +0000996 // Finally, if the node is now dead, remove it from the graph. The node
997 // may not be dead if the replacement process recursively simplified to
998 // something else needing this node.
999 if (N->use_empty()) {
1000 // Nodes can be reintroduced into the worklist. Make sure we do not
1001 // process a node that has been replaced.
1002 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001003
Dan Gohmandbe664a2009-01-19 21:44:21 +00001004 // Finally, since the node is now dead, remove it from the graph.
1005 DAG.DeleteNode(N);
1006 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001007 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001008
Chris Lattner95038592005-10-05 06:35:28 +00001009 // If the root changed (e.g. it was a dead load, update the root).
1010 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011}
1012
Dan Gohman475871a2008-07-27 21:46:04 +00001013SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 switch(N->getOpcode()) {
1015 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001016 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001017 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 case ISD::ADD: return visitADD(N);
1019 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001020 case ISD::ADDC: return visitADDC(N);
1021 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001022 case ISD::MUL: return visitMUL(N);
1023 case ISD::SDIV: return visitSDIV(N);
1024 case ISD::UDIV: return visitUDIV(N);
1025 case ISD::SREM: return visitSREM(N);
1026 case ISD::UREM: return visitUREM(N);
1027 case ISD::MULHU: return visitMULHU(N);
1028 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001029 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1030 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
1031 case ISD::SDIVREM: return visitSDIVREM(N);
1032 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 case ISD::AND: return visitAND(N);
1034 case ISD::OR: return visitOR(N);
1035 case ISD::XOR: return visitXOR(N);
1036 case ISD::SHL: return visitSHL(N);
1037 case ISD::SRA: return visitSRA(N);
1038 case ISD::SRL: return visitSRL(N);
1039 case ISD::CTLZ: return visitCTLZ(N);
1040 case ISD::CTTZ: return visitCTTZ(N);
1041 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001042 case ISD::SELECT: return visitSELECT(N);
1043 case ISD::SELECT_CC: return visitSELECT_CC(N);
1044 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001045 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1046 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001047 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001048 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1049 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +00001050 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001051 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001052 case ISD::FADD: return visitFADD(N);
1053 case ISD::FSUB: return visitFSUB(N);
1054 case ISD::FMUL: return visitFMUL(N);
1055 case ISD::FDIV: return visitFDIV(N);
1056 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001057 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001058 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1059 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1060 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1061 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1062 case ISD::FP_ROUND: return visitFP_ROUND(N);
1063 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1064 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1065 case ISD::FNEG: return visitFNEG(N);
1066 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001067 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001068 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001069 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001070 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001071 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001072 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001073 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1074 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001075 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001076 }
Dan Gohman475871a2008-07-27 21:46:04 +00001077 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078}
1079
Dan Gohman475871a2008-07-27 21:46:04 +00001080SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001082
1083 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001085 assert(N->getOpcode() != ISD::DELETED_NODE &&
1086 "Node was deleted but visit returned NULL!");
1087
1088 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1089 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1090
1091 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001092 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001093 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001094
1095 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1096 }
1097 }
1098
Scott Michelfdc40a02009-02-17 22:15:04 +00001099 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001100 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001101 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001102 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1103 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001104 SDValue N0 = N->getOperand(0);
1105 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001106
Evan Cheng08b11732008-03-22 01:55:50 +00001107 // Constant operands are canonicalized to RHS.
1108 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001109 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001110 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1111 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001112 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001113 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001114 }
1115 }
1116
Dan Gohman389079b2007-10-08 17:57:15 +00001117 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001118}
Dan Gohman389079b2007-10-08 17:57:15 +00001119
Chris Lattner6270f682006-10-08 22:57:01 +00001120/// getInputChainForNode - Given a node, return its input chain if it has one,
1121/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001122static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001123 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001125 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001127 return N->getOperand(NumOps-1);
1128 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001129 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001130 return N->getOperand(i);
1131 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001132 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001133}
1134
Dan Gohman475871a2008-07-27 21:46:04 +00001135SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001136 // If N has two operands, where one has an input chain equal to the other,
1137 // the 'other' chain is redundant.
1138 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001139 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001140 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001141 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001142 return N->getOperand(1);
1143 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001144
Chris Lattnerc76d4412007-05-16 06:37:59 +00001145 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001146 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001147 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001148 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001149
Jim Laskey6ff23e52006-10-04 16:53:27 +00001150 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001151 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Jim Laskey71382342006-10-07 23:37:56 +00001153 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001154 // encountered.
1155 for (unsigned i = 0; i < TFs.size(); ++i) {
1156 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001157
Jim Laskey6ff23e52006-10-04 16:53:27 +00001158 // Check each of the operands.
1159 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001160 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001161
Jim Laskey6ff23e52006-10-04 16:53:27 +00001162 switch (Op.getOpcode()) {
1163 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001164 // Entry tokens don't need to be added to the list. They are
1165 // rededundant.
1166 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001167 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001168
Jim Laskey6ff23e52006-10-04 16:53:27 +00001169 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001170 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001171 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001172 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001174 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001175 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001176 Changed = true;
1177 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001178 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001179 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001180
Jim Laskey6ff23e52006-10-04 16:53:27 +00001181 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001182 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001183 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001184 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001185 else
1186 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001187 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001188 }
1189 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001190 }
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001191
Dan Gohman475871a2008-07-27 21:46:04 +00001192 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001193
1194 // If we've change things around then replace token factor.
1195 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001196 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001197 // The entry token is the only possible outcome.
1198 Result = DAG.getEntryNode();
1199 } else {
1200 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001201 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001203 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001204
Jim Laskey274062c2006-10-13 23:32:28 +00001205 // Don't add users to work list.
1206 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001208
Jim Laskey6ff23e52006-10-04 16:53:27 +00001209 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210}
1211
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001212/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001213SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001214 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001215 // Replacing results may cause a different MERGE_VALUES to suddenly
1216 // be CSE'd with N, and carry its uses with it. Iterate until no
1217 // uses remain, to ensure that the node can be safely deleted.
1218 do {
1219 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1220 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
1221 &DeadNodes);
1222 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001223 removeFromWorkList(N);
1224 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001225 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001226}
1227
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001228static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001229SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1230 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001231 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SDValue N00 = N0.getOperand(0);
1233 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001234 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001235
Gabor Greifba36cb52008-08-28 21:40:38 +00001236 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001237 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001238 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1239 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1240 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1241 N00.getOperand(0), N01),
1242 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1243 N00.getOperand(1), N01));
1244 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001245 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001248}
1249
Dan Gohman475871a2008-07-27 21:46:04 +00001250SDValue DAGCombiner::visitADD(SDNode *N) {
1251 SDValue N0 = N->getOperand(0);
1252 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001253 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1254 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001255 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001256
1257 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001258 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001261 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001262
Dan Gohman613e0d82007-07-03 14:03:57 +00001263 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001264 if (N0.getOpcode() == ISD::UNDEF)
1265 return N0;
1266 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001267 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001269 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001270 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001271 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001272 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001273 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001275 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001276 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001277 // fold (add Sym, c) -> Sym+c
1278 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001279 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001280 GA->getOpcode() == ISD::GlobalAddress)
1281 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1282 GA->getOffset() +
1283 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001284 // fold ((c1-A)+c2) -> (c1+c2)-A
1285 if (N1C && N0.getOpcode() == ISD::SUB)
1286 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001287 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001288 DAG.getConstant(N1C->getAPIntValue()+
1289 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001290 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001291 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001292 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001293 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001294 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295 // fold ((0-A) + B) -> B-A
1296 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1297 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001298 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 // fold (A + (0-B)) -> A-B
1300 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1301 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001302 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001303 // fold (A+(B-A)) -> B
1304 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001306 // fold ((B-A)+A) -> B
1307 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1308 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001309 // fold (A+(B-(A+C))) to (B-C)
1310 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001311 N0 == N1.getOperand(1).getOperand(0))
1312 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001313 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001314 // fold (A+(B-(C+A))) to (B-C)
1315 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001316 N0 == N1.getOperand(1).getOperand(1))
1317 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001318 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001319 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001320 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1321 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001322 N0 == N1.getOperand(0).getOperand(1))
1323 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1324 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001325
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001326 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1327 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1328 SDValue N00 = N0.getOperand(0);
1329 SDValue N01 = N0.getOperand(1);
1330 SDValue N10 = N1.getOperand(0);
1331 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001332
1333 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1334 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1335 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1336 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001337 }
Chris Lattner947c2892006-03-13 06:51:27 +00001338
Dan Gohman475871a2008-07-27 21:46:04 +00001339 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1340 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Chris Lattner947c2892006-03-13 06:51:27 +00001342 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001343 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001344 APInt LHSZero, LHSOne;
1345 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001346 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001347 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001348
Dan Gohman948d8ea2008-02-20 16:33:30 +00001349 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001350 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Chris Lattner947c2892006-03-13 06:51:27 +00001352 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1353 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1354 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1355 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001356 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001357 }
1358 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001359
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001360 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001361 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001362 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001363 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001364 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001365 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001366 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001368 }
1369
Dan Gohmancd9e1552010-01-19 23:30:49 +00001370 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1371 if (N1.getOpcode() == ISD::SHL &&
1372 N1.getOperand(0).getOpcode() == ISD::SUB)
1373 if (ConstantSDNode *C =
1374 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1375 if (C->getAPIntValue() == 0)
1376 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1377 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1378 N1.getOperand(0).getOperand(1),
1379 N1.getOperand(1)));
1380 if (N0.getOpcode() == ISD::SHL &&
1381 N0.getOperand(0).getOpcode() == ISD::SUB)
1382 if (ConstantSDNode *C =
1383 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1384 if (C->getAPIntValue() == 0)
1385 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1386 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1387 N0.getOperand(0).getOperand(1),
1388 N0.getOperand(1)));
1389
Evan Cheng64b7bf72010-04-16 06:14:10 +00001390 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391}
1392
Dan Gohman475871a2008-07-27 21:46:04 +00001393SDValue DAGCombiner::visitADDC(SDNode *N) {
1394 SDValue N0 = N->getOperand(0);
1395 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001396 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001398 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001399
Chris Lattner91153682007-03-04 20:03:15 +00001400 // If the flag result is dead, turn this into an ADD.
1401 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-01-30 02:38:00 +00001402 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen874ae252009-06-02 03:12:52 +00001403 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
Chris Lattner91153682007-03-04 20:03:15 +00001406 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001407 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001408 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001409
Chris Lattnerb6541762007-03-04 20:40:38 +00001410 // fold (addc x, 0) -> x + no carry out
1411 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001412 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001413 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001414
Dale Johannesen874ae252009-06-02 03:12:52 +00001415 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001416 APInt LHSZero, LHSOne;
1417 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001418 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001419 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001420
Dan Gohman948d8ea2008-02-20 16:33:30 +00001421 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001422 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001423
Chris Lattnerb6541762007-03-04 20:40:38 +00001424 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1425 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1426 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1427 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001428 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001429 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001432
Dan Gohman475871a2008-07-27 21:46:04 +00001433 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001434}
1435
Dan Gohman475871a2008-07-27 21:46:04 +00001436SDValue DAGCombiner::visitADDE(SDNode *N) {
1437 SDValue N0 = N->getOperand(0);
1438 SDValue N1 = N->getOperand(1);
1439 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001440 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1441 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001442
Chris Lattner91153682007-03-04 20:03:15 +00001443 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001444 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001445 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1446 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001447
Chris Lattnerb6541762007-03-04 20:40:38 +00001448 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001449 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1450 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001451
Dan Gohman475871a2008-07-27 21:46:04 +00001452 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001453}
1454
Dan Gohman475871a2008-07-27 21:46:04 +00001455SDValue DAGCombiner::visitSUB(SDNode *N) {
1456 SDValue N0 = N->getOperand(0);
1457 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001460 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Dan Gohman7f321562007-06-25 16:23:39 +00001462 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001463 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001464 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001466 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001467
Chris Lattner854077d2005-10-17 01:07:11 +00001468 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001469 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001470 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001473 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001474 // fold (sub x, c) -> (add x, -c)
1475 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001476 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001477 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001478 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1479 if (N0C && N0C->isAllOnesValue())
1480 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001482 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001483 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001485 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001486 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001487 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001488 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001489 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1490 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001491 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001492 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1493 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001494 // fold ((A+(C+B))-B) -> A+C
1495 if (N0.getOpcode() == ISD::ADD &&
1496 N0.getOperand(1).getOpcode() == ISD::ADD &&
1497 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001498 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1499 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001500 // fold ((A-(B-C))-C) -> A-B
1501 if (N0.getOpcode() == ISD::SUB &&
1502 N0.getOperand(1).getOpcode() == ISD::SUB &&
1503 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001504 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1505 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001506
Dan Gohman613e0d82007-07-03 14:03:57 +00001507 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001508 if (N0.getOpcode() == ISD::UNDEF)
1509 return N0;
1510 if (N1.getOpcode() == ISD::UNDEF)
1511 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001512
Dan Gohman6520e202008-10-18 02:06:02 +00001513 // If the relocation model supports it, consider symbol offsets.
1514 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001515 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001516 // fold (sub Sym, c) -> Sym-c
1517 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1518 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1519 GA->getOffset() -
1520 (uint64_t)N1C->getSExtValue());
1521 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1522 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1523 if (GA->getGlobal() == GB->getGlobal())
1524 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1525 VT);
1526 }
1527
Evan Cheng64b7bf72010-04-16 06:14:10 +00001528 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529}
1530
Dan Gohman475871a2008-07-27 21:46:04 +00001531SDValue DAGCombiner::visitMUL(SDNode *N) {
1532 SDValue N0 = N->getOperand(0);
1533 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001534 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1535 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001536 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001537
Dan Gohman7f321562007-06-25 16:23:39 +00001538 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001539 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001542 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Dan Gohman613e0d82007-07-03 14:03:57 +00001544 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001545 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001546 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001547 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001549 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001550 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001551 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001552 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // fold (mul 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 (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001558 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1559 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001561 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001562 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001563 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001564 getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001565 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001566 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1567 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001568 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001569 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001570 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1571 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001572 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner66b8bc32009-03-09 20:22:18 +00001573 DAG.getConstant(Log2Val, getShiftAmountTy())));
1574 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001575 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001576 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001577 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001578 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1579 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001580 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001581 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1582 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001585 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1586 // use.
1587 {
Dan Gohman475871a2008-07-27 21:46:04 +00001588 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001589 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1590 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001591 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001592 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001593 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001594 isa<ConstantSDNode>(N1.getOperand(1)) &&
1595 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001596 Sh = N1; Y = N0;
1597 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001598
Gabor Greifba36cb52008-08-28 21:40:38 +00001599 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001600 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1601 Sh.getOperand(0), Y);
1602 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1603 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001604 }
1605 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001606
Chris Lattnera1deca32006-03-04 23:33:26 +00001607 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001608 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001609 isa<ConstantSDNode>(N0.getOperand(1)))
1610 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1611 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1612 N0.getOperand(0), N1),
1613 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1614 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001615
Nate Begemancd4d58c2006-02-03 06:46:56 +00001616 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001617 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001619 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001620
Evan Cheng64b7bf72010-04-16 06:14:10 +00001621 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622}
1623
Dan Gohman475871a2008-07-27 21:46:04 +00001624SDValue DAGCombiner::visitSDIV(SDNode *N) {
1625 SDValue N0 = N->getOperand(0);
1626 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001629 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630
Dan Gohman7f321562007-06-25 16:23:39 +00001631 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001632 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001634 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001636
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001638 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001639 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001640 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001641 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001642 return N0;
1643 // fold (sdiv X, -1) -> 0-X
1644 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001645 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1646 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001647 // If we know the sign bits of both operands are zero, strength reduce to a
1648 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001649 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001650 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001651 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1652 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001653 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001654 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001655 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michelfdc40a02009-02-17 22:15:04 +00001656 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman7810bfe2008-09-26 21:54:37 +00001657 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001658 // If dividing by powers of two is cheap, then don't perform the following
1659 // fold.
1660 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001661 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001662
Dan Gohman7810bfe2008-09-26 21:54:37 +00001663 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001664 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001665 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001666
Chris Lattner8f4880b2006-02-16 08:02:36 +00001667 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001668 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1669 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00001670 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001671 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001672
Chris Lattner8f4880b2006-02-16 08:02:36 +00001673 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001674 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1675 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands92abc622009-01-31 15:50:11 +00001676 getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001677 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001678 AddToWorkList(SRL.getNode());
1679 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001680 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands92abc622009-01-31 15:50:11 +00001681 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001682
Nate Begeman405e3ec2005-10-21 00:02:42 +00001683 // If we're dividing by a positive value, we're done. Otherwise, we must
1684 // negate the result.
1685 if (pow2 > 0)
1686 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001687
Gabor Greifba36cb52008-08-28 21:40:38 +00001688 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001689 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1690 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001691 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001692
Nate Begeman69575232005-10-20 02:15:44 +00001693 // if integer divide is expensive and we satisfy the requirements, emit an
1694 // alternate sequence.
Scott Michelfdc40a02009-02-17 22:15:04 +00001695 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001696 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001697 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001698 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001699 }
Dan Gohman7f321562007-06-25 16:23:39 +00001700
Dan Gohman613e0d82007-07-03 14:03:57 +00001701 // undef / X -> 0
1702 if (N0.getOpcode() == ISD::UNDEF)
1703 return DAG.getConstant(0, VT);
1704 // X / undef -> undef
1705 if (N1.getOpcode() == ISD::UNDEF)
1706 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001707
Dan Gohman475871a2008-07-27 21:46:04 +00001708 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709}
1710
Dan Gohman475871a2008-07-27 21:46:04 +00001711SDValue DAGCombiner::visitUDIV(SDNode *N) {
1712 SDValue N0 = N->getOperand(0);
1713 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001714 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1715 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001716 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001717
Dan Gohman7f321562007-06-25 16:23:39 +00001718 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001719 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001721 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001722 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001723
Nate Begeman1d4d4142005-09-01 00:19:25 +00001724 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001725 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001726 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001728 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001729 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001730 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001731 getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001732 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1733 if (N1.getOpcode() == ISD::SHL) {
1734 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001735 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001736 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001737 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1738 N1.getOperand(1),
1739 DAG.getConstant(SHC->getAPIntValue()
1740 .logBase2(),
1741 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001742 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001743 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001744 }
1745 }
1746 }
Nate Begeman69575232005-10-20 02:15:44 +00001747 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001748 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001749 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001750 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001751 }
Dan Gohman7f321562007-06-25 16:23:39 +00001752
Dan Gohman613e0d82007-07-03 14:03:57 +00001753 // undef / X -> 0
1754 if (N0.getOpcode() == ISD::UNDEF)
1755 return DAG.getConstant(0, VT);
1756 // X / undef -> undef
1757 if (N1.getOpcode() == ISD::UNDEF)
1758 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001759
Dan Gohman475871a2008-07-27 21:46:04 +00001760 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761}
1762
Dan Gohman475871a2008-07-27 21:46:04 +00001763SDValue DAGCombiner::visitSREM(SDNode *N) {
1764 SDValue N0 = N->getOperand(0);
1765 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001766 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001768 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001771 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001772 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001773 // If we know the sign bits of both operands are zero, strength reduce to a
1774 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001776 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001777 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001779
Dan Gohman77003042007-11-26 23:46:11 +00001780 // If X/C can be simplified by the division-by-constant logic, lower
1781 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001782 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001783 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001784 AddToWorkList(Div.getNode());
1785 SDValue OptimizedDiv = combine(Div.getNode());
1786 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001787 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1788 OptimizedDiv, N1);
1789 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001790 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001791 return Sub;
1792 }
Chris Lattner26d29902006-10-12 20:58:32 +00001793 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001794
Dan Gohman613e0d82007-07-03 14:03:57 +00001795 // undef % X -> 0
1796 if (N0.getOpcode() == ISD::UNDEF)
1797 return DAG.getConstant(0, VT);
1798 // X % undef -> undef
1799 if (N1.getOpcode() == ISD::UNDEF)
1800 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001801
Dan Gohman475871a2008-07-27 21:46:04 +00001802 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803}
1804
Dan Gohman475871a2008-07-27 21:46:04 +00001805SDValue DAGCombiner::visitUREM(SDNode *N) {
1806 SDValue N0 = N->getOperand(0);
1807 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001810 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001811
Nate Begeman1d4d4142005-09-01 00:19:25 +00001812 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001813 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001814 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001815 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001816 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001817 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001818 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001819 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1820 if (N1.getOpcode() == ISD::SHL) {
1821 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001822 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001824 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001825 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001826 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001828 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001829 }
1830 }
1831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001832
Dan Gohman77003042007-11-26 23:46:11 +00001833 // If X/C can be simplified by the division-by-constant logic, lower
1834 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001835 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001836 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001837 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001838 SDValue OptimizedDiv = combine(Div.getNode());
1839 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001840 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1841 OptimizedDiv, N1);
1842 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001844 return Sub;
1845 }
Chris Lattner26d29902006-10-12 20:58:32 +00001846 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001847
Dan Gohman613e0d82007-07-03 14:03:57 +00001848 // undef % X -> 0
1849 if (N0.getOpcode() == ISD::UNDEF)
1850 return DAG.getConstant(0, VT);
1851 // X % undef -> undef
1852 if (N1.getOpcode() == ISD::UNDEF)
1853 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001854
Dan Gohman475871a2008-07-27 21:46:04 +00001855 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001856}
1857
Dan Gohman475871a2008-07-27 21:46:04 +00001858SDValue DAGCombiner::visitMULHS(SDNode *N) {
1859 SDValue N0 = N->getOperand(0);
1860 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001862 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001863
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001865 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001866 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001867 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001868 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001869 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1870 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands92abc622009-01-31 15:50:11 +00001871 getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001872 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001873 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001874 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001875
Dan Gohman475871a2008-07-27 21:46:04 +00001876 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001877}
1878
Dan Gohman475871a2008-07-27 21:46:04 +00001879SDValue DAGCombiner::visitMULHU(SDNode *N) {
1880 SDValue N0 = N->getOperand(0);
1881 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001886 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001889 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001890 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001891 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001892 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001893 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001894
Dan Gohman475871a2008-07-27 21:46:04 +00001895 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001896}
1897
Dan Gohman389079b2007-10-08 17:57:15 +00001898/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1899/// compute two values. LoOp and HiOp give the opcodes for the two computations
1900/// that are being performed. Return true if a simplification was made.
1901///
Scott Michelfdc40a02009-02-17 22:15:04 +00001902SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00001903 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001904 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001905 bool HiExists = N->hasAnyUseOfValue(1);
1906 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001907 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001908 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001909 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1910 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001911 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001912 }
1913
1914 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001915 bool LoExists = N->hasAnyUseOfValue(0);
1916 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001917 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001918 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001919 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1920 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001921 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001922 }
1923
Evan Cheng44711942007-11-08 09:25:29 +00001924 // If both halves are used, return as it is.
1925 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001926 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001927
1928 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001929 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001930 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1931 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001932 AddToWorkList(Lo.getNode());
1933 SDValue LoOpt = combine(Lo.getNode());
1934 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001935 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001936 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001937 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001938 }
1939
Evan Cheng44711942007-11-08 09:25:29 +00001940 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001941 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001942 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001943 AddToWorkList(Hi.getNode());
1944 SDValue HiOpt = combine(Hi.getNode());
1945 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001946 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001947 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001948 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001949 }
Bill Wendling826d1142009-01-30 03:08:40 +00001950
Dan Gohman475871a2008-07-27 21:46:04 +00001951 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001952}
1953
Dan Gohman475871a2008-07-27 21:46:04 +00001954SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1955 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001957
Dan Gohman475871a2008-07-27 21:46:04 +00001958 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001959}
1960
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1962 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001963 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001964
Dan Gohman475871a2008-07-27 21:46:04 +00001965 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001966}
1967
Dan Gohman475871a2008-07-27 21:46:04 +00001968SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1969 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001970 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001971
Dan Gohman475871a2008-07-27 21:46:04 +00001972 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001973}
1974
Dan Gohman475871a2008-07-27 21:46:04 +00001975SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1976 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001977 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001978
Dan Gohman475871a2008-07-27 21:46:04 +00001979 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001980}
1981
Chris Lattner35e5c142006-05-05 05:51:50 +00001982/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1983/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001984SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1985 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001987 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001988
Dan Gohmanff00a552010-01-14 03:08:49 +00001989 // Bail early if none of these transforms apply.
1990 if (N0.getNode()->getNumOperands() == 0) return SDValue();
1991
Chris Lattner540121f2006-05-05 06:31:05 +00001992 // For each of OP in AND/OR/XOR:
1993 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1994 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1995 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Evan Chengd40d03e2010-01-06 19:38:29 +00001996 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Nate Begeman93e0ed32009-12-03 07:11:29 +00001997 //
1998 // do not sink logical op inside of a vector extend, since it may combine
1999 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002000 EVT Op0VT = N0.getOperand(0).getValueType();
2001 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002002 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002003 // Avoid infinite looping with PromoteIntBinOp.
2004 (N0.getOpcode() == ISD::ANY_EXTEND &&
2005 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Evan Chengd40d03e2010-01-06 19:38:29 +00002006 (N0.getOpcode() == ISD::TRUNCATE && TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002007 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002008 Op0VT == N1.getOperand(0).getValueType() &&
2009 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002010 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2011 N0.getOperand(0).getValueType(),
2012 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002014 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002016
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002017 // For each of OP in SHL/SRL/SRA/AND...
2018 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2019 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2020 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002021 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002022 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002023 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002024 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2025 N0.getOperand(0).getValueType(),
2026 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002027 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002028 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2029 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Dan Gohman475871a2008-07-27 21:46:04 +00002032 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002033}
2034
Dan Gohman475871a2008-07-27 21:46:04 +00002035SDValue DAGCombiner::visitAND(SDNode *N) {
2036 SDValue N0 = N->getOperand(0);
2037 SDValue N1 = N->getOperand(1);
2038 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002039 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2040 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002041 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002042 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002043
Dan Gohman7f321562007-06-25 16:23:39 +00002044 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002045 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002047 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Dan Gohman613e0d82007-07-03 14:03:57 +00002050 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002051 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002052 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002053 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002054 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002055 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002056 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002057 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002058 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002059 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002060 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002061 return N0;
2062 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002063 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002064 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002065 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002066 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002067 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002069 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002070 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002071 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002072 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002073 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002074 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002075 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2076 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002078 APInt Mask = ~N1C->getAPIntValue();
2079 Mask.trunc(N0Op0.getValueSizeInBits());
2080 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002081 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2082 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002083
Chris Lattner1ec05d12006-03-01 21:47:21 +00002084 // Replace uses of the AND with uses of the Zero extend node.
2085 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
Chris Lattner3603cd62006-02-02 07:17:31 +00002087 // We actually want to replace all uses of the any_extend with the
2088 // zero_extend, to avoid duplicating things. This will later cause this
2089 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002090 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002091 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002092 }
2093 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002094 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2095 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2096 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2097 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002098
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002099 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002100 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002101 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002102 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002103 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2104 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002105 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002106 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002107 }
Bill Wendling2627a882009-01-30 20:43:18 +00002108 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002109 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002110 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2111 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002112 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002113 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002114 }
Bill Wendling2627a882009-01-30 20:43:18 +00002115 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002116 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002117 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2118 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002119 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002120 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002121 }
2122 }
2123 // canonicalize equivalent to ll == rl
2124 if (LL == RR && LR == RL) {
2125 Op1 = ISD::getSetCCSwappedOperands(Op1);
2126 std::swap(RL, RR);
2127 }
2128 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002130 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002131 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002132 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002133 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2134 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002135 }
2136 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002137
Bill Wendling2627a882009-01-30 20:43:18 +00002138 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002139 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002141 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Nate Begemande996292006-02-03 22:24:05 +00002144 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2145 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002146 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002147 SimplifyDemandedBits(SDValue(N, 0)))
2148 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002149
Nate Begemanded49632005-10-13 03:11:28 +00002150 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002152 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002153 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002154 // If we zero all the possible extended bits, then we can turn this into
2155 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002156 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002157 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002158 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002159 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002160 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002161 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2162 LN0->getChain(), LN0->getBasePtr(),
2163 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002164 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002165 LN0->isVolatile(), LN0->isNonTemporal(),
2166 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002167 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002168 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002169 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002170 }
2171 }
2172 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002173 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002174 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002175 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002176 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002177 // If we zero all the possible extended bits, then we can turn this into
2178 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002179 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002180 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002181 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002182 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002183 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002184 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2185 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002186 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002187 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002188 LN0->isVolatile(), LN0->isNonTemporal(),
2189 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002190 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002191 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002192 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002193 }
2194 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002196 // fold (and (load x), 255) -> (zextload x, i8)
2197 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002198 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2199 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2200 (N0.getOpcode() == ISD::ANY_EXTEND &&
2201 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2202 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2203 LoadSDNode *LN0 = HasAnyExt
2204 ? cast<LoadSDNode>(N0.getOperand(0))
2205 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002206 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002207 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002208 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002209 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2210 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2211 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002212
Evan Chengd40d03e2010-01-06 19:38:29 +00002213 if (ExtVT == LoadedVT &&
2214 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002215 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2216
2217 SDValue NewLoad =
2218 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2219 LN0->getChain(), LN0->getBasePtr(),
2220 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002221 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2222 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002223 AddToWorkList(N);
2224 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2225 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2226 }
2227
2228 // Do not change the width of a volatile load.
2229 // Do not generate loads of non-round integer types since these can
2230 // be expensive (and would be wrong if the type is not byte sized).
2231 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2232 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2233 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002234
Chris Lattneref7634c2010-01-07 21:53:27 +00002235 unsigned Alignment = LN0->getAlignment();
2236 SDValue NewPtr = LN0->getBasePtr();
2237
2238 // For big endian targets, we need to add an offset to the pointer
2239 // to load the correct bytes. For little endian systems, we merely
2240 // need to read fewer bytes from the same pointer.
2241 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002242 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2243 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2244 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002245 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2246 NewPtr, DAG.getConstant(PtrOff, PtrType));
2247 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002248 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002249
2250 AddToWorkList(NewPtr.getNode());
2251
2252 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2253 SDValue Load =
2254 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2255 LN0->getChain(), NewPtr,
2256 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002257 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2258 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002259 AddToWorkList(N);
2260 CombineTo(LN0, Load, Load.getValue(1));
2261 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002262 }
Evan Cheng466685d2006-10-09 20:57:25 +00002263 }
Chris Lattner15045b62006-02-28 06:35:35 +00002264 }
2265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002266
Evan Cheng64b7bf72010-04-16 06:14:10 +00002267 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002268}
2269
Dan Gohman475871a2008-07-27 21:46:04 +00002270SDValue DAGCombiner::visitOR(SDNode *N) {
2271 SDValue N0 = N->getOperand(0);
2272 SDValue N1 = N->getOperand(1);
2273 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002274 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002277
Dan Gohman7f321562007-06-25 16:23:39 +00002278 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002279 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002280 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002281 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002282 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002283
Dan Gohman613e0d82007-07-03 14:03:57 +00002284 // fold (or x, undef) -> -1
Nate Begeman93e0ed32009-12-03 07:11:29 +00002285 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) {
2286 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
2287 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
2288 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002291 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002292 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002293 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00002294 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002295 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002296 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002297 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300 return N1;
2301 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002302 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002304 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00002305 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002306 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002307 return ROR;
2308 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002309 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00002310 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00002311 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00002312 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00002313 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002314 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2315 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2316 N0.getOperand(0), N1),
2317 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00002318 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002319 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2320 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2321 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2322 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002323
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002324 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00002326 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2327 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002328 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002329 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002330 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2331 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002332 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002333 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002334 }
Bill Wendling09025642009-01-30 20:59:34 +00002335 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2336 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00002337 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002338 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002339 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2340 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002341 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002342 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002343 }
2344 }
2345 // canonicalize equivalent to ll == rl
2346 if (LL == RR && LR == RL) {
2347 Op1 = ISD::getSetCCSwappedOperands(Op1);
2348 std::swap(RL, RR);
2349 }
2350 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002351 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002352 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002353 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002354 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002355 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2356 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002357 }
2358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002359
Bill Wendling09025642009-01-30 20:59:34 +00002360 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002361 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002362 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002363 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002364 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Bill Wendling09025642009-01-30 20:59:34 +00002366 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002367 if (N0.getOpcode() == ISD::AND &&
2368 N1.getOpcode() == ISD::AND &&
2369 N0.getOperand(1).getOpcode() == ISD::Constant &&
2370 N1.getOperand(1).getOpcode() == ISD::Constant &&
2371 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002372 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002373 // We can only do this xform if we know that bits from X that are set in C2
2374 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002375 const APInt &LHSMask =
2376 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2377 const APInt &RHSMask =
2378 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002379
Dan Gohmanea859be2007-06-22 14:59:07 +00002380 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2381 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002382 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2383 N0.getOperand(0), N1.getOperand(0));
2384 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2385 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002386 }
2387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002388
Chris Lattner516b9622006-09-14 20:50:57 +00002389 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002390 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002391 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002392
Evan Cheng64b7bf72010-04-16 06:14:10 +00002393 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002394}
2395
Chris Lattner516b9622006-09-14 20:50:57 +00002396/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002397static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002398 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002399 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002400 Mask = Op.getOperand(1);
2401 Op = Op.getOperand(0);
2402 } else {
2403 return false;
2404 }
2405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Chris Lattner516b9622006-09-14 20:50:57 +00002407 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2408 Shift = Op;
2409 return true;
2410 }
Bill Wendling09025642009-01-30 20:59:34 +00002411
Scott Michelfdc40a02009-02-17 22:15:04 +00002412 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002413}
2414
Chris Lattner516b9622006-09-14 20:50:57 +00002415// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2416// idioms for rotate, and if the target supports rotation instructions, generate
2417// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002418SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002419 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002420 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002421 if (!TLI.isTypeLegal(VT)) return 0;
2422
2423 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002424 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2425 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002426 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002427
Chris Lattner516b9622006-09-14 20:50:57 +00002428 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002429 SDValue LHSShift; // The shift.
2430 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002431 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2432 return 0; // Not part of a rotate.
2433
Dan Gohman475871a2008-07-27 21:46:04 +00002434 SDValue RHSShift; // The shift.
2435 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002436 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2437 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002438
Chris Lattner516b9622006-09-14 20:50:57 +00002439 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2440 return 0; // Not shifting the same value.
2441
2442 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2443 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002444
Chris Lattner516b9622006-09-14 20:50:57 +00002445 // Canonicalize shl to left side in a shl/srl pair.
2446 if (RHSShift.getOpcode() == ISD::SHL) {
2447 std::swap(LHS, RHS);
2448 std::swap(LHSShift, RHSShift);
2449 std::swap(LHSMask , RHSMask );
2450 }
2451
Duncan Sands83ec4b62008-06-06 12:08:01 +00002452 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002453 SDValue LHSShiftArg = LHSShift.getOperand(0);
2454 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2455 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002456
2457 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2458 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002459 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2460 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002461 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2462 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002463 if ((LShVal + RShVal) != OpSizeInBits)
2464 return 0;
2465
Dan Gohman475871a2008-07-27 21:46:04 +00002466 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002467 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002468 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002469 else
Bill Wendling317bd702009-01-30 21:14:50 +00002470 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002471
Chris Lattner516b9622006-09-14 20:50:57 +00002472 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002473 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002474 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002475
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002477 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2478 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002479 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002480 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002481 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2482 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002484
Bill Wendling317bd702009-01-30 21:14:50 +00002485 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002487
Gabor Greifba36cb52008-08-28 21:40:38 +00002488 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002490
Chris Lattner516b9622006-09-14 20:50:57 +00002491 // If there is a mask here, and we have a variable shift, we can't be sure
2492 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002493 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002494 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002495
Chris Lattner516b9622006-09-14 20:50:57 +00002496 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2497 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002498 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2499 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002500 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002501 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002502 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002503 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002504 return DAG.getNode(ISD::ROTL, DL, VT,
2505 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002506 else
Bill Wendling317bd702009-01-30 21:14:50 +00002507 return DAG.getNode(ISD::ROTR, DL, VT,
2508 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002509 }
Chris Lattner516b9622006-09-14 20:50:57 +00002510 }
2511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002512
Chris Lattner516b9622006-09-14 20:50:57 +00002513 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2514 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002515 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2516 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002517 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002518 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002519 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002520 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002521 return DAG.getNode(ISD::ROTR, DL, VT,
2522 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002523 else
Bill Wendling317bd702009-01-30 21:14:50 +00002524 return DAG.getNode(ISD::ROTL, DL, VT,
2525 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002526 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002527 }
2528 }
2529
Dan Gohman74feef22008-10-17 01:23:35 +00002530 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002531 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2532 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002533 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2534 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002535 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2536 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002537 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2538 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002539 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2540 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002541 if (RExtOp0.getOpcode() == ISD::SUB &&
2542 RExtOp0.getOperand(1) == LExtOp0) {
2543 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002544 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002545 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002546 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002547 if (ConstantSDNode *SUBC =
2548 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002549 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002550 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2551 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002552 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002553 }
2554 }
2555 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2556 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002557 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002558 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002559 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002560 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002561 if (ConstantSDNode *SUBC =
2562 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002563 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002564 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2565 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002566 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002567 }
2568 }
Chris Lattner516b9622006-09-14 20:50:57 +00002569 }
2570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002571
Chris Lattner516b9622006-09-14 20:50:57 +00002572 return 0;
2573}
2574
Dan Gohman475871a2008-07-27 21:46:04 +00002575SDValue DAGCombiner::visitXOR(SDNode *N) {
2576 SDValue N0 = N->getOperand(0);
2577 SDValue N1 = N->getOperand(1);
2578 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002579 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2580 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002581 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002582
Dan Gohman7f321562007-06-25 16:23:39 +00002583 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002584 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002585 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002586 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002588
Evan Cheng26471c42008-03-25 20:08:07 +00002589 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2590 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2591 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002592 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002593 if (N0.getOpcode() == ISD::UNDEF)
2594 return N0;
2595 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002596 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002597 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002598 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002599 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002600 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002601 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002602 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002603 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002604 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002605 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002606 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002607 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002608 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002609 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002610
Nate Begeman1d4d4142005-09-01 00:19:25 +00002611 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002612 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002614 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2615 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002616
Duncan Sands25cf2272008-11-24 14:53:14 +00002617 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002618 switch (N0.getOpcode()) {
2619 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002620 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00002621 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002622 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002623 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002624 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002625 N0.getOperand(3), NotCC);
2626 }
2627 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002628 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002629
Chris Lattner61c5ff42007-09-10 21:39:07 +00002630 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002631 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002632 N0.getNode()->hasOneUse() &&
2633 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002634 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002635 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002636 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002637 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002638 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002639 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002640
Bill Wendling317bd702009-01-30 21:14:50 +00002641 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002643 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002645 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2646 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002647 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2648 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002649 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002650 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002651 }
2652 }
Bill Wendling317bd702009-01-30 21:14:50 +00002653 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00002654 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00002655 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002656 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002657 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2658 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002659 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2660 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002662 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002663 }
2664 }
Bill Wendling317bd702009-01-30 21:14:50 +00002665 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00002666 if (N1C && N0.getOpcode() == ISD::XOR) {
2667 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2668 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2669 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00002670 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2671 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002672 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002673 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002674 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2675 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002676 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002677 }
2678 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002679 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002680 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002681 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002682 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002683 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002684 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2685 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Chenga87008d2009-02-25 22:49:59 +00002686 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2687 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002688 }
2689 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002690
Chris Lattner35e5c142006-05-05 05:51:50 +00002691 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2692 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002693 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002694 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002696
Chris Lattner3e104b12006-04-08 04:15:24 +00002697 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002698 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002699 SimplifyDemandedBits(SDValue(N, 0)))
2700 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002701
Evan Cheng64b7bf72010-04-16 06:14:10 +00002702 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002703}
2704
Chris Lattnere70da202007-12-06 07:33:36 +00002705/// visitShiftByConstant - Handle transforms common to the three shifts, when
2706/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002707SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002708 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002709 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002710
Chris Lattnere70da202007-12-06 07:33:36 +00002711 // We want to pull some binops through shifts, so that we have (and (shift))
2712 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2713 // thing happens with address calculations, so it's important to canonicalize
2714 // it.
2715 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00002716
Chris Lattnere70da202007-12-06 07:33:36 +00002717 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002718 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002719 case ISD::OR:
2720 case ISD::XOR:
2721 HighBitSet = false; // We can only transform sra if the high bit is clear.
2722 break;
2723 case ISD::AND:
2724 HighBitSet = true; // We can only transform sra if the high bit is set.
2725 break;
2726 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00002727 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002728 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002729 HighBitSet = false; // We can only transform sra if the high bit is clear.
2730 break;
2731 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002732
Chris Lattnere70da202007-12-06 07:33:36 +00002733 // We require the RHS of the binop to be a constant as well.
2734 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002735 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002736
2737 // FIXME: disable this unless the input to the binop is a shift by a constant.
2738 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002739 //
Bill Wendling88103372009-01-30 21:37:17 +00002740 // void foo(int *X, int i) { X[i & 1235] = 1; }
2741 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002742 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002743 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002744 BinOpLHSVal->getOpcode() != ISD::SRA &&
2745 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2746 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002747 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002748
Owen Andersone50ed302009-08-10 22:56:29 +00002749 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002750
Bill Wendling88103372009-01-30 21:37:17 +00002751 // If this is a signed shift right, and the high bit is modified by the
2752 // logical operation, do not perform the transformation. The highBitSet
2753 // boolean indicates the value of the high bit of the constant which would
2754 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002755 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002756 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2757 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002758 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002759 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002760
Chris Lattnere70da202007-12-06 07:33:36 +00002761 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002762 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2763 N->getValueType(0),
2764 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002765
2766 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002767 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2768 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002769
2770 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002771 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002772}
2773
Dan Gohman475871a2008-07-27 21:46:04 +00002774SDValue DAGCombiner::visitSHL(SDNode *N) {
2775 SDValue N0 = N->getOperand(0);
2776 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002777 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2778 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002779 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002780 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002781
Nate Begeman1d4d4142005-09-01 00:19:25 +00002782 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002783 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002784 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002785 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002786 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002787 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002788 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002789 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002790 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002791 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002792 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002793 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002794 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002795 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00002796 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002797 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002798 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002799 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002800 N1.getOperand(0).getOpcode() == ISD::AND &&
2801 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002802 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002803 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002804 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002805 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002806 APInt TruncC = N101C->getAPIntValue();
2807 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002808 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002809 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2810 DAG.getNode(ISD::TRUNCATE,
2811 N->getDebugLoc(),
2812 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002813 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002814 }
2815 }
2816
Dan Gohman475871a2008-07-27 21:46:04 +00002817 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2818 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002819
2820 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002821 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002822 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002823 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2824 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002825 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002826 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002827 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002828 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002829 }
Bill Wendling88103372009-01-30 21:37:17 +00002830 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2831 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002832 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002833 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002834 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00002835 if (c1 < VT.getSizeInBits()) {
2836 uint64_t c2 = N1C->getZExtValue();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002837 SDValue HiBitsMask =
2838 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2839 VT.getSizeInBits() - c1),
2840 VT);
Evan Chengd101a722009-07-21 05:40:15 +00002841 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2842 N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002843 HiBitsMask);
Evan Chengd101a722009-07-21 05:40:15 +00002844 if (c2 > c1)
2845 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2846 DAG.getConstant(c2-c1, N1.getValueType()));
2847 else
2848 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2849 DAG.getConstant(c1-c2, N1.getValueType()));
2850 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851 }
Bill Wendling88103372009-01-30 21:37:17 +00002852 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002853 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2854 SDValue HiBitsMask =
2855 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2856 VT.getSizeInBits() -
2857 N1C->getZExtValue()),
2858 VT);
Bill Wendling88103372009-01-30 21:37:17 +00002859 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002860 HiBitsMask);
2861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002862
Evan Chenge5b51ac2010-04-17 06:13:15 +00002863 if (N1C) {
2864 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
2865 if (NewSHL.getNode())
2866 return NewSHL;
2867 }
2868
Evan Cheng07c4e102010-04-22 20:19:46 +00002869 return PromoteIntShiftOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002870}
2871
Dan Gohman475871a2008-07-27 21:46:04 +00002872SDValue DAGCombiner::visitSRA(SDNode *N) {
2873 SDValue N0 = N->getOperand(0);
2874 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002875 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2876 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002877 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002878 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002879
Bill Wendling88103372009-01-30 21:37:17 +00002880 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002881 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002882 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002883 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002884 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002885 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002886 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002887 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002888 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002889 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00002890 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002891 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002892 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002893 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002894 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002895 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2896 // sext_inreg.
2897 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00002898 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002899 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
2900 if (VT.isVector())
2901 ExtVT = EVT::getVectorVT(*DAG.getContext(),
2902 ExtVT, VT.getVectorNumElements());
2903 if ((!LegalOperations ||
2904 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002905 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00002906 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002907 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002908
Bill Wendling88103372009-01-30 21:37:17 +00002909 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002910 if (N1C && N0.getOpcode() == ISD::SRA) {
2911 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002912 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002913 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00002914 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002915 DAG.getConstant(Sum, N1C->getValueType(0)));
2916 }
2917 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002918
Bill Wendling88103372009-01-30 21:37:17 +00002919 // fold (sra (shl X, m), (sub result_size, n))
2920 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00002921 // result_size - n != m.
2922 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00002923 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002924 if (N0.getOpcode() == ISD::SHL) {
2925 // Get the two constanst of the shifts, CN0 = m, CN = n.
2926 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2927 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002928 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00002929 EVT TruncVT =
Dan Gohman87862e72009-12-11 21:31:27 +00002930 EVT::getIntegerVT(*DAG.getContext(), OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002931 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00002932 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002933
Scott Michelfdc40a02009-02-17 22:15:04 +00002934 // If the shift is not a no-op (in which case this should be just a sign
2935 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00002936 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002937 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00002938 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002939 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2940 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002941 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002942
Duncan Sands92abc622009-01-31 15:50:11 +00002943 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002944 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2945 N0.getOperand(0), Amt);
2946 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2947 Shift);
2948 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2949 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002950 }
2951 }
2952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002953
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002954 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002955 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002956 N1.getOperand(0).getOpcode() == ISD::AND &&
2957 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002958 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002959 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002960 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002961 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002962 APInt TruncC = N101C->getAPIntValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002963 TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002964 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002965 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002966 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002967 DAG.getNode(ISD::TRUNCATE,
2968 N->getDebugLoc(),
2969 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002970 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002971 }
2972 }
2973
Scott Michelfdc40a02009-02-17 22:15:04 +00002974 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002975 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2976 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002977
2978
Nate Begeman1d4d4142005-09-01 00:19:25 +00002979 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002980 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002981 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002982
Evan Chenge5b51ac2010-04-17 06:13:15 +00002983 if (N1C) {
2984 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
2985 if (NewSRA.getNode())
2986 return NewSRA;
2987 }
2988
Evan Cheng07c4e102010-04-22 20:19:46 +00002989 return PromoteIntShiftOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002990}
2991
Dan Gohman475871a2008-07-27 21:46:04 +00002992SDValue DAGCombiner::visitSRL(SDNode *N) {
2993 SDValue N0 = N->getOperand(0);
2994 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002995 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2996 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002997 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002998 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002999
Nate Begeman1d4d4142005-09-01 00:19:25 +00003000 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003001 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003002 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003003 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003004 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003005 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003006 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003007 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003008 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003009 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003010 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003011 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003012 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003013 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003014 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003015 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003016
Bill Wendling88103372009-01-30 21:37:17 +00003017 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003018 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003019 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003020 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3021 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003022 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003023 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003024 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003025 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003026 }
Chris Lattnerefcddc32010-04-15 05:28:43 +00003027
3028 // fold (srl (shl x, c), c) -> (and x, cst2)
3029 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3030 N0.getValueSizeInBits() <= 64) {
3031 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3032 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3033 DAG.getConstant(~0ULL >> ShAmt, VT));
3034 }
3035
Scott Michelfdc40a02009-02-17 22:15:04 +00003036
Chris Lattner06afe072006-05-05 22:53:17 +00003037 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3038 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3039 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003040 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003041 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003042 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003043
Evan Chenge5b51ac2010-04-17 06:13:15 +00003044 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
3045 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
3046 N0.getOperand(0), N1);
3047 AddToWorkList(SmallShift.getNode());
3048 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3049 }
Chris Lattner06afe072006-05-05 22:53:17 +00003050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003051
Chris Lattner3657ffe2006-10-12 20:23:19 +00003052 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3053 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003054 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003055 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003056 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003058
Chris Lattner350bec02006-04-02 06:11:11 +00003059 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003060 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003061 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003062 APInt KnownZero, KnownOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00003063 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00003064 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003065
Chris Lattner350bec02006-04-02 06:11:11 +00003066 // If any of the input bits are KnownOne, then the input couldn't be all
3067 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003068 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003069
Chris Lattner350bec02006-04-02 06:11:11 +00003070 // If all of the bits input the to ctlz node are known to be zero, then
3071 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003072 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00003073 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003074
Chris Lattner350bec02006-04-02 06:11:11 +00003075 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003076 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003077 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003078 // could be set on input to the CTLZ node. If this bit is set, the SRL
3079 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3080 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003081 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003082 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003083
Chris Lattner350bec02006-04-02 06:11:11 +00003084 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003085 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands92abc622009-01-31 15:50:11 +00003086 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003087 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003088 }
Bill Wendling88103372009-01-30 21:37:17 +00003089
3090 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3091 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003092 }
3093 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003094
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003095 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003096 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003097 N1.getOperand(0).getOpcode() == ISD::AND &&
3098 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003099 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003100 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003101 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003102 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003103 APInt TruncC = N101C->getAPIntValue();
3104 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003105 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003106 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003107 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003108 DAG.getNode(ISD::TRUNCATE,
3109 N->getDebugLoc(),
3110 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003111 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003112 }
3113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003114
Chris Lattner61a4c072007-04-18 03:06:49 +00003115 // fold operands of srl based on knowledge that the low bits are not
3116 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003117 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3118 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003119
Evan Cheng9ab2b982009-12-18 21:31:31 +00003120 if (N1C) {
3121 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3122 if (NewSRL.getNode())
3123 return NewSRL;
3124 }
3125
3126 // Here is a common situation. We want to optimize:
3127 //
3128 // %a = ...
3129 // %b = and i32 %a, 2
3130 // %c = srl i32 %b, 1
3131 // brcond i32 %c ...
3132 //
3133 // into
3134 //
3135 // %a = ...
3136 // %b = and %a, 2
3137 // %c = setcc eq %b, 0
3138 // brcond %c ...
3139 //
3140 // However when after the source operand of SRL is optimized into AND, the SRL
3141 // itself may not be optimized further. Look for it and add the BRCOND into
3142 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003143 if (N->hasOneUse()) {
3144 SDNode *Use = *N->use_begin();
3145 if (Use->getOpcode() == ISD::BRCOND)
3146 AddToWorkList(Use);
3147 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3148 // Also look pass the truncate.
3149 Use = *Use->use_begin();
3150 if (Use->getOpcode() == ISD::BRCOND)
3151 AddToWorkList(Use);
3152 }
3153 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003154
Evan Cheng07c4e102010-04-22 20:19:46 +00003155 return PromoteIntShiftOp(SDValue(N, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +00003156}
3157
Dan Gohman475871a2008-07-27 21:46:04 +00003158SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3159 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003160 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003161
3162 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003163 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003164 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003165 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003166}
3167
Dan Gohman475871a2008-07-27 21:46:04 +00003168SDValue DAGCombiner::visitCTTZ(SDNode *N) {
3169 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003170 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003171
Nate Begeman1d4d4142005-09-01 00:19:25 +00003172 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003173 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003174 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003175 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003176}
3177
Dan Gohman475871a2008-07-27 21:46:04 +00003178SDValue DAGCombiner::visitCTPOP(SDNode *N) {
3179 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003180 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003181
Nate Begeman1d4d4142005-09-01 00:19:25 +00003182 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003183 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003184 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003185 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003186}
3187
Dan Gohman475871a2008-07-27 21:46:04 +00003188SDValue DAGCombiner::visitSELECT(SDNode *N) {
3189 SDValue N0 = N->getOperand(0);
3190 SDValue N1 = N->getOperand(1);
3191 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003192 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3193 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3194 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00003195 EVT VT = N->getValueType(0);
3196 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00003197
Bill Wendling34584e62009-01-30 22:02:18 +00003198 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003199 if (N1 == N2)
3200 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003201 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003202 if (N0C && !N0C->isNullValue())
3203 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003204 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00003205 if (N0C && N0C->isNullValue())
3206 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00003207 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003208 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00003209 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3210 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00003211 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00003213 (VT0.isInteger() &&
3214 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00003215 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003216 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00003217 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00003218 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
3219 N0, DAG.getConstant(1, VT0));
3220 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
3221 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00003223 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00003224 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
3225 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00003226 }
Bill Wendling34584e62009-01-30 22:02:18 +00003227 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00003229 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003230 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003231 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003232 }
Bill Wendling34584e62009-01-30 22:02:18 +00003233 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003235 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003236 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003237 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00003238 }
Bill Wendling34584e62009-01-30 22:02:18 +00003239 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00003241 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
3242 // fold (select X, X, Y) -> (or X, Y)
3243 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00003245 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3246 // fold (select X, Y, X) -> (and X, Y)
3247 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00003249 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003250
Chris Lattner40c62d52005-10-18 06:04:22 +00003251 // If we can fold this based on the true/false value, do so.
3252 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00003253 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003254
Nate Begeman44728a72005-09-19 22:34:01 +00003255 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003256 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003257 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00003259 // having to say they don't support SELECT_CC on every type the DAG knows
3260 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00003261 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00003262 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00003263 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
3264 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00003265 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00003266 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003267 }
Bill Wendling34584e62009-01-30 22:02:18 +00003268
Dan Gohman475871a2008-07-27 21:46:04 +00003269 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00003270}
3271
Dan Gohman475871a2008-07-27 21:46:04 +00003272SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
3273 SDValue N0 = N->getOperand(0);
3274 SDValue N1 = N->getOperand(1);
3275 SDValue N2 = N->getOperand(2);
3276 SDValue N3 = N->getOperand(3);
3277 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00003278 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003279
Nate Begeman44728a72005-09-19 22:34:01 +00003280 // fold select_cc lhs, rhs, x, x, cc -> x
3281 if (N2 == N3)
3282 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003283
Chris Lattner5f42a242006-09-20 06:19:26 +00003284 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00003285 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003286 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00003287 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00003288
Gabor Greifba36cb52008-08-28 21:40:38 +00003289 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003290 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00003291 return N2; // cond always true -> true val
3292 else
3293 return N3; // cond always false -> false val
3294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003295
Chris Lattner5f42a242006-09-20 06:19:26 +00003296 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00003297 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00003298 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
3299 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00003300 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003301
Chris Lattner40c62d52005-10-18 06:04:22 +00003302 // If we can fold this based on the true/false value, do so.
3303 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00003304 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00003305
Nate Begeman44728a72005-09-19 22:34:01 +00003306 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00003307 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00003308}
3309
Dan Gohman475871a2008-07-27 21:46:04 +00003310SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003311 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003312 cast<CondCodeSDNode>(N->getOperand(2))->get(),
3313 N->getDebugLoc());
Nate Begeman452d7be2005-09-16 00:54:12 +00003314}
3315
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003316// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00003317// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003318// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00003319// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00003320static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003321 unsigned ExtOpc,
3322 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00003323 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003324 bool HasCopyToRegUses = false;
3325 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00003326 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
3327 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003328 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00003329 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003330 if (User == N)
3331 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00003332 if (UI.getUse().getResNo() != N0.getResNo())
3333 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003334 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00003335 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003336 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
3337 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
3338 // Sign bits will be lost after a zext.
3339 return false;
3340 bool Add = false;
3341 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003343 if (UseOp == N0)
3344 continue;
3345 if (!isa<ConstantSDNode>(UseOp))
3346 return false;
3347 Add = true;
3348 }
3349 if (Add)
3350 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003351 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003352 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00003353 // If truncates aren't free and there are users we can't
3354 // extend, it isn't worthwhile.
3355 if (!isTruncFree)
3356 return false;
3357 // Remember if this value is live-out.
3358 if (User->getOpcode() == ISD::CopyToReg)
3359 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003360 }
3361
3362 if (HasCopyToRegUses) {
3363 bool BothLiveOut = false;
3364 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3365 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003366 SDUse &Use = UI.getUse();
3367 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
3368 BothLiveOut = true;
3369 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003370 }
3371 }
3372 if (BothLiveOut)
3373 // Both unextended and extended values are live out. There had better be
3374 // good a reason for the transformation.
3375 return ExtendNodes.size();
3376 }
3377 return true;
3378}
3379
Dan Gohman475871a2008-07-27 21:46:04 +00003380SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
3381 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003382 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003383
Nate Begeman1d4d4142005-09-01 00:19:25 +00003384 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003385 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003386 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003387
Nate Begeman1d4d4142005-09-01 00:19:25 +00003388 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003389 // fold (sext (aext x)) -> (sext x)
3390 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003391 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
3392 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003393
Chris Lattner22558872007-02-26 03:13:59 +00003394 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003395 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3396 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003397 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3398 if (NarrowLoad.getNode()) {
3399 if (NarrowLoad.getNode() != N0.getNode())
3400 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanc7b34442009-04-27 02:00:55 +00003401 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00003402 }
Evan Chengc88138f2007-03-22 01:54:19 +00003403
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003404 // See if the value being truncated is already sign extended. If so, just
3405 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00003406 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00003407 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
3408 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
3409 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00003410 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00003411
Chris Lattner22558872007-02-26 03:13:59 +00003412 if (OpBits == DestBits) {
3413 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3414 // bits, it is already ready.
3415 if (NumSignBits > DestBits-MidBits)
3416 return Op;
3417 } else if (OpBits < DestBits) {
3418 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3419 // bits, just sext from i32.
3420 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003421 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003422 } else {
3423 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3424 // bits, just truncate to i32.
3425 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003426 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003427 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003428
Chris Lattner22558872007-02-26 03:13:59 +00003429 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003430 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3431 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00003432 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003433 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00003434 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003435 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3436 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00003437 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00003438 }
Chris Lattner6007b842006-09-21 06:00:20 +00003439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003440
Evan Cheng110dec22005-12-14 02:19:23 +00003441 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003442 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003443 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003444 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003445 bool DoXform = true;
3446 SmallVector<SDNode*, 4> SetCCs;
3447 if (!N0.hasOneUse())
3448 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3449 if (DoXform) {
3450 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003451 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3452 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003453 LN0->getBasePtr(), LN0->getSrcValue(),
3454 LN0->getSrcValueOffset(),
3455 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003456 LN0->isVolatile(), LN0->isNonTemporal(),
3457 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003458 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003459 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3460 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003461 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003462
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003463 // Extend SetCC uses if necessary.
3464 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3465 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003467
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003468 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003469 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003470 if (SOp == Trunc)
3471 Ops.push_back(ExtLoad);
3472 else
Dan Gohman57fc82d2009-04-09 03:51:29 +00003473 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3474 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003475 }
3476
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003477 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003478 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003479 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003480 &Ops[0], Ops.size()));
3481 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003482
Dan Gohman475871a2008-07-27 21:46:04 +00003483 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003484 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003485 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003486
3487 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3488 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003489 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3490 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003491 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003492 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003493 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003494 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003495 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3496 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003497 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003498 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003499 LN0->isVolatile(), LN0->isNonTemporal(),
3500 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003501 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003502 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003503 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3504 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003505 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003506 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003507 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003508 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003509
Chris Lattner20a35c32007-04-11 05:32:27 +00003510 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00003511 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3512 if (VT.isVector() &&
3513 // We know that the # elements of the results is the same as the
3514 // # elements of the compare (and the # elements of the compare result
3515 // for that matter). Check to see that they are the same size. If so,
3516 // we know that the element size of the sext'd result matches the
3517 // element size of the compare operands.
3518 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3519
3520 // Only do this before legalize for now.
3521 !LegalOperations) {
3522 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3523 N0.getOperand(1),
3524 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3525 }
3526
3527 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00003528 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003529 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00003530 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003531 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003532 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003533 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003534 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003535 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00003536 if (!LegalOperations ||
3537 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
3538 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3539 DAG.getSetCC(N->getDebugLoc(),
3540 TLI.getSetCCResultType(VT),
3541 N0.getOperand(0), N0.getOperand(1),
3542 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
3543 NegOne, DAG.getConstant(0, VT));
Chris Lattner20a35c32007-04-11 05:32:27 +00003544 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00003545
3546
Scott Michelfdc40a02009-02-17 22:15:04 +00003547
Dan Gohman8f0ad582008-04-28 16:58:24 +00003548 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003549 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003550 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003551 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003552
Evan Cheng4c26e932010-04-19 19:29:22 +00003553 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003554}
3555
Dan Gohman475871a2008-07-27 21:46:04 +00003556SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3557 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003558 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003559
Nate Begeman1d4d4142005-09-01 00:19:25 +00003560 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003561 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003562 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003563 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003564 // fold (zext (aext x)) -> (zext x)
3565 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003566 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3567 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003568
Evan Chengc88138f2007-03-22 01:54:19 +00003569 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3570 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003571 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003572 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3573 if (NarrowLoad.getNode()) {
3574 if (NarrowLoad.getNode() != N0.getNode())
3575 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003576 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003577 }
Evan Chengc88138f2007-03-22 01:54:19 +00003578 }
3579
Chris Lattner6007b842006-09-21 06:00:20 +00003580 // fold (zext (truncate x)) -> (and x, mask)
3581 if (N0.getOpcode() == ISD::TRUNCATE &&
Evan Chengd40d03e2010-01-06 19:38:29 +00003582 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) &&
3583 (!TLI.isTruncateFree(N0.getOperand(0).getValueType(),
3584 N0.getValueType()) ||
3585 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003586 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003587 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003588 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003589 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003590 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003591 }
Dan Gohman87862e72009-12-11 21:31:27 +00003592 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
3593 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00003594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003595
Dan Gohman97121ba2009-04-08 00:15:30 +00003596 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3597 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00003598 if (N0.getOpcode() == ISD::AND &&
3599 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003600 N0.getOperand(1).getOpcode() == ISD::Constant &&
3601 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3602 N0.getValueType()) ||
3603 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003604 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003605 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003606 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003607 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003608 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003609 }
Dan Gohman220a8232008-03-03 23:51:38 +00003610 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003611 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003612 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3613 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003615
Evan Cheng110dec22005-12-14 02:19:23 +00003616 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003617 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003618 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003619 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003620 bool DoXform = true;
3621 SmallVector<SDNode*, 4> SetCCs;
3622 if (!N0.hasOneUse())
3623 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3624 if (DoXform) {
3625 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003626 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3627 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003628 LN0->getBasePtr(), LN0->getSrcValue(),
3629 LN0->getSrcValueOffset(),
3630 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003631 LN0->isVolatile(), LN0->isNonTemporal(),
3632 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003633 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003634 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3635 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003636 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003637
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003638 // Extend SetCC uses if necessary.
3639 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3640 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003641 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003642
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003643 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003644 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003645 if (SOp == Trunc)
3646 Ops.push_back(ExtLoad);
3647 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00003648 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3649 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003650 }
3651
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003652 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003653 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003654 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003655 &Ops[0], Ops.size()));
3656 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003657
Dan Gohman475871a2008-07-27 21:46:04 +00003658 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003659 }
Evan Cheng110dec22005-12-14 02:19:23 +00003660 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003661
3662 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3663 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003664 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3665 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003666 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003667 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003668 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003669 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003670 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3671 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003672 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003673 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003674 LN0->isVolatile(), LN0->isNonTemporal(),
3675 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003676 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003677 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003678 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3679 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003680 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003681 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003682 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003683 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003684
Chris Lattner20a35c32007-04-11 05:32:27 +00003685 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3686 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003687 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003688 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00003689 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003690 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003691 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003692 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003693
Evan Cheng9818c042009-12-15 03:00:32 +00003694 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00003695 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00003696 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00003697 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
3698 N0.hasOneUse()) {
Evan Cheng9818c042009-12-15 03:00:32 +00003699 if (N0.getOpcode() == ISD::SHL) {
3700 // If the original shl may be shifting out bits, do not perform this
3701 // transformation.
3702 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3703 unsigned KnownZeroBits = N0.getOperand(0).getValueType().getSizeInBits() -
3704 N0.getOperand(0).getOperand(0).getValueType().getSizeInBits();
3705 if (ShAmt > KnownZeroBits)
3706 return SDValue();
3707 }
Evan Cheng99b653c2009-12-15 00:41:36 +00003708 DebugLoc dl = N->getDebugLoc();
3709 return DAG.getNode(N0.getOpcode(), dl, VT,
3710 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0.getOperand(0)),
Bill Wendling9f7c5c02010-01-05 22:39:10 +00003711 DAG.getNode(ISD::ZERO_EXTEND, dl,
3712 N0.getOperand(1).getValueType(),
3713 N0.getOperand(1)));
Evan Cheng99b653c2009-12-15 00:41:36 +00003714 }
3715
Evan Cheng4c26e932010-04-19 19:29:22 +00003716 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003717}
3718
Dan Gohman475871a2008-07-27 21:46:04 +00003719SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3720 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003721 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003722
Chris Lattner5ffc0662006-05-05 05:58:59 +00003723 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003724 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003725 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003726 // fold (aext (aext x)) -> (aext x)
3727 // fold (aext (zext x)) -> (zext x)
3728 // fold (aext (sext x)) -> (sext x)
3729 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3730 N0.getOpcode() == ISD::ZERO_EXTEND ||
3731 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003732 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003733
Evan Chengc88138f2007-03-22 01:54:19 +00003734 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3735 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3736 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003737 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3738 if (NarrowLoad.getNode()) {
3739 if (NarrowLoad.getNode() != N0.getNode())
3740 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003741 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003742 }
Evan Chengc88138f2007-03-22 01:54:19 +00003743 }
3744
Chris Lattner84750582006-09-20 06:29:17 +00003745 // fold (aext (truncate x))
3746 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003747 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003748 if (TruncOp.getValueType() == VT)
3749 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003750 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003751 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3752 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003754
Dan Gohman97121ba2009-04-08 00:15:30 +00003755 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3756 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00003757 if (N0.getOpcode() == ISD::AND &&
3758 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003759 N0.getOperand(1).getOpcode() == ISD::Constant &&
3760 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3761 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003762 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003763 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003764 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003765 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003766 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003767 }
Dan Gohman220a8232008-03-03 23:51:38 +00003768 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003769 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003770 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3771 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003772 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003773
Chris Lattner5ffc0662006-05-05 05:58:59 +00003774 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman57fc82d2009-04-09 03:51:29 +00003775 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003776 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003777 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003778 bool DoXform = true;
3779 SmallVector<SDNode*, 4> SetCCs;
3780 if (!N0.hasOneUse())
3781 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3782 if (DoXform) {
3783 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3784 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3785 LN0->getChain(),
3786 LN0->getBasePtr(), LN0->getSrcValue(),
3787 LN0->getSrcValueOffset(),
3788 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003789 LN0->isVolatile(), LN0->isNonTemporal(),
3790 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00003791 CombineTo(N, ExtLoad);
3792 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3793 N0.getValueType(), ExtLoad);
3794 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3795
3796 // Extend SetCC uses if necessary.
3797 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3798 SDNode *SetCC = SetCCs[i];
3799 SmallVector<SDValue, 4> Ops;
3800
3801 for (unsigned j = 0; j != 2; ++j) {
3802 SDValue SOp = SetCC->getOperand(j);
3803 if (SOp == Trunc)
3804 Ops.push_back(ExtLoad);
3805 else
3806 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3807 N->getDebugLoc(), VT, SOp));
3808 }
3809
3810 Ops.push_back(SetCC->getOperand(2));
3811 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3812 SetCC->getValueType(0),
3813 &Ops[0], Ops.size()));
3814 }
3815
3816 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3817 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00003818 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003819
Chris Lattner5ffc0662006-05-05 05:58:59 +00003820 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3821 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3822 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003823 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003824 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003825 N0.hasOneUse()) {
3826 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003827 EVT MemVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003828 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3829 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003830 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003831 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003832 LN0->isVolatile(), LN0->isNonTemporal(),
3833 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003834 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003835 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003836 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3837 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003838 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003839 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003841
Chris Lattner20a35c32007-04-11 05:32:27 +00003842 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3843 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003844 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003845 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003846 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003847 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003848 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003849 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003851
Evan Cheng4c26e932010-04-19 19:29:22 +00003852 return PromoteExtend(SDValue(N, 0));
Chris Lattner5ffc0662006-05-05 05:58:59 +00003853}
3854
Chris Lattner2b4c2792007-10-13 06:35:54 +00003855/// GetDemandedBits - See if the specified operand can be simplified with the
3856/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003857/// simpler operand, otherwise return a null SDValue.
3858SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003859 switch (V.getOpcode()) {
3860 default: break;
3861 case ISD::OR:
3862 case ISD::XOR:
3863 // If the LHS or RHS don't contribute bits to the or, drop them.
3864 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3865 return V.getOperand(1);
3866 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3867 return V.getOperand(0);
3868 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003869 case ISD::SRL:
3870 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003871 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003872 break;
3873 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3874 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003875 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00003876
Dan Gohmancc91d632009-01-03 19:22:06 +00003877 // Watch out for shift count overflow though.
3878 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003879 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003880 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00003881 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003882 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00003883 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00003884 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003885 }
Dan Gohman475871a2008-07-27 21:46:04 +00003886 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003887}
3888
Evan Chengc88138f2007-03-22 01:54:19 +00003889/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3890/// bits and then truncated to a narrower type and where N is a multiple
3891/// of number of bits of the narrower type, transform it to a narrower load
3892/// from address + N / num of bits of new type. If the result is to be
3893/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003894SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003895 unsigned Opc = N->getOpcode();
3896 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003897 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003898 EVT VT = N->getValueType(0);
3899 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003900
Dan Gohman7f8613e2008-08-14 20:04:46 +00003901 // This transformation isn't valid for vector loads.
3902 if (VT.isVector())
3903 return SDValue();
3904
Dan Gohmand1996362010-01-09 02:13:55 +00003905 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00003906 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003907 if (Opc == ISD::SIGN_EXTEND_INREG) {
3908 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00003909 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3910 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003911 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003912 }
3913
Owen Andersone50ed302009-08-10 22:56:29 +00003914 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003915 unsigned ShAmt = 0;
Eli Friedman5aba5c02009-08-23 00:14:19 +00003916 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003917 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003918 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003919 // Is the shift amount a multiple of size of VT?
3920 if ((ShAmt & (EVTBits-1)) == 0) {
3921 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00003922 // Is the load width a multiple of size of VT?
3923 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00003924 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003925 }
3926 }
3927 }
3928
Duncan Sandsad205a72008-06-16 08:14:38 +00003929 // Do not generate loads of non-round integer types since these can
3930 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersone50ed302009-08-10 22:56:29 +00003931 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Chris Lattner6dc86852010-04-15 05:40:59 +00003932 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() >= EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003933 // Do not change the width of a volatile load.
3934 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003935 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003936 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00003937
Evan Chengdae54ce2007-03-24 00:02:43 +00003938 // For big endian targets, we need to adjust the offset to the pointer to
3939 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003940 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003941 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003942 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003943 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3944 }
Bill Wendling8509c902009-01-30 22:33:24 +00003945
Evan Chengdae54ce2007-03-24 00:02:43 +00003946 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003947 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendling9729c5a2009-01-31 03:12:48 +00003948 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling8509c902009-01-30 22:33:24 +00003949 PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003950 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003951 AddToWorkList(NewPtr.getNode());
Bill Wendling8509c902009-01-30 22:33:24 +00003952
Dan Gohman475871a2008-07-27 21:46:04 +00003953 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling8509c902009-01-30 22:33:24 +00003954 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003955 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003956 LN0->isVolatile(), LN0->isNonTemporal(), NewAlign)
Bill Wendling8509c902009-01-30 22:33:24 +00003957 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003958 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003959 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
3960 NewAlign);
Bill Wendling8509c902009-01-30 22:33:24 +00003961
Dan Gohman764fd0c2009-01-21 15:17:51 +00003962 // Replace the old load's chain with the new load's chain.
3963 WorkListRemover DeadNodes(*this);
3964 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3965 &DeadNodes);
Bill Wendling8509c902009-01-30 22:33:24 +00003966
Dan Gohman764fd0c2009-01-21 15:17:51 +00003967 // Return the new loaded value.
3968 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003969 }
3970
Dan Gohman475871a2008-07-27 21:46:04 +00003971 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003972}
3973
Dan Gohman475871a2008-07-27 21:46:04 +00003974SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3975 SDValue N0 = N->getOperand(0);
3976 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003977 EVT VT = N->getValueType(0);
3978 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003979 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00003980 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003981
Nate Begeman1d4d4142005-09-01 00:19:25 +00003982 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003983 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00003984 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003985
Chris Lattner541a24f2006-05-06 22:43:44 +00003986 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00003987 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003988 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003989
Nate Begeman646d7e22005-09-02 21:18:40 +00003990 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3991 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003992 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00003993 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3994 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003995 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003996
Dan Gohman75dcf082008-07-31 00:50:31 +00003997 // fold (sext_in_reg (sext x)) -> (sext x)
3998 // fold (sext_in_reg (aext x)) -> (sext x)
3999 // if x is small enough.
4000 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
4001 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00004002 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
4003 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00004004 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00004005 }
4006
Chris Lattner95a5e052007-04-17 19:03:21 +00004007 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004008 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004009 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004010
Chris Lattner95a5e052007-04-17 19:03:21 +00004011 // fold operands of sext_in_reg based on knowledge that the top bits are not
4012 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004013 if (SimplifyDemandedBits(SDValue(N, 0)))
4014 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004015
Evan Chengc88138f2007-03-22 01:54:19 +00004016 // fold (sext_in_reg (load x)) -> (smaller sextload x)
4017 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00004018 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004019 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00004020 return NarrowLoad;
4021
Bill Wendling8509c902009-01-30 22:33:24 +00004022 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
4023 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00004024 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
4025 if (N0.getOpcode() == ISD::SRL) {
4026 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00004027 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00004028 // We can turn this into an SRA iff the input to the SRL is already sign
4029 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00004030 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00004031 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00004032 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
4033 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00004034 }
4035 }
Evan Chengc88138f2007-03-22 01:54:19 +00004036
Nate Begemanded49632005-10-13 03:11:28 +00004037 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00004038 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004039 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004040 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004041 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004042 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004043 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00004044 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
4045 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004046 LN0->getBasePtr(), LN0->getSrcValue(),
4047 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00004048 LN0->isVolatile(), LN0->isNonTemporal(),
4049 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004050 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004051 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004052 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004053 }
4054 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00004055 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00004056 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004057 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004058 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004059 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004060 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00004061 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
4062 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004063 LN0->getBasePtr(), LN0->getSrcValue(),
4064 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00004065 LN0->isVolatile(), LN0->isNonTemporal(),
4066 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004067 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004068 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004069 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004070 }
Dan Gohman475871a2008-07-27 21:46:04 +00004071 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004072}
4073
Dan Gohman475871a2008-07-27 21:46:04 +00004074SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
4075 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004076 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004077
4078 // noop truncate
4079 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004080 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004081 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004082 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00004083 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004084 // fold (truncate (truncate x)) -> (truncate x)
4085 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00004086 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004087 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00004088 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
4089 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00004090 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004091 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004092 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00004093 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4094 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004095 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004096 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00004097 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004098 else
4099 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00004100 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00004101 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004102 }
Evan Cheng007b69e2007-03-21 20:14:05 +00004103
Chris Lattner2b4c2792007-10-13 06:35:54 +00004104 // See if we can simplify the input to this truncate through knowledge that
4105 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
4106 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004108 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004109 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004110 if (Shorter.getNode())
Bill Wendling67a67682009-01-30 22:44:24 +00004111 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004112
Nate Begeman3df4d522005-10-12 20:40:40 +00004113 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00004114 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chenge5b51ac2010-04-17 06:13:15 +00004115 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT))
4116 return ReduceLoadWidth(N);
4117 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004118}
4119
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004120static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004122 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00004123 return Elt.getNode();
4124 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004125}
4126
4127/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00004128/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00004129SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004130 assert(N->getOpcode() == ISD::BUILD_PAIR);
4131
Nate Begemanabc01992009-06-05 21:37:30 +00004132 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
4133 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
4134 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004135 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00004136 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00004137
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004138 if (ISD::isNON_EXTLoad(LD2) &&
4139 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004140 // If both are volatile this would reduce the number of volatile loads.
4141 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00004142 !LD1->isVolatile() &&
4143 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00004144 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00004145 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00004146 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004147 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00004148
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004149 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004150 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00004151 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
4152 LD1->getBasePtr(), LD1->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00004153 LD1->getSrcValueOffset(), false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004154 }
Bill Wendling67a67682009-01-30 22:44:24 +00004155
Dan Gohman475871a2008-07-27 21:46:04 +00004156 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004157}
4158
Dan Gohman475871a2008-07-27 21:46:04 +00004159SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
4160 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004161 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00004162
Dan Gohman7f321562007-06-25 16:23:39 +00004163 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
4164 // Only do this before legalize, since afterward the target may be depending
4165 // on the bitconvert.
4166 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00004167 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004168 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004169 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00004170 bool isSimple = true;
4171 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
4172 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
4173 N0.getOperand(i).getOpcode() != ISD::Constant &&
4174 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004175 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00004176 break;
4177 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004178
Owen Andersone50ed302009-08-10 22:56:29 +00004179 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004180 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00004181 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00004182 if (isSimple)
Gabor Greifba36cb52008-08-28 21:40:38 +00004183 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00004184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004185
Dan Gohman3dd168d2008-09-05 01:58:21 +00004186 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00004187 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling67a67682009-01-30 22:44:24 +00004188 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00004189 if (Res.getNode() != N) {
4190 if (!LegalOperations ||
4191 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
4192 return Res;
4193
4194 // Folding it resulted in an illegal node, and it's too late to
4195 // do that. Clean up the old node and forego the transformation.
4196 // Ideally this won't happen very often, because instcombine
4197 // and the earlier dagcombine runs (where illegal nodes are
4198 // permitted) should have folded most of them already.
4199 DAG.DeleteNode(Res.getNode());
4200 }
Chris Lattner94683772005-12-23 05:30:37 +00004201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004202
Bill Wendling67a67682009-01-30 22:44:24 +00004203 // (conv (conv x, t1), t2) -> (conv x, t2)
4204 if (N0.getOpcode() == ISD::BIT_CONVERT)
4205 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
4206 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00004207
Chris Lattner57104102005-12-23 05:44:41 +00004208 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00004209 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004211 // Do not change the width of a volatile load.
4212 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004213 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004214 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00004215 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004216 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00004217 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00004218
Evan Cheng59d5b682007-05-07 21:27:48 +00004219 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00004220 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
4221 LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004222 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00004223 LN0->isVolatile(), LN0->isNonTemporal(),
4224 OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00004225 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00004226 CombineTo(N0.getNode(),
Bill Wendling67a67682009-01-30 22:44:24 +00004227 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
4228 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00004229 Load.getValue(1));
4230 return Load;
4231 }
Chris Lattner57104102005-12-23 05:44:41 +00004232 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004233
Bill Wendling67a67682009-01-30 22:44:24 +00004234 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
4235 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00004236 // This often reduces constant pool loads.
4237 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004238 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling67a67682009-01-30 22:44:24 +00004239 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
4240 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004241 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004242
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004244 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00004245 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
4246 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004247 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00004248 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4249 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004250 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004251
Bill Wendling67a67682009-01-30 22:44:24 +00004252 // fold (bitconvert (fcopysign cst, x)) ->
4253 // (or (and (bitconvert x), sign), (and cst, (not sign)))
4254 // Note that we don't handle (copysign x, cst) because this can always be
4255 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004256 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00004257 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004258 VT.isInteger() && !VT.isVector()) {
4259 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00004260 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00004261 if (isTypeLegal(IntXVT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004262 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004263 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00004264 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004265
Duncan Sands25cf2272008-11-24 14:53:14 +00004266 // If X has a different width than the result/lhs, sext it or truncate it.
4267 unsigned VTWidth = VT.getSizeInBits();
4268 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004269 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004270 AddToWorkList(X.getNode());
4271 } else if (OrigXWidth > VTWidth) {
4272 // To get the sign bit in the right place, we have to shift it right
4273 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00004274 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004275 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00004276 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
4277 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004278 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004279 AddToWorkList(X.getNode());
4280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004281
Duncan Sands25cf2272008-11-24 14:53:14 +00004282 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004283 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004284 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004285 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004286
Bill Wendling9729c5a2009-01-31 03:12:48 +00004287 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004288 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00004289 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004290 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004291 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004292
Bill Wendling67a67682009-01-30 22:44:24 +00004293 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00004294 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00004295 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004296
Scott Michelfdc40a02009-02-17 22:15:04 +00004297 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004298 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004299 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
4300 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004301 return CombineLD;
4302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Dan Gohman475871a2008-07-27 21:46:04 +00004304 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00004305}
4306
Dan Gohman475871a2008-07-27 21:46:04 +00004307SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00004308 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004309 return CombineConsecutiveLoads(N, VT);
4310}
4311
Dan Gohman7f321562007-06-25 16:23:39 +00004312/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00004313/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00004314/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00004315SDValue DAGCombiner::
Owen Andersone50ed302009-08-10 22:56:29 +00004316ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
4317 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004318
Chris Lattner6258fb22006-04-02 02:53:43 +00004319 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00004320 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004321
Duncan Sands83ec4b62008-06-06 12:08:01 +00004322 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
4323 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004324
Chris Lattner6258fb22006-04-02 02:53:43 +00004325 // If this is a conversion of N elements of one type to N elements of another
4326 // type, convert each element. This handles FP<->INT cases.
4327 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004329 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00004330 SDValue Op = BV->getOperand(i);
4331 // If the vector element type is not legal, the BUILD_VECTOR operands
4332 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00004333 if (Op.getValueType() != SrcEltVT)
4334 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004335 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00004336 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00004337 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00004338 }
Owen Anderson23b9b192009-08-12 00:36:31 +00004339 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004340 BV->getValueType(0).getVectorNumElements());
Evan Chenga87008d2009-02-25 22:49:59 +00004341 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4342 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004343 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004344
Chris Lattner6258fb22006-04-02 02:53:43 +00004345 // Otherwise, we're growing or shrinking the elements. To avoid having to
4346 // handle annoying details of growing/shrinking FP values, we convert them to
4347 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004348 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004349 // Convert the input float vector to a int vector where the elements are the
4350 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004352 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004353 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00004354 SrcEltVT = IntVT;
4355 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004356
Chris Lattner6258fb22006-04-02 02:53:43 +00004357 // Now we know the input is an integer vector. If the output is a FP type,
4358 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004359 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004360 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004361 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004362 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004363
Chris Lattner6258fb22006-04-02 02:53:43 +00004364 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00004365 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00004366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004367
Chris Lattner6258fb22006-04-02 02:53:43 +00004368 // Okay, we know the src/dst types are both integers of differing types.
4369 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004370 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00004371 if (SrcBitSize < DstBitSize) {
4372 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00004373
Dan Gohman475871a2008-07-27 21:46:04 +00004374 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004375 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00004376 i += NumInputsPerOutput) {
4377 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00004378 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00004379 bool EltIsUndef = true;
4380 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
4381 // Shift the previously computed bits over.
4382 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00004384 if (Op.getOpcode() == ISD::UNDEF) continue;
4385 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004386
Dan Gohman58c25872010-04-12 02:24:01 +00004387 NewBits |= APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
4388 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004390
Chris Lattner6258fb22006-04-02 02:53:43 +00004391 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00004392 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004393 else
4394 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
4395 }
4396
Owen Anderson23b9b192009-08-12 00:36:31 +00004397 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00004398 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4399 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004400 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004401
Chris Lattner6258fb22006-04-02 02:53:43 +00004402 // Finally, this must be the case where we are shrinking elements: each input
4403 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00004404 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00004405 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00004406 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
4407 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00004408 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004409
Dan Gohman7f321562007-06-25 16:23:39 +00004410 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004411 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
4412 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00004413 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004414 continue;
4415 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004416
Bob Wilsonb1303d02009-04-13 22:05:19 +00004417 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
4418 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004419
Chris Lattner6258fb22006-04-02 02:53:43 +00004420 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00004421 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004422 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00004423 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00004424 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00004425 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
4426 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00004427 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004428 }
4429
4430 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00004431 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00004432 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
4433 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004434
Evan Chenga87008d2009-02-25 22:49:59 +00004435 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4436 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004437}
4438
Dan Gohman475871a2008-07-27 21:46:04 +00004439SDValue DAGCombiner::visitFADD(SDNode *N) {
4440 SDValue N0 = N->getOperand(0);
4441 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004442 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4443 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004444 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004445
Dan Gohman7f321562007-06-25 16:23:39 +00004446 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004447 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004448 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004449 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004450 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004451
Bill Wendlingb0162f52009-01-30 22:53:48 +00004452 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004454 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004455 // canonicalize constant to RHS
4456 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004457 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
4458 // fold (fadd A, 0) -> A
Dan Gohman760f86f2009-01-22 21:58:43 +00004459 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4460 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004461 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004462 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004463 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004464 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00004465 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sands25cf2272008-11-24 14:53:14 +00004466 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004467 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00004468 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004469
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004470 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4471 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004472 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004473 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004474 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4475 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004476
Dan Gohman475871a2008-07-27 21:46:04 +00004477 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004478}
4479
Dan Gohman475871a2008-07-27 21:46:04 +00004480SDValue DAGCombiner::visitFSUB(SDNode *N) {
4481 SDValue N0 = N->getOperand(0);
4482 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004483 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4484 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004485 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004486
Dan Gohman7f321562007-06-25 16:23:39 +00004487 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004488 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004489 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004490 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004492
Nate Begemana0e221d2005-10-18 00:28:13 +00004493 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004495 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004496 // fold (fsub A, 0) -> A
Dan Gohmana90c8e62009-01-23 19:10:37 +00004497 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4498 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004499 // fold (fsub 0, B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004500 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004501 if (isNegatibleForFree(N1, LegalOperations))
4502 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00004503 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004504 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00004505 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004506 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004507 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004508 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004509 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004510
Dan Gohman475871a2008-07-27 21:46:04 +00004511 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004512}
4513
Dan Gohman475871a2008-07-27 21:46:04 +00004514SDValue DAGCombiner::visitFMUL(SDNode *N) {
4515 SDValue N0 = N->getOperand(0);
4516 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004517 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4518 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004519 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004520
Dan Gohman7f321562007-06-25 16:23:39 +00004521 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004522 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004523 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004524 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004525 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004526
Nate Begeman11af4ea2005-10-17 20:40:11 +00004527 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004528 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004529 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004530 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004531 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004532 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4533 // fold (fmul A, 0) -> 0
Dan Gohman760f86f2009-01-22 21:58:43 +00004534 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4535 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00004536 // fold (fmul A, 0) -> 0, vector edition.
4537 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4538 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004539 // fold (fmul X, 2.0) -> (fadd X, X)
4540 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004541 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00004542 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00004543 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004544 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004545 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004546
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004547 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004548 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4549 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004550 // Both can be negated for free, check to see if at least one is cheaper
4551 // negated.
4552 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004553 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004554 GetNegatedExpression(N0, DAG, LegalOperations),
4555 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004556 }
4557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004558
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004559 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4560 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004561 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004562 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00004563 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00004564 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004565
Dan Gohman475871a2008-07-27 21:46:04 +00004566 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004567}
4568
Dan Gohman475871a2008-07-27 21:46:04 +00004569SDValue DAGCombiner::visitFDIV(SDNode *N) {
4570 SDValue N0 = N->getOperand(0);
4571 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004572 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4573 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004574 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004575
Dan Gohman7f321562007-06-25 16:23:39 +00004576 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004577 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004578 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004579 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004581
Nate Begemana148d982006-01-18 22:35:16 +00004582 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004583 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004584 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004585
4586
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004587 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004588 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4589 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004590 // Both can be negated for free, check to see if at least one is cheaper
4591 // negated.
4592 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00004593 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004594 GetNegatedExpression(N0, DAG, LegalOperations),
4595 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004596 }
4597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004598
Dan Gohman475871a2008-07-27 21:46:04 +00004599 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004600}
4601
Dan Gohman475871a2008-07-27 21:46:04 +00004602SDValue DAGCombiner::visitFREM(SDNode *N) {
4603 SDValue N0 = N->getOperand(0);
4604 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004605 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4606 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004607 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004608
Nate Begemana148d982006-01-18 22:35:16 +00004609 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004610 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004611 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004612
Dan Gohman475871a2008-07-27 21:46:04 +00004613 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004614}
4615
Dan Gohman475871a2008-07-27 21:46:04 +00004616SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4617 SDValue N0 = N->getOperand(0);
4618 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004619 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4620 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004621 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004622
Owen Anderson825b72b2009-08-11 20:47:22 +00004623 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004624 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004625
Chris Lattner12d83032006-03-05 05:30:57 +00004626 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004627 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004628 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4629 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004630 if (!V.isNegative()) {
4631 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004632 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004633 } else {
4634 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004635 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004636 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00004637 }
Chris Lattner12d83032006-03-05 05:30:57 +00004638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004639
Chris Lattner12d83032006-03-05 05:30:57 +00004640 // copysign(fabs(x), y) -> copysign(x, y)
4641 // copysign(fneg(x), y) -> copysign(x, y)
4642 // copysign(copysign(x,z), y) -> copysign(x, y)
4643 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4644 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004645 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4646 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00004647
4648 // copysign(x, abs(y)) -> abs(x)
4649 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004650 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004651
Chris Lattner12d83032006-03-05 05:30:57 +00004652 // copysign(x, copysign(y,z)) -> copysign(x, z)
4653 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004654 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4655 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004656
Chris Lattner12d83032006-03-05 05:30:57 +00004657 // copysign(x, fp_extend(y)) -> copysign(x, y)
4658 // copysign(x, fp_round(y)) -> copysign(x, y)
4659 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004660 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4661 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004662
Dan Gohman475871a2008-07-27 21:46:04 +00004663 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004664}
4665
Dan Gohman475871a2008-07-27 21:46:04 +00004666SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4667 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004668 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004669 EVT VT = N->getValueType(0);
4670 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00004671
Nate Begeman1d4d4142005-09-01 00:19:25 +00004672 // fold (sint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004673 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004674 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004675
Chris Lattnercda88752008-06-26 00:16:49 +00004676 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4677 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004678 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4679 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004680 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004681 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004682 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004683 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004684
Dan Gohman475871a2008-07-27 21:46:04 +00004685 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004686}
4687
Dan Gohman475871a2008-07-27 21:46:04 +00004688SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4689 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004690 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004691 EVT VT = N->getValueType(0);
4692 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004693
Nate Begeman1d4d4142005-09-01 00:19:25 +00004694 // fold (uint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004696 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004697
Chris Lattnercda88752008-06-26 00:16:49 +00004698 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4699 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004700 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4701 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004702 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004703 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004704 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004705 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004706
Dan Gohman475871a2008-07-27 21:46:04 +00004707 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004708}
4709
Dan Gohman475871a2008-07-27 21:46:04 +00004710SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4711 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004712 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004713 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004714
Nate Begeman1d4d4142005-09-01 00:19:25 +00004715 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004716 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004717 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4718
Dan Gohman475871a2008-07-27 21:46:04 +00004719 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004720}
4721
Dan Gohman475871a2008-07-27 21:46:04 +00004722SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4723 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004724 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004725 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004726
Nate Begeman1d4d4142005-09-01 00:19:25 +00004727 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00004728 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004729 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4730
Dan Gohman475871a2008-07-27 21:46:04 +00004731 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004732}
4733
Dan Gohman475871a2008-07-27 21:46:04 +00004734SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4735 SDValue N0 = N->getOperand(0);
4736 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004737 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004738 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004739
Nate Begeman1d4d4142005-09-01 00:19:25 +00004740 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004741 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004742 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004743
Chris Lattner79dbea52006-03-13 06:26:26 +00004744 // fold (fp_round (fp_extend x)) -> x
4745 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4746 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004748 // fold (fp_round (fp_round x)) -> (fp_round x)
4749 if (N0.getOpcode() == ISD::FP_ROUND) {
4750 // This is a value preserving truncation if both round's are.
4751 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004752 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00004753 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004754 DAG.getIntPtrConstant(IsTrunc));
4755 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004756
Chris Lattner79dbea52006-03-13 06:26:26 +00004757 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004758 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00004759 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4760 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004761 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004762 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4763 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00004764 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004765
Dan Gohman475871a2008-07-27 21:46:04 +00004766 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004767}
4768
Dan Gohman475871a2008-07-27 21:46:04 +00004769SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4770 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004771 EVT VT = N->getValueType(0);
4772 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004773 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004774
Nate Begeman1d4d4142005-09-01 00:19:25 +00004775 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00004776 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004777 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004778 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004779 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004780
Dan Gohman475871a2008-07-27 21:46:04 +00004781 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004782}
4783
Dan Gohman475871a2008-07-27 21:46:04 +00004784SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4785 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004786 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004787 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004788
Chris Lattner5938bef2007-12-29 06:55:23 +00004789 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00004790 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004791 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004792 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004793
Nate Begeman1d4d4142005-09-01 00:19:25 +00004794 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004796 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004797
4798 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4799 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004800 if (N0.getOpcode() == ISD::FP_ROUND
4801 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004802 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004803 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004804 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004805 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4806 In, N0.getOperand(1));
4807 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00004808 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004809
Chris Lattner0bd48932008-01-17 07:00:52 +00004810 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004811 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004812 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004813 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004814 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004815 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4816 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004817 LN0->getBasePtr(), LN0->getSrcValue(),
4818 LN0->getSrcValueOffset(),
4819 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004820 LN0->isVolatile(), LN0->isNonTemporal(),
4821 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004822 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004823 CombineTo(N0.getNode(),
4824 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4825 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004826 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004827 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004828 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004829
Dan Gohman475871a2008-07-27 21:46:04 +00004830 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004831}
4832
Dan Gohman475871a2008-07-27 21:46:04 +00004833SDValue DAGCombiner::visitFNEG(SDNode *N) {
4834 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004835 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004836
Duncan Sands25cf2272008-11-24 14:53:14 +00004837 if (isNegatibleForFree(N0, LegalOperations))
4838 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004839
Chris Lattner3bd39d42008-01-27 17:42:27 +00004840 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4841 // constant pool values.
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004842 if (N0.getOpcode() == ISD::BIT_CONVERT &&
4843 !VT.isVector() &&
4844 N0.getNode()->hasOneUse() &&
4845 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004846 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004847 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004848 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004849 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4850 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004851 AddToWorkList(Int.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004852 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004853 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004854 }
4855 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004856
Dan Gohman475871a2008-07-27 21:46:04 +00004857 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004858}
4859
Dan Gohman475871a2008-07-27 21:46:04 +00004860SDValue DAGCombiner::visitFABS(SDNode *N) {
4861 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004862 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004863 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004864
Nate Begeman1d4d4142005-09-01 00:19:25 +00004865 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004867 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004868 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004869 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004870 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004871 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004872 // fold (fabs (fcopysign x, y)) -> (fabs x)
4873 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004874 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004875
Chris Lattner3bd39d42008-01-27 17:42:27 +00004876 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4877 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004878 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004879 N0.getOperand(0).getValueType().isInteger() &&
4880 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004881 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004882 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004883 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004884 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004885 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004886 AddToWorkList(Int.getNode());
Bill Wendlingc0debad2009-01-30 23:27:35 +00004887 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4888 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004889 }
4890 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004891
Dan Gohman475871a2008-07-27 21:46:04 +00004892 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004893}
4894
Dan Gohman475871a2008-07-27 21:46:04 +00004895SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4896 SDValue Chain = N->getOperand(0);
4897 SDValue N1 = N->getOperand(1);
4898 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004899
Dan Gohmane0f06c72009-11-17 00:47:23 +00004900 // If N is a constant we could fold this into a fallthrough or unconditional
4901 // branch. However that doesn't happen very often in normal code, because
4902 // Instcombine/SimplifyCFG should have handled the available opportunities.
4903 // If we did this folding here, it would be necessary to update the
4904 // MachineBasicBlock CFG, which is awkward.
4905
Nate Begeman750ac1b2006-02-01 07:19:44 +00004906 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4907 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00004908 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4910 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004911 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004912 N1.getOperand(0), N1.getOperand(1), N2);
4913 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004914
Evan Chengd40d03e2010-01-06 19:38:29 +00004915 SDNode *Trunc = 0;
4916 if (N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) {
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004917 // Look past truncate.
Evan Chengd40d03e2010-01-06 19:38:29 +00004918 Trunc = N1.getNode();
4919 N1 = N1.getOperand(0);
4920 }
4921
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004922 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4923 // Match this pattern so that we can generate simpler code:
4924 //
4925 // %a = ...
4926 // %b = and i32 %a, 2
4927 // %c = srl i32 %b, 1
4928 // brcond i32 %c ...
4929 //
4930 // into
4931 //
4932 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00004933 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004934 // %c = setcc eq %b, 0
4935 // brcond %c ...
4936 //
4937 // This applies only when the AND constant value has one bit set and the
4938 // SRL constant is equal to the log2 of the AND constant. The back-end is
4939 // smart enough to convert the result into a TEST/JMP sequence.
4940 SDValue Op0 = N1.getOperand(0);
4941 SDValue Op1 = N1.getOperand(1);
4942
4943 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004944 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004945 SDValue AndOp1 = Op0.getOperand(1);
4946
4947 if (AndOp1.getOpcode() == ISD::Constant) {
4948 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4949
4950 if (AndConst.isPowerOf2() &&
4951 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4952 SDValue SetCC =
4953 DAG.getSetCC(N->getDebugLoc(),
4954 TLI.getSetCCResultType(Op0.getValueType()),
4955 Op0, DAG.getConstant(0, Op0.getValueType()),
4956 ISD::SETNE);
4957
Evan Chengd40d03e2010-01-06 19:38:29 +00004958 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4959 MVT::Other, Chain, SetCC, N2);
4960 // Don't add the new BRCond into the worklist or else SimplifySelectCC
4961 // will convert it back to (X & C1) >> C2.
4962 CombineTo(N, NewBRCond, false);
4963 // Truncate is dead.
4964 if (Trunc) {
4965 removeFromWorkList(Trunc);
4966 DAG.DeleteNode(Trunc);
4967 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004968 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00004969 WorkListRemover DeadNodes(*this);
4970 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004971 removeFromWorkList(N1.getNode());
4972 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00004973 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004974 }
4975 }
4976 }
4977 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00004978
4979 // Transform br(xor(x, y)) -> br(x != y)
4980 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
4981 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
4982 SDNode *TheXor = N1.getNode();
4983 SDValue Op0 = TheXor->getOperand(0);
4984 SDValue Op1 = TheXor->getOperand(1);
4985 if (Op0.getOpcode() == Op1.getOpcode()) {
4986 // Avoid missing important xor optimizations.
4987 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00004988 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00004989 DEBUG(dbgs() << "\nReplacing.8 ";
4990 TheXor->dump(&DAG);
4991 dbgs() << "\nWith: ";
4992 Tmp.getNode()->dump(&DAG);
4993 dbgs() << '\n');
4994 WorkListRemover DeadNodes(*this);
4995 DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes);
4996 removeFromWorkList(TheXor);
4997 DAG.DeleteNode(TheXor);
4998 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4999 MVT::Other, Chain, Tmp, N2);
5000 }
5001 }
5002
5003 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
5004 bool Equal = false;
5005 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
5006 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
5007 Op0.getOpcode() == ISD::XOR) {
5008 TheXor = Op0.getNode();
5009 Equal = true;
5010 }
5011
Chris Lattnerf2f64e92010-03-10 23:46:44 +00005012 SDValue NodeToReplace = Trunc ? SDValue(Trunc, 0) : N1;
5013
5014 EVT SetCCVT = NodeToReplace.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00005015 if (LegalTypes)
5016 SetCCVT = TLI.getSetCCResultType(SetCCVT);
5017 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
5018 SetCCVT,
5019 Op0, Op1,
5020 Equal ? ISD::SETEQ : ISD::SETNE);
5021 // Replace the uses of XOR with SETCC
5022 WorkListRemover DeadNodes(*this);
Chris Lattnerf2f64e92010-03-10 23:46:44 +00005023 DAG.ReplaceAllUsesOfValueWith(NodeToReplace, SetCC, &DeadNodes);
5024 removeFromWorkList(NodeToReplace.getNode());
5025 DAG.DeleteNode(NodeToReplace.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00005026 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
5027 MVT::Other, Chain, SetCC, N2);
5028 }
5029 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005030
Dan Gohman475871a2008-07-27 21:46:04 +00005031 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005032}
5033
Chris Lattner3ea0b472005-10-05 06:47:48 +00005034// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
5035//
Dan Gohman475871a2008-07-27 21:46:04 +00005036SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00005037 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005039
Dan Gohmane0f06c72009-11-17 00:47:23 +00005040 // If N is a constant we could fold this into a fallthrough or unconditional
5041 // branch. However that doesn't happen very often in normal code, because
5042 // Instcombine/SimplifyCFG should have handled the available opportunities.
5043 // If we did this folding here, it would be necessary to update the
5044 // MachineBasicBlock CFG, which is awkward.
5045
Duncan Sands8eab8a22008-06-09 11:32:28 +00005046 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00005047 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005048 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
5049 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005050 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00005051
Nate Begemane17daeb2005-10-05 21:43:42 +00005052 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00005053 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00005055 N->getOperand(0), Simp.getOperand(2),
5056 Simp.getOperand(0), Simp.getOperand(1),
5057 N->getOperand(4));
5058
Dan Gohman475871a2008-07-27 21:46:04 +00005059 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005060}
5061
Duncan Sandsec87aa82008-06-15 20:12:31 +00005062/// CombineToPreIndexedLoadStore - Try turning a load / store into a
5063/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00005064/// and it has other uses besides the load / store. After the
5065/// transformation, the new indexed load / store has effectively folded
5066/// the add / subtract in and all of its other uses are redirected to the
5067/// new load / store.
5068bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00005069 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00005070 return false;
5071
5072 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005073 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005074 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005075 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005076 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005077 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005078 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00005079 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00005080 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
5081 return false;
5082 Ptr = LD->getBasePtr();
5083 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005084 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005085 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005086 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005087 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
5088 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
5089 return false;
5090 Ptr = ST->getBasePtr();
5091 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005092 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005093 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005094 }
Chris Lattner448f2192006-11-11 00:39:41 +00005095
Chris Lattner9f1794e2006-11-11 00:56:29 +00005096 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
5097 // out. There is no reason to make this a preinc/predec.
5098 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00005099 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005100 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005101
Chris Lattner9f1794e2006-11-11 00:56:29 +00005102 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00005103 SDValue BasePtr;
5104 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005105 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5106 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
5107 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00005108 // Don't create a indexed load / store with zero offset.
5109 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005110 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005111 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005112
Chris Lattner41e53fd2006-11-11 01:00:15 +00005113 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00005114 // 1) The new base ptr is a frame index.
5115 // 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 +00005116 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00005117 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00005118 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00005119 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00005120
Chris Lattner41e53fd2006-11-11 01:00:15 +00005121 // Check #1. Preinc'ing a frame index would require copying the stack pointer
5122 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00005123 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00005124 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005125
Chris Lattner41e53fd2006-11-11 01:00:15 +00005126 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005127 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005128 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005129 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005130 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005131 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005132
Evan Chengc843abe2007-05-24 02:35:39 +00005133 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005134 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005135 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5136 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005137 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005138 if (Use == N)
5139 continue;
Evan Cheng917be682008-03-04 00:41:45 +00005140 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005141 return false;
5142
5143 if (!((Use->getOpcode() == ISD::LOAD &&
5144 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005145 (Use->getOpcode() == ISD::STORE &&
5146 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005147 RealUse = true;
5148 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005149
Chris Lattner9f1794e2006-11-11 00:56:29 +00005150 if (!RealUse)
5151 return false;
5152
Dan Gohman475871a2008-07-27 21:46:04 +00005153 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005154 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00005155 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5156 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005157 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00005158 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5159 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005160 ++PreIndexedNodes;
5161 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005162 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005163 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005164 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005165 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005166 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005167 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005168 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005169 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005170 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005171 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005172 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005173 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005174 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005175 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005176 }
5177
Chris Lattner9f1794e2006-11-11 00:56:29 +00005178 // Finally, since the node is now dead, remove it from the graph.
5179 DAG.DeleteNode(N);
5180
5181 // Replace the uses of Ptr with uses of the updated base value.
5182 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005183 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00005184 removeFromWorkList(Ptr.getNode());
5185 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00005186
5187 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005188}
5189
Duncan Sandsec87aa82008-06-15 20:12:31 +00005190/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00005191/// add / sub of the base pointer node into a post-indexed load / store.
5192/// The transformation folded the add / subtract into the new indexed
5193/// load / store effectively and all of its uses are redirected to the
5194/// new load / store.
5195bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00005196 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00005197 return false;
5198
5199 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005200 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005201 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005202 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005203 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005204 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005205 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005206 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
5207 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
5208 return false;
5209 Ptr = LD->getBasePtr();
5210 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005211 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005212 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005213 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005214 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
5215 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
5216 return false;
5217 Ptr = ST->getBasePtr();
5218 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005219 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005220 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005221 }
Chris Lattner448f2192006-11-11 00:39:41 +00005222
Gabor Greifba36cb52008-08-28 21:40:38 +00005223 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005224 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005225
Gabor Greifba36cb52008-08-28 21:40:38 +00005226 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5227 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005228 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005229 if (Op == N ||
5230 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
5231 continue;
5232
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue BasePtr;
5234 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005235 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5236 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Bob Wilson92ad3632009-09-10 22:09:31 +00005237 if (Ptr == Offset && Op->getOpcode() == ISD::ADD)
Chris Lattner9f1794e2006-11-11 00:56:29 +00005238 std::swap(BasePtr, Offset);
5239 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00005240 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00005241 // Don't create a indexed load / store with zero offset.
5242 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005243 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005244 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005245
Chris Lattner9f1794e2006-11-11 00:56:29 +00005246 // Try turning it into a post-indexed load / store except when
5247 // 1) All uses are load / store ops that use it as base ptr.
5248 // 2) Op must be independent of N, i.e. Op is neither a predecessor
5249 // nor a successor of N. Otherwise, if Op is folded that would
5250 // create a cycle.
5251
Evan Chengcaab1292009-05-06 18:25:01 +00005252 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
5253 continue;
5254
Chris Lattner9f1794e2006-11-11 00:56:29 +00005255 // Check for #1.
5256 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005257 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
5258 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00005259 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00005260 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00005261 continue;
5262
Chris Lattner9f1794e2006-11-11 00:56:29 +00005263 // If all the uses are load / store addresses, then don't do the
5264 // transformation.
5265 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
5266 bool RealUse = false;
5267 for (SDNode::use_iterator III = Use->use_begin(),
5268 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00005269 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005270 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005271 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005272 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005273 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005274 RealUse = true;
5275 }
Chris Lattner448f2192006-11-11 00:39:41 +00005276
Chris Lattner9f1794e2006-11-11 00:56:29 +00005277 if (!RealUse) {
5278 TryNext = true;
5279 break;
Chris Lattner448f2192006-11-11 00:39:41 +00005280 }
5281 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005282 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005283
Chris Lattner9f1794e2006-11-11 00:56:29 +00005284 if (TryNext)
5285 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005286
Chris Lattner9f1794e2006-11-11 00:56:29 +00005287 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00005288 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005289 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00005290 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5291 BasePtr, Offset, AM)
5292 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5293 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005294 ++PostIndexedNodes;
5295 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005296 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005297 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005298 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005299 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005300 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005301 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005302 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005303 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005304 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005305 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005306 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005307 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005308 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005309 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00005310 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005311
Chris Lattner9f1794e2006-11-11 00:56:29 +00005312 // Finally, since the node is now dead, remove it from the graph.
5313 DAG.DeleteNode(N);
5314
5315 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00005316 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00005317 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005318 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005319 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005320 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005321 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005322 }
5323 }
5324 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005325
Chris Lattner448f2192006-11-11 00:39:41 +00005326 return false;
5327}
5328
Dan Gohman475871a2008-07-27 21:46:04 +00005329SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00005330 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005331 SDValue Chain = LD->getChain();
5332 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Evan Cheng45a7ca92007-05-01 00:38:21 +00005334 // If load is not volatile and there are no uses of the loaded value (and
5335 // the updated indexed value in case of indexed loads), change uses of the
5336 // chain value into uses of the chain input (i.e. delete the dead load).
5337 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00005339 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00005340 if (N->hasNUsesOfValue(0, 0)) {
5341 // It's not safe to use the two value CombineTo variant here. e.g.
5342 // v1, chain2 = load chain1, loc
5343 // v2, chain3 = load chain2, loc
5344 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00005345 // Now we replace use of chain2 with chain1. This makes the second load
5346 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00005347 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005348 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005349 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005350 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005351 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005352 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005353 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00005354
Chris Lattner125991a2008-01-24 07:57:06 +00005355 if (N->use_empty()) {
5356 removeFromWorkList(N);
5357 DAG.DeleteNode(N);
5358 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005359
Dan Gohman475871a2008-07-27 21:46:04 +00005360 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00005361 }
Evan Cheng498f5592007-05-01 08:53:39 +00005362 } else {
5363 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00005364 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Evan Cheng498f5592007-05-01 08:53:39 +00005365 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00005366 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00005367 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005368 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005369 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005370 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005371 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005372 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005373 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
5374 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00005375 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005376 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005377 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00005378 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00005379 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005380 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00005381 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00005382 }
5383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005384
Chris Lattner01a22022005-10-10 22:04:48 +00005385 // If this load is directly stored, replace the load value with the stored
5386 // value.
5387 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005388 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00005389 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
5390 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005391 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005392 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
5393 if (PrevST->getBasePtr() == Ptr &&
5394 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005395 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00005396 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005398
Evan Cheng255f20f2010-04-01 06:04:33 +00005399 // Try to infer better alignment information than the load already has.
5400 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
5401 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5402 if (Align > LD->getAlignment())
5403 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
5404 LD->getValueType(0),
5405 Chain, Ptr, LD->getSrcValue(),
5406 LD->getSrcValueOffset(), LD->getMemoryVT(),
5407 LD->isVolatile(), LD->isNonTemporal(), Align);
5408 }
5409 }
5410
Jim Laskey7ca56af2006-10-11 13:47:09 +00005411 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005412 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005414
Jim Laskey6ff23e52006-10-04 16:53:27 +00005415 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005416 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005418
Jim Laskey279f0532006-09-25 16:29:54 +00005419 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005420 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005421 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
5422 BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00005423 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005424 LD->isVolatile(), LD->isNonTemporal(),
5425 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005426 } else {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005427 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005428 LD->getValueType(0),
5429 BetterChain, Ptr, LD->getSrcValue(),
5430 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005431 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005432 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005433 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005434 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005435 }
Jim Laskey279f0532006-09-25 16:29:54 +00005436
Jim Laskey6ff23e52006-10-04 16:53:27 +00005437 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00005438 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005439 MVT::Other, Chain, ReplLoad.getValue(1));
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005440
5441 // Make sure the new and old chains are cleaned up.
5442 AddToWorkList(Token.getNode());
5443
Jim Laskey274062c2006-10-13 23:32:28 +00005444 // Replace uses with load result and token factor. Don't add users
5445 // to work list.
5446 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005447 }
5448 }
5449
Evan Cheng7fc033a2006-11-03 03:06:21 +00005450 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005451 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005452 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00005453
Evan Cheng4c26e932010-04-19 19:29:22 +00005454 if (PromoteLoad(SDValue(N, 0)))
5455 return SDValue(N, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00005456 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00005457}
5458
Chris Lattner2392ae72010-04-15 04:48:01 +00005459/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
5460/// load is having specific bytes cleared out. If so, return the byte size
5461/// being masked out and the shift amount.
5462static std::pair<unsigned, unsigned>
5463CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
5464 std::pair<unsigned, unsigned> Result(0, 0);
5465
5466 // Check for the structure we're looking for.
5467 if (V->getOpcode() != ISD::AND ||
5468 !isa<ConstantSDNode>(V->getOperand(1)) ||
5469 !ISD::isNormalLoad(V->getOperand(0).getNode()))
5470 return Result;
5471
Chris Lattnere6987582010-04-15 06:10:49 +00005472 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00005473 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00005474 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
5475
5476 // The store should be chained directly to the load or be an operand of a
5477 // tokenfactor.
5478 if (LD == Chain.getNode())
5479 ; // ok.
5480 else if (Chain->getOpcode() != ISD::TokenFactor)
5481 return Result; // Fail.
5482 else {
5483 bool isOk = false;
5484 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
5485 if (Chain->getOperand(i).getNode() == LD) {
5486 isOk = true;
5487 break;
5488 }
5489 if (!isOk) return Result;
5490 }
Chris Lattner2392ae72010-04-15 04:48:01 +00005491
5492 // This only handles simple types.
5493 if (V.getValueType() != MVT::i16 &&
5494 V.getValueType() != MVT::i32 &&
5495 V.getValueType() != MVT::i64)
5496 return Result;
5497
5498 // Check the constant mask. Invert it so that the bits being masked out are
5499 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
5500 // follow the sign bit for uniformity.
5501 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
5502 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
5503 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
5504 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
5505 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
5506 if (NotMaskLZ == 64) return Result; // All zero mask.
5507
5508 // See if we have a continuous run of bits. If so, we have 0*1+0*
5509 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
5510 return Result;
5511
5512 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
5513 if (V.getValueType() != MVT::i64 && NotMaskLZ)
5514 NotMaskLZ -= 64-V.getValueSizeInBits();
5515
5516 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
5517 switch (MaskedBytes) {
5518 case 1:
5519 case 2:
5520 case 4: break;
5521 default: return Result; // All one mask, or 5-byte mask.
5522 }
5523
5524 // Verify that the first bit starts at a multiple of mask so that the access
5525 // is aligned the same as the access width.
5526 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
5527
5528 Result.first = MaskedBytes;
5529 Result.second = NotMaskTZ/8;
5530 return Result;
5531}
5532
5533
5534/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
5535/// provides a value as specified by MaskInfo. If so, replace the specified
5536/// store with a narrower store of truncated IVal.
5537static SDNode *
5538ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
5539 SDValue IVal, StoreSDNode *St,
5540 DAGCombiner *DC) {
5541 unsigned NumBytes = MaskInfo.first;
5542 unsigned ByteShift = MaskInfo.second;
5543 SelectionDAG &DAG = DC->getDAG();
5544
5545 // Check to see if IVal is all zeros in the part being masked in by the 'or'
5546 // that uses this. If not, this is not a replacement.
5547 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
5548 ByteShift*8, (ByteShift+NumBytes)*8);
5549 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
5550
5551 // Check that it is legal on the target to do this. It is legal if the new
5552 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
5553 // legalization.
5554 MVT VT = MVT::getIntegerVT(NumBytes*8);
5555 if (!DC->isTypeLegal(VT))
5556 return 0;
5557
5558 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
5559 // shifted by ByteShift and truncated down to NumBytes.
5560 if (ByteShift)
5561 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
5562 DAG.getConstant(ByteShift*8, DC->getShiftAmountTy()));
5563
5564 // Figure out the offset for the store and the alignment of the access.
5565 unsigned StOffset;
5566 unsigned NewAlign = St->getAlignment();
5567
5568 if (DAG.getTargetLoweringInfo().isLittleEndian())
5569 StOffset = ByteShift;
5570 else
5571 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
5572
5573 SDValue Ptr = St->getBasePtr();
5574 if (StOffset) {
5575 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
5576 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
5577 NewAlign = MinAlign(NewAlign, StOffset);
5578 }
5579
5580 // Truncate down to the new size.
5581 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
5582
5583 ++OpsNarrowed;
5584 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
5585 St->getSrcValue(), St->getSrcValueOffset()+StOffset,
5586 false, false, NewAlign).getNode();
5587}
5588
Evan Cheng8b944d32009-05-28 00:35:15 +00005589
5590/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
5591/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
5592/// of the loaded bits, try narrowing the load and store if it would end up
5593/// being a win for performance or code size.
5594SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
5595 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00005596 if (ST->isVolatile())
5597 return SDValue();
5598
Evan Cheng8b944d32009-05-28 00:35:15 +00005599 SDValue Chain = ST->getChain();
5600 SDValue Value = ST->getValue();
5601 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00005602 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00005603
5604 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00005605 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005606
5607 unsigned Opc = Value.getOpcode();
Chris Lattner2392ae72010-04-15 04:48:01 +00005608
5609 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
5610 // is a byte mask indicating a consecutive number of bytes, check to see if
5611 // Y is known to provide just those bytes. If so, we try to replace the
5612 // load + replace + store sequence with a single (narrower) store, which makes
5613 // the load dead.
5614 if (Opc == ISD::OR) {
5615 std::pair<unsigned, unsigned> MaskedLoad;
5616 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
5617 if (MaskedLoad.first)
5618 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5619 Value.getOperand(1), ST,this))
5620 return SDValue(NewST, 0);
5621
5622 // Or is commutative, so try swapping X and Y.
5623 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
5624 if (MaskedLoad.first)
5625 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5626 Value.getOperand(0), ST,this))
5627 return SDValue(NewST, 0);
5628 }
5629
Evan Cheng8b944d32009-05-28 00:35:15 +00005630 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
5631 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00005632 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005633
5634 SDValue N0 = Value.getOperand(0);
5635 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
5636 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengcdcecc02009-05-28 18:41:02 +00005637 if (LD->getBasePtr() != Ptr)
5638 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005639
5640 // Find the type to narrow it the load / op / store to.
5641 SDValue N1 = Value.getOperand(1);
5642 unsigned BitWidth = N1.getValueSizeInBits();
5643 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
5644 if (Opc == ISD::AND)
5645 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00005646 if (Imm == 0 || Imm.isAllOnesValue())
5647 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005648 unsigned ShAmt = Imm.countTrailingZeros();
5649 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5650 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00005651 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005652 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00005653 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00005654 TLI.isNarrowingProfitable(VT, NewVT))) {
5655 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00005656 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005657 }
Evan Chengcdcecc02009-05-28 18:41:02 +00005658 if (NewBW >= BitWidth)
5659 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005660
5661 // If the lsb changed does not start at the type bitwidth boundary,
5662 // start at the previous one.
5663 if (ShAmt % NewBW)
5664 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5665 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5666 if ((Imm & Mask) == Imm) {
5667 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5668 if (Opc == ISD::AND)
5669 NewImm ^= APInt::getAllOnesValue(NewBW);
5670 uint64_t PtrOff = ShAmt / 8;
5671 // For big endian targets, we need to adjust the offset to the pointer to
5672 // load the correct bytes.
5673 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00005674 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00005675
5676 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner2392ae72010-04-15 04:48:01 +00005677 const Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
5678 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00005679 return SDValue();
5680
Evan Cheng8b944d32009-05-28 00:35:15 +00005681 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5682 Ptr.getValueType(), Ptr,
5683 DAG.getConstant(PtrOff, Ptr.getValueType()));
5684 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5685 LD->getChain(), NewPtr,
5686 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005687 LD->isVolatile(), LD->isNonTemporal(),
5688 NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005689 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5690 DAG.getConstant(NewImm, NewVT));
5691 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5692 NewVal, NewPtr,
5693 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005694 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005695
5696 AddToWorkList(NewPtr.getNode());
5697 AddToWorkList(NewLD.getNode());
5698 AddToWorkList(NewVal.getNode());
5699 WorkListRemover DeadNodes(*this);
5700 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5701 &DeadNodes);
5702 ++OpsNarrowed;
5703 return NewST;
5704 }
5705 }
5706
Evan Chengcdcecc02009-05-28 18:41:02 +00005707 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005708}
5709
Dan Gohman475871a2008-07-27 21:46:04 +00005710SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005711 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005712 SDValue Chain = ST->getChain();
5713 SDValue Value = ST->getValue();
5714 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005715
Evan Cheng59d5b682007-05-07 21:27:48 +00005716 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00005717 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00005718 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005719 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00005720 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00005721 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00005722 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005723 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005724 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005725 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005726 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00005727 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5728 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005729 ST->getSrcValueOffset(), ST->isVolatile(),
5730 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00005731 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005732
Nate Begeman2cbba892006-12-11 02:23:46 +00005733 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00005734 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005735 // NOTE: If the original store is volatile, this transform must not increase
5736 // the number of stores. For example, on x86-32 an f64 can be stored in one
5737 // processor operation but an i64 (which is not legal) requires two. So the
5738 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00005739 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005742 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 case MVT::f80: // We don't do this for these yet.
5744 case MVT::f128:
5745 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00005746 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005747 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00005748 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00005750 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00005752 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5753 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005754 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005755 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00005756 }
5757 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00005759 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005760 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00005762 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00005763 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00005764 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5765 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005766 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005767 ST->isNonTemporal(), ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005768 } else if (!ST->isVolatile() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00005770 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00005771 // argument passing. Since this is so common, custom legalize the
5772 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00005773 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5775 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00005776 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00005777
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005778 int SVOffset = ST->getSrcValueOffset();
5779 unsigned Alignment = ST->getAlignment();
5780 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00005781 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005782
Bill Wendlingc144a572009-01-30 23:36:47 +00005783 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5784 Ptr, ST->getSrcValue(),
5785 ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005786 isVolatile, isNonTemporal,
5787 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00005788 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00005789 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005790 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00005791 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00005792 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5793 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005794 SVOffset, isVolatile, isNonTemporal,
5795 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00005796 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00005797 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00005798 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005799
Chris Lattner62be1a72006-12-12 04:16:14 +00005800 break;
Evan Cheng25ece662006-12-11 17:25:19 +00005801 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005802 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005803 }
5804
Evan Cheng255f20f2010-04-01 06:04:33 +00005805 // Try to infer better alignment information than the store already has.
5806 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
5807 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5808 if (Align > ST->getAlignment())
5809 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5810 Ptr, ST->getSrcValue(),
5811 ST->getSrcValueOffset(), ST->getMemoryVT(),
5812 ST->isVolatile(), ST->isNonTemporal(), Align);
5813 }
5814 }
5815
Scott Michelfdc40a02009-02-17 22:15:04 +00005816 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005817 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005819
Jim Laskey6ff23e52006-10-04 16:53:27 +00005820 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005821 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005822 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005823
5824 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005825 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005826 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005827 ST->getSrcValue(),ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005828 ST->getMemoryVT(), ST->isVolatile(),
5829 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005830 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00005831 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005832 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005833 ST->isVolatile(), ST->isNonTemporal(),
5834 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005835 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005836
Jim Laskey279f0532006-09-25 16:29:54 +00005837 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00005838 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005839 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00005840
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005841 // Make sure the new and old chains are cleaned up.
5842 AddToWorkList(Token.getNode());
5843
Jim Laskey274062c2006-10-13 23:32:28 +00005844 // Don't add users to work list.
5845 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005846 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00005847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005848
Evan Cheng33dbedc2006-11-05 09:31:14 +00005849 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005850 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005851 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00005852
Chris Lattner3c872852007-12-29 06:26:16 +00005853 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00005854 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005855 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005856 // See if we can simplify the input to this truncstore with knowledge that
5857 // only the low bits are being used. For example:
5858 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00005859 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005860 GetDemandedBits(Value,
Bill Wendlingc144a572009-01-30 23:36:47 +00005861 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5862 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005863 AddToWorkList(Value.getNode());
5864 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00005865 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5866 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005867 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005868 ST->isVolatile(), ST->isNonTemporal(),
5869 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00005870
Chris Lattnere33544c2007-10-13 06:58:48 +00005871 // Otherwise, see if we can simplify the operation with
5872 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00005873 if (SimplifyDemandedBits(Value,
5874 APInt::getLowBitsSet(
Dan Gohman2e141d72009-12-14 23:40:38 +00005875 Value.getValueType().getScalarType().getSizeInBits(),
Dan Gohman8f78e3c2010-03-10 21:04:53 +00005876 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00005877 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00005878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005879
Chris Lattner3c872852007-12-29 06:26:16 +00005880 // If this is a load followed by a store to the same location, then the store
5881 // is dead/noop.
5882 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005883 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005884 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00005885 // There can't be any side effects between the load and store, such as
5886 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00005887 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00005888 // The store is dead, remove it.
5889 return Chain;
5890 }
5891 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005892
Chris Lattnerddf89562008-01-17 19:59:44 +00005893 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5894 // truncating store. We can do this even if this is already a truncstore.
5895 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00005896 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005897 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005898 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005899 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5900 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005901 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005902 ST->isVolatile(), ST->isNonTemporal(),
5903 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00005904 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005905
Evan Cheng8b944d32009-05-28 00:35:15 +00005906 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00005907}
5908
Dan Gohman475871a2008-07-27 21:46:04 +00005909SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5910 SDValue InVec = N->getOperand(0);
5911 SDValue InVal = N->getOperand(1);
5912 SDValue EltNo = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005913
Chris Lattnerca242442006-03-19 01:27:56 +00005914 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5915 // vector with the inserted element.
5916 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005917 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005918 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5919 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005920 if (Elt < Ops.size())
5921 Ops[Elt] = InVal;
Evan Chenga87008d2009-02-25 22:49:59 +00005922 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5923 InVec.getValueType(), &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005924 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005925 // If the invec is an UNDEF and if EltNo is a constant, create a new
5926 // BUILD_VECTOR with undef elements and the inserted element.
5927 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5928 isa<ConstantSDNode>(EltNo)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005929 EVT VT = InVec.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005930 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005931 unsigned NElts = VT.getVectorNumElements();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005932 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005933
Nate Begeman9008ca62009-04-27 18:41:29 +00005934 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5935 if (Elt < Ops.size())
5936 Ops[Elt] = InVal;
5937 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5938 InVec.getValueType(), &Ops[0], Ops.size());
5939 }
Dan Gohman475871a2008-07-27 21:46:04 +00005940 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005941}
5942
Dan Gohman475871a2008-07-27 21:46:04 +00005943SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005944 // (vextract (scalar_to_vector val, 0) -> val
5945 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005946
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005947 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Mon P Wangc6654ec42010-02-01 19:03:18 +00005948 // Check if the result type doesn't match the inserted element type. A
5949 // SCALAR_TO_VECTOR may truncate the inserted element and the
5950 // EXTRACT_VECTOR_ELT may widen the extracted vector.
Owen Andersone50ed302009-08-10 22:56:29 +00005951 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005952 SDValue InOp = InVec.getOperand(0);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005953 EVT NVT = N->getValueType(0);
5954 if (InOp.getValueType() != NVT) {
5955 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Mon P Wang87c46d82010-02-01 22:15:09 +00005956 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005957 }
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005958 return InOp;
5959 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005960
5961 // Perform only after legalization to ensure build_vector / vector_shuffle
5962 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005963 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005964
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005965 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5966 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5967 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005968 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005969
Evan Cheng513da432007-10-06 08:19:55 +00005970 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005971 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005972 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005973 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00005974 EVT VT = InVec.getValueType();
5975 EVT ExtVT = VT.getVectorElementType();
5976 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00005977
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005978 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005979 EVT BCVT = InVec.getOperand(0).getValueType();
5980 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005981 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005982 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5983 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005984 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005985 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005986 NewLoad = true;
5987 }
Evan Cheng513da432007-10-06 08:19:55 +00005988
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005989 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00005990 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00005991 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005992 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00005993 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00005994 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00005995 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005996 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00005997 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005998 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5999 // =>
6000 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00006001
Mon P Wanga60b5232008-12-11 00:26:16 +00006002 // If the bit convert changed the number of elements, it is unsafe
6003 // to examine the mask.
6004 if (BCNumEltsChanged)
6005 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00006006
6007 // Select the input vector, guarding against out of range extract vector.
6008 unsigned NumElems = VT.getVectorNumElements();
6009 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
6010 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
6011
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006012 if (InVec.getOpcode() == ISD::BIT_CONVERT)
6013 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00006014 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006015 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00006016 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00006017 }
6018 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006019
Duncan Sandsec87aa82008-06-15 20:12:31 +00006020 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00006021 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006022
6023 unsigned Align = LN0->getAlignment();
6024 if (NewLoad) {
6025 // Check the resultant load doesn't need a higher alignment than the
6026 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00006027 unsigned NewAlign =
Owen Anderson23b9b192009-08-12 00:36:31 +00006028 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00006029
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006030 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00006031 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00006032
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006033 Align = NewAlign;
6034 }
6035
Dan Gohman475871a2008-07-27 21:46:04 +00006036 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006037 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006038 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006039 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006040 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006041 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00006042 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006043 DAG.getConstant(PtrOff, PtrType));
6044 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006045
6046 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00006047 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00006048 LN0->isVolatile(), LN0->isNonTemporal(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00006049 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006050
Dan Gohman475871a2008-07-27 21:46:04 +00006051 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00006052}
Evan Cheng513da432007-10-06 08:19:55 +00006053
Dan Gohman475871a2008-07-27 21:46:04 +00006054SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006055 unsigned NumInScalars = N->getNumOperands();
Owen Andersone50ed302009-08-10 22:56:29 +00006056 EVT VT = N->getValueType(0);
Chris Lattnerca242442006-03-19 01:27:56 +00006057
Dan Gohman7f321562007-06-25 16:23:39 +00006058 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
6059 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
6060 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00006061 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00006062 for (unsigned i = 0; i != NumInScalars; ++i) {
6063 // Ignore undef inputs.
6064 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006065
Dan Gohman7f321562007-06-25 16:23:39 +00006066 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00006067 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00006068 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00006069 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00006070 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006071 break;
6072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006073
Dan Gohman7f321562007-06-25 16:23:39 +00006074 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00006075 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00006076 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006077 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006078 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006079 break;
6080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006081
Chris Lattnerd7648c82006-03-28 20:28:38 +00006082 // Otherwise, remember this. We allow up to two distinct input vectors.
6083 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
6084 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006085
Gabor Greifba36cb52008-08-28 21:40:38 +00006086 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006087 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00006088 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006089 VecIn2 = ExtractedFromVec;
6090 } else {
6091 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006092 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006093 break;
6094 }
6095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006096
Chris Lattnerd7648c82006-03-28 20:28:38 +00006097 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00006098 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00006100 for (unsigned i = 0; i != NumInScalars; ++i) {
6101 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006102 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006103 continue;
6104 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006105
Rafael Espindola15684b22009-04-24 12:40:33 +00006106 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00006108 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006109 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006110 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
6111 if (ExtIndex > VT.getVectorNumElements())
6112 return SDValue();
6113
6114 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006115 continue;
6116 }
6117
6118 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00006119 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006122
Chris Lattnerd7648c82006-03-28 20:28:38 +00006123 // Add count and size info.
Chris Lattner2392ae72010-04-15 04:48:01 +00006124 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00006125 return SDValue();
6126
Dan Gohman7f321562007-06-25 16:23:39 +00006127 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00006128 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006129 Ops[0] = VecIn1;
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
6131 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006133
Dan Gohman475871a2008-07-27 21:46:04 +00006134 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00006135}
6136
Dan Gohman475871a2008-07-27 21:46:04 +00006137SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006138 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
6139 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
6140 // inputs come from at most two distinct vectors, turn this into a shuffle
6141 // node.
6142
6143 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00006144 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00006145 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006146
Dan Gohman475871a2008-07-27 21:46:04 +00006147 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006148}
6149
Dan Gohman475871a2008-07-27 21:46:04 +00006150SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006151 return SDValue();
6152
Owen Andersone50ed302009-08-10 22:56:29 +00006153 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006155
Mon P Wangaeb06d22008-11-10 04:46:22 +00006156 SDValue N0 = N->getOperand(0);
Mon P Wangaeb06d22008-11-10 04:46:22 +00006157
6158 assert(N0.getValueType().getVectorNumElements() == NumElts &&
6159 "Vector shuffle must be normalized in DAG");
6160
Nate Begeman9008ca62009-04-27 18:41:29 +00006161 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Evan Chenge7bec0d2006-07-20 22:44:41 +00006162
Evan Cheng917ec982006-07-21 08:25:53 +00006163 // If it is a splat, check if the argument vector is a build_vector with
6164 // all scalar elements the same.
Nate Begeman9008ca62009-04-27 18:41:29 +00006165 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006166 SDNode *V = N0.getNode();
Nate Begeman9008ca62009-04-27 18:41:29 +00006167
Evan Cheng917ec982006-07-21 08:25:53 +00006168
Dan Gohman7f321562007-06-25 16:23:39 +00006169 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00006170 // not the number of vector elements, look through it. Be careful not to
6171 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00006172 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006173 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00006174 if (ConvInput.getValueType().isVector() &&
6175 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00006176 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00006177 }
6178
Dan Gohman7f321562007-06-25 16:23:39 +00006179 if (V->getOpcode() == ISD::BUILD_VECTOR) {
6180 unsigned NumElems = V->getNumOperands();
Nate Begeman9008ca62009-04-27 18:41:29 +00006181 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Evan Cheng917ec982006-07-21 08:25:53 +00006182 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00006183 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00006184 bool AllSame = true;
6185 for (unsigned i = 0; i != NumElems; ++i) {
6186 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
6187 Base = V->getOperand(i);
6188 break;
6189 }
6190 }
6191 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00006192 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00006193 return N0;
6194 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00006195 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00006196 AllSame = false;
6197 break;
6198 }
6199 }
6200 // Splat of <x, x, x, x>, return <x, x, x, x>
6201 if (AllSame)
6202 return N0;
6203 }
6204 }
6205 }
Dan Gohman475871a2008-07-27 21:46:04 +00006206 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006207}
6208
Evan Cheng44f1f092006-04-20 08:56:16 +00006209/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00006210/// an AND to a vector_shuffle with the destination vector and a zero vector.
6211/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00006212/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00006213SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00006214 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006215 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006216 SDValue LHS = N->getOperand(0);
6217 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00006218 if (N->getOpcode() == ISD::AND) {
6219 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00006220 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006221 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006222 SmallVector<int, 8> Indices;
6223 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00006224 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006225 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006226 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00006227 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006228 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006230 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00006232 else
Dan Gohman475871a2008-07-27 21:46:04 +00006233 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006234 }
6235
6236 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00006237 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006238 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006240
Dan Gohman7f321562007-06-25 16:23:39 +00006241 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006242 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006243 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00006244 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00006245 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
6246 RVT, &ZeroOps[0], ZeroOps.size());
6247 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
6248 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
6249 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00006250 }
6251 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006252
Dan Gohman475871a2008-07-27 21:46:04 +00006253 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006254}
6255
Dan Gohman7f321562007-06-25 16:23:39 +00006256/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00006257SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006258 // After legalize, the target may be depending on adds and other
6259 // binary ops to provide legal ways to construct constants or other
6260 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00006261 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006262
Owen Andersone50ed302009-08-10 22:56:29 +00006263 EVT VT = N->getValueType(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006264 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00006265
Owen Andersone50ed302009-08-10 22:56:29 +00006266 EVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006267 SDValue LHS = N->getOperand(0);
6268 SDValue RHS = N->getOperand(1);
6269 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006270 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00006271
Dan Gohman7f321562007-06-25 16:23:39 +00006272 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00006273 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00006274 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00006275 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00006276 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00006277 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006278 SDValue LHSOp = LHS.getOperand(i);
6279 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00006280 // If these two elements can't be folded, bail out.
6281 if ((LHSOp.getOpcode() != ISD::UNDEF &&
6282 LHSOp.getOpcode() != ISD::Constant &&
6283 LHSOp.getOpcode() != ISD::ConstantFP) ||
6284 (RHSOp.getOpcode() != ISD::UNDEF &&
6285 RHSOp.getOpcode() != ISD::Constant &&
6286 RHSOp.getOpcode() != ISD::ConstantFP))
6287 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006288
Evan Cheng7b336a82006-05-31 06:08:35 +00006289 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00006290 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
6291 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00006292 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006293 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00006294 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006295 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00006296 break;
6297 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006298
Bill Wendling9729c5a2009-01-31 03:12:48 +00006299 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006300 EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00006301 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00006302 assert((Ops.back().getOpcode() == ISD::UNDEF ||
6303 Ops.back().getOpcode() == ISD::Constant ||
6304 Ops.back().getOpcode() == ISD::ConstantFP) &&
6305 "Scalar binop didn't fold!");
6306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Dan Gohman7f321562007-06-25 16:23:39 +00006308 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006309 EVT VT = LHS.getValueType();
Evan Chenga87008d2009-02-25 22:49:59 +00006310 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
6311 &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00006312 }
Chris Lattneredab1b92006-04-02 03:25:57 +00006313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006314
Dan Gohman475871a2008-07-27 21:46:04 +00006315 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00006316}
6317
Bill Wendling836ca7d2009-01-30 23:59:18 +00006318SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
6319 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00006320 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006321
Bill Wendling836ca7d2009-01-30 23:59:18 +00006322 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00006323 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006324
Nate Begemanf845b452005-10-08 00:29:44 +00006325 // If we got a simplified select_cc node back from SimplifySelectCC, then
6326 // break it down into a new SETCC node, and a new SELECT node, and then return
6327 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00006328 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006329 // Check to see if we got a select_cc back (to turn into setcc/select).
6330 // Otherwise, just return whatever node we got back, like fabs.
6331 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006332 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
6333 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006334 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006335 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00006336 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006337 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
6338 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00006339 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006340
Nate Begemanf845b452005-10-08 00:29:44 +00006341 return SCC;
6342 }
Dan Gohman475871a2008-07-27 21:46:04 +00006343 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006344}
6345
Chris Lattner40c62d52005-10-18 06:04:22 +00006346/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
6347/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00006348/// select. Callers of this should assume that TheSelect is deleted if this
6349/// returns true. As such, they should return the appropriate thing (e.g. the
6350/// node) back to the top-level of the DAG combiner loop to avoid it being
6351/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00006352bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00006353 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006354
Chris Lattner40c62d52005-10-18 06:04:22 +00006355 // If this is a select from two identical things, try to pull the operation
6356 // through the select.
6357 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00006358 // If this is a load and the token chain is identical, replace the select
6359 // of two loads with a load through a select of the address to load from.
6360 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
6361 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00006362 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006363 // Do not let this transformation reduce the number of volatile loads.
6364 !cast<LoadSDNode>(LHS)->isVolatile() &&
6365 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00006366 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00006367 LHS.getOperand(0) == RHS.getOperand(0)) {
6368 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
6369 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
6370
6371 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006372 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohman75832d72009-10-31 14:14:04 +00006373 // FIXME: this discards src value information. This is
6374 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00006375 // both potential memory locations. Since we are discarding
6376 // src value info, don't do the transformation if the memory
6377 // locations are not in the default address space.
6378 unsigned LLDAddrSpace = 0, RLDAddrSpace = 0;
6379 if (const Value *LLDVal = LLD->getMemOperand()->getValue()) {
6380 if (const PointerType *PT = dyn_cast<PointerType>(LLDVal->getType()))
6381 LLDAddrSpace = PT->getAddressSpace();
6382 }
6383 if (const Value *RLDVal = RLD->getMemOperand()->getValue()) {
6384 if (const PointerType *PT = dyn_cast<PointerType>(RLDVal->getType()))
6385 RLDAddrSpace = PT->getAddressSpace();
6386 }
Dan Gohman475871a2008-07-27 21:46:04 +00006387 SDValue Addr;
Mon P Wangfe240b12010-01-11 20:12:49 +00006388 if (LLDAddrSpace == 0 && RLDAddrSpace == 0) {
6389 if (TheSelect->getOpcode() == ISD::SELECT) {
6390 // Check that the condition doesn't reach either load. If so, folding
6391 // this will induce a cycle into the DAG.
6392 if ((!LLD->hasAnyUseOfValue(1) ||
6393 !LLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) &&
6394 (!RLD->hasAnyUseOfValue(1) ||
6395 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()))) {
6396 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
6397 LLD->getBasePtr().getValueType(),
6398 TheSelect->getOperand(0), LLD->getBasePtr(),
6399 RLD->getBasePtr());
6400 }
6401 } else {
6402 // Check that the condition doesn't reach either load. If so, folding
6403 // this will induce a cycle into the DAG.
6404 if ((!LLD->hasAnyUseOfValue(1) ||
6405 (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6406 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()))) &&
6407 (!RLD->hasAnyUseOfValue(1) ||
6408 (!RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6409 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())))) {
6410 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
6411 LLD->getBasePtr().getValueType(),
6412 TheSelect->getOperand(0),
6413 TheSelect->getOperand(1),
6414 LLD->getBasePtr(), RLD->getBasePtr(),
6415 TheSelect->getOperand(4));
6416 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006417 }
Evan Cheng466685d2006-10-09 20:57:25 +00006418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006419
Gabor Greifba36cb52008-08-28 21:40:38 +00006420 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006421 SDValue Load;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006422 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
6423 Load = DAG.getLoad(TheSelect->getValueType(0),
6424 TheSelect->getDebugLoc(),
6425 LLD->getChain(),
Dan Gohman75832d72009-10-31 14:14:04 +00006426 Addr, 0, 0,
Scott Michelfdc40a02009-02-17 22:15:04 +00006427 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006428 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006429 LLD->getAlignment());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006430 } else {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006431 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006432 TheSelect->getDebugLoc(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006433 TheSelect->getValueType(0),
Dan Gohman75832d72009-10-31 14:14:04 +00006434 LLD->getChain(), Addr, 0, 0,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006435 LLD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006436 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006437 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006438 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006439 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006440
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006441 // Users of the select now use the result of the load.
6442 CombineTo(TheSelect, Load);
Scott Michelfdc40a02009-02-17 22:15:04 +00006443
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006444 // Users of the old loads now use the new load's chain. We know the
6445 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00006446 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
6447 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006448 return true;
6449 }
Evan Chengc5484282006-10-04 00:56:09 +00006450 }
Chris Lattner40c62d52005-10-18 06:04:22 +00006451 }
6452 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006453
Chris Lattner40c62d52005-10-18 06:04:22 +00006454 return false;
6455}
6456
Chris Lattner600fec32009-03-11 05:08:08 +00006457/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
6458/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00006459SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00006460 SDValue N2, SDValue N3,
6461 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00006462 // (x ? y : y) -> y.
6463 if (N2 == N3) return N2;
6464
Owen Andersone50ed302009-08-10 22:56:29 +00006465 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00006466 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
6467 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
6468 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006469
6470 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00006471 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006472 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006473 if (SCC.getNode()) AddToWorkList(SCC.getNode());
6474 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006475
6476 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00006477 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006478 return N2;
6479 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00006480 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006481 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00006482
Nate Begemanf845b452005-10-08 00:29:44 +00006483 // Check to see if we can simplify the select into an fabs node
6484 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
6485 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00006486 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006487 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
6488 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
6489 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
6490 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006491 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006492
Nate Begemanf845b452005-10-08 00:29:44 +00006493 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
6494 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
6495 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
6496 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006497 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00006498 }
6499 }
Chris Lattner600fec32009-03-11 05:08:08 +00006500
6501 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
6502 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
6503 // in it. This is a win when the constant is not otherwise available because
6504 // it replaces two constant pool loads with one. We only do this if the FP
6505 // type is known to be legal, because if it isn't, then we are before legalize
6506 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00006507 // messing with soft float) and if the ConstantFP is not legal, because if
6508 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00006509 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
6510 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
6511 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00006512 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
6513 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00006514 // If both constants have multiple uses, then we won't need to do an
6515 // extra load, they are likely around in registers for other users.
6516 (TV->hasOneUse() || FV->hasOneUse())) {
6517 Constant *Elts[] = {
6518 const_cast<ConstantFP*>(FV->getConstantFPValue()),
6519 const_cast<ConstantFP*>(TV->getConstantFPValue())
6520 };
6521 const Type *FPTy = Elts[0]->getType();
6522 const TargetData &TD = *TLI.getTargetData();
6523
6524 // Create a ConstantArray of the two constants.
Owen Andersondebcb012009-07-29 22:17:13 +00006525 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner600fec32009-03-11 05:08:08 +00006526 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
6527 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00006528 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00006529
6530 // Get the offsets to the 0 and 1 element of the array so that we can
6531 // select between them.
6532 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00006533 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00006534 SDValue One = DAG.getIntPtrConstant(EltSize);
6535
6536 SDValue Cond = DAG.getSetCC(DL,
6537 TLI.getSetCCResultType(N0.getValueType()),
6538 N0, N1, CC);
6539 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
6540 Cond, One, Zero);
6541 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
6542 CstOffset);
6543 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
6544 PseudoSourceValue::getConstantPool(), 0, false,
David Greene1e559442010-02-15 17:00:31 +00006545 false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00006546
6547 }
6548 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006549
Nate Begemanf845b452005-10-08 00:29:44 +00006550 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00006551 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00006552 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006553 N0.getValueType().isInteger() &&
6554 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006555 (N1C->isNullValue() || // (a < 0) ? b : 0
6556 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00006557 EVT XType = N0.getValueType();
6558 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00006559 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00006560 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00006561 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00006562 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
6563 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006564 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands92abc622009-01-31 15:50:11 +00006565 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendling9729c5a2009-01-31 03:12:48 +00006566 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006567 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00006568 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006569
Duncan Sands8e4eb092008-06-08 20:54:56 +00006570 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006571 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006572 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006573 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006574
6575 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006576 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006577
Bill Wendling9729c5a2009-01-31 03:12:48 +00006578 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006579 XType, N0,
6580 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006581 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00006582 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006583
Duncan Sands8e4eb092008-06-08 20:54:56 +00006584 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006585 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006586 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006587 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006588
6589 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006590 }
6591 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006592
Nate Begeman07ed4172005-10-10 21:26:48 +00006593 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00006594 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00006595 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006596
Chris Lattner1eba01e2007-04-11 06:50:51 +00006597 // If the caller doesn't want us to simplify this into a zext of a compare,
6598 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00006599 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006600 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006601
Nate Begeman07ed4172005-10-10 21:26:48 +00006602 // Get a SetCC of the condition
6603 // FIXME: Should probably make sure that setcc is legal if we ever have a
6604 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00006606 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00006607 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006608 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006609 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00006610 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00006611 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00006612 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00006613 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006614 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006615 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00006617 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006618 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006619 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006620
Gabor Greifba36cb52008-08-28 21:40:38 +00006621 AddToWorkList(SCC.getNode());
6622 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006623
Dan Gohman002e5d02008-03-13 22:13:53 +00006624 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00006625 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006626
Nate Begeman07ed4172005-10-10 21:26:48 +00006627 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00006628 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00006629 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00006630 getShiftAmountTy()));
Nate Begeman07ed4172005-10-10 21:26:48 +00006631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006632
Nate Begemanf845b452005-10-08 00:29:44 +00006633 // Check to see if this is the equivalent of setcc
6634 // FIXME: Turn all of these into setcc if setcc if setcc is legal
6635 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00006636 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006637 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00006638 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00006639 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006640 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00006641 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006642 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00006643 return Res;
6644 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006645
Bill Wendling836ca7d2009-01-30 23:59:18 +00006646 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00006647 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006648 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00006649 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006650 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006651 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006652 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands92abc622009-01-31 15:50:11 +00006653 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006654 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006655 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00006656 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006657 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
6658 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00006659 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00006660 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006661 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00006662 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006663 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006664 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006665 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00006666 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006667 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006668 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006669 getShiftAmountTy()));
Bill Wendling836ca7d2009-01-30 23:59:18 +00006670 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00006671 }
6672 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006673
Nate Begemanf845b452005-10-08 00:29:44 +00006674 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
6675 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6676 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00006677 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006678 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006679 EVT XType = N0.getValueType();
Bill Wendling9729c5a2009-01-31 03:12:48 +00006680 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006681 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006682 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006683 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006684 N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006685 AddToWorkList(Shift.getNode());
6686 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006687 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00006688 }
6689 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6690 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6691 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6692 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6693 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersone50ed302009-08-10 22:56:29 +00006694 EVT XType = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006695 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006696 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006697 N0,
6698 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006699 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006700 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006701 XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006702 AddToWorkList(Shift.getNode());
6703 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006704 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00006705 }
6706 }
6707 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006708
Dan Gohman475871a2008-07-27 21:46:04 +00006709 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006710}
6711
Evan Chengfa1eb272007-02-08 22:13:59 +00006712/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00006713SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006715 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006716 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00006717 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006718 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +00006719}
6720
Nate Begeman69575232005-10-20 02:15:44 +00006721/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6722/// return a DAG expression to select that will generate the same value by
6723/// multiplying by a magic number. See:
6724/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006725SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006726 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006728
Andrew Lenharth232c9102006-06-12 16:07:18 +00006729 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006730 ii != ee; ++ii)
6731 AddToWorkList(*ii);
6732 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006733}
6734
6735/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6736/// return a DAG expression to select that will generate the same value by
6737/// multiplying by a magic number. See:
6738/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006739SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006740 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00006742
Andrew Lenharth232c9102006-06-12 16:07:18 +00006743 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006744 ii != ee; ++ii)
6745 AddToWorkList(*ii);
6746 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006747}
6748
Nate Begemancc66cdd2009-09-25 06:05:26 +00006749/// FindBaseOffset - Return true if base is a frame index, which is known not
6750// to alias with anything but itself. Provides base object and offset as results.
6751static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Dan Gohman46510a72010-04-15 01:51:59 +00006752 const GlobalValue *&GV, void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00006753 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006754 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006755
Jim Laskey71382342006-10-07 23:37:56 +00006756 // If it's an adding a simple constant then integrate the offset.
6757 if (Base.getOpcode() == ISD::ADD) {
6758 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6759 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006760 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00006761 }
6762 }
Nate Begemancc66cdd2009-09-25 06:05:26 +00006763
6764 // Return the underlying GlobalValue, and update the Offset. Return false
6765 // for GlobalAddressSDNode since the same GlobalAddress may be represented
6766 // by multiple nodes with different offsets.
6767 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
6768 GV = G->getGlobal();
6769 Offset += G->getOffset();
6770 return false;
6771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006772
Nate Begemancc66cdd2009-09-25 06:05:26 +00006773 // Return the underlying Constant value, and update the Offset. Return false
6774 // for ConstantSDNodes since the same constant pool entry may be represented
6775 // by multiple nodes with different offsets.
6776 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
6777 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
6778 : (void *)C->getConstVal();
6779 Offset += C->getOffset();
6780 return false;
6781 }
Jim Laskey71382342006-10-07 23:37:56 +00006782 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006783 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00006784}
6785
6786/// isAlias - Return true if there is any possibility that the two addresses
6787/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00006788bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00006789 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006790 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +00006791 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006792 const Value *SrcValue2, int SrcValueOffset2,
6793 unsigned SrcValueAlign2) const {
Jim Laskey71382342006-10-07 23:37:56 +00006794 // If they are the same then they must be aliases.
6795 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006796
Jim Laskey71382342006-10-07 23:37:56 +00006797 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00006798 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00006799 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00006800 const GlobalValue *GV1, *GV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00006801 void *CV1, *CV2;
6802 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
6803 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006804
Nate Begemancc66cdd2009-09-25 06:05:26 +00006805 // If they have a same base address then check to see if they overlap.
6806 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006807 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006808
Nate Begemancc66cdd2009-09-25 06:05:26 +00006809 // If we know what the bases are, and they aren't identical, then we know they
6810 // cannot alias.
6811 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
6812 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00006813
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006814 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
6815 // compared to the size and offset of the access, we may be able to prove they
6816 // do not alias. This check is conservative for now to catch cases created by
6817 // splitting vector types.
6818 if ((SrcValueAlign1 == SrcValueAlign2) &&
6819 (SrcValueOffset1 != SrcValueOffset2) &&
6820 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
6821 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
6822 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
6823
6824 // There is no overlap between these relatively aligned accesses of similar
6825 // size, return no alias.
6826 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
6827 return false;
6828 }
6829
Jim Laskey07a27092006-10-18 19:08:31 +00006830 if (CombinerGlobalAA) {
6831 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00006832 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6833 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6834 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00006835 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00006836 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00006837 if (AAResult == AliasAnalysis::NoAlias)
6838 return false;
6839 }
Jim Laskey096c22e2006-10-18 12:29:57 +00006840
6841 // Otherwise we have to assume they alias.
6842 return true;
Jim Laskey71382342006-10-07 23:37:56 +00006843}
6844
6845/// FindAliasInfo - Extracts the relevant alias information from the memory
6846/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00006847bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00006848 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006849 const Value *&SrcValue,
6850 int &SrcValueOffset,
6851 unsigned &SrcValueAlign) const {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006852 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6853 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006854 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006855 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006856 SrcValueOffset = LD->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006857 SrcValueAlign = LD->getOriginalAlignment();
Jim Laskey71382342006-10-07 23:37:56 +00006858 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006859 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006860 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006861 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006862 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006863 SrcValueOffset = ST->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006864 SrcValueAlign = ST->getOriginalAlignment();
Jim Laskey7ca56af2006-10-11 13:47:09 +00006865 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006866 llvm_unreachable("FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00006867 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006868
Jim Laskey71382342006-10-07 23:37:56 +00006869 return false;
6870}
6871
Jim Laskey6ff23e52006-10-04 16:53:27 +00006872/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6873/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006874void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6875 SmallVector<SDValue, 8> &Aliases) {
6876 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006877 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00006878
Jim Laskey279f0532006-09-25 16:29:54 +00006879 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00006880 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006881 int64_t Size;
6882 const Value *SrcValue;
6883 int SrcValueOffset;
6884 unsigned SrcValueAlign;
6885 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
6886 SrcValueAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00006887
Jim Laskey6ff23e52006-10-04 16:53:27 +00006888 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00006889 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00006890 unsigned Depth = 0;
6891
Jim Laskeybc588b82006-10-05 15:07:25 +00006892 // Look at each chain and determine if it is an alias. If so, add it to the
6893 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00006894 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00006895 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006896 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00006897 Chains.pop_back();
Nate Begeman677c89d2009-10-12 05:53:58 +00006898
6899 // For TokenFactor nodes, look at each operand and only continue up the
6900 // chain until we find two aliases. If we've seen two aliases, assume we'll
6901 // find more and revert to original chain since the xform is unlikely to be
6902 // profitable.
6903 //
6904 // FIXME: The depth check could be made to return the last non-aliasing
6905 // chain we found before we hit a tokenfactor rather than the original
6906 // chain.
6907 if (Depth > 6 || Aliases.size() == 2) {
6908 Aliases.clear();
6909 Aliases.push_back(OriginalChain);
6910 break;
6911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006912
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006913 // Don't bother if we've been before.
6914 if (!Visited.insert(Chain.getNode()))
6915 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006916
Jim Laskeybc588b82006-10-05 15:07:25 +00006917 switch (Chain.getOpcode()) {
6918 case ISD::EntryToken:
6919 // Entry token is ideal chain operand, but handled in FindBetterChain.
6920 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006921
Jim Laskeybc588b82006-10-05 15:07:25 +00006922 case ISD::LOAD:
6923 case ISD::STORE: {
6924 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006925 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006926 int64_t OpSize;
6927 const Value *OpSrcValue;
6928 int OpSrcValueOffset;
6929 unsigned OpSrcValueAlign;
Gabor Greifba36cb52008-08-28 21:40:38 +00006930 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006931 OpSrcValue, OpSrcValueOffset,
6932 OpSrcValueAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00006933
Jim Laskeybc588b82006-10-05 15:07:25 +00006934 // If chain is alias then stop here.
6935 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006936 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
6937 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
6938 OpSrcValueAlign)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00006939 Aliases.push_back(Chain);
6940 } else {
6941 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00006942 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00006943 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00006944 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006945 break;
6946 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006947
Jim Laskeybc588b82006-10-05 15:07:25 +00006948 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006949 // We have to check each of the operands of the token factor for "small"
6950 // token factors, so we queue them up. Adding the operands to the queue
6951 // (stack) in reverse order maintains the original order and increases the
6952 // likelihood that getNode will find a matching token factor (CSE.)
6953 if (Chain.getNumOperands() > 16) {
6954 Aliases.push_back(Chain);
6955 break;
6956 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006957 for (unsigned n = Chain.getNumOperands(); n;)
6958 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00006959 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00006960 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006961
Jim Laskeybc588b82006-10-05 15:07:25 +00006962 default:
6963 // For all other instructions we will just have to take what we can get.
6964 Aliases.push_back(Chain);
6965 break;
Jim Laskey279f0532006-09-25 16:29:54 +00006966 }
6967 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00006968}
6969
6970/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6971/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00006972SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6973 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00006974
Jim Laskey6ff23e52006-10-04 16:53:27 +00006975 // Accumulate all the aliases to this node.
6976 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00006977
Jim Laskey6ff23e52006-10-04 16:53:27 +00006978 if (Aliases.size() == 0) {
6979 // If no operands then chain to entry token.
6980 return DAG.getEntryNode();
6981 } else if (Aliases.size() == 1) {
6982 // If a single operand then chain to it. We don't need to revisit it.
6983 return Aliases[0];
6984 }
Nate Begeman677c89d2009-10-12 05:53:58 +00006985
Jim Laskey6ff23e52006-10-04 16:53:27 +00006986 // Construct a custom tailored token factor.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006987 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
6988 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00006989}
6990
Nate Begeman1d4d4142005-09-01 00:19:25 +00006991// SelectionDAG::Combine - This is the entry point for the file.
6992//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006993void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00006994 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00006995 /// run - This is the main entry point to this class.
6996 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006997 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006998}