blob: f15c121668f7d995a405c9f0c2e48d37e0daf407 [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 Cheng64b7bf72010-04-16 06:14:10 +0000132 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000133 SDValue PromoteExtend(SDValue Op);
134 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000135
Dan Gohman389079b2007-10-08 17:57:15 +0000136 /// combine - call the node-specific routine that knows how to fold each
137 /// particular type of node. If that doesn't do anything, try the
138 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000139 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000140
141 // Visitation implementation - Implement dag node combining for different
142 // node types. The semantics are as follows:
143 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000144 // SDValue.getNode() == 0 - No change was made
145 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
146 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000147 //
Dan Gohman475871a2008-07-27 21:46:04 +0000148 SDValue visitTokenFactor(SDNode *N);
149 SDValue visitMERGE_VALUES(SDNode *N);
150 SDValue visitADD(SDNode *N);
151 SDValue visitSUB(SDNode *N);
152 SDValue visitADDC(SDNode *N);
153 SDValue visitADDE(SDNode *N);
154 SDValue visitMUL(SDNode *N);
155 SDValue visitSDIV(SDNode *N);
156 SDValue visitUDIV(SDNode *N);
157 SDValue visitSREM(SDNode *N);
158 SDValue visitUREM(SDNode *N);
159 SDValue visitMULHU(SDNode *N);
160 SDValue visitMULHS(SDNode *N);
161 SDValue visitSMUL_LOHI(SDNode *N);
162 SDValue visitUMUL_LOHI(SDNode *N);
163 SDValue visitSDIVREM(SDNode *N);
164 SDValue visitUDIVREM(SDNode *N);
165 SDValue visitAND(SDNode *N);
166 SDValue visitOR(SDNode *N);
167 SDValue visitXOR(SDNode *N);
168 SDValue SimplifyVBinOp(SDNode *N);
169 SDValue visitSHL(SDNode *N);
170 SDValue visitSRA(SDNode *N);
171 SDValue visitSRL(SDNode *N);
Evan Cheng4c26e932010-04-19 19:29:22 +0000172 SDValue visitROTL(SDNode *N);
173 SDValue visitROTR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000174 SDValue visitCTLZ(SDNode *N);
175 SDValue visitCTTZ(SDNode *N);
176 SDValue visitCTPOP(SDNode *N);
177 SDValue visitSELECT(SDNode *N);
178 SDValue visitSELECT_CC(SDNode *N);
179 SDValue visitSETCC(SDNode *N);
180 SDValue visitSIGN_EXTEND(SDNode *N);
181 SDValue visitZERO_EXTEND(SDNode *N);
182 SDValue visitANY_EXTEND(SDNode *N);
183 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
184 SDValue visitTRUNCATE(SDNode *N);
185 SDValue visitBIT_CONVERT(SDNode *N);
186 SDValue visitBUILD_PAIR(SDNode *N);
187 SDValue visitFADD(SDNode *N);
188 SDValue visitFSUB(SDNode *N);
189 SDValue visitFMUL(SDNode *N);
190 SDValue visitFDIV(SDNode *N);
191 SDValue visitFREM(SDNode *N);
192 SDValue visitFCOPYSIGN(SDNode *N);
193 SDValue visitSINT_TO_FP(SDNode *N);
194 SDValue visitUINT_TO_FP(SDNode *N);
195 SDValue visitFP_TO_SINT(SDNode *N);
196 SDValue visitFP_TO_UINT(SDNode *N);
197 SDValue visitFP_ROUND(SDNode *N);
198 SDValue visitFP_ROUND_INREG(SDNode *N);
199 SDValue visitFP_EXTEND(SDNode *N);
200 SDValue visitFNEG(SDNode *N);
201 SDValue visitFABS(SDNode *N);
202 SDValue visitBRCOND(SDNode *N);
203 SDValue visitBR_CC(SDNode *N);
204 SDValue visitLOAD(SDNode *N);
205 SDValue visitSTORE(SDNode *N);
206 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
207 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
208 SDValue visitBUILD_VECTOR(SDNode *N);
209 SDValue visitCONCAT_VECTORS(SDNode *N);
210 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000211
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000213 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000216
Dan Gohman475871a2008-07-27 21:46:04 +0000217 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
218 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000219 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000220 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
221 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000222 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000223 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000224 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000225 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000226 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000227 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
228 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000229 SDValue BuildSDIV(SDNode *N);
230 SDValue BuildUDIV(SDNode *N);
Bill Wendling317bd702009-01-30 21:14:50 +0000231 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000232 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000233 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000234
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000236
Jim Laskey6ff23e52006-10-04 16:53:27 +0000237 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
238 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000239 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
240 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000241
Jim Laskey096c22e2006-10-18 12:29:57 +0000242 /// isAlias - Return true if there is any possibility that the two addresses
243 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000244 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000245 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000246 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000248 const Value *SrcValue2, int SrcValueOffset2,
249 unsigned SrcValueAlign2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000250
Jim Laskey7ca56af2006-10-11 13:47:09 +0000251 /// FindAliasInfo - Extracts the relevant alias information from the memory
252 /// node. Returns true if the operand was a load.
253 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000254 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000255 const Value *&SrcValue, int &SrcValueOffset,
256 unsigned &SrcValueAlignment) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000257
Jim Laskey279f0532006-09-25 16:29:54 +0000258 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000259 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000260 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000261
Chris Lattner2392ae72010-04-15 04:48:01 +0000262 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000263 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Chris Lattner2392ae72010-04-15 04:48:01 +0000264 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(Unrestricted),
265 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000266
Nate Begeman1d4d4142005-09-01 00:19:25 +0000267 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000268 void Run(CombineLevel AtLevel);
Chris Lattner2392ae72010-04-15 04:48:01 +0000269
270 SelectionDAG &getDAG() const { return DAG; }
271
272 /// getShiftAmountTy - Returns a type large enough to hold any valid
273 /// shift amount - before type legalization these can be huge.
274 EVT getShiftAmountTy() {
275 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
276 }
277
278 /// isTypeLegal - This method returns true if we are running before type
279 /// legalization or if the specified VT is legal.
280 bool isTypeLegal(const EVT &VT) {
281 if (!LegalTypes) return true;
282 return TLI.isTypeLegal(VT);
283 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000284 };
285}
286
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000287
288namespace {
289/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
290/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000291class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000292 DAGCombiner &DC;
293public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000294 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000295
Duncan Sandsedfcf592008-06-11 11:42:12 +0000296 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000297 DC.removeFromWorkList(N);
298 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000299
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000300 virtual void NodeUpdated(SDNode *N) {
301 // Ignore updates.
302 }
303};
304}
305
Chris Lattner24664722006-03-01 04:53:38 +0000306//===----------------------------------------------------------------------===//
307// TargetLowering::DAGCombinerInfo implementation
308//===----------------------------------------------------------------------===//
309
310void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
311 ((DAGCombiner*)DC)->AddToWorkList(N);
312}
313
Dan Gohman475871a2008-07-27 21:46:04 +0000314SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000315CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
316 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000317}
318
Dan Gohman475871a2008-07-27 21:46:04 +0000319SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000320CombineTo(SDNode *N, SDValue Res, bool AddTo) {
321 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000322}
323
324
Dan Gohman475871a2008-07-27 21:46:04 +0000325SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000326CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
327 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000328}
329
Dan Gohmane5af2d32009-01-29 01:59:02 +0000330void TargetLowering::DAGCombinerInfo::
331CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
332 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
333}
Chris Lattner24664722006-03-01 04:53:38 +0000334
Chris Lattner24664722006-03-01 04:53:38 +0000335//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000336// Helper Functions
337//===----------------------------------------------------------------------===//
338
339/// isNegatibleForFree - Return 1 if we can compute the negated form of the
340/// specified expression for the same cost as the expression itself, or 2 if we
341/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000342static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000343 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000344 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000346 return 0;
347
Chris Lattner29446522007-05-14 22:04:50 +0000348 // fneg is removable even if it has multiple uses.
349 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000350
Chris Lattner29446522007-05-14 22:04:50 +0000351 // Don't allow anything with multiple uses.
352 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000353
Chris Lattner3adf9512007-05-25 02:19:06 +0000354 // Don't recurse exponentially.
355 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Chris Lattner29446522007-05-14 22:04:50 +0000357 switch (Op.getOpcode()) {
358 default: return false;
359 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000360 // Don't invert constant FP values after legalize. The negated constant
361 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000362 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000363 case ISD::FADD:
364 // FIXME: determine better conditions for this xform.
365 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000366
Bill Wendlingd34470c2009-01-30 23:10:18 +0000367 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000368 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000369 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000370 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sands25cf2272008-11-24 14:53:14 +0000371 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000372 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000373 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000374 if (!UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000375
Bill Wendlingd34470c2009-01-30 23:10:18 +0000376 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000377 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000378
Chris Lattner29446522007-05-14 22:04:50 +0000379 case ISD::FMUL:
380 case ISD::FDIV:
381 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000382
Bill Wendlingd34470c2009-01-30 23:10:18 +0000383 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sands25cf2272008-11-24 14:53:14 +0000384 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000385 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000386
Duncan Sands25cf2272008-11-24 14:53:14 +0000387 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000388
Chris Lattner29446522007-05-14 22:04:50 +0000389 case ISD::FP_EXTEND:
390 case ISD::FP_ROUND:
391 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000392 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000393 }
394}
395
396/// GetNegatedExpression - If isNegatibleForFree returns true, this function
397/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000398static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000399 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000400 // fneg is removable even if it has multiple uses.
401 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000402
Chris Lattner29446522007-05-14 22:04:50 +0000403 // Don't allow anything with multiple uses.
404 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000405
Chris Lattner3adf9512007-05-25 02:19:06 +0000406 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000407 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000408 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000409 case ISD::ConstantFP: {
410 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
411 V.changeSign();
412 return DAG.getConstantFP(V, Op.getValueType());
413 }
Chris Lattner29446522007-05-14 22:04:50 +0000414 case ISD::FADD:
415 // FIXME: determine better conditions for this xform.
416 assert(UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000417
Bill Wendlingd34470c2009-01-30 23:10:18 +0000418 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sands25cf2272008-11-24 14:53:14 +0000419 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000420 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000421 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000422 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000423 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000424 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000425 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000426 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000427 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000428 Op.getOperand(0));
429 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000430 // We can't turn -(A-B) into B-A when we honor signed zeros.
Chris Lattner29446522007-05-14 22:04:50 +0000431 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000432
Bill Wendlingd34470c2009-01-30 23:10:18 +0000433 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000434 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000435 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000436 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000437
Bill Wendlingd34470c2009-01-30 23:10:18 +0000438 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000439 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
440 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FMUL:
443 case ISD::FDIV:
444 assert(!HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000445
Bill Wendlingd34470c2009-01-30 23:10:18 +0000446 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000447 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000448 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000449 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000450 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000451 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000452
Bill Wendlingd34470c2009-01-30 23:10:18 +0000453 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000454 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000455 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000456 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000457 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000458
Chris Lattner29446522007-05-14 22:04:50 +0000459 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000460 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000461 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000462 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000463 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000464 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000465 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000466 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000467 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000468 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000469 }
470}
Chris Lattner24664722006-03-01 04:53:38 +0000471
472
Nate Begeman4ebd8052005-09-01 23:24:04 +0000473// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
474// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000475// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000476// nodes based on the type of node we are checking. This simplifies life a
477// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000478static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
479 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 if (N.getOpcode() == ISD::SETCC) {
481 LHS = N.getOperand(0);
482 RHS = N.getOperand(1);
483 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000484 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000486 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 N.getOperand(2).getOpcode() == ISD::Constant &&
488 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000489 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
491 LHS = N.getOperand(0);
492 RHS = N.getOperand(1);
493 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000495 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496 return false;
497}
498
Nate Begeman99801192005-09-07 23:25:52 +0000499// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
500// one use. If this is true, it allows the users to invert the operation for
501// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000502static bool isOneUseSetCC(SDValue N) {
503 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000504 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000505 return true;
506 return false;
507}
508
Bill Wendling35247c32009-01-30 00:45:56 +0000509SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
510 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000511 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000512 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
513 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000514 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000515 SDValue OpNode =
516 DAG.FoldConstantArithmetic(Opc, VT,
517 cast<ConstantSDNode>(N0.getOperand(1)),
518 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000519 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000520 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000521 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
522 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
523 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000524 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000525 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000526 }
527 }
Bill Wendling35247c32009-01-30 00:45:56 +0000528
Nate Begemancd4d58c2006-02-03 06:46:56 +0000529 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
530 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000531 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000532 SDValue OpNode =
533 DAG.FoldConstantArithmetic(Opc, VT,
534 cast<ConstantSDNode>(N1.getOperand(1)),
535 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000536 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000537 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000538 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000539 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000540 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000541 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000542 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000543 }
544 }
Bill Wendling35247c32009-01-30 00:45:56 +0000545
Dan Gohman475871a2008-07-27 21:46:04 +0000546 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000547}
548
Dan Gohman475871a2008-07-27 21:46:04 +0000549SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
550 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000551 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
552 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000553 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000554 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000555 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000556 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000557 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000558 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000559 assert((!To[i].getNode() ||
560 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000561 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000562 WorkListRemover DeadNodes(*this);
563 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000564
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565 if (AddTo) {
566 // Push the new nodes and any users onto the worklist
567 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000568 if (To[i].getNode()) {
569 AddToWorkList(To[i].getNode());
570 AddUsersToWorkList(To[i].getNode());
571 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000572 }
573 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000574
Dan Gohmandbe664a2009-01-19 21:44:21 +0000575 // Finally, if the node is now dead, remove it from the graph. The node
576 // may not be dead if the replacement process recursively simplified to
577 // something else needing this node.
578 if (N->use_empty()) {
579 // Nodes can be reintroduced into the worklist. Make sure we do not
580 // process a node that has been replaced.
581 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000582
Dan Gohmandbe664a2009-01-19 21:44:21 +0000583 // Finally, since the node is now dead, remove it from the graph.
584 DAG.DeleteNode(N);
585 }
Dan Gohman475871a2008-07-27 21:46:04 +0000586 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000587}
588
Evan Chenge5b51ac2010-04-17 06:13:15 +0000589void DAGCombiner::
590CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000591 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000592 // are deleted, make sure to remove them from our worklist.
593 WorkListRemover DeadNodes(*this);
594 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000595
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000596 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000597 AddToWorkList(TLO.New.getNode());
598 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000599
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000600 // Finally, if the node is now dead, remove it from the graph. The node
601 // may not be dead if the replacement process recursively simplified to
602 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000603 if (TLO.Old.getNode()->use_empty()) {
604 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000605
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000606 // If the operands of this node are only used by the node, they will now
607 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000608 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
609 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
610 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000611
Gabor Greifba36cb52008-08-28 21:40:38 +0000612 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000613 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000614}
615
616/// SimplifyDemandedBits - Check the specified integer node value to see if
617/// it can be simplified or if things it uses can be simplified by bit
618/// propagation. If so, return true.
619bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000620 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000621 APInt KnownZero, KnownOne;
622 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
623 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000624
Dan Gohmane5af2d32009-01-29 01:59:02 +0000625 // Revisit the node.
626 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000627
Dan Gohmane5af2d32009-01-29 01:59:02 +0000628 // Replace the old value with the new one.
629 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000630 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000631 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000632 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000633 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000634 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000635
Dan Gohmane5af2d32009-01-29 01:59:02 +0000636 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000637 return true;
638}
639
Evan Cheng4c26e932010-04-19 19:29:22 +0000640static SDValue SExtPromoteOperand(SDValue Op, EVT PVT, SelectionDAG &DAG,
641 const TargetLowering &TLI);
642static SDValue ZExtPromoteOperand(SDValue Op, EVT PVT, SelectionDAG &DAG,
643 const TargetLowering &TLI);
644
Evan Chenge5b51ac2010-04-17 06:13:15 +0000645static SDValue PromoteOperand(SDValue Op, EVT PVT, SelectionDAG &DAG,
646 const TargetLowering &TLI) {
Evan Cheng4c26e932010-04-19 19:29:22 +0000647 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000648 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Cheng4c26e932010-04-19 19:29:22 +0000649 ISD::LoadExtType ExtType =
650 ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD : LD->getExtensionType();
651 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000652 LD->getChain(), LD->getBasePtr(),
653 LD->getSrcValue(), LD->getSrcValueOffset(),
654 LD->getMemoryVT(), LD->isVolatile(),
655 LD->isNonTemporal(), LD->getAlignment());
656 }
657
Evan Cheng4c26e932010-04-19 19:29:22 +0000658 unsigned Opc = Op.getOpcode();
659 if (Opc == ISD::AssertSext)
660 return DAG.getNode(ISD::AssertSext, dl, PVT,
661 SExtPromoteOperand(Op.getOperand(0), PVT, DAG, TLI),
662 Op.getOperand(1));
663 else if (Opc == ISD::AssertZext)
664 return DAG.getNode(ISD::AssertZext, dl, PVT,
665 ZExtPromoteOperand(Op.getOperand(0), PVT, DAG, TLI),
666 Op.getOperand(1));
667
668 unsigned ExtOpc = ISD::ANY_EXTEND;
669 if (Opc == ISD::Constant)
Evan Cheng64b7bf72010-04-16 06:14:10 +0000670 // Zero extend things like i1, sign extend everything else. It shouldn't
671 // matter in theory which one we pick, but this tends to give better code?
672 // See DAGTypeLegalizer::PromoteIntRes_Constant.
Evan Cheng4c26e932010-04-19 19:29:22 +0000673 ExtOpc =
674 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
675 if (!TLI.isOperationLegal(ExtOpc, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000676 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +0000677 return DAG.getNode(ExtOpc, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000678}
679
Evan Chenge5b51ac2010-04-17 06:13:15 +0000680static SDValue SExtPromoteOperand(SDValue Op, EVT PVT, SelectionDAG &DAG,
681 const TargetLowering &TLI) {
682 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
683 return SDValue();
684 EVT OldVT = Op.getValueType();
685 DebugLoc dl = Op.getDebugLoc();
686 Op = PromoteOperand(Op, PVT, DAG, TLI);
687 if (Op.getNode() == 0)
688 return SDValue();
689 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(), Op,
690 DAG.getValueType(OldVT));
691}
692
693static SDValue ZExtPromoteOperand(SDValue Op, EVT PVT, SelectionDAG &DAG,
694 const TargetLowering &TLI) {
695 EVT OldVT = Op.getValueType();
696 DebugLoc dl = Op.getDebugLoc();
697 Op = PromoteOperand(Op, PVT, DAG, TLI);
698 if (Op.getNode() == 0)
699 return SDValue();
700 return DAG.getZeroExtendInReg(Op, dl, OldVT);
701}
702
Evan Cheng64b7bf72010-04-16 06:14:10 +0000703/// PromoteIntBinOp - Promote the specified integer binary operation if the
704/// target indicates it is beneficial. e.g. On x86, it's usually better to
705/// promote i16 operations to i32 since i16 instructions are longer.
706SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
707 if (!LegalOperations)
708 return SDValue();
709
710 EVT VT = Op.getValueType();
711 if (VT.isVector() || !VT.isInteger())
712 return SDValue();
713
Evan Chenge5b51ac2010-04-17 06:13:15 +0000714 // If operation type is 'undesirable', e.g. i16 on x86, consider
715 // promoting it.
716 unsigned Opc = Op.getOpcode();
717 if (TLI.isTypeDesirableForOp(Opc, VT))
718 return SDValue();
719
Evan Cheng64b7bf72010-04-16 06:14:10 +0000720 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000721 // Consult target whether it is a good idea to promote this operation and
722 // what's the right type to promote it to.
723 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000724 assert(PVT != VT && "Don't know what type to promote to!");
725
Evan Chenge5b51ac2010-04-17 06:13:15 +0000726 bool isShift = (Opc == ISD::SHL) || (Opc == ISD::SRA) || (Opc == ISD::SRL);
727 SDValue N0 = Op.getOperand(0);
728 if (Opc == ISD::SRA)
729 N0 = SExtPromoteOperand(Op.getOperand(0), PVT, DAG, TLI);
730 else if (Opc == ISD::SRL)
731 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT, DAG, TLI);
732 else
733 N0 = PromoteOperand(N0, PVT, DAG, TLI);
734 if (N0.getNode() == 0)
735 return SDValue();
Evan Cheng64b7bf72010-04-16 06:14:10 +0000736
Evan Chenge5b51ac2010-04-17 06:13:15 +0000737 SDValue N1 = Op.getOperand(1);
738 if (!isShift) {
739 N1 = PromoteOperand(N1, PVT, DAG, TLI);
740 if (N1.getNode() == 0)
741 return SDValue();
742 AddToWorkList(N1.getNode());
743 }
744 AddToWorkList(N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000745
746 DebugLoc dl = Op.getDebugLoc();
747 return DAG.getNode(ISD::TRUNCATE, dl, VT,
748 DAG.getNode(Op.getOpcode(), dl, PVT, N0, N1));
749 }
750 return SDValue();
751}
752
Evan Cheng4c26e932010-04-19 19:29:22 +0000753SDValue DAGCombiner::PromoteExtend(SDValue Op) {
754 if (!LegalOperations)
755 return SDValue();
756
757 EVT VT = Op.getValueType();
758 if (VT.isVector() || !VT.isInteger())
759 return SDValue();
760
761 // If operation type is 'undesirable', e.g. i16 on x86, consider
762 // promoting it.
763 unsigned Opc = Op.getOpcode();
764 if (TLI.isTypeDesirableForOp(Opc, VT))
765 return SDValue();
766
767 EVT PVT = VT;
768 // Consult target whether it is a good idea to promote this operation and
769 // what's the right type to promote it to.
770 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
771 assert(PVT != VT && "Don't know what type to promote to!");
772 // fold (aext (aext x)) -> (aext x)
773 // fold (aext (zext x)) -> (zext x)
774 // fold (aext (sext x)) -> (sext x)
775 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
776 }
777 return SDValue();
778}
779
780bool DAGCombiner::PromoteLoad(SDValue Op) {
781 if (!LegalOperations)
782 return false;
783
784 EVT VT = Op.getValueType();
785 if (VT.isVector() || !VT.isInteger())
786 return false;
787
788 // If operation type is 'undesirable', e.g. i16 on x86, consider
789 // promoting it.
790 unsigned Opc = Op.getOpcode();
791 if (TLI.isTypeDesirableForOp(Opc, VT))
792 return false;
793
794 EVT PVT = VT;
795 // Consult target whether it is a good idea to promote this operation and
796 // what's the right type to promote it to.
797 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
798 assert(PVT != VT && "Don't know what type to promote to!");
799
800 DebugLoc dl = Op.getDebugLoc();
801 SDNode *N = Op.getNode();
802 LoadSDNode *LD = cast<LoadSDNode>(N);
803 ISD::LoadExtType ExtType =
804 ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD : LD->getExtensionType();
805 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
806 LD->getChain(), LD->getBasePtr(),
807 LD->getSrcValue(), LD->getSrcValueOffset(),
808 LD->getMemoryVT(), LD->isVolatile(),
809 LD->isNonTemporal(), LD->getAlignment());
810 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
811
812 DEBUG(dbgs() << "\nReplacing.x ";
813 N->dump(&DAG);
814 dbgs() << "\nWith: ";
815 Result.getNode()->dump(&DAG);
816 dbgs() << '\n');
817 WorkListRemover DeadNodes(*this);
818 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result, &DeadNodes);
819 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1), &DeadNodes);
820 removeFromWorkList(N);
821 DAG.DeleteNode(N);
822 return true;
823 }
824 return false;
825}
826
Evan Chenge5b51ac2010-04-17 06:13:15 +0000827
Chris Lattner29446522007-05-14 22:04:50 +0000828//===----------------------------------------------------------------------===//
829// Main DAG Combiner implementation
830//===----------------------------------------------------------------------===//
831
Duncan Sands25cf2272008-11-24 14:53:14 +0000832void DAGCombiner::Run(CombineLevel AtLevel) {
833 // set the instance variables, so that the various visit routines may use it.
834 Level = AtLevel;
835 LegalOperations = Level >= NoIllegalOperations;
836 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000837
Evan Cheng17a568b2008-08-29 22:21:44 +0000838 // Add all the dag nodes to the worklist.
839 WorkList.reserve(DAG.allnodes_size());
840 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
841 E = DAG.allnodes_end(); I != E; ++I)
842 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000843
Evan Cheng17a568b2008-08-29 22:21:44 +0000844 // Create a dummy node (which is not added to allnodes), that adds a reference
845 // to the root node, preventing it from being deleted, and tracking any
846 // changes of the root.
847 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000848
Jim Laskey26f7fa72006-10-17 19:33:52 +0000849 // The root of the dag may dangle to deleted nodes until the dag combiner is
850 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000851 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000852
Evan Cheng17a568b2008-08-29 22:21:44 +0000853 // while the worklist isn't empty, inspect the node on the end of it and
854 // try and combine it.
855 while (!WorkList.empty()) {
856 SDNode *N = WorkList.back();
857 WorkList.pop_back();
Scott Michelfdc40a02009-02-17 22:15:04 +0000858
Evan Cheng17a568b2008-08-29 22:21:44 +0000859 // If N has no uses, it is dead. Make sure to revisit all N's operands once
860 // N is deleted from the DAG, since they too may now be dead or may have a
861 // reduced number of uses, allowing other xforms.
862 if (N->use_empty() && N != &Dummy) {
863 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
864 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000865
Evan Cheng17a568b2008-08-29 22:21:44 +0000866 DAG.DeleteNode(N);
867 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000869
Evan Cheng17a568b2008-08-29 22:21:44 +0000870 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000871
Evan Cheng17a568b2008-08-29 22:21:44 +0000872 if (RV.getNode() == 0)
873 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000874
Evan Cheng17a568b2008-08-29 22:21:44 +0000875 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +0000876
Evan Cheng17a568b2008-08-29 22:21:44 +0000877 // If we get back the same node we passed in, rather than a new node or
878 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +0000879 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +0000880 // mechanics for us, we have no work to do in this case.
881 if (RV.getNode() == N)
882 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +0000883
Evan Cheng17a568b2008-08-29 22:21:44 +0000884 assert(N->getOpcode() != ISD::DELETED_NODE &&
885 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
886 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000887
David Greenef1090292010-01-05 01:25:00 +0000888 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000889 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000890 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000891 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000892 dbgs() << '\n');
Evan Cheng17a568b2008-08-29 22:21:44 +0000893 WorkListRemover DeadNodes(*this);
894 if (N->getNumValues() == RV.getNode()->getNumValues())
895 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
896 else {
897 assert(N->getValueType(0) == RV.getValueType() &&
898 N->getNumValues() == 1 && "Type mismatch");
899 SDValue OpV = RV;
900 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
901 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000902
Evan Cheng17a568b2008-08-29 22:21:44 +0000903 // Push the new node and any users onto the worklist
904 AddToWorkList(RV.getNode());
905 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Evan Cheng17a568b2008-08-29 22:21:44 +0000907 // Add any uses of the old node to the worklist in case this node is the
908 // last one that uses them. They may become dead after this node is
909 // deleted.
910 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
911 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000912
Dan Gohmandbe664a2009-01-19 21:44:21 +0000913 // Finally, if the node is now dead, remove it from the graph. The node
914 // may not be dead if the replacement process recursively simplified to
915 // something else needing this node.
916 if (N->use_empty()) {
917 // Nodes can be reintroduced into the worklist. Make sure we do not
918 // process a node that has been replaced.
919 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000920
Dan Gohmandbe664a2009-01-19 21:44:21 +0000921 // Finally, since the node is now dead, remove it from the graph.
922 DAG.DeleteNode(N);
923 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000924 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000925
Chris Lattner95038592005-10-05 06:35:28 +0000926 // If the root changed (e.g. it was a dead load, update the root).
927 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928}
929
Dan Gohman475871a2008-07-27 21:46:04 +0000930SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 switch(N->getOpcode()) {
932 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000933 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000934 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 case ISD::ADD: return visitADD(N);
936 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000937 case ISD::ADDC: return visitADDC(N);
938 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 case ISD::MUL: return visitMUL(N);
940 case ISD::SDIV: return visitSDIV(N);
941 case ISD::UDIV: return visitUDIV(N);
942 case ISD::SREM: return visitSREM(N);
943 case ISD::UREM: return visitUREM(N);
944 case ISD::MULHU: return visitMULHU(N);
945 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000946 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
947 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
948 case ISD::SDIVREM: return visitSDIVREM(N);
949 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 case ISD::AND: return visitAND(N);
951 case ISD::OR: return visitOR(N);
952 case ISD::XOR: return visitXOR(N);
953 case ISD::SHL: return visitSHL(N);
954 case ISD::SRA: return visitSRA(N);
955 case ISD::SRL: return visitSRL(N);
Evan Cheng4c26e932010-04-19 19:29:22 +0000956 case ISD::ROTL: return visitROTL(N);
957 case ISD::ROTR: return visitROTR(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 case ISD::CTLZ: return visitCTLZ(N);
959 case ISD::CTTZ: return visitCTTZ(N);
960 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000961 case ISD::SELECT: return visitSELECT(N);
962 case ISD::SELECT_CC: return visitSELECT_CC(N);
963 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
965 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000966 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
968 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000969 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000970 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000971 case ISD::FADD: return visitFADD(N);
972 case ISD::FSUB: return visitFSUB(N);
973 case ISD::FMUL: return visitFMUL(N);
974 case ISD::FDIV: return visitFDIV(N);
975 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000976 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
978 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
979 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
980 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
981 case ISD::FP_ROUND: return visitFP_ROUND(N);
982 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
983 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
984 case ISD::FNEG: return visitFNEG(N);
985 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000986 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000987 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000988 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000989 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000990 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000991 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000992 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
993 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000994 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 }
Dan Gohman475871a2008-07-27 21:46:04 +0000996 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997}
998
Dan Gohman475871a2008-07-27 21:46:04 +0000999SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001000 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001001
1002 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001003 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001004 assert(N->getOpcode() != ISD::DELETED_NODE &&
1005 "Node was deleted but visit returned NULL!");
1006
1007 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1008 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1009
1010 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001011 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001012 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001013
1014 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1015 }
1016 }
1017
Scott Michelfdc40a02009-02-17 22:15:04 +00001018 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001019 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001020 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001021 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1022 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001023 SDValue N0 = N->getOperand(0);
1024 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001025
Evan Cheng08b11732008-03-22 01:55:50 +00001026 // Constant operands are canonicalized to RHS.
1027 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001028 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001029 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1030 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001031 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001032 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001033 }
1034 }
1035
Dan Gohman389079b2007-10-08 17:57:15 +00001036 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001037}
Dan Gohman389079b2007-10-08 17:57:15 +00001038
Chris Lattner6270f682006-10-08 22:57:01 +00001039/// getInputChainForNode - Given a node, return its input chain if it has one,
1040/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001041static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001042 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001044 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001045 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001046 return N->getOperand(NumOps-1);
1047 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001048 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001049 return N->getOperand(i);
1050 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001051 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001052}
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001055 // If N has two operands, where one has an input chain equal to the other,
1056 // the 'other' chain is redundant.
1057 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001058 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001059 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001060 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001061 return N->getOperand(1);
1062 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001063
Chris Lattnerc76d4412007-05-16 06:37:59 +00001064 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001065 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001066 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001067 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001068
Jim Laskey6ff23e52006-10-04 16:53:27 +00001069 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001070 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Jim Laskey71382342006-10-07 23:37:56 +00001072 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001073 // encountered.
1074 for (unsigned i = 0; i < TFs.size(); ++i) {
1075 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001076
Jim Laskey6ff23e52006-10-04 16:53:27 +00001077 // Check each of the operands.
1078 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001079 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001080
Jim Laskey6ff23e52006-10-04 16:53:27 +00001081 switch (Op.getOpcode()) {
1082 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001083 // Entry tokens don't need to be added to the list. They are
1084 // rededundant.
1085 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001086 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001087
Jim Laskey6ff23e52006-10-04 16:53:27 +00001088 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001089 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001090 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001091 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001092 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001093 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001095 Changed = true;
1096 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001097 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001098 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001099
Jim Laskey6ff23e52006-10-04 16:53:27 +00001100 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001101 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001102 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001103 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001104 else
1105 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001106 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001107 }
1108 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001109 }
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001110
Dan Gohman475871a2008-07-27 21:46:04 +00001111 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001112
1113 // If we've change things around then replace token factor.
1114 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001115 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001116 // The entry token is the only possible outcome.
1117 Result = DAG.getEntryNode();
1118 } else {
1119 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001120 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001122 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001123
Jim Laskey274062c2006-10-13 23:32:28 +00001124 // Don't add users to work list.
1125 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001127
Jim Laskey6ff23e52006-10-04 16:53:27 +00001128 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129}
1130
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001131/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001133 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001134 // Replacing results may cause a different MERGE_VALUES to suddenly
1135 // be CSE'd with N, and carry its uses with it. Iterate until no
1136 // uses remain, to ensure that the node can be safely deleted.
1137 do {
1138 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1139 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
1140 &DeadNodes);
1141 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001142 removeFromWorkList(N);
1143 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001144 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001145}
1146
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001147static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001148SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1149 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001150 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001151 SDValue N00 = N0.getOperand(0);
1152 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001153 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001154
Gabor Greifba36cb52008-08-28 21:40:38 +00001155 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001156 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001157 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1158 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1159 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1160 N00.getOperand(0), N01),
1161 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1162 N00.getOperand(1), N01));
1163 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001164 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001165
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001167}
1168
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue DAGCombiner::visitADD(SDNode *N) {
1170 SDValue N0 = N->getOperand(0);
1171 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001174 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001175
1176 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001177 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001179 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001180 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001181
Dan Gohman613e0d82007-07-03 14:03:57 +00001182 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001183 if (N0.getOpcode() == ISD::UNDEF)
1184 return N0;
1185 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001186 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001188 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001189 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001190 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001191 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001192 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001196 // fold (add Sym, c) -> Sym+c
1197 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001198 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001199 GA->getOpcode() == ISD::GlobalAddress)
1200 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1201 GA->getOffset() +
1202 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001203 // fold ((c1-A)+c2) -> (c1+c2)-A
1204 if (N1C && N0.getOpcode() == ISD::SUB)
1205 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001206 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001207 DAG.getConstant(N1C->getAPIntValue()+
1208 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001209 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001210 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001211 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001212 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001213 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 // fold ((0-A) + B) -> B-A
1215 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1216 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001217 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 // fold (A + (0-B)) -> A-B
1219 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1220 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001221 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001222 // fold (A+(B-A)) -> B
1223 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001224 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001225 // fold ((B-A)+A) -> B
1226 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1227 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001228 // fold (A+(B-(A+C))) to (B-C)
1229 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001230 N0 == N1.getOperand(1).getOperand(0))
1231 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001232 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001233 // fold (A+(B-(C+A))) to (B-C)
1234 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001235 N0 == N1.getOperand(1).getOperand(1))
1236 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001237 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001238 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001239 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1240 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001241 N0 == N1.getOperand(0).getOperand(1))
1242 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1243 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001244
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001245 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1246 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1247 SDValue N00 = N0.getOperand(0);
1248 SDValue N01 = N0.getOperand(1);
1249 SDValue N10 = N1.getOperand(0);
1250 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001251
1252 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1253 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1254 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1255 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001256 }
Chris Lattner947c2892006-03-13 06:51:27 +00001257
Dan Gohman475871a2008-07-27 21:46:04 +00001258 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1259 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
Chris Lattner947c2892006-03-13 06:51:27 +00001261 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001263 APInt LHSZero, LHSOne;
1264 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001265 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001266 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001267
Dan Gohman948d8ea2008-02-20 16:33:30 +00001268 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001269 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Chris Lattner947c2892006-03-13 06:51:27 +00001271 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1272 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1273 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1274 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001275 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001276 }
1277 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001278
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001279 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001281 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001282 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001283 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001285 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001287 }
1288
Dan Gohmancd9e1552010-01-19 23:30:49 +00001289 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1290 if (N1.getOpcode() == ISD::SHL &&
1291 N1.getOperand(0).getOpcode() == ISD::SUB)
1292 if (ConstantSDNode *C =
1293 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1294 if (C->getAPIntValue() == 0)
1295 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1296 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1297 N1.getOperand(0).getOperand(1),
1298 N1.getOperand(1)));
1299 if (N0.getOpcode() == ISD::SHL &&
1300 N0.getOperand(0).getOpcode() == ISD::SUB)
1301 if (ConstantSDNode *C =
1302 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1303 if (C->getAPIntValue() == 0)
1304 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1305 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1306 N0.getOperand(0).getOperand(1),
1307 N0.getOperand(1)));
1308
Evan Cheng64b7bf72010-04-16 06:14:10 +00001309 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310}
1311
Dan Gohman475871a2008-07-27 21:46:04 +00001312SDValue DAGCombiner::visitADDC(SDNode *N) {
1313 SDValue N0 = N->getOperand(0);
1314 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001315 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1316 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001317 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001318
Chris Lattner91153682007-03-04 20:03:15 +00001319 // If the flag result is dead, turn this into an ADD.
1320 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-01-30 02:38:00 +00001321 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen874ae252009-06-02 03:12:52 +00001322 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001324
Chris Lattner91153682007-03-04 20:03:15 +00001325 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001326 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001327 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001328
Chris Lattnerb6541762007-03-04 20:40:38 +00001329 // fold (addc x, 0) -> x + no carry out
1330 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001331 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001332 N->getDebugLoc(), MVT::Flag));
Scott Michelfdc40a02009-02-17 22:15:04 +00001333
Dale Johannesen874ae252009-06-02 03:12:52 +00001334 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001335 APInt LHSZero, LHSOne;
1336 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001337 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001338 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001339
Dan Gohman948d8ea2008-02-20 16:33:30 +00001340 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001341 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Chris Lattnerb6541762007-03-04 20:40:38 +00001343 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1344 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1345 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1346 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001347 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001348 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Dan Gohman475871a2008-07-27 21:46:04 +00001352 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001353}
1354
Dan Gohman475871a2008-07-27 21:46:04 +00001355SDValue DAGCombiner::visitADDE(SDNode *N) {
1356 SDValue N0 = N->getOperand(0);
1357 SDValue N1 = N->getOperand(1);
1358 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001359 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1360 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Chris Lattner91153682007-03-04 20:03:15 +00001362 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001363 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001364 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1365 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001366
Chris Lattnerb6541762007-03-04 20:40:38 +00001367 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001368 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1369 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001370
Dan Gohman475871a2008-07-27 21:46:04 +00001371 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001372}
1373
Dan Gohman475871a2008-07-27 21:46:04 +00001374SDValue DAGCombiner::visitSUB(SDNode *N) {
1375 SDValue N0 = N->getOperand(0);
1376 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001377 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1378 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001379 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001380
Dan Gohman7f321562007-06-25 16:23:39 +00001381 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001382 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001384 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001385 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001386
Chris Lattner854077d2005-10-17 01:07:11 +00001387 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001388 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001389 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001392 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001393 // fold (sub x, c) -> (add x, -c)
1394 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001395 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001396 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001397 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1398 if (N0C && N0C->isAllOnesValue())
1399 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001401 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001404 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001405 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001406 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001407 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001408 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1409 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001410 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001411 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1412 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001413 // fold ((A+(C+B))-B) -> A+C
1414 if (N0.getOpcode() == ISD::ADD &&
1415 N0.getOperand(1).getOpcode() == ISD::ADD &&
1416 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001417 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1418 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001419 // fold ((A-(B-C))-C) -> A-B
1420 if (N0.getOpcode() == ISD::SUB &&
1421 N0.getOperand(1).getOpcode() == ISD::SUB &&
1422 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001423 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1424 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001425
Dan Gohman613e0d82007-07-03 14:03:57 +00001426 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001427 if (N0.getOpcode() == ISD::UNDEF)
1428 return N0;
1429 if (N1.getOpcode() == ISD::UNDEF)
1430 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001431
Dan Gohman6520e202008-10-18 02:06:02 +00001432 // If the relocation model supports it, consider symbol offsets.
1433 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001434 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001435 // fold (sub Sym, c) -> Sym-c
1436 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1437 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1438 GA->getOffset() -
1439 (uint64_t)N1C->getSExtValue());
1440 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1441 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1442 if (GA->getGlobal() == GB->getGlobal())
1443 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1444 VT);
1445 }
1446
Evan Cheng64b7bf72010-04-16 06:14:10 +00001447 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448}
1449
Dan Gohman475871a2008-07-27 21:46:04 +00001450SDValue DAGCombiner::visitMUL(SDNode *N) {
1451 SDValue N0 = N->getOperand(0);
1452 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001455 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Dan Gohman7f321562007-06-25 16:23:39 +00001457 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001460 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001462
Dan Gohman613e0d82007-07-03 14:03:57 +00001463 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001464 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001465 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001467 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001468 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001469 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001470 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001471 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001473 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001476 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001477 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1478 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001480 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001481 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001482 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001483 getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001484 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001485 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1486 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001487 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001488 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001489 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1490 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001491 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner66b8bc32009-03-09 20:22:18 +00001492 DAG.getConstant(Log2Val, getShiftAmountTy())));
1493 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001494 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001495 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001496 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001497 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1498 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001500 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1501 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001503
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001504 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1505 // use.
1506 {
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001508 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1509 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001510 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001511 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001512 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001513 isa<ConstantSDNode>(N1.getOperand(1)) &&
1514 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001515 Sh = N1; Y = N0;
1516 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001517
Gabor Greifba36cb52008-08-28 21:40:38 +00001518 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001519 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1520 Sh.getOperand(0), Y);
1521 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1522 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001523 }
1524 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001525
Chris Lattnera1deca32006-03-04 23:33:26 +00001526 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001527 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001528 isa<ConstantSDNode>(N0.getOperand(1)))
1529 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1530 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1531 N0.getOperand(0), N1),
1532 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1533 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Nate Begemancd4d58c2006-02-03 06:46:56 +00001535 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001536 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001537 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001538 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001539
Evan Cheng64b7bf72010-04-16 06:14:10 +00001540 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001541}
1542
Dan Gohman475871a2008-07-27 21:46:04 +00001543SDValue DAGCombiner::visitSDIV(SDNode *N) {
1544 SDValue N0 = N->getOperand(0);
1545 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001548 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001549
Dan Gohman7f321562007-06-25 16:23:39 +00001550 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001551 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001553 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001558 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001559 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001560 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001561 return N0;
1562 // fold (sdiv X, -1) -> 0-X
1563 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001564 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1565 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001566 // If we know the sign bits of both operands are zero, strength reduce to a
1567 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001568 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001569 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001570 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1571 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001572 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001573 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001574 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michelfdc40a02009-02-17 22:15:04 +00001575 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman7810bfe2008-09-26 21:54:37 +00001576 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001577 // If dividing by powers of two is cheap, then don't perform the following
1578 // fold.
1579 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001580 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001581
Dan Gohman7810bfe2008-09-26 21:54:37 +00001582 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001583 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001584 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001585
Chris Lattner8f4880b2006-02-16 08:02:36 +00001586 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001587 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1588 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00001589 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001590 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001591
Chris Lattner8f4880b2006-02-16 08:02:36 +00001592 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001593 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1594 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands92abc622009-01-31 15:50:11 +00001595 getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001596 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001597 AddToWorkList(SRL.getNode());
1598 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001599 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands92abc622009-01-31 15:50:11 +00001600 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling944d34b2009-01-30 02:52:17 +00001601
Nate Begeman405e3ec2005-10-21 00:02:42 +00001602 // If we're dividing by a positive value, we're done. Otherwise, we must
1603 // negate the result.
1604 if (pow2 > 0)
1605 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001606
Gabor Greifba36cb52008-08-28 21:40:38 +00001607 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001608 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1609 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001610 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001611
Nate Begeman69575232005-10-20 02:15:44 +00001612 // if integer divide is expensive and we satisfy the requirements, emit an
1613 // alternate sequence.
Scott Michelfdc40a02009-02-17 22:15:04 +00001614 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001615 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001616 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001617 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001618 }
Dan Gohman7f321562007-06-25 16:23:39 +00001619
Dan Gohman613e0d82007-07-03 14:03:57 +00001620 // undef / X -> 0
1621 if (N0.getOpcode() == ISD::UNDEF)
1622 return DAG.getConstant(0, VT);
1623 // X / undef -> undef
1624 if (N1.getOpcode() == ISD::UNDEF)
1625 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001626
Dan Gohman475871a2008-07-27 21:46:04 +00001627 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628}
1629
Dan Gohman475871a2008-07-27 21:46:04 +00001630SDValue DAGCombiner::visitUDIV(SDNode *N) {
1631 SDValue N0 = N->getOperand(0);
1632 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001633 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001635 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001636
Dan Gohman7f321562007-06-25 16:23:39 +00001637 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001638 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001639 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001640 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001641 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001642
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001644 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001645 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001647 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001648 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001649 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00001650 getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001651 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1652 if (N1.getOpcode() == ISD::SHL) {
1653 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001654 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001655 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001656 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1657 N1.getOperand(1),
1658 DAG.getConstant(SHC->getAPIntValue()
1659 .logBase2(),
1660 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001661 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001662 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001663 }
1664 }
1665 }
Nate Begeman69575232005-10-20 02:15:44 +00001666 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001667 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001668 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001669 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001670 }
Dan Gohman7f321562007-06-25 16:23:39 +00001671
Dan Gohman613e0d82007-07-03 14:03:57 +00001672 // undef / X -> 0
1673 if (N0.getOpcode() == ISD::UNDEF)
1674 return DAG.getConstant(0, VT);
1675 // X / undef -> undef
1676 if (N1.getOpcode() == ISD::UNDEF)
1677 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001678
Dan Gohman475871a2008-07-27 21:46:04 +00001679 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680}
1681
Dan Gohman475871a2008-07-27 21:46:04 +00001682SDValue DAGCombiner::visitSREM(SDNode *N) {
1683 SDValue N0 = N->getOperand(0);
1684 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001685 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001687 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001688
Nate Begeman1d4d4142005-09-01 00:19:25 +00001689 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001690 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001691 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001692 // If we know the sign bits of both operands are zero, strength reduce to a
1693 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001694 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001695 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001696 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001698
Dan Gohman77003042007-11-26 23:46:11 +00001699 // If X/C can be simplified by the division-by-constant logic, lower
1700 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001701 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001702 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001703 AddToWorkList(Div.getNode());
1704 SDValue OptimizedDiv = combine(Div.getNode());
1705 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001706 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1707 OptimizedDiv, N1);
1708 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001710 return Sub;
1711 }
Chris Lattner26d29902006-10-12 20:58:32 +00001712 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
Dan Gohman613e0d82007-07-03 14:03:57 +00001714 // undef % X -> 0
1715 if (N0.getOpcode() == ISD::UNDEF)
1716 return DAG.getConstant(0, VT);
1717 // X % undef -> undef
1718 if (N1.getOpcode() == ISD::UNDEF)
1719 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001720
Dan Gohman475871a2008-07-27 21:46:04 +00001721 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001722}
1723
Dan Gohman475871a2008-07-27 21:46:04 +00001724SDValue DAGCombiner::visitUREM(SDNode *N) {
1725 SDValue N0 = N->getOperand(0);
1726 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001727 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1728 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001729 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001730
Nate Begeman1d4d4142005-09-01 00:19:25 +00001731 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001732 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001733 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001734 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001735 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001736 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001737 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001738 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1739 if (N1.getOpcode() == ISD::SHL) {
1740 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001741 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001742 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001743 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001744 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001745 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001746 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001747 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001748 }
1749 }
1750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001751
Dan Gohman77003042007-11-26 23:46:11 +00001752 // If X/C can be simplified by the division-by-constant logic, lower
1753 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001754 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001755 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001756 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001757 SDValue OptimizedDiv = combine(Div.getNode());
1758 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001759 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1760 OptimizedDiv, N1);
1761 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001762 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001763 return Sub;
1764 }
Chris Lattner26d29902006-10-12 20:58:32 +00001765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Dan Gohman613e0d82007-07-03 14:03:57 +00001767 // undef % X -> 0
1768 if (N0.getOpcode() == ISD::UNDEF)
1769 return DAG.getConstant(0, VT);
1770 // X % undef -> undef
1771 if (N1.getOpcode() == ISD::UNDEF)
1772 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001773
Dan Gohman475871a2008-07-27 21:46:04 +00001774 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775}
1776
Dan Gohman475871a2008-07-27 21:46:04 +00001777SDValue DAGCombiner::visitMULHS(SDNode *N) {
1778 SDValue N0 = N->getOperand(0);
1779 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001780 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001781 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001782
Nate Begeman1d4d4142005-09-01 00:19:25 +00001783 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001784 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001785 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001787 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001788 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1789 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands92abc622009-01-31 15:50:11 +00001790 getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001791 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001792 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001793 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001794
Dan Gohman475871a2008-07-27 21:46:04 +00001795 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001796}
1797
Dan Gohman475871a2008-07-27 21:46:04 +00001798SDValue DAGCombiner::visitMULHU(SDNode *N) {
1799 SDValue N0 = N->getOperand(0);
1800 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001801 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001802 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Nate Begeman1d4d4142005-09-01 00:19:25 +00001804 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001805 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001806 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001807 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001808 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001809 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001810 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001811 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001812 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001813
Dan Gohman475871a2008-07-27 21:46:04 +00001814 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815}
1816
Dan Gohman389079b2007-10-08 17:57:15 +00001817/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1818/// compute two values. LoOp and HiOp give the opcodes for the two computations
1819/// that are being performed. Return true if a simplification was made.
1820///
Scott Michelfdc40a02009-02-17 22:15:04 +00001821SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00001822 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001823 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001824 bool HiExists = N->hasAnyUseOfValue(1);
1825 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001826 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001827 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001828 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1829 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001830 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001831 }
1832
1833 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001834 bool LoExists = N->hasAnyUseOfValue(0);
1835 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001836 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001837 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001838 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1839 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001840 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001841 }
1842
Evan Cheng44711942007-11-08 09:25:29 +00001843 // If both halves are used, return as it is.
1844 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001845 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001846
1847 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001848 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001849 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1850 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001851 AddToWorkList(Lo.getNode());
1852 SDValue LoOpt = combine(Lo.getNode());
1853 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001854 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001855 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001856 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001857 }
1858
Evan Cheng44711942007-11-08 09:25:29 +00001859 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001860 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001861 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001862 AddToWorkList(Hi.getNode());
1863 SDValue HiOpt = combine(Hi.getNode());
1864 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001865 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001866 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001867 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001868 }
Bill Wendling826d1142009-01-30 03:08:40 +00001869
Dan Gohman475871a2008-07-27 21:46:04 +00001870 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001871}
1872
Dan Gohman475871a2008-07-27 21:46:04 +00001873SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1874 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001875 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001876
Dan Gohman475871a2008-07-27 21:46:04 +00001877 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001878}
1879
Dan Gohman475871a2008-07-27 21:46:04 +00001880SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1881 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001882 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001883
Dan Gohman475871a2008-07-27 21:46:04 +00001884 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001885}
1886
Dan Gohman475871a2008-07-27 21:46:04 +00001887SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1888 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001889 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001890
Dan Gohman475871a2008-07-27 21:46:04 +00001891 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001892}
1893
Dan Gohman475871a2008-07-27 21:46:04 +00001894SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1895 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
Dan Gohman475871a2008-07-27 21:46:04 +00001898 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001899}
1900
Chris Lattner35e5c142006-05-05 05:51:50 +00001901/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1902/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001903SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1904 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001905 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001906 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001907
Dan Gohmanff00a552010-01-14 03:08:49 +00001908 // Bail early if none of these transforms apply.
1909 if (N0.getNode()->getNumOperands() == 0) return SDValue();
1910
Chris Lattner540121f2006-05-05 06:31:05 +00001911 // For each of OP in AND/OR/XOR:
1912 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1913 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1914 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Evan Chengd40d03e2010-01-06 19:38:29 +00001915 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Nate Begeman93e0ed32009-12-03 07:11:29 +00001916 //
1917 // do not sink logical op inside of a vector extend, since it may combine
1918 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00001919 EVT Op0VT = N0.getOperand(0).getValueType();
1920 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00001921 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00001922 // Avoid infinite looping with PromoteIntBinOp.
1923 (N0.getOpcode() == ISD::ANY_EXTEND &&
1924 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Evan Chengd40d03e2010-01-06 19:38:29 +00001925 (N0.getOpcode() == ISD::TRUNCATE && TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00001926 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00001927 Op0VT == N1.getOperand(0).getValueType() &&
1928 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001929 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1930 N0.getOperand(0).getValueType(),
1931 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001932 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001933 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001934 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001935
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001936 // For each of OP in SHL/SRL/SRA/AND...
1937 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1938 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1939 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001940 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001941 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001942 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00001943 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1944 N0.getOperand(0).getValueType(),
1945 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001946 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001947 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1948 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00001949 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001950
Dan Gohman475871a2008-07-27 21:46:04 +00001951 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001952}
1953
Dan Gohman475871a2008-07-27 21:46:04 +00001954SDValue DAGCombiner::visitAND(SDNode *N) {
1955 SDValue N0 = N->getOperand(0);
1956 SDValue N1 = N->getOperand(1);
1957 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001960 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00001961 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001962
Dan Gohman7f321562007-06-25 16:23:39 +00001963 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001964 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001966 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001967 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001968
Dan Gohman613e0d82007-07-03 14:03:57 +00001969 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001970 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001971 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001972 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001973 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001974 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001975 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001976 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00001977 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001978 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001979 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001980 return N0;
1981 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001982 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001983 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001984 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001985 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00001986 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001987 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001988 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00001989 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00001990 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001991 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001992 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001993 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001994 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1995 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001997 APInt Mask = ~N1C->getAPIntValue();
1998 Mask.trunc(N0Op0.getValueSizeInBits());
1999 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002000 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2001 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Chris Lattner1ec05d12006-03-01 21:47:21 +00002003 // Replace uses of the AND with uses of the Zero extend node.
2004 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Chris Lattner3603cd62006-02-02 07:17:31 +00002006 // We actually want to replace all uses of the any_extend with the
2007 // zero_extend, to avoid duplicating things. This will later cause this
2008 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002009 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002010 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002011 }
2012 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002013 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2014 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2015 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2016 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002018 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002019 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002020 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002021 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002022 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2023 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002024 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002025 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002026 }
Bill Wendling2627a882009-01-30 20:43:18 +00002027 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002028 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002029 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2030 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002031 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002032 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002033 }
Bill Wendling2627a882009-01-30 20:43:18 +00002034 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002035 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002036 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2037 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002038 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002039 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002040 }
2041 }
2042 // canonicalize equivalent to ll == rl
2043 if (LL == RR && LR == RL) {
2044 Op1 = ISD::getSetCCSwappedOperands(Op1);
2045 std::swap(RL, RR);
2046 }
2047 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002048 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002049 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002050 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002051 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002052 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2053 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002054 }
2055 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002056
Bill Wendling2627a882009-01-30 20:43:18 +00002057 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002058 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002060 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002061 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002062
Nate Begemande996292006-02-03 22:24:05 +00002063 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2064 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002065 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002066 SimplifyDemandedBits(SDValue(N, 0)))
2067 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002068
Nate Begemanded49632005-10-13 03:11:28 +00002069 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002071 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002072 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002073 // If we zero all the possible extended bits, then we can turn this into
2074 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002075 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002076 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002077 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002078 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002079 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002080 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2081 LN0->getChain(), LN0->getBasePtr(),
2082 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002083 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002084 LN0->isVolatile(), LN0->isNonTemporal(),
2085 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002086 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002087 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002088 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002089 }
2090 }
2091 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002092 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002093 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002094 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002095 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002096 // If we zero all the possible extended bits, then we can turn this into
2097 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002098 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002099 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002100 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002101 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002102 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00002103 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
2104 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002105 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00002106 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002107 LN0->isVolatile(), LN0->isNonTemporal(),
2108 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002109 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002110 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002111 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002112 }
2113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002115 // fold (and (load x), 255) -> (zextload x, i8)
2116 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002117 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2118 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2119 (N0.getOpcode() == ISD::ANY_EXTEND &&
2120 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2121 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2122 LoadSDNode *LN0 = HasAnyExt
2123 ? cast<LoadSDNode>(N0.getOperand(0))
2124 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002125 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002126 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002127 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002128 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2129 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2130 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002131
Evan Chengd40d03e2010-01-06 19:38:29 +00002132 if (ExtVT == LoadedVT &&
2133 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002134 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2135
2136 SDValue NewLoad =
2137 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2138 LN0->getChain(), LN0->getBasePtr(),
2139 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002140 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2141 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002142 AddToWorkList(N);
2143 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2144 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2145 }
2146
2147 // Do not change the width of a volatile load.
2148 // Do not generate loads of non-round integer types since these can
2149 // be expensive (and would be wrong if the type is not byte sized).
2150 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2151 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2152 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002153
Chris Lattneref7634c2010-01-07 21:53:27 +00002154 unsigned Alignment = LN0->getAlignment();
2155 SDValue NewPtr = LN0->getBasePtr();
2156
2157 // For big endian targets, we need to add an offset to the pointer
2158 // to load the correct bytes. For little endian systems, we merely
2159 // need to read fewer bytes from the same pointer.
2160 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002161 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2162 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2163 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002164 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2165 NewPtr, DAG.getConstant(PtrOff, PtrType));
2166 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002167 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002168
2169 AddToWorkList(NewPtr.getNode());
2170
2171 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2172 SDValue Load =
2173 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
2174 LN0->getChain(), NewPtr,
2175 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00002176 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2177 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002178 AddToWorkList(N);
2179 CombineTo(LN0, Load, Load.getValue(1));
2180 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002181 }
Evan Cheng466685d2006-10-09 20:57:25 +00002182 }
Chris Lattner15045b62006-02-28 06:35:35 +00002183 }
2184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002185
Evan Cheng64b7bf72010-04-16 06:14:10 +00002186 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002187}
2188
Dan Gohman475871a2008-07-27 21:46:04 +00002189SDValue DAGCombiner::visitOR(SDNode *N) {
2190 SDValue N0 = N->getOperand(0);
2191 SDValue N1 = N->getOperand(1);
2192 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002193 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002196
Dan Gohman7f321562007-06-25 16:23:39 +00002197 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002198 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002199 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002200 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002202
Dan Gohman613e0d82007-07-03 14:03:57 +00002203 // fold (or x, undef) -> -1
Nate Begeman93e0ed32009-12-03 07:11:29 +00002204 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) {
2205 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
2206 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
2207 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002208 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002209 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002210 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002211 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002212 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00002213 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002214 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002215 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002216 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002217 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002218 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002219 return N1;
2220 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002221 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002222 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002223 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00002224 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002225 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002226 return ROR;
2227 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002228 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00002229 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00002230 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00002231 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00002232 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002233 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2234 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2235 N0.getOperand(0), N1),
2236 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00002237 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002238 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2239 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2240 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2241 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002242
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002243 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00002245 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2246 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002247 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002248 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002249 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2250 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002251 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002252 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002253 }
Bill Wendling09025642009-01-30 20:59:34 +00002254 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2255 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00002256 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002257 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002258 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2259 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002260 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002261 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002262 }
2263 }
2264 // canonicalize equivalent to ll == rl
2265 if (LL == RR && LR == RL) {
2266 Op1 = ISD::getSetCCSwappedOperands(Op1);
2267 std::swap(RL, RR);
2268 }
2269 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002270 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002271 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002272 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002273 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002274 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2275 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002276 }
2277 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Bill Wendling09025642009-01-30 20:59:34 +00002279 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002280 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002281 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002284
Bill Wendling09025642009-01-30 20:59:34 +00002285 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002286 if (N0.getOpcode() == ISD::AND &&
2287 N1.getOpcode() == ISD::AND &&
2288 N0.getOperand(1).getOpcode() == ISD::Constant &&
2289 N1.getOperand(1).getOpcode() == ISD::Constant &&
2290 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002291 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002292 // We can only do this xform if we know that bits from X that are set in C2
2293 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002294 const APInt &LHSMask =
2295 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2296 const APInt &RHSMask =
2297 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002298
Dan Gohmanea859be2007-06-22 14:59:07 +00002299 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2300 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002301 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2302 N0.getOperand(0), N1.getOperand(0));
2303 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2304 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002305 }
2306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002307
Chris Lattner516b9622006-09-14 20:50:57 +00002308 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002309 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002310 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002311
Evan Cheng64b7bf72010-04-16 06:14:10 +00002312 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313}
2314
Chris Lattner516b9622006-09-14 20:50:57 +00002315/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002316static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002317 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002318 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002319 Mask = Op.getOperand(1);
2320 Op = Op.getOperand(0);
2321 } else {
2322 return false;
2323 }
2324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Chris Lattner516b9622006-09-14 20:50:57 +00002326 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2327 Shift = Op;
2328 return true;
2329 }
Bill Wendling09025642009-01-30 20:59:34 +00002330
Scott Michelfdc40a02009-02-17 22:15:04 +00002331 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002332}
2333
Chris Lattner516b9622006-09-14 20:50:57 +00002334// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2335// idioms for rotate, and if the target supports rotation instructions, generate
2336// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002337SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002338 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002339 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002340 if (!TLI.isTypeLegal(VT)) return 0;
2341
2342 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002343 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2344 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002345 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002346
Chris Lattner516b9622006-09-14 20:50:57 +00002347 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue LHSShift; // The shift.
2349 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002350 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2351 return 0; // Not part of a rotate.
2352
Dan Gohman475871a2008-07-27 21:46:04 +00002353 SDValue RHSShift; // The shift.
2354 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002355 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2356 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002357
Chris Lattner516b9622006-09-14 20:50:57 +00002358 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2359 return 0; // Not shifting the same value.
2360
2361 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2362 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002363
Chris Lattner516b9622006-09-14 20:50:57 +00002364 // Canonicalize shl to left side in a shl/srl pair.
2365 if (RHSShift.getOpcode() == ISD::SHL) {
2366 std::swap(LHS, RHS);
2367 std::swap(LHSShift, RHSShift);
2368 std::swap(LHSMask , RHSMask );
2369 }
2370
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SDValue LHSShiftArg = LHSShift.getOperand(0);
2373 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2374 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002375
2376 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2377 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002378 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2379 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002380 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2381 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002382 if ((LShVal + RShVal) != OpSizeInBits)
2383 return 0;
2384
Dan Gohman475871a2008-07-27 21:46:04 +00002385 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002386 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002387 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002388 else
Bill Wendling317bd702009-01-30 21:14:50 +00002389 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002390
Chris Lattner516b9622006-09-14 20:50:57 +00002391 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002392 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002393 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002394
Gabor Greifba36cb52008-08-28 21:40:38 +00002395 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002396 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2397 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002398 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002399 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002400 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2401 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002403
Bill Wendling317bd702009-01-30 21:14:50 +00002404 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Gabor Greifba36cb52008-08-28 21:40:38 +00002407 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002409
Chris Lattner516b9622006-09-14 20:50:57 +00002410 // If there is a mask here, and we have a variable shift, we can't be sure
2411 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002413 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002414
Chris Lattner516b9622006-09-14 20:50:57 +00002415 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2416 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002417 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2418 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002419 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002420 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002421 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002422 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002423 return DAG.getNode(ISD::ROTL, DL, VT,
2424 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002425 else
Bill Wendling317bd702009-01-30 21:14:50 +00002426 return DAG.getNode(ISD::ROTR, DL, VT,
2427 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002428 }
Chris Lattner516b9622006-09-14 20:50:57 +00002429 }
2430 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002431
Chris Lattner516b9622006-09-14 20:50:57 +00002432 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2433 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002434 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2435 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002436 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002437 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002438 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002439 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002440 return DAG.getNode(ISD::ROTR, DL, VT,
2441 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002442 else
Bill Wendling317bd702009-01-30 21:14:50 +00002443 return DAG.getNode(ISD::ROTL, DL, VT,
2444 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002445 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002446 }
2447 }
2448
Dan Gohman74feef22008-10-17 01:23:35 +00002449 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002450 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2451 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002452 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2453 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002454 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2455 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002456 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2457 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002458 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2459 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002460 if (RExtOp0.getOpcode() == ISD::SUB &&
2461 RExtOp0.getOperand(1) == LExtOp0) {
2462 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002463 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002464 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002465 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002466 if (ConstantSDNode *SUBC =
2467 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002468 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002469 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2470 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002471 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002472 }
2473 }
2474 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2475 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002476 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002477 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002478 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002479 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002480 if (ConstantSDNode *SUBC =
2481 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002482 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002483 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2484 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002485 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002486 }
2487 }
Chris Lattner516b9622006-09-14 20:50:57 +00002488 }
2489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002490
Chris Lattner516b9622006-09-14 20:50:57 +00002491 return 0;
2492}
2493
Dan Gohman475871a2008-07-27 21:46:04 +00002494SDValue DAGCombiner::visitXOR(SDNode *N) {
2495 SDValue N0 = N->getOperand(0);
2496 SDValue N1 = N->getOperand(1);
2497 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002498 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2499 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002500 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002501
Dan Gohman7f321562007-06-25 16:23:39 +00002502 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002503 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002505 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002506 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002507
Evan Cheng26471c42008-03-25 20:08:07 +00002508 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2509 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2510 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002511 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002512 if (N0.getOpcode() == ISD::UNDEF)
2513 return N0;
2514 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002515 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002516 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002517 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002518 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002519 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002520 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002521 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002522 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002523 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002525 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002526 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002527 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002528 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002529
Nate Begeman1d4d4142005-09-01 00:19:25 +00002530 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002531 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002532 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002533 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2534 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002535
Duncan Sands25cf2272008-11-24 14:53:14 +00002536 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002537 switch (N0.getOpcode()) {
2538 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002539 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00002540 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002541 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002542 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002543 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002544 N0.getOperand(3), NotCC);
2545 }
2546 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002547 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002548
Chris Lattner61c5ff42007-09-10 21:39:07 +00002549 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002550 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002551 N0.getNode()->hasOneUse() &&
2552 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002553 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002554 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002555 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002556 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002557 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002559
Bill Wendling317bd702009-01-30 21:14:50 +00002560 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00002561 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002562 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002563 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002564 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2565 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002566 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2567 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002568 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002569 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002570 }
2571 }
Bill Wendling317bd702009-01-30 21:14:50 +00002572 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00002573 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00002574 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002575 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002576 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2577 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00002578 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2579 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002580 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002581 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002582 }
2583 }
Bill Wendling317bd702009-01-30 21:14:50 +00002584 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00002585 if (N1C && N0.getOpcode() == ISD::XOR) {
2586 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2587 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2588 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00002589 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2590 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002591 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002592 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002593 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2594 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002595 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002596 }
2597 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002598 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002599 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002600 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002601 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002602 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002603 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2604 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Chenga87008d2009-02-25 22:49:59 +00002605 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2606 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002607 }
2608 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002609
Chris Lattner35e5c142006-05-05 05:51:50 +00002610 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2611 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002612 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002613 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002615
Chris Lattner3e104b12006-04-08 04:15:24 +00002616 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002617 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002618 SimplifyDemandedBits(SDValue(N, 0)))
2619 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002620
Evan Cheng64b7bf72010-04-16 06:14:10 +00002621 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002622}
2623
Chris Lattnere70da202007-12-06 07:33:36 +00002624/// visitShiftByConstant - Handle transforms common to the three shifts, when
2625/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002626SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002627 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002628 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002629
Chris Lattnere70da202007-12-06 07:33:36 +00002630 // We want to pull some binops through shifts, so that we have (and (shift))
2631 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2632 // thing happens with address calculations, so it's important to canonicalize
2633 // it.
2634 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00002635
Chris Lattnere70da202007-12-06 07:33:36 +00002636 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002637 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002638 case ISD::OR:
2639 case ISD::XOR:
2640 HighBitSet = false; // We can only transform sra if the high bit is clear.
2641 break;
2642 case ISD::AND:
2643 HighBitSet = true; // We can only transform sra if the high bit is set.
2644 break;
2645 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00002646 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002647 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002648 HighBitSet = false; // We can only transform sra if the high bit is clear.
2649 break;
2650 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002651
Chris Lattnere70da202007-12-06 07:33:36 +00002652 // We require the RHS of the binop to be a constant as well.
2653 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002654 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002655
2656 // FIXME: disable this unless the input to the binop is a shift by a constant.
2657 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002658 //
Bill Wendling88103372009-01-30 21:37:17 +00002659 // void foo(int *X, int i) { X[i & 1235] = 1; }
2660 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002662 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002663 BinOpLHSVal->getOpcode() != ISD::SRA &&
2664 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2665 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002666 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002667
Owen Andersone50ed302009-08-10 22:56:29 +00002668 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002669
Bill Wendling88103372009-01-30 21:37:17 +00002670 // If this is a signed shift right, and the high bit is modified by the
2671 // logical operation, do not perform the transformation. The highBitSet
2672 // boolean indicates the value of the high bit of the constant which would
2673 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002674 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002675 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2676 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002677 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002678 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002679
Chris Lattnere70da202007-12-06 07:33:36 +00002680 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002681 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2682 N->getValueType(0),
2683 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002684
2685 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002686 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2687 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002688
2689 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002690 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002691}
2692
Dan Gohman475871a2008-07-27 21:46:04 +00002693SDValue DAGCombiner::visitSHL(SDNode *N) {
2694 SDValue N0 = N->getOperand(0);
2695 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002696 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2697 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002698 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002699 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002700
Nate Begeman1d4d4142005-09-01 00:19:25 +00002701 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002702 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002703 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002704 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002705 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002706 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002707 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002708 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002709 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002710 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002711 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002712 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002713 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002714 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00002715 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002716 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002717 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002718 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002719 N1.getOperand(0).getOpcode() == ISD::AND &&
2720 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002721 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002722 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002723 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002724 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002725 APInt TruncC = N101C->getAPIntValue();
2726 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002727 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002728 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2729 DAG.getNode(ISD::TRUNCATE,
2730 N->getDebugLoc(),
2731 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002732 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002733 }
2734 }
2735
Dan Gohman475871a2008-07-27 21:46:04 +00002736 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2737 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002738
2739 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002740 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002741 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002742 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2743 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002744 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002745 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002746 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002747 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002748 }
Bill Wendling88103372009-01-30 21:37:17 +00002749 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2750 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002751 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002752 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002753 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00002754 if (c1 < VT.getSizeInBits()) {
2755 uint64_t c2 = N1C->getZExtValue();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002756 SDValue HiBitsMask =
2757 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2758 VT.getSizeInBits() - c1),
2759 VT);
Evan Chengd101a722009-07-21 05:40:15 +00002760 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2761 N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002762 HiBitsMask);
Evan Chengd101a722009-07-21 05:40:15 +00002763 if (c2 > c1)
2764 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2765 DAG.getConstant(c2-c1, N1.getValueType()));
2766 else
2767 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2768 DAG.getConstant(c1-c2, N1.getValueType()));
2769 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002770 }
Bill Wendling88103372009-01-30 21:37:17 +00002771 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002772 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2773 SDValue HiBitsMask =
2774 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2775 VT.getSizeInBits() -
2776 N1C->getZExtValue()),
2777 VT);
Bill Wendling88103372009-01-30 21:37:17 +00002778 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00002779 HiBitsMask);
2780 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002781
Evan Chenge5b51ac2010-04-17 06:13:15 +00002782 if (N1C) {
2783 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
2784 if (NewSHL.getNode())
2785 return NewSHL;
2786 }
2787
2788 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002789}
2790
Dan Gohman475871a2008-07-27 21:46:04 +00002791SDValue DAGCombiner::visitSRA(SDNode *N) {
2792 SDValue N0 = N->getOperand(0);
2793 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002796 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002797 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002798
Bill Wendling88103372009-01-30 21:37:17 +00002799 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002800 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002801 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002802 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002803 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002804 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002805 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002806 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002807 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002808 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00002809 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002810 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002811 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002812 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002813 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002814 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2815 // sext_inreg.
2816 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00002817 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002818 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
2819 if (VT.isVector())
2820 ExtVT = EVT::getVectorVT(*DAG.getContext(),
2821 ExtVT, VT.getVectorNumElements());
2822 if ((!LegalOperations ||
2823 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002824 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00002825 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002826 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002827
Bill Wendling88103372009-01-30 21:37:17 +00002828 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002829 if (N1C && N0.getOpcode() == ISD::SRA) {
2830 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002831 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002832 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00002833 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002834 DAG.getConstant(Sum, N1C->getValueType(0)));
2835 }
2836 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002837
Bill Wendling88103372009-01-30 21:37:17 +00002838 // fold (sra (shl X, m), (sub result_size, n))
2839 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00002840 // result_size - n != m.
2841 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00002842 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002843 if (N0.getOpcode() == ISD::SHL) {
2844 // Get the two constanst of the shifts, CN0 = m, CN = n.
2845 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2846 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002847 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00002848 EVT TruncVT =
Dan Gohman87862e72009-12-11 21:31:27 +00002849 EVT::getIntegerVT(*DAG.getContext(), OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002850 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00002851 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002852
Scott Michelfdc40a02009-02-17 22:15:04 +00002853 // If the shift is not a no-op (in which case this should be just a sign
2854 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00002855 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002856 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00002857 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002858 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2859 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002860 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002861
Duncan Sands92abc622009-01-31 15:50:11 +00002862 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002863 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2864 N0.getOperand(0), Amt);
2865 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2866 Shift);
2867 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2868 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002869 }
2870 }
2871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002872
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002873 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00002874 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002875 N1.getOperand(0).getOpcode() == ISD::AND &&
2876 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002877 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002878 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00002879 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002880 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002881 APInt TruncC = N101C->getAPIntValue();
Dan Gohman87862e72009-12-11 21:31:27 +00002882 TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00002883 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002884 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00002885 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00002886 DAG.getNode(ISD::TRUNCATE,
2887 N->getDebugLoc(),
2888 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002889 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002890 }
2891 }
2892
Scott Michelfdc40a02009-02-17 22:15:04 +00002893 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002894 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2895 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002896
2897
Nate Begeman1d4d4142005-09-01 00:19:25 +00002898 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002899 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002900 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002901
Evan Chenge5b51ac2010-04-17 06:13:15 +00002902 if (N1C) {
2903 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
2904 if (NewSRA.getNode())
2905 return NewSRA;
2906 }
2907
2908 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002909}
2910
Dan Gohman475871a2008-07-27 21:46:04 +00002911SDValue DAGCombiner::visitSRL(SDNode *N) {
2912 SDValue N0 = N->getOperand(0);
2913 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002914 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2915 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002916 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002917 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002918
Nate Begeman1d4d4142005-09-01 00:19:25 +00002919 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002920 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002921 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002922 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002923 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002924 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002925 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002926 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00002927 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002928 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002929 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002930 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002931 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002932 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002933 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002934 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002935
Bill Wendling88103372009-01-30 21:37:17 +00002936 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00002937 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002938 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002939 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2940 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002941 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002942 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002943 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002944 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002945 }
Chris Lattnerefcddc32010-04-15 05:28:43 +00002946
2947 // fold (srl (shl x, c), c) -> (and x, cst2)
2948 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
2949 N0.getValueSizeInBits() <= 64) {
2950 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
2951 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
2952 DAG.getConstant(~0ULL >> ShAmt, VT));
2953 }
2954
Scott Michelfdc40a02009-02-17 22:15:04 +00002955
Chris Lattner06afe072006-05-05 22:53:17 +00002956 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2957 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2958 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00002959 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002960 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00002961 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00002962
Evan Chenge5b51ac2010-04-17 06:13:15 +00002963 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
2964 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2965 N0.getOperand(0), N1);
2966 AddToWorkList(SmallShift.getNode());
2967 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
2968 }
Chris Lattner06afe072006-05-05 22:53:17 +00002969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002970
Chris Lattner3657ffe2006-10-12 20:23:19 +00002971 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2972 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00002973 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002974 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00002975 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00002976 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002977
Chris Lattner350bec02006-04-02 06:11:11 +00002978 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00002979 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002980 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002981 APInt KnownZero, KnownOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00002982 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002983 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00002984
Chris Lattner350bec02006-04-02 06:11:11 +00002985 // If any of the input bits are KnownOne, then the input couldn't be all
2986 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002987 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002988
Chris Lattner350bec02006-04-02 06:11:11 +00002989 // If all of the bits input the to ctlz node are known to be zero, then
2990 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002991 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002992 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002993
Chris Lattner350bec02006-04-02 06:11:11 +00002994 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00002995 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00002996 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00002997 // could be set on input to the CTLZ node. If this bit is set, the SRL
2998 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2999 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003000 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003001 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003002
Chris Lattner350bec02006-04-02 06:11:11 +00003003 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003004 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands92abc622009-01-31 15:50:11 +00003005 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003006 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003007 }
Bill Wendling88103372009-01-30 21:37:17 +00003008
3009 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3010 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003011 }
3012 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003013
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003014 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003015 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003016 N1.getOperand(0).getOpcode() == ISD::AND &&
3017 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003018 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003019 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003020 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003021 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003022 APInt TruncC = N101C->getAPIntValue();
3023 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003024 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003025 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003026 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003027 DAG.getNode(ISD::TRUNCATE,
3028 N->getDebugLoc(),
3029 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003030 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003031 }
3032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003033
Chris Lattner61a4c072007-04-18 03:06:49 +00003034 // fold operands of srl based on knowledge that the low bits are not
3035 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003036 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3037 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003038
Evan Cheng9ab2b982009-12-18 21:31:31 +00003039 if (N1C) {
3040 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3041 if (NewSRL.getNode())
3042 return NewSRL;
3043 }
3044
3045 // Here is a common situation. We want to optimize:
3046 //
3047 // %a = ...
3048 // %b = and i32 %a, 2
3049 // %c = srl i32 %b, 1
3050 // brcond i32 %c ...
3051 //
3052 // into
3053 //
3054 // %a = ...
3055 // %b = and %a, 2
3056 // %c = setcc eq %b, 0
3057 // brcond %c ...
3058 //
3059 // However when after the source operand of SRL is optimized into AND, the SRL
3060 // itself may not be optimized further. Look for it and add the BRCOND into
3061 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003062 if (N->hasOneUse()) {
3063 SDNode *Use = *N->use_begin();
3064 if (Use->getOpcode() == ISD::BRCOND)
3065 AddToWorkList(Use);
3066 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3067 // Also look pass the truncate.
3068 Use = *Use->use_begin();
3069 if (Use->getOpcode() == ISD::BRCOND)
3070 AddToWorkList(Use);
3071 }
3072 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003073
Evan Chenge5b51ac2010-04-17 06:13:15 +00003074 return PromoteIntBinOp(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003075}
3076
Evan Cheng4c26e932010-04-19 19:29:22 +00003077SDValue DAGCombiner::visitROTL(SDNode *N) {
3078 return PromoteIntBinOp(SDValue(N, 0));
3079}
3080
3081SDValue DAGCombiner::visitROTR(SDNode *N) {
3082 return PromoteIntBinOp(SDValue(N, 0));
3083}
3084
Dan Gohman475871a2008-07-27 21:46:04 +00003085SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3086 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003087 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003088
3089 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003090 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003091 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003092 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003093}
3094
Dan Gohman475871a2008-07-27 21:46:04 +00003095SDValue DAGCombiner::visitCTTZ(SDNode *N) {
3096 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003097 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003098
Nate Begeman1d4d4142005-09-01 00:19:25 +00003099 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003100 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003101 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003102 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003103}
3104
Dan Gohman475871a2008-07-27 21:46:04 +00003105SDValue DAGCombiner::visitCTPOP(SDNode *N) {
3106 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003107 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003108
Nate Begeman1d4d4142005-09-01 00:19:25 +00003109 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003110 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003111 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003112 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003113}
3114
Dan Gohman475871a2008-07-27 21:46:04 +00003115SDValue DAGCombiner::visitSELECT(SDNode *N) {
3116 SDValue N0 = N->getOperand(0);
3117 SDValue N1 = N->getOperand(1);
3118 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003119 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3120 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3121 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00003122 EVT VT = N->getValueType(0);
3123 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00003124
Bill Wendling34584e62009-01-30 22:02:18 +00003125 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003126 if (N1 == N2)
3127 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003128 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00003129 if (N0C && !N0C->isNullValue())
3130 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003131 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00003132 if (N0C && N0C->isNullValue())
3133 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00003134 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00003136 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3137 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00003138 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00003140 (VT0.isInteger() &&
3141 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00003142 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003143 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00003144 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00003145 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
3146 N0, DAG.getConstant(1, VT0));
3147 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
3148 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00003150 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00003151 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
3152 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00003153 }
Bill Wendling34584e62009-01-30 22:02:18 +00003154 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00003156 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003157 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003158 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00003159 }
Bill Wendling34584e62009-01-30 22:02:18 +00003160 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003162 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003163 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003164 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00003165 }
Bill Wendling34584e62009-01-30 22:02:18 +00003166 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003167 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00003168 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
3169 // fold (select X, X, Y) -> (or X, Y)
3170 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003171 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00003172 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3173 // fold (select X, Y, X) -> (and X, Y)
3174 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00003176 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003177
Chris Lattner40c62d52005-10-18 06:04:22 +00003178 // If we can fold this based on the true/false value, do so.
3179 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00003180 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003181
Nate Begeman44728a72005-09-19 22:34:01 +00003182 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003183 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003184 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00003185 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00003186 // having to say they don't support SELECT_CC on every type the DAG knows
3187 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00003188 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00003189 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00003190 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
3191 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00003192 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00003193 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003194 }
Bill Wendling34584e62009-01-30 22:02:18 +00003195
Dan Gohman475871a2008-07-27 21:46:04 +00003196 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00003197}
3198
Dan Gohman475871a2008-07-27 21:46:04 +00003199SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
3200 SDValue N0 = N->getOperand(0);
3201 SDValue N1 = N->getOperand(1);
3202 SDValue N2 = N->getOperand(2);
3203 SDValue N3 = N->getOperand(3);
3204 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00003205 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003206
Nate Begeman44728a72005-09-19 22:34:01 +00003207 // fold select_cc lhs, rhs, x, x, cc -> x
3208 if (N2 == N3)
3209 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003210
Chris Lattner5f42a242006-09-20 06:19:26 +00003211 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00003212 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003213 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00003215
Gabor Greifba36cb52008-08-28 21:40:38 +00003216 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003217 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00003218 return N2; // cond always true -> true val
3219 else
3220 return N3; // cond always false -> false val
3221 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003222
Chris Lattner5f42a242006-09-20 06:19:26 +00003223 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00003224 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00003225 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
3226 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00003227 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003228
Chris Lattner40c62d52005-10-18 06:04:22 +00003229 // If we can fold this based on the true/false value, do so.
3230 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00003231 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00003232
Nate Begeman44728a72005-09-19 22:34:01 +00003233 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00003234 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00003235}
3236
Dan Gohman475871a2008-07-27 21:46:04 +00003237SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003238 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003239 cast<CondCodeSDNode>(N->getOperand(2))->get(),
3240 N->getDebugLoc());
Nate Begeman452d7be2005-09-16 00:54:12 +00003241}
3242
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003243// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00003244// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003245// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00003246// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00003247static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003248 unsigned ExtOpc,
3249 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00003250 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003251 bool HasCopyToRegUses = false;
3252 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00003253 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
3254 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003255 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00003256 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003257 if (User == N)
3258 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00003259 if (UI.getUse().getResNo() != N0.getResNo())
3260 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003261 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00003262 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003263 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
3264 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
3265 // Sign bits will be lost after a zext.
3266 return false;
3267 bool Add = false;
3268 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003270 if (UseOp == N0)
3271 continue;
3272 if (!isa<ConstantSDNode>(UseOp))
3273 return false;
3274 Add = true;
3275 }
3276 if (Add)
3277 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003278 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003279 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00003280 // If truncates aren't free and there are users we can't
3281 // extend, it isn't worthwhile.
3282 if (!isTruncFree)
3283 return false;
3284 // Remember if this value is live-out.
3285 if (User->getOpcode() == ISD::CopyToReg)
3286 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003287 }
3288
3289 if (HasCopyToRegUses) {
3290 bool BothLiveOut = false;
3291 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3292 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003293 SDUse &Use = UI.getUse();
3294 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
3295 BothLiveOut = true;
3296 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003297 }
3298 }
3299 if (BothLiveOut)
3300 // Both unextended and extended values are live out. There had better be
3301 // good a reason for the transformation.
3302 return ExtendNodes.size();
3303 }
3304 return true;
3305}
3306
Dan Gohman475871a2008-07-27 21:46:04 +00003307SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
3308 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003309 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003310
Nate Begeman1d4d4142005-09-01 00:19:25 +00003311 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003312 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003313 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003314
Nate Begeman1d4d4142005-09-01 00:19:25 +00003315 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003316 // fold (sext (aext x)) -> (sext x)
3317 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003318 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
3319 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003320
Chris Lattner22558872007-02-26 03:13:59 +00003321 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003322 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3323 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003324 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3325 if (NarrowLoad.getNode()) {
3326 if (NarrowLoad.getNode() != N0.getNode())
3327 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanc7b34442009-04-27 02:00:55 +00003328 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00003329 }
Evan Chengc88138f2007-03-22 01:54:19 +00003330
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003331 // See if the value being truncated is already sign extended. If so, just
3332 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00003333 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00003334 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
3335 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
3336 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00003337 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00003338
Chris Lattner22558872007-02-26 03:13:59 +00003339 if (OpBits == DestBits) {
3340 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3341 // bits, it is already ready.
3342 if (NumSignBits > DestBits-MidBits)
3343 return Op;
3344 } else if (OpBits < DestBits) {
3345 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3346 // bits, just sext from i32.
3347 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003348 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003349 } else {
3350 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3351 // bits, just truncate to i32.
3352 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003353 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003354 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003355
Chris Lattner22558872007-02-26 03:13:59 +00003356 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003357 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3358 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00003359 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003360 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00003361 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003362 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3363 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00003364 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00003365 }
Chris Lattner6007b842006-09-21 06:00:20 +00003366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003367
Evan Cheng110dec22005-12-14 02:19:23 +00003368 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003369 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003370 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003371 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003372 bool DoXform = true;
3373 SmallVector<SDNode*, 4> SetCCs;
3374 if (!N0.hasOneUse())
3375 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3376 if (DoXform) {
3377 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman57fc82d2009-04-09 03:51:29 +00003378 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3379 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003380 LN0->getBasePtr(), LN0->getSrcValue(),
3381 LN0->getSrcValueOffset(),
3382 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003383 LN0->isVolatile(), LN0->isNonTemporal(),
3384 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003385 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003386 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3387 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003388 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003389
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003390 // Extend SetCC uses if necessary.
3391 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3392 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003393 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003394
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003395 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003396 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003397 if (SOp == Trunc)
3398 Ops.push_back(ExtLoad);
3399 else
Dan Gohman57fc82d2009-04-09 03:51:29 +00003400 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3401 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003402 }
3403
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003404 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003405 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003406 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003407 &Ops[0], Ops.size()));
3408 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003409
Dan Gohman475871a2008-07-27 21:46:04 +00003410 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003411 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003412 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003413
3414 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3415 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003416 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3417 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003418 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003419 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003420 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003421 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003422 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3423 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003424 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003425 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003426 LN0->isVolatile(), LN0->isNonTemporal(),
3427 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003428 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003429 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003430 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3431 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003432 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003433 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003434 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003436
Chris Lattner20a35c32007-04-11 05:32:27 +00003437 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00003438 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3439 if (VT.isVector() &&
3440 // We know that the # elements of the results is the same as the
3441 // # elements of the compare (and the # elements of the compare result
3442 // for that matter). Check to see that they are the same size. If so,
3443 // we know that the element size of the sext'd result matches the
3444 // element size of the compare operands.
3445 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3446
3447 // Only do this before legalize for now.
3448 !LegalOperations) {
3449 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3450 N0.getOperand(1),
3451 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3452 }
3453
3454 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003455 SDValue NegOne =
3456 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003457 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003458 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003459 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003460 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003461 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00003462 if (!LegalOperations ||
3463 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
3464 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3465 DAG.getSetCC(N->getDebugLoc(),
3466 TLI.getSetCCResultType(VT),
3467 N0.getOperand(0), N0.getOperand(1),
3468 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
3469 NegOne, DAG.getConstant(0, VT));
Chris Lattner20a35c32007-04-11 05:32:27 +00003470 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00003471
3472
Scott Michelfdc40a02009-02-17 22:15:04 +00003473
Dan Gohman8f0ad582008-04-28 16:58:24 +00003474 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003475 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003476 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003477 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003478
Evan Cheng4c26e932010-04-19 19:29:22 +00003479 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003480}
3481
Dan Gohman475871a2008-07-27 21:46:04 +00003482SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3483 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003484 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003485
Nate Begeman1d4d4142005-09-01 00:19:25 +00003486 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003487 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003488 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003489 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003490 // fold (zext (aext x)) -> (zext x)
3491 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003492 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3493 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003494
Evan Chengc88138f2007-03-22 01:54:19 +00003495 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3496 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003497 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003498 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3499 if (NarrowLoad.getNode()) {
3500 if (NarrowLoad.getNode() != N0.getNode())
3501 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003502 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003503 }
Evan Chengc88138f2007-03-22 01:54:19 +00003504 }
3505
Chris Lattner6007b842006-09-21 06:00:20 +00003506 // fold (zext (truncate x)) -> (and x, mask)
3507 if (N0.getOpcode() == ISD::TRUNCATE &&
Evan Chengd40d03e2010-01-06 19:38:29 +00003508 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) &&
3509 (!TLI.isTruncateFree(N0.getOperand(0).getValueType(),
3510 N0.getValueType()) ||
3511 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003512 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003513 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003514 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003515 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003516 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003517 }
Dan Gohman87862e72009-12-11 21:31:27 +00003518 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
3519 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00003520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003521
Dan Gohman97121ba2009-04-08 00:15:30 +00003522 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3523 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00003524 if (N0.getOpcode() == ISD::AND &&
3525 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003526 N0.getOperand(1).getOpcode() == ISD::Constant &&
3527 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3528 N0.getValueType()) ||
3529 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003531 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003532 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003533 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003534 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003535 }
Dan Gohman220a8232008-03-03 23:51:38 +00003536 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003538 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3539 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003541
Evan Cheng110dec22005-12-14 02:19:23 +00003542 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003543 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003544 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003545 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003546 bool DoXform = true;
3547 SmallVector<SDNode*, 4> SetCCs;
3548 if (!N0.hasOneUse())
3549 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3550 if (DoXform) {
3551 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003552 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3553 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003554 LN0->getBasePtr(), LN0->getSrcValue(),
3555 LN0->getSrcValueOffset(),
3556 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003557 LN0->isVolatile(), LN0->isNonTemporal(),
3558 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003559 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003560 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3561 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003562 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003563
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003564 // Extend SetCC uses if necessary.
3565 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3566 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003567 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003568
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003569 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003571 if (SOp == Trunc)
3572 Ops.push_back(ExtLoad);
3573 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00003574 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3575 N->getDebugLoc(), VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003576 }
3577
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003578 Ops.push_back(SetCC->getOperand(2));
Bill Wendling9729c5a2009-01-31 03:12:48 +00003579 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003580 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003581 &Ops[0], Ops.size()));
3582 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003583
Dan Gohman475871a2008-07-27 21:46:04 +00003584 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003585 }
Evan Cheng110dec22005-12-14 02:19:23 +00003586 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003587
3588 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3589 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003590 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3591 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003592 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003593 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003594 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00003595 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003596 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3597 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003598 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003599 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003600 LN0->isVolatile(), LN0->isNonTemporal(),
3601 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003602 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003603 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003604 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3605 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003606 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003607 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003608 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003609 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003610
Chris Lattner20a35c32007-04-11 05:32:27 +00003611 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3612 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003613 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003614 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00003615 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003616 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003617 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003618 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003619
Evan Cheng9818c042009-12-15 03:00:32 +00003620 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00003621 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00003622 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00003623 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
3624 N0.hasOneUse()) {
Evan Cheng9818c042009-12-15 03:00:32 +00003625 if (N0.getOpcode() == ISD::SHL) {
3626 // If the original shl may be shifting out bits, do not perform this
3627 // transformation.
3628 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3629 unsigned KnownZeroBits = N0.getOperand(0).getValueType().getSizeInBits() -
3630 N0.getOperand(0).getOperand(0).getValueType().getSizeInBits();
3631 if (ShAmt > KnownZeroBits)
3632 return SDValue();
3633 }
Evan Cheng99b653c2009-12-15 00:41:36 +00003634 DebugLoc dl = N->getDebugLoc();
3635 return DAG.getNode(N0.getOpcode(), dl, VT,
3636 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0.getOperand(0)),
Bill Wendling9f7c5c02010-01-05 22:39:10 +00003637 DAG.getNode(ISD::ZERO_EXTEND, dl,
3638 N0.getOperand(1).getValueType(),
3639 N0.getOperand(1)));
Evan Cheng99b653c2009-12-15 00:41:36 +00003640 }
3641
Evan Cheng4c26e932010-04-19 19:29:22 +00003642 return PromoteExtend(SDValue(N, 0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003643}
3644
Dan Gohman475871a2008-07-27 21:46:04 +00003645SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3646 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003647 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003648
Chris Lattner5ffc0662006-05-05 05:58:59 +00003649 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003650 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003651 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003652 // fold (aext (aext x)) -> (aext x)
3653 // fold (aext (zext x)) -> (zext x)
3654 // fold (aext (sext x)) -> (sext x)
3655 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3656 N0.getOpcode() == ISD::ZERO_EXTEND ||
3657 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003658 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003659
Evan Chengc88138f2007-03-22 01:54:19 +00003660 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3661 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3662 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003663 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3664 if (NarrowLoad.getNode()) {
3665 if (NarrowLoad.getNode() != N0.getNode())
3666 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003667 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003668 }
Evan Chengc88138f2007-03-22 01:54:19 +00003669 }
3670
Chris Lattner84750582006-09-20 06:29:17 +00003671 // fold (aext (truncate x))
3672 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003673 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003674 if (TruncOp.getValueType() == VT)
3675 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003676 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003677 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3678 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003679 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003680
Dan Gohman97121ba2009-04-08 00:15:30 +00003681 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3682 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00003683 if (N0.getOpcode() == ISD::AND &&
3684 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00003685 N0.getOperand(1).getOpcode() == ISD::Constant &&
3686 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3687 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003688 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003689 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003690 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003691 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00003692 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003693 }
Dan Gohman220a8232008-03-03 23:51:38 +00003694 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003695 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003696 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3697 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003699
Chris Lattner5ffc0662006-05-05 05:58:59 +00003700 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman57fc82d2009-04-09 03:51:29 +00003701 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003702 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003703 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00003704 bool DoXform = true;
3705 SmallVector<SDNode*, 4> SetCCs;
3706 if (!N0.hasOneUse())
3707 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3708 if (DoXform) {
3709 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3710 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3711 LN0->getChain(),
3712 LN0->getBasePtr(), LN0->getSrcValue(),
3713 LN0->getSrcValueOffset(),
3714 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00003715 LN0->isVolatile(), LN0->isNonTemporal(),
3716 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00003717 CombineTo(N, ExtLoad);
3718 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3719 N0.getValueType(), ExtLoad);
3720 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3721
3722 // Extend SetCC uses if necessary.
3723 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3724 SDNode *SetCC = SetCCs[i];
3725 SmallVector<SDValue, 4> Ops;
3726
3727 for (unsigned j = 0; j != 2; ++j) {
3728 SDValue SOp = SetCC->getOperand(j);
3729 if (SOp == Trunc)
3730 Ops.push_back(ExtLoad);
3731 else
3732 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3733 N->getDebugLoc(), VT, SOp));
3734 }
3735
3736 Ops.push_back(SetCC->getOperand(2));
3737 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3738 SetCC->getValueType(0),
3739 &Ops[0], Ops.size()));
3740 }
3741
3742 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3743 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00003744 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003745
Chris Lattner5ffc0662006-05-05 05:58:59 +00003746 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3747 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3748 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003749 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003750 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003751 N0.hasOneUse()) {
3752 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003753 EVT MemVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003754 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3755 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003756 LN0->getSrcValue(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00003757 LN0->getSrcValueOffset(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003758 LN0->isVolatile(), LN0->isNonTemporal(),
3759 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003760 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003761 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003762 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3763 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003764 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003765 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003766 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003767
Chris Lattner20a35c32007-04-11 05:32:27 +00003768 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3769 if (N0.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003770 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00003771 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003772 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003773 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003774 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003775 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003777
Evan Cheng4c26e932010-04-19 19:29:22 +00003778 return PromoteExtend(SDValue(N, 0));
Chris Lattner5ffc0662006-05-05 05:58:59 +00003779}
3780
Chris Lattner2b4c2792007-10-13 06:35:54 +00003781/// GetDemandedBits - See if the specified operand can be simplified with the
3782/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003783/// simpler operand, otherwise return a null SDValue.
3784SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003785 switch (V.getOpcode()) {
3786 default: break;
3787 case ISD::OR:
3788 case ISD::XOR:
3789 // If the LHS or RHS don't contribute bits to the or, drop them.
3790 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3791 return V.getOperand(1);
3792 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3793 return V.getOperand(0);
3794 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003795 case ISD::SRL:
3796 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003797 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003798 break;
3799 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3800 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003801 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00003802
Dan Gohmancc91d632009-01-03 19:22:06 +00003803 // Watch out for shift count overflow though.
3804 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003805 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003806 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00003807 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003808 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00003809 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00003810 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003811 }
Dan Gohman475871a2008-07-27 21:46:04 +00003812 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003813}
3814
Evan Chengc88138f2007-03-22 01:54:19 +00003815/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3816/// bits and then truncated to a narrower type and where N is a multiple
3817/// of number of bits of the narrower type, transform it to a narrower load
3818/// from address + N / num of bits of new type. If the result is to be
3819/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003820SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003821 unsigned Opc = N->getOpcode();
3822 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003823 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003824 EVT VT = N->getValueType(0);
3825 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003826
Dan Gohman7f8613e2008-08-14 20:04:46 +00003827 // This transformation isn't valid for vector loads.
3828 if (VT.isVector())
3829 return SDValue();
3830
Dan Gohmand1996362010-01-09 02:13:55 +00003831 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00003832 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003833 if (Opc == ISD::SIGN_EXTEND_INREG) {
3834 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00003835 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3836 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003837 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003838 }
3839
Owen Andersone50ed302009-08-10 22:56:29 +00003840 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003841 unsigned ShAmt = 0;
Eli Friedman5aba5c02009-08-23 00:14:19 +00003842 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003843 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003844 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003845 // Is the shift amount a multiple of size of VT?
3846 if ((ShAmt & (EVTBits-1)) == 0) {
3847 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00003848 // Is the load width a multiple of size of VT?
3849 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00003850 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003851 }
3852 }
3853 }
3854
Duncan Sandsad205a72008-06-16 08:14:38 +00003855 // Do not generate loads of non-round integer types since these can
3856 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersone50ed302009-08-10 22:56:29 +00003857 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Chris Lattner6dc86852010-04-15 05:40:59 +00003858 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() >= EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003859 // Do not change the width of a volatile load.
3860 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003861 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00003862 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00003863
Evan Chengdae54ce2007-03-24 00:02:43 +00003864 // For big endian targets, we need to adjust the offset to the pointer to
3865 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003866 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003867 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +00003868 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003869 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3870 }
Bill Wendling8509c902009-01-30 22:33:24 +00003871
Evan Chengdae54ce2007-03-24 00:02:43 +00003872 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003873 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendling9729c5a2009-01-31 03:12:48 +00003874 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling8509c902009-01-30 22:33:24 +00003875 PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003876 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003877 AddToWorkList(NewPtr.getNode());
Bill Wendling8509c902009-01-30 22:33:24 +00003878
Dan Gohman475871a2008-07-27 21:46:04 +00003879 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling8509c902009-01-30 22:33:24 +00003880 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003881 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003882 LN0->isVolatile(), LN0->isNonTemporal(), NewAlign)
Bill Wendling8509c902009-01-30 22:33:24 +00003883 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003884 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene1e559442010-02-15 17:00:31 +00003885 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
3886 NewAlign);
Bill Wendling8509c902009-01-30 22:33:24 +00003887
Dan Gohman764fd0c2009-01-21 15:17:51 +00003888 // Replace the old load's chain with the new load's chain.
3889 WorkListRemover DeadNodes(*this);
3890 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3891 &DeadNodes);
Bill Wendling8509c902009-01-30 22:33:24 +00003892
Dan Gohman764fd0c2009-01-21 15:17:51 +00003893 // Return the new loaded value.
3894 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003895 }
3896
Dan Gohman475871a2008-07-27 21:46:04 +00003897 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003898}
3899
Dan Gohman475871a2008-07-27 21:46:04 +00003900SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3901 SDValue N0 = N->getOperand(0);
3902 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00003903 EVT VT = N->getValueType(0);
3904 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003905 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00003906 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Nate Begeman1d4d4142005-09-01 00:19:25 +00003908 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003909 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00003910 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003911
Chris Lattner541a24f2006-05-06 22:43:44 +00003912 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00003913 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003914 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003915
Nate Begeman646d7e22005-09-02 21:18:40 +00003916 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3917 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003918 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00003919 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3920 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003921 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003922
Dan Gohman75dcf082008-07-31 00:50:31 +00003923 // fold (sext_in_reg (sext x)) -> (sext x)
3924 // fold (sext_in_reg (aext x)) -> (sext x)
3925 // if x is small enough.
3926 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3927 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00003928 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
3929 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00003930 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00003931 }
3932
Chris Lattner95a5e052007-04-17 19:03:21 +00003933 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003934 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003935 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003936
Chris Lattner95a5e052007-04-17 19:03:21 +00003937 // fold operands of sext_in_reg based on knowledge that the top bits are not
3938 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003939 if (SimplifyDemandedBits(SDValue(N, 0)))
3940 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003941
Evan Chengc88138f2007-03-22 01:54:19 +00003942 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3943 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003945 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003946 return NarrowLoad;
3947
Bill Wendling8509c902009-01-30 22:33:24 +00003948 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
3949 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00003950 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3951 if (N0.getOpcode() == ISD::SRL) {
3952 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00003953 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003954 // We can turn this into an SRA iff the input to the SRL is already sign
3955 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003956 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00003957 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00003958 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
3959 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00003960 }
3961 }
Evan Chengc88138f2007-03-22 01:54:19 +00003962
Nate Begemanded49632005-10-13 03:11:28 +00003963 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00003964 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003965 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003966 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003967 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003968 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003969 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003970 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3971 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003972 LN0->getBasePtr(), LN0->getSrcValue(),
3973 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00003974 LN0->isVolatile(), LN0->isNonTemporal(),
3975 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003976 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003977 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003978 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003979 }
3980 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003981 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003982 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003983 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003984 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003985 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003986 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling8509c902009-01-30 22:33:24 +00003987 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3988 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003989 LN0->getBasePtr(), LN0->getSrcValue(),
3990 LN0->getSrcValueOffset(), EVT,
David Greene1e559442010-02-15 17:00:31 +00003991 LN0->isVolatile(), LN0->isNonTemporal(),
3992 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003993 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003994 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003996 }
Dan Gohman475871a2008-07-27 21:46:04 +00003997 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003998}
3999
Dan Gohman475871a2008-07-27 21:46:04 +00004000SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
4001 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004002 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004003
4004 // noop truncate
4005 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004006 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004007 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004008 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00004009 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004010 // fold (truncate (truncate x)) -> (truncate x)
4011 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00004012 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004013 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00004014 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
4015 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00004016 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004017 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004018 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00004019 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4020 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004021 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004022 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00004023 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004024 else
4025 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00004026 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00004027 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004028 }
Evan Cheng007b69e2007-03-21 20:14:05 +00004029
Chris Lattner2b4c2792007-10-13 06:35:54 +00004030 // See if we can simplify the input to this truncate through knowledge that
4031 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
4032 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00004033 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004034 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004035 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004036 if (Shorter.getNode())
Bill Wendling67a67682009-01-30 22:44:24 +00004037 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004038
Nate Begeman3df4d522005-10-12 20:40:40 +00004039 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00004040 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chenge5b51ac2010-04-17 06:13:15 +00004041 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT))
4042 return ReduceLoadWidth(N);
4043 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004044}
4045
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004046static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004047 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004048 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00004049 return Elt.getNode();
4050 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004051}
4052
4053/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00004054/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00004055SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004056 assert(N->getOpcode() == ISD::BUILD_PAIR);
4057
Nate Begemanabc01992009-06-05 21:37:30 +00004058 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
4059 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
4060 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004061 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00004062 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00004063
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004064 if (ISD::isNON_EXTLoad(LD2) &&
4065 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004066 // If both are volatile this would reduce the number of volatile loads.
4067 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00004068 !LD1->isVolatile() &&
4069 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00004070 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00004071 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00004072 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004073 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00004074
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004075 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004076 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00004077 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
4078 LD1->getBasePtr(), LD1->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00004079 LD1->getSrcValueOffset(), false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004080 }
Bill Wendling67a67682009-01-30 22:44:24 +00004081
Dan Gohman475871a2008-07-27 21:46:04 +00004082 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004083}
4084
Dan Gohman475871a2008-07-27 21:46:04 +00004085SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
4086 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004087 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00004088
Dan Gohman7f321562007-06-25 16:23:39 +00004089 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
4090 // Only do this before legalize, since afterward the target may be depending
4091 // on the bitconvert.
4092 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00004093 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004094 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00004096 bool isSimple = true;
4097 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
4098 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
4099 N0.getOperand(i).getOpcode() != ISD::Constant &&
4100 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004101 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00004102 break;
4103 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004104
Owen Andersone50ed302009-08-10 22:56:29 +00004105 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004106 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00004107 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00004108 if (isSimple)
Gabor Greifba36cb52008-08-28 21:40:38 +00004109 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00004110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004111
Dan Gohman3dd168d2008-09-05 01:58:21 +00004112 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00004113 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling67a67682009-01-30 22:44:24 +00004114 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00004115 if (Res.getNode() != N) {
4116 if (!LegalOperations ||
4117 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
4118 return Res;
4119
4120 // Folding it resulted in an illegal node, and it's too late to
4121 // do that. Clean up the old node and forego the transformation.
4122 // Ideally this won't happen very often, because instcombine
4123 // and the earlier dagcombine runs (where illegal nodes are
4124 // permitted) should have folded most of them already.
4125 DAG.DeleteNode(Res.getNode());
4126 }
Chris Lattner94683772005-12-23 05:30:37 +00004127 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Bill Wendling67a67682009-01-30 22:44:24 +00004129 // (conv (conv x, t1), t2) -> (conv x, t2)
4130 if (N0.getOpcode() == ISD::BIT_CONVERT)
4131 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
4132 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00004133
Chris Lattner57104102005-12-23 05:44:41 +00004134 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00004135 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00004136 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004137 // Do not change the width of a volatile load.
4138 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004139 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004140 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00004141 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00004142 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00004143 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00004144
Evan Cheng59d5b682007-05-07 21:27:48 +00004145 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00004146 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
4147 LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004148 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00004149 LN0->isVolatile(), LN0->isNonTemporal(),
4150 OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00004151 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00004152 CombineTo(N0.getNode(),
Bill Wendling67a67682009-01-30 22:44:24 +00004153 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
4154 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00004155 Load.getValue(1));
4156 return Load;
4157 }
Chris Lattner57104102005-12-23 05:44:41 +00004158 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004159
Bill Wendling67a67682009-01-30 22:44:24 +00004160 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
4161 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00004162 // This often reduces constant pool loads.
4163 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004164 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling67a67682009-01-30 22:44:24 +00004165 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
4166 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004167 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004168
Duncan Sands83ec4b62008-06-06 12:08:01 +00004169 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004170 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00004171 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
4172 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004173 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00004174 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4175 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004177
Bill Wendling67a67682009-01-30 22:44:24 +00004178 // fold (bitconvert (fcopysign cst, x)) ->
4179 // (or (and (bitconvert x), sign), (and cst, (not sign)))
4180 // Note that we don't handle (copysign x, cst) because this can always be
4181 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00004183 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004184 VT.isInteger() && !VT.isVector()) {
4185 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00004186 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00004187 if (isTypeLegal(IntXVT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004188 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004189 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00004190 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004191
Duncan Sands25cf2272008-11-24 14:53:14 +00004192 // If X has a different width than the result/lhs, sext it or truncate it.
4193 unsigned VTWidth = VT.getSizeInBits();
4194 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004195 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004196 AddToWorkList(X.getNode());
4197 } else if (OrigXWidth > VTWidth) {
4198 // To get the sign bit in the right place, we have to shift it right
4199 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00004200 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004201 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00004202 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
4203 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004204 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00004205 AddToWorkList(X.getNode());
4206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004207
Duncan Sands25cf2272008-11-24 14:53:14 +00004208 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00004209 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004210 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004211 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004212
Bill Wendling9729c5a2009-01-31 03:12:48 +00004213 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00004214 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00004215 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00004216 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00004217 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004218
Bill Wendling67a67682009-01-30 22:44:24 +00004219 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00004220 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00004221 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004222
Scott Michelfdc40a02009-02-17 22:15:04 +00004223 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004224 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004225 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
4226 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004227 return CombineLD;
4228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004229
Dan Gohman475871a2008-07-27 21:46:04 +00004230 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00004231}
4232
Dan Gohman475871a2008-07-27 21:46:04 +00004233SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00004234 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00004235 return CombineConsecutiveLoads(N, VT);
4236}
4237
Dan Gohman7f321562007-06-25 16:23:39 +00004238/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00004239/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00004240/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00004241SDValue DAGCombiner::
Owen Andersone50ed302009-08-10 22:56:29 +00004242ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
4243 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Chris Lattner6258fb22006-04-02 02:53:43 +00004245 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00004246 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Duncan Sands83ec4b62008-06-06 12:08:01 +00004248 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
4249 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004250
Chris Lattner6258fb22006-04-02 02:53:43 +00004251 // If this is a conversion of N elements of one type to N elements of another
4252 // type, convert each element. This handles FP<->INT cases.
4253 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004255 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00004256 SDValue Op = BV->getOperand(i);
4257 // If the vector element type is not legal, the BUILD_VECTOR operands
4258 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00004259 if (Op.getValueType() != SrcEltVT)
4260 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004261 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00004262 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00004263 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00004264 }
Owen Anderson23b9b192009-08-12 00:36:31 +00004265 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004266 BV->getValueType(0).getVectorNumElements());
Evan Chenga87008d2009-02-25 22:49:59 +00004267 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4268 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004270
Chris Lattner6258fb22006-04-02 02:53:43 +00004271 // Otherwise, we're growing or shrinking the elements. To avoid having to
4272 // handle annoying details of growing/shrinking FP values, we convert them to
4273 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004274 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004275 // Convert the input float vector to a int vector where the elements are the
4276 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00004277 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004278 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004279 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00004280 SrcEltVT = IntVT;
4281 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004282
Chris Lattner6258fb22006-04-02 02:53:43 +00004283 // Now we know the input is an integer vector. If the output is a FP type,
4284 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004285 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00004287 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00004288 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004289
Chris Lattner6258fb22006-04-02 02:53:43 +00004290 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00004291 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00004292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004293
Chris Lattner6258fb22006-04-02 02:53:43 +00004294 // Okay, we know the src/dst types are both integers of differing types.
4295 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004296 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00004297 if (SrcBitSize < DstBitSize) {
4298 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00004299
Dan Gohman475871a2008-07-27 21:46:04 +00004300 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004301 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00004302 i += NumInputsPerOutput) {
4303 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00004304 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00004305 bool EltIsUndef = true;
4306 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
4307 // Shift the previously computed bits over.
4308 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00004309 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00004310 if (Op.getOpcode() == ISD::UNDEF) continue;
4311 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00004312
Dan Gohman58c25872010-04-12 02:24:01 +00004313 NewBits |= APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
4314 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004316
Chris Lattner6258fb22006-04-02 02:53:43 +00004317 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00004318 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004319 else
4320 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
4321 }
4322
Owen Anderson23b9b192009-08-12 00:36:31 +00004323 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00004324 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4325 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004326 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004327
Chris Lattner6258fb22006-04-02 02:53:43 +00004328 // Finally, this must be the case where we are shrinking elements: each input
4329 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00004330 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00004331 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00004332 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
4333 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00004334 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004335
Dan Gohman7f321562007-06-25 16:23:39 +00004336 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00004337 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
4338 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00004339 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00004340 continue;
4341 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004342
Bob Wilsonb1303d02009-04-13 22:05:19 +00004343 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
4344 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004345
Chris Lattner6258fb22006-04-02 02:53:43 +00004346 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00004347 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004348 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00004349 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00004350 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00004351 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
4352 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00004353 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00004354 }
4355
4356 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00004357 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00004358 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
4359 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004360
Evan Chenga87008d2009-02-25 22:49:59 +00004361 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4362 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00004363}
4364
Dan Gohman475871a2008-07-27 21:46:04 +00004365SDValue DAGCombiner::visitFADD(SDNode *N) {
4366 SDValue N0 = N->getOperand(0);
4367 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004368 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4369 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004370 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004371
Dan Gohman7f321562007-06-25 16:23:39 +00004372 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004373 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004374 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004375 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004376 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004377
Bill Wendlingb0162f52009-01-30 22:53:48 +00004378 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004380 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004381 // canonicalize constant to RHS
4382 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004383 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
4384 // fold (fadd A, 0) -> A
Dan Gohman760f86f2009-01-22 21:58:43 +00004385 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4386 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004387 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004388 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004389 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004390 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00004391 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sands25cf2272008-11-24 14:53:14 +00004392 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00004393 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00004394 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004395
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004396 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4397 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004398 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004399 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004400 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4401 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004402
Dan Gohman475871a2008-07-27 21:46:04 +00004403 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004404}
4405
Dan Gohman475871a2008-07-27 21:46:04 +00004406SDValue DAGCombiner::visitFSUB(SDNode *N) {
4407 SDValue N0 = N->getOperand(0);
4408 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00004409 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4410 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004411 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004412
Dan Gohman7f321562007-06-25 16:23:39 +00004413 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004414 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004415 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004416 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004418
Nate Begemana0e221d2005-10-18 00:28:13 +00004419 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004421 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00004422 // fold (fsub A, 0) -> A
Dan Gohmana90c8e62009-01-23 19:10:37 +00004423 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4424 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00004425 // fold (fsub 0, B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004426 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004427 if (isNegatibleForFree(N1, LegalOperations))
4428 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00004429 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004430 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00004431 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00004432 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sands25cf2272008-11-24 14:53:14 +00004433 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendlingb0162f52009-01-30 22:53:48 +00004434 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00004435 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00004436
Dan Gohman475871a2008-07-27 21:46:04 +00004437 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004438}
4439
Dan Gohman475871a2008-07-27 21:46:04 +00004440SDValue DAGCombiner::visitFMUL(SDNode *N) {
4441 SDValue N0 = N->getOperand(0);
4442 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004443 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4444 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004445 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004446
Dan Gohman7f321562007-06-25 16:23:39 +00004447 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004448 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004449 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004450 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004451 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004452
Nate Begeman11af4ea2005-10-17 20:40:11 +00004453 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004454 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004455 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004456 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004457 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004458 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4459 // fold (fmul A, 0) -> 0
Dan Gohman760f86f2009-01-22 21:58:43 +00004460 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4461 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00004462 // fold (fmul A, 0) -> 0, vector edition.
4463 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4464 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004465 // fold (fmul X, 2.0) -> (fadd X, X)
4466 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004467 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00004468 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00004469 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004470 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004471 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004472
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004473 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004474 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4475 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004476 // Both can be negated for free, check to see if at least one is cheaper
4477 // negated.
4478 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004479 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004480 GetNegatedExpression(N0, DAG, LegalOperations),
4481 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004482 }
4483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004484
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004485 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4486 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004487 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004488 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00004489 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00004490 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004491
Dan Gohman475871a2008-07-27 21:46:04 +00004492 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004493}
4494
Dan Gohman475871a2008-07-27 21:46:04 +00004495SDValue DAGCombiner::visitFDIV(SDNode *N) {
4496 SDValue N0 = N->getOperand(0);
4497 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004498 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4499 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004500 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004501
Dan Gohman7f321562007-06-25 16:23:39 +00004502 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004503 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004504 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004505 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004506 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004507
Nate Begemana148d982006-01-18 22:35:16 +00004508 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00004509 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004510 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004511
4512
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004513 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sands25cf2272008-11-24 14:53:14 +00004514 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4515 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004516 // Both can be negated for free, check to see if at least one is cheaper
4517 // negated.
4518 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00004519 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004520 GetNegatedExpression(N0, DAG, LegalOperations),
4521 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004522 }
4523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
Dan Gohman475871a2008-07-27 21:46:04 +00004525 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004526}
4527
Dan Gohman475871a2008-07-27 21:46:04 +00004528SDValue DAGCombiner::visitFREM(SDNode *N) {
4529 SDValue N0 = N->getOperand(0);
4530 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004531 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4532 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004533 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004534
Nate Begemana148d982006-01-18 22:35:16 +00004535 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00004536 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00004537 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004538
Dan Gohman475871a2008-07-27 21:46:04 +00004539 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004540}
4541
Dan Gohman475871a2008-07-27 21:46:04 +00004542SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4543 SDValue N0 = N->getOperand(0);
4544 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004545 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4546 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00004547 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004548
Owen Anderson825b72b2009-08-11 20:47:22 +00004549 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004550 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004551
Chris Lattner12d83032006-03-05 05:30:57 +00004552 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004553 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004554 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4555 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004556 if (!V.isNegative()) {
4557 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004558 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004559 } else {
4560 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004561 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004562 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00004563 }
Chris Lattner12d83032006-03-05 05:30:57 +00004564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004565
Chris Lattner12d83032006-03-05 05:30:57 +00004566 // copysign(fabs(x), y) -> copysign(x, y)
4567 // copysign(fneg(x), y) -> copysign(x, y)
4568 // copysign(copysign(x,z), y) -> copysign(x, y)
4569 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4570 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004571 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4572 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00004573
4574 // copysign(x, abs(y)) -> abs(x)
4575 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004576 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004577
Chris Lattner12d83032006-03-05 05:30:57 +00004578 // copysign(x, copysign(y,z)) -> copysign(x, z)
4579 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004580 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4581 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004582
Chris Lattner12d83032006-03-05 05:30:57 +00004583 // copysign(x, fp_extend(y)) -> copysign(x, y)
4584 // copysign(x, fp_round(y)) -> copysign(x, y)
4585 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004586 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4587 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004588
Dan Gohman475871a2008-07-27 21:46:04 +00004589 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004590}
4591
Dan Gohman475871a2008-07-27 21:46:04 +00004592SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4593 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004594 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004595 EVT VT = N->getValueType(0);
4596 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00004597
Nate Begeman1d4d4142005-09-01 00:19:25 +00004598 // fold (sint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004600 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Chris Lattnercda88752008-06-26 00:16:49 +00004602 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4603 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004604 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4605 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004606 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004607 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004608 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004609 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00004610
Dan Gohman475871a2008-07-27 21:46:04 +00004611 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004612}
4613
Dan Gohman475871a2008-07-27 21:46:04 +00004614SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4615 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004616 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004617 EVT VT = N->getValueType(0);
4618 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004619
Nate Begeman1d4d4142005-09-01 00:19:25 +00004620 // fold (uint_to_fp c1) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004621 if (N0C && OpVT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004622 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004623
Chris Lattnercda88752008-06-26 00:16:49 +00004624 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4625 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004626 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4627 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004628 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00004629 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004630 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004632
Dan Gohman475871a2008-07-27 21:46:04 +00004633 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004634}
4635
Dan Gohman475871a2008-07-27 21:46:04 +00004636SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4637 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004638 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004639 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004640
Nate Begeman1d4d4142005-09-01 00:19:25 +00004641 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004642 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004643 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4644
Dan Gohman475871a2008-07-27 21:46:04 +00004645 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004646}
4647
Dan Gohman475871a2008-07-27 21:46:04 +00004648SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4649 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004650 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004651 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004652
Nate Begeman1d4d4142005-09-01 00:19:25 +00004653 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00004654 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004655 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4656
Dan Gohman475871a2008-07-27 21:46:04 +00004657 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004658}
4659
Dan Gohman475871a2008-07-27 21:46:04 +00004660SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4661 SDValue N0 = N->getOperand(0);
4662 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004663 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004664 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004665
Nate Begeman1d4d4142005-09-01 00:19:25 +00004666 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004667 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004668 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004669
Chris Lattner79dbea52006-03-13 06:26:26 +00004670 // fold (fp_round (fp_extend x)) -> x
4671 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4672 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004673
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004674 // fold (fp_round (fp_round x)) -> (fp_round x)
4675 if (N0.getOpcode() == ISD::FP_ROUND) {
4676 // This is a value preserving truncation if both round's are.
4677 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004678 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00004679 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004680 DAG.getIntPtrConstant(IsTrunc));
4681 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004682
Chris Lattner79dbea52006-03-13 06:26:26 +00004683 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004684 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00004685 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4686 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004687 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004688 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4689 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00004690 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004691
Dan Gohman475871a2008-07-27 21:46:04 +00004692 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004693}
4694
Dan Gohman475871a2008-07-27 21:46:04 +00004695SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4696 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004697 EVT VT = N->getValueType(0);
4698 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004699 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004700
Nate Begeman1d4d4142005-09-01 00:19:25 +00004701 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00004702 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004703 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004704 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004705 }
Bill Wendling0225a1d2009-01-30 23:15:49 +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_EXTEND(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
Chris Lattner5938bef2007-12-29 06:55:23 +00004715 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00004716 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004717 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004718 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004719
Nate Begeman1d4d4142005-09-01 00:19:25 +00004720 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00004721 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00004722 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004723
4724 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4725 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004726 if (N0.getOpcode() == ISD::FP_ROUND
4727 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004728 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004729 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004730 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00004731 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4732 In, N0.getOperand(1));
4733 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00004734 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004735
Chris Lattner0bd48932008-01-17 07:00:52 +00004736 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004737 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004738 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004739 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004740 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004741 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4742 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004743 LN0->getBasePtr(), LN0->getSrcValue(),
4744 LN0->getSrcValueOffset(),
4745 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004746 LN0->isVolatile(), LN0->isNonTemporal(),
4747 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004748 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00004749 CombineTo(N0.getNode(),
4750 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4751 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004752 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004753 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004754 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004755
Dan Gohman475871a2008-07-27 21:46:04 +00004756 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004757}
4758
Dan Gohman475871a2008-07-27 21:46:04 +00004759SDValue DAGCombiner::visitFNEG(SDNode *N) {
4760 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004761 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004762
Duncan Sands25cf2272008-11-24 14:53:14 +00004763 if (isNegatibleForFree(N0, LegalOperations))
4764 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004765
Chris Lattner3bd39d42008-01-27 17:42:27 +00004766 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4767 // constant pool values.
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004768 if (N0.getOpcode() == ISD::BIT_CONVERT &&
4769 !VT.isVector() &&
4770 N0.getNode()->hasOneUse() &&
4771 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004772 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004773 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004774 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004775 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4776 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004777 AddToWorkList(Int.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00004778 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00004779 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004780 }
4781 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004782
Dan Gohman475871a2008-07-27 21:46:04 +00004783 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004784}
4785
Dan Gohman475871a2008-07-27 21:46:04 +00004786SDValue DAGCombiner::visitFABS(SDNode *N) {
4787 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004788 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00004789 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004790
Nate Begeman1d4d4142005-09-01 00:19:25 +00004791 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004793 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004794 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004795 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004796 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004797 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004798 // fold (fabs (fcopysign x, y)) -> (fabs x)
4799 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00004800 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004801
Chris Lattner3bd39d42008-01-27 17:42:27 +00004802 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4803 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004804 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004805 N0.getOperand(0).getValueType().isInteger() &&
4806 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004807 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004808 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004809 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004810 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004811 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004812 AddToWorkList(Int.getNode());
Bill Wendlingc0debad2009-01-30 23:27:35 +00004813 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4814 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00004815 }
4816 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004817
Dan Gohman475871a2008-07-27 21:46:04 +00004818 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004819}
4820
Dan Gohman475871a2008-07-27 21:46:04 +00004821SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4822 SDValue Chain = N->getOperand(0);
4823 SDValue N1 = N->getOperand(1);
4824 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004825
Dan Gohmane0f06c72009-11-17 00:47:23 +00004826 // If N is a constant we could fold this into a fallthrough or unconditional
4827 // branch. However that doesn't happen very often in normal code, because
4828 // Instcombine/SimplifyCFG should have handled the available opportunities.
4829 // If we did this folding here, it would be necessary to update the
4830 // MachineBasicBlock CFG, which is awkward.
4831
Nate Begeman750ac1b2006-02-01 07:19:44 +00004832 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4833 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00004834 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4836 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004837 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004838 N1.getOperand(0), N1.getOperand(1), N2);
4839 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00004840
Evan Chengd40d03e2010-01-06 19:38:29 +00004841 SDNode *Trunc = 0;
4842 if (N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) {
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004843 // Look past truncate.
Evan Chengd40d03e2010-01-06 19:38:29 +00004844 Trunc = N1.getNode();
4845 N1 = N1.getOperand(0);
4846 }
4847
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004848 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4849 // Match this pattern so that we can generate simpler code:
4850 //
4851 // %a = ...
4852 // %b = and i32 %a, 2
4853 // %c = srl i32 %b, 1
4854 // brcond i32 %c ...
4855 //
4856 // into
4857 //
4858 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00004859 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004860 // %c = setcc eq %b, 0
4861 // brcond %c ...
4862 //
4863 // This applies only when the AND constant value has one bit set and the
4864 // SRL constant is equal to the log2 of the AND constant. The back-end is
4865 // smart enough to convert the result into a TEST/JMP sequence.
4866 SDValue Op0 = N1.getOperand(0);
4867 SDValue Op1 = N1.getOperand(1);
4868
4869 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004870 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004871 SDValue AndOp1 = Op0.getOperand(1);
4872
4873 if (AndOp1.getOpcode() == ISD::Constant) {
4874 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4875
4876 if (AndConst.isPowerOf2() &&
4877 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4878 SDValue SetCC =
4879 DAG.getSetCC(N->getDebugLoc(),
4880 TLI.getSetCCResultType(Op0.getValueType()),
4881 Op0, DAG.getConstant(0, Op0.getValueType()),
4882 ISD::SETNE);
4883
Evan Chengd40d03e2010-01-06 19:38:29 +00004884 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4885 MVT::Other, Chain, SetCC, N2);
4886 // Don't add the new BRCond into the worklist or else SimplifySelectCC
4887 // will convert it back to (X & C1) >> C2.
4888 CombineTo(N, NewBRCond, false);
4889 // Truncate is dead.
4890 if (Trunc) {
4891 removeFromWorkList(Trunc);
4892 DAG.DeleteNode(Trunc);
4893 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004894 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00004895 WorkListRemover DeadNodes(*this);
4896 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004897 removeFromWorkList(N1.getNode());
4898 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00004899 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004900 }
4901 }
4902 }
4903 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00004904
4905 // Transform br(xor(x, y)) -> br(x != y)
4906 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
4907 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
4908 SDNode *TheXor = N1.getNode();
4909 SDValue Op0 = TheXor->getOperand(0);
4910 SDValue Op1 = TheXor->getOperand(1);
4911 if (Op0.getOpcode() == Op1.getOpcode()) {
4912 // Avoid missing important xor optimizations.
4913 SDValue Tmp = visitXOR(TheXor);
4914 if (Tmp.getNode()) {
4915 DEBUG(dbgs() << "\nReplacing.8 ";
4916 TheXor->dump(&DAG);
4917 dbgs() << "\nWith: ";
4918 Tmp.getNode()->dump(&DAG);
4919 dbgs() << '\n');
4920 WorkListRemover DeadNodes(*this);
4921 DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes);
4922 removeFromWorkList(TheXor);
4923 DAG.DeleteNode(TheXor);
4924 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4925 MVT::Other, Chain, Tmp, N2);
4926 }
4927 }
4928
4929 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
4930 bool Equal = false;
4931 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
4932 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
4933 Op0.getOpcode() == ISD::XOR) {
4934 TheXor = Op0.getNode();
4935 Equal = true;
4936 }
4937
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004938 SDValue NodeToReplace = Trunc ? SDValue(Trunc, 0) : N1;
4939
4940 EVT SetCCVT = NodeToReplace.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00004941 if (LegalTypes)
4942 SetCCVT = TLI.getSetCCResultType(SetCCVT);
4943 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
4944 SetCCVT,
4945 Op0, Op1,
4946 Equal ? ISD::SETEQ : ISD::SETNE);
4947 // Replace the uses of XOR with SETCC
4948 WorkListRemover DeadNodes(*this);
Chris Lattnerf2f64e92010-03-10 23:46:44 +00004949 DAG.ReplaceAllUsesOfValueWith(NodeToReplace, SetCC, &DeadNodes);
4950 removeFromWorkList(NodeToReplace.getNode());
4951 DAG.DeleteNode(NodeToReplace.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00004952 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4953 MVT::Other, Chain, SetCC, N2);
4954 }
4955 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00004956
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004958}
4959
Chris Lattner3ea0b472005-10-05 06:47:48 +00004960// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4961//
Dan Gohman475871a2008-07-27 21:46:04 +00004962SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004963 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004964 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004965
Dan Gohmane0f06c72009-11-17 00:47:23 +00004966 // If N is a constant we could fold this into a fallthrough or unconditional
4967 // branch. However that doesn't happen very often in normal code, because
4968 // Instcombine/SimplifyCFG should have handled the available opportunities.
4969 // If we did this folding here, it would be necessary to update the
4970 // MachineBasicBlock CFG, which is awkward.
4971
Duncan Sands8eab8a22008-06-09 11:32:28 +00004972 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004973 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004974 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
4975 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004976 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004977
Nate Begemane17daeb2005-10-05 21:43:42 +00004978 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004979 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00004980 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00004981 N->getOperand(0), Simp.getOperand(2),
4982 Simp.getOperand(0), Simp.getOperand(1),
4983 N->getOperand(4));
4984
Dan Gohman475871a2008-07-27 21:46:04 +00004985 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004986}
4987
Duncan Sandsec87aa82008-06-15 20:12:31 +00004988/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4989/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004990/// and it has other uses besides the load / store. After the
4991/// transformation, the new indexed load / store has effectively folded
4992/// the add / subtract in and all of its other uses are redirected to the
4993/// new load / store.
4994bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004995 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004996 return false;
4997
4998 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004999 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005000 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005001 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005002 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005003 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005004 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00005005 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00005006 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
5007 return false;
5008 Ptr = LD->getBasePtr();
5009 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005010 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005011 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005012 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005013 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
5014 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
5015 return false;
5016 Ptr = ST->getBasePtr();
5017 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005018 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005019 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005020 }
Chris Lattner448f2192006-11-11 00:39:41 +00005021
Chris Lattner9f1794e2006-11-11 00:56:29 +00005022 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
5023 // out. There is no reason to make this a preinc/predec.
5024 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00005025 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005026 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005027
Chris Lattner9f1794e2006-11-11 00:56:29 +00005028 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue BasePtr;
5030 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005031 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5032 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
5033 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00005034 // Don't create a indexed load / store with zero offset.
5035 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005036 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005037 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005038
Chris Lattner41e53fd2006-11-11 01:00:15 +00005039 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00005040 // 1) The new base ptr is a frame index.
5041 // 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 +00005042 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00005043 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00005044 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00005045 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00005046
Chris Lattner41e53fd2006-11-11 01:00:15 +00005047 // Check #1. Preinc'ing a frame index would require copying the stack pointer
5048 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00005049 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00005050 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005051
Chris Lattner41e53fd2006-11-11 01:00:15 +00005052 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005053 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005054 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005055 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005056 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00005057 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005058
Evan Chengc843abe2007-05-24 02:35:39 +00005059 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00005060 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005061 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5062 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005063 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005064 if (Use == N)
5065 continue;
Evan Cheng917be682008-03-04 00:41:45 +00005066 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005067 return false;
5068
5069 if (!((Use->getOpcode() == ISD::LOAD &&
5070 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005071 (Use->getOpcode() == ISD::STORE &&
5072 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005073 RealUse = true;
5074 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005075
Chris Lattner9f1794e2006-11-11 00:56:29 +00005076 if (!RealUse)
5077 return false;
5078
Dan Gohman475871a2008-07-27 21:46:04 +00005079 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005080 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00005081 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5082 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005083 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00005084 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5085 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005086 ++PreIndexedNodes;
5087 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005088 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005089 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005090 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005091 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005092 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005093 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005094 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005095 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005096 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005097 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005098 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005099 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005100 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005101 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005102 }
5103
Chris Lattner9f1794e2006-11-11 00:56:29 +00005104 // Finally, since the node is now dead, remove it from the graph.
5105 DAG.DeleteNode(N);
5106
5107 // Replace the uses of Ptr with uses of the updated base value.
5108 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005109 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00005110 removeFromWorkList(Ptr.getNode());
5111 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00005112
5113 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005114}
5115
Duncan Sandsec87aa82008-06-15 20:12:31 +00005116/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00005117/// add / sub of the base pointer node into a post-indexed load / store.
5118/// The transformation folded the add / subtract into the new indexed
5119/// load / store effectively and all of its uses are redirected to the
5120/// new load / store.
5121bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00005122 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00005123 return false;
5124
5125 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00005127 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00005128 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005129 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005130 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005131 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005132 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
5133 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
5134 return false;
5135 Ptr = LD->getBasePtr();
5136 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00005137 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00005138 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005139 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00005140 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
5141 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
5142 return false;
5143 Ptr = ST->getBasePtr();
5144 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005145 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00005146 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00005147 }
Chris Lattner448f2192006-11-11 00:39:41 +00005148
Gabor Greifba36cb52008-08-28 21:40:38 +00005149 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00005150 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005151
Gabor Greifba36cb52008-08-28 21:40:38 +00005152 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
5153 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005154 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005155 if (Op == N ||
5156 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
5157 continue;
5158
Dan Gohman475871a2008-07-27 21:46:04 +00005159 SDValue BasePtr;
5160 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005161 ISD::MemIndexedMode AM = ISD::UNINDEXED;
5162 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Bob Wilson92ad3632009-09-10 22:09:31 +00005163 if (Ptr == Offset && Op->getOpcode() == ISD::ADD)
Chris Lattner9f1794e2006-11-11 00:56:29 +00005164 std::swap(BasePtr, Offset);
5165 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00005166 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00005167 // Don't create a indexed load / store with zero offset.
5168 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005169 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00005170 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005171
Chris Lattner9f1794e2006-11-11 00:56:29 +00005172 // Try turning it into a post-indexed load / store except when
5173 // 1) All uses are load / store ops that use it as base ptr.
5174 // 2) Op must be independent of N, i.e. Op is neither a predecessor
5175 // nor a successor of N. Otherwise, if Op is folded that would
5176 // create a cycle.
5177
Evan Chengcaab1292009-05-06 18:25:01 +00005178 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
5179 continue;
5180
Chris Lattner9f1794e2006-11-11 00:56:29 +00005181 // Check for #1.
5182 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00005183 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
5184 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00005185 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00005186 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00005187 continue;
5188
Chris Lattner9f1794e2006-11-11 00:56:29 +00005189 // If all the uses are load / store addresses, then don't do the
5190 // transformation.
5191 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
5192 bool RealUse = false;
5193 for (SDNode::use_iterator III = Use->use_begin(),
5194 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00005195 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00005196 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00005198 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005199 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00005200 RealUse = true;
5201 }
Chris Lattner448f2192006-11-11 00:39:41 +00005202
Chris Lattner9f1794e2006-11-11 00:56:29 +00005203 if (!RealUse) {
5204 TryNext = true;
5205 break;
Chris Lattner448f2192006-11-11 00:39:41 +00005206 }
5207 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005208 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005209
Chris Lattner9f1794e2006-11-11 00:56:29 +00005210 if (TryNext)
5211 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00005212
Chris Lattner9f1794e2006-11-11 00:56:29 +00005213 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00005214 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005215 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00005216 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
5217 BasePtr, Offset, AM)
5218 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
5219 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005220 ++PostIndexedNodes;
5221 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00005222 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005223 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005224 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005225 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005226 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005227 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005228 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00005229 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005230 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005231 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005232 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005233 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00005234 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005235 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00005236 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00005237
Chris Lattner9f1794e2006-11-11 00:56:29 +00005238 // Finally, since the node is now dead, remove it from the graph.
5239 DAG.DeleteNode(N);
5240
5241 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00005242 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00005243 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005244 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005245 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005246 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00005247 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00005248 }
5249 }
5250 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005251
Chris Lattner448f2192006-11-11 00:39:41 +00005252 return false;
5253}
5254
Dan Gohman475871a2008-07-27 21:46:04 +00005255SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00005256 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SDValue Chain = LD->getChain();
5258 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005259
Evan Cheng45a7ca92007-05-01 00:38:21 +00005260 // If load is not volatile and there are no uses of the loaded value (and
5261 // the updated indexed value in case of indexed loads), change uses of the
5262 // chain value into uses of the chain input (i.e. delete the dead load).
5263 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005264 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00005265 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00005266 if (N->hasNUsesOfValue(0, 0)) {
5267 // It's not safe to use the two value CombineTo variant here. e.g.
5268 // v1, chain2 = load chain1, loc
5269 // v2, chain3 = load chain2, loc
5270 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00005271 // Now we replace use of chain2 with chain1. This makes the second load
5272 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00005273 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005274 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005275 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005276 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005277 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005278 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005279 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00005280
Chris Lattner125991a2008-01-24 07:57:06 +00005281 if (N->use_empty()) {
5282 removeFromWorkList(N);
5283 DAG.DeleteNode(N);
5284 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005285
Dan Gohman475871a2008-07-27 21:46:04 +00005286 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00005287 }
Evan Cheng498f5592007-05-01 08:53:39 +00005288 } else {
5289 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00005290 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Evan Cheng498f5592007-05-01 08:53:39 +00005291 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00005292 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00005293 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005294 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005295 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00005296 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00005297 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005298 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00005299 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
5300 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00005301 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005302 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00005303 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00005304 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00005305 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005306 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00005307 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00005308 }
5309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005310
Chris Lattner01a22022005-10-10 22:04:48 +00005311 // If this load is directly stored, replace the load value with the stored
5312 // value.
5313 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005314 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00005315 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
5316 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005317 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005318 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
5319 if (PrevST->getBasePtr() == Ptr &&
5320 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005321 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00005322 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005323 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Evan Cheng255f20f2010-04-01 06:04:33 +00005325 // Try to infer better alignment information than the load already has.
5326 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
5327 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5328 if (Align > LD->getAlignment())
5329 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
5330 LD->getValueType(0),
5331 Chain, Ptr, LD->getSrcValue(),
5332 LD->getSrcValueOffset(), LD->getMemoryVT(),
5333 LD->isVolatile(), LD->isNonTemporal(), Align);
5334 }
5335 }
5336
Jim Laskey7ca56af2006-10-11 13:47:09 +00005337 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005338 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005339 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005340
Jim Laskey6ff23e52006-10-04 16:53:27 +00005341 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005342 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005343 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005344
Jim Laskey279f0532006-09-25 16:29:54 +00005345 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005346 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005347 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
5348 BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00005349 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005350 LD->isVolatile(), LD->isNonTemporal(),
5351 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005352 } else {
Bill Wendlingc0debad2009-01-30 23:27:35 +00005353 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005354 LD->getValueType(0),
5355 BetterChain, Ptr, LD->getSrcValue(),
5356 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005357 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00005358 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005359 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005360 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005361 }
Jim Laskey279f0532006-09-25 16:29:54 +00005362
Jim Laskey6ff23e52006-10-04 16:53:27 +00005363 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00005364 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 MVT::Other, Chain, ReplLoad.getValue(1));
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005366
5367 // Make sure the new and old chains are cleaned up.
5368 AddToWorkList(Token.getNode());
5369
Jim Laskey274062c2006-10-13 23:32:28 +00005370 // Replace uses with load result and token factor. Don't add users
5371 // to work list.
5372 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005373 }
5374 }
5375
Evan Cheng7fc033a2006-11-03 03:06:21 +00005376 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005377 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005378 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00005379
Evan Cheng4c26e932010-04-19 19:29:22 +00005380 if (PromoteLoad(SDValue(N, 0)))
5381 return SDValue(N, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00005382 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00005383}
5384
Chris Lattner2392ae72010-04-15 04:48:01 +00005385/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
5386/// load is having specific bytes cleared out. If so, return the byte size
5387/// being masked out and the shift amount.
5388static std::pair<unsigned, unsigned>
5389CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
5390 std::pair<unsigned, unsigned> Result(0, 0);
5391
5392 // Check for the structure we're looking for.
5393 if (V->getOpcode() != ISD::AND ||
5394 !isa<ConstantSDNode>(V->getOperand(1)) ||
5395 !ISD::isNormalLoad(V->getOperand(0).getNode()))
5396 return Result;
5397
Chris Lattnere6987582010-04-15 06:10:49 +00005398 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00005399 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00005400 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
5401
5402 // The store should be chained directly to the load or be an operand of a
5403 // tokenfactor.
5404 if (LD == Chain.getNode())
5405 ; // ok.
5406 else if (Chain->getOpcode() != ISD::TokenFactor)
5407 return Result; // Fail.
5408 else {
5409 bool isOk = false;
5410 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
5411 if (Chain->getOperand(i).getNode() == LD) {
5412 isOk = true;
5413 break;
5414 }
5415 if (!isOk) return Result;
5416 }
Chris Lattner2392ae72010-04-15 04:48:01 +00005417
5418 // This only handles simple types.
5419 if (V.getValueType() != MVT::i16 &&
5420 V.getValueType() != MVT::i32 &&
5421 V.getValueType() != MVT::i64)
5422 return Result;
5423
5424 // Check the constant mask. Invert it so that the bits being masked out are
5425 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
5426 // follow the sign bit for uniformity.
5427 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
5428 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
5429 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
5430 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
5431 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
5432 if (NotMaskLZ == 64) return Result; // All zero mask.
5433
5434 // See if we have a continuous run of bits. If so, we have 0*1+0*
5435 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
5436 return Result;
5437
5438 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
5439 if (V.getValueType() != MVT::i64 && NotMaskLZ)
5440 NotMaskLZ -= 64-V.getValueSizeInBits();
5441
5442 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
5443 switch (MaskedBytes) {
5444 case 1:
5445 case 2:
5446 case 4: break;
5447 default: return Result; // All one mask, or 5-byte mask.
5448 }
5449
5450 // Verify that the first bit starts at a multiple of mask so that the access
5451 // is aligned the same as the access width.
5452 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
5453
5454 Result.first = MaskedBytes;
5455 Result.second = NotMaskTZ/8;
5456 return Result;
5457}
5458
5459
5460/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
5461/// provides a value as specified by MaskInfo. If so, replace the specified
5462/// store with a narrower store of truncated IVal.
5463static SDNode *
5464ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
5465 SDValue IVal, StoreSDNode *St,
5466 DAGCombiner *DC) {
5467 unsigned NumBytes = MaskInfo.first;
5468 unsigned ByteShift = MaskInfo.second;
5469 SelectionDAG &DAG = DC->getDAG();
5470
5471 // Check to see if IVal is all zeros in the part being masked in by the 'or'
5472 // that uses this. If not, this is not a replacement.
5473 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
5474 ByteShift*8, (ByteShift+NumBytes)*8);
5475 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
5476
5477 // Check that it is legal on the target to do this. It is legal if the new
5478 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
5479 // legalization.
5480 MVT VT = MVT::getIntegerVT(NumBytes*8);
5481 if (!DC->isTypeLegal(VT))
5482 return 0;
5483
5484 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
5485 // shifted by ByteShift and truncated down to NumBytes.
5486 if (ByteShift)
5487 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
5488 DAG.getConstant(ByteShift*8, DC->getShiftAmountTy()));
5489
5490 // Figure out the offset for the store and the alignment of the access.
5491 unsigned StOffset;
5492 unsigned NewAlign = St->getAlignment();
5493
5494 if (DAG.getTargetLoweringInfo().isLittleEndian())
5495 StOffset = ByteShift;
5496 else
5497 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
5498
5499 SDValue Ptr = St->getBasePtr();
5500 if (StOffset) {
5501 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
5502 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
5503 NewAlign = MinAlign(NewAlign, StOffset);
5504 }
5505
5506 // Truncate down to the new size.
5507 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
5508
5509 ++OpsNarrowed;
5510 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
5511 St->getSrcValue(), St->getSrcValueOffset()+StOffset,
5512 false, false, NewAlign).getNode();
5513}
5514
Evan Cheng8b944d32009-05-28 00:35:15 +00005515
5516/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
5517/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
5518/// of the loaded bits, try narrowing the load and store if it would end up
5519/// being a win for performance or code size.
5520SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
5521 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00005522 if (ST->isVolatile())
5523 return SDValue();
5524
Evan Cheng8b944d32009-05-28 00:35:15 +00005525 SDValue Chain = ST->getChain();
5526 SDValue Value = ST->getValue();
5527 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00005528 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00005529
5530 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00005531 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005532
5533 unsigned Opc = Value.getOpcode();
Chris Lattner2392ae72010-04-15 04:48:01 +00005534
5535 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
5536 // is a byte mask indicating a consecutive number of bytes, check to see if
5537 // Y is known to provide just those bytes. If so, we try to replace the
5538 // load + replace + store sequence with a single (narrower) store, which makes
5539 // the load dead.
5540 if (Opc == ISD::OR) {
5541 std::pair<unsigned, unsigned> MaskedLoad;
5542 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
5543 if (MaskedLoad.first)
5544 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5545 Value.getOperand(1), ST,this))
5546 return SDValue(NewST, 0);
5547
5548 // Or is commutative, so try swapping X and Y.
5549 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
5550 if (MaskedLoad.first)
5551 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
5552 Value.getOperand(0), ST,this))
5553 return SDValue(NewST, 0);
5554 }
5555
Evan Cheng8b944d32009-05-28 00:35:15 +00005556 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
5557 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00005558 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005559
5560 SDValue N0 = Value.getOperand(0);
5561 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
5562 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengcdcecc02009-05-28 18:41:02 +00005563 if (LD->getBasePtr() != Ptr)
5564 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005565
5566 // Find the type to narrow it the load / op / store to.
5567 SDValue N1 = Value.getOperand(1);
5568 unsigned BitWidth = N1.getValueSizeInBits();
5569 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
5570 if (Opc == ISD::AND)
5571 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00005572 if (Imm == 0 || Imm.isAllOnesValue())
5573 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005574 unsigned ShAmt = Imm.countTrailingZeros();
5575 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5576 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00005577 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005578 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00005579 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00005580 TLI.isNarrowingProfitable(VT, NewVT))) {
5581 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00005582 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00005583 }
Evan Chengcdcecc02009-05-28 18:41:02 +00005584 if (NewBW >= BitWidth)
5585 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005586
5587 // If the lsb changed does not start at the type bitwidth boundary,
5588 // start at the previous one.
5589 if (ShAmt % NewBW)
5590 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5591 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5592 if ((Imm & Mask) == Imm) {
5593 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5594 if (Opc == ISD::AND)
5595 NewImm ^= APInt::getAllOnesValue(NewBW);
5596 uint64_t PtrOff = ShAmt / 8;
5597 // For big endian targets, we need to adjust the offset to the pointer to
5598 // load the correct bytes.
5599 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00005600 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00005601
5602 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner2392ae72010-04-15 04:48:01 +00005603 const Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
5604 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00005605 return SDValue();
5606
Evan Cheng8b944d32009-05-28 00:35:15 +00005607 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5608 Ptr.getValueType(), Ptr,
5609 DAG.getConstant(PtrOff, Ptr.getValueType()));
5610 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5611 LD->getChain(), NewPtr,
5612 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005613 LD->isVolatile(), LD->isNonTemporal(),
5614 NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005615 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5616 DAG.getConstant(NewImm, NewVT));
5617 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5618 NewVal, NewPtr,
5619 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005620 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00005621
5622 AddToWorkList(NewPtr.getNode());
5623 AddToWorkList(NewLD.getNode());
5624 AddToWorkList(NewVal.getNode());
5625 WorkListRemover DeadNodes(*this);
5626 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5627 &DeadNodes);
5628 ++OpsNarrowed;
5629 return NewST;
5630 }
5631 }
5632
Evan Chengcdcecc02009-05-28 18:41:02 +00005633 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00005634}
5635
Dan Gohman475871a2008-07-27 21:46:04 +00005636SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00005637 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005638 SDValue Chain = ST->getChain();
5639 SDValue Value = ST->getValue();
5640 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00005641
Evan Cheng59d5b682007-05-07 21:27:48 +00005642 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00005643 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00005644 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005645 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00005646 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00005647 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00005648 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005649 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005650 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005651 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005652 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00005653 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5654 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005655 ST->getSrcValueOffset(), ST->isVolatile(),
5656 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00005657 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005658
Nate Begeman2cbba892006-12-11 02:23:46 +00005659 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00005660 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005661 // NOTE: If the original store is volatile, this transform must not increase
5662 // the number of stores. For example, on x86-32 an f64 can be stored in one
5663 // processor operation but an i64 (which is not legal) requires two. So the
5664 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00005665 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00005666 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005668 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00005669 case MVT::f80: // We don't do this for these yet.
5670 case MVT::f128:
5671 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00005672 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005673 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00005674 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00005676 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00005678 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5679 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005680 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005681 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00005682 }
5683 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005684 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00005685 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005686 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00005688 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00005689 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00005690 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5691 Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005692 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00005693 ST->isNonTemporal(), ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005694 } else if (!ST->isVolatile() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00005696 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00005697 // argument passing. Since this is so common, custom legalize the
5698 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00005699 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5701 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00005702 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00005703
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005704 int SVOffset = ST->getSrcValueOffset();
5705 unsigned Alignment = ST->getAlignment();
5706 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00005707 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005708
Bill Wendlingc144a572009-01-30 23:36:47 +00005709 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5710 Ptr, ST->getSrcValue(),
5711 ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005712 isVolatile, isNonTemporal,
5713 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00005714 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00005715 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005716 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00005717 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00005718 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5719 Ptr, ST->getSrcValue(),
David Greene1e559442010-02-15 17:00:31 +00005720 SVOffset, isVolatile, isNonTemporal,
5721 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00005723 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00005724 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005725
Chris Lattner62be1a72006-12-12 04:16:14 +00005726 break;
Evan Cheng25ece662006-12-11 17:25:19 +00005727 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005728 }
Nate Begeman2cbba892006-12-11 02:23:46 +00005729 }
5730
Evan Cheng255f20f2010-04-01 06:04:33 +00005731 // Try to infer better alignment information than the store already has.
5732 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
5733 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
5734 if (Align > ST->getAlignment())
5735 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5736 Ptr, ST->getSrcValue(),
5737 ST->getSrcValueOffset(), ST->getMemoryVT(),
5738 ST->isVolatile(), ST->isNonTemporal(), Align);
5739 }
5740 }
5741
Scott Michelfdc40a02009-02-17 22:15:04 +00005742 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00005743 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00005744 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00005745
Jim Laskey6ff23e52006-10-04 16:53:27 +00005746 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00005747 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00005748 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005749
5750 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005751 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005752 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005753 ST->getSrcValue(),ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005754 ST->getMemoryVT(), ST->isVolatile(),
5755 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005756 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00005757 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00005758 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005759 ST->isVolatile(), ST->isNonTemporal(),
5760 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00005761 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005762
Jim Laskey279f0532006-09-25 16:29:54 +00005763 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00005764 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00005766
Nate Begemanb6aef5c2009-09-15 00:18:30 +00005767 // Make sure the new and old chains are cleaned up.
5768 AddToWorkList(Token.getNode());
5769
Jim Laskey274062c2006-10-13 23:32:28 +00005770 // Don't add users to work list.
5771 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00005772 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00005773 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005774
Evan Cheng33dbedc2006-11-05 09:31:14 +00005775 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00005776 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00005777 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00005778
Chris Lattner3c872852007-12-29 06:26:16 +00005779 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00005780 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005781 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005782 // See if we can simplify the input to this truncstore with knowledge that
5783 // only the low bits are being used. For example:
5784 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00005785 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005786 GetDemandedBits(Value,
Bill Wendlingc144a572009-01-30 23:36:47 +00005787 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5788 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005789 AddToWorkList(Value.getNode());
5790 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00005791 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5792 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005793 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005794 ST->isVolatile(), ST->isNonTemporal(),
5795 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00005796
Chris Lattnere33544c2007-10-13 06:58:48 +00005797 // Otherwise, see if we can simplify the operation with
5798 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00005799 if (SimplifyDemandedBits(Value,
5800 APInt::getLowBitsSet(
Dan Gohman2e141d72009-12-14 23:40:38 +00005801 Value.getValueType().getScalarType().getSizeInBits(),
Dan Gohman8f78e3c2010-03-10 21:04:53 +00005802 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00005803 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00005804 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005805
Chris Lattner3c872852007-12-29 06:26:16 +00005806 // If this is a load followed by a store to the same location, then the store
5807 // is dead/noop.
5808 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005809 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005810 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00005811 // There can't be any side effects between the load and store, such as
5812 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00005813 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00005814 // The store is dead, remove it.
5815 return Chain;
5816 }
5817 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005818
Chris Lattnerddf89562008-01-17 19:59:44 +00005819 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5820 // truncating store. We can do this even if this is already a truncstore.
5821 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00005822 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00005823 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005824 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00005825 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5826 Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005827 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00005828 ST->isVolatile(), ST->isNonTemporal(),
5829 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00005830 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005831
Evan Cheng8b944d32009-05-28 00:35:15 +00005832 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00005833}
5834
Dan Gohman475871a2008-07-27 21:46:04 +00005835SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5836 SDValue InVec = N->getOperand(0);
5837 SDValue InVal = N->getOperand(1);
5838 SDValue EltNo = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005839
Chris Lattnerca242442006-03-19 01:27:56 +00005840 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5841 // vector with the inserted element.
5842 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005843 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005844 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5845 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005846 if (Elt < Ops.size())
5847 Ops[Elt] = InVal;
Evan Chenga87008d2009-02-25 22:49:59 +00005848 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5849 InVec.getValueType(), &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005850 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 // If the invec is an UNDEF and if EltNo is a constant, create a new
5852 // BUILD_VECTOR with undef elements and the inserted element.
5853 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5854 isa<ConstantSDNode>(EltNo)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005855 EVT VT = InVec.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005856 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00005857 unsigned NElts = VT.getVectorNumElements();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005858 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005859
Nate Begeman9008ca62009-04-27 18:41:29 +00005860 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5861 if (Elt < Ops.size())
5862 Ops[Elt] = InVal;
5863 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5864 InVec.getValueType(), &Ops[0], Ops.size());
5865 }
Dan Gohman475871a2008-07-27 21:46:04 +00005866 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005867}
5868
Dan Gohman475871a2008-07-27 21:46:04 +00005869SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005870 // (vextract (scalar_to_vector val, 0) -> val
5871 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005872
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005873 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Mon P Wangc6654ec42010-02-01 19:03:18 +00005874 // Check if the result type doesn't match the inserted element type. A
5875 // SCALAR_TO_VECTOR may truncate the inserted element and the
5876 // EXTRACT_VECTOR_ELT may widen the extracted vector.
Owen Andersone50ed302009-08-10 22:56:29 +00005877 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005878 SDValue InOp = InVec.getOperand(0);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005879 EVT NVT = N->getValueType(0);
5880 if (InOp.getValueType() != NVT) {
5881 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Mon P Wang87c46d82010-02-01 22:15:09 +00005882 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
Mon P Wangc6654ec42010-02-01 19:03:18 +00005883 }
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00005884 return InOp;
5885 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005886
5887 // Perform only after legalization to ensure build_vector / vector_shuffle
5888 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005889 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005890
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005891 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5892 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5893 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005894 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005895
Evan Cheng513da432007-10-06 08:19:55 +00005896 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005897 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005898 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005899 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00005900 EVT VT = InVec.getValueType();
5901 EVT ExtVT = VT.getVectorElementType();
5902 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00005903
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005904 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005905 EVT BCVT = InVec.getOperand(0).getValueType();
5906 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005907 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005908 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5909 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005910 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005911 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005912 NewLoad = true;
5913 }
Evan Cheng513da432007-10-06 08:19:55 +00005914
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005915 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00005916 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00005917 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005918 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00005919 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00005920 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00005921 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005922 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00005923 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005924 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5925 // =>
5926 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00005927
Mon P Wanga60b5232008-12-11 00:26:16 +00005928 // If the bit convert changed the number of elements, it is unsafe
5929 // to examine the mask.
5930 if (BCNumEltsChanged)
5931 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00005932
5933 // Select the input vector, guarding against out of range extract vector.
5934 unsigned NumElems = VT.getVectorNumElements();
5935 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5936 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5937
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005938 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5939 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00005940 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005941 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00005942 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00005943 }
5944 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005945
Duncan Sandsec87aa82008-06-15 20:12:31 +00005946 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00005947 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005948
5949 unsigned Align = LN0->getAlignment();
5950 if (NewLoad) {
5951 // Check the resultant load doesn't need a higher alignment than the
5952 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00005953 unsigned NewAlign =
Owen Anderson23b9b192009-08-12 00:36:31 +00005954 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00005955
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005956 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005957 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00005958
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005959 Align = NewAlign;
5960 }
5961
Dan Gohman475871a2008-07-27 21:46:04 +00005962 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005963 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005964 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005965 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005966 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005967 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00005968 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005969 DAG.getConstant(PtrOff, PtrType));
5970 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005971
5972 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005973 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene1e559442010-02-15 17:00:31 +00005974 LN0->isVolatile(), LN0->isNonTemporal(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005975 }
Bill Wendlingc144a572009-01-30 23:36:47 +00005976
Dan Gohman475871a2008-07-27 21:46:04 +00005977 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005978}
Evan Cheng513da432007-10-06 08:19:55 +00005979
Dan Gohman475871a2008-07-27 21:46:04 +00005980SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005981 unsigned NumInScalars = N->getNumOperands();
Owen Andersone50ed302009-08-10 22:56:29 +00005982 EVT VT = N->getValueType(0);
Chris Lattnerca242442006-03-19 01:27:56 +00005983
Dan Gohman7f321562007-06-25 16:23:39 +00005984 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5985 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5986 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005987 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005988 for (unsigned i = 0; i != NumInScalars; ++i) {
5989 // Ignore undef inputs.
5990 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00005991
Dan Gohman7f321562007-06-25 16:23:39 +00005992 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005993 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005994 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005995 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005996 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005997 break;
5998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005999
Dan Gohman7f321562007-06-25 16:23:39 +00006000 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00006001 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00006002 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006003 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006004 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006005 break;
6006 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006007
Chris Lattnerd7648c82006-03-28 20:28:38 +00006008 // Otherwise, remember this. We allow up to two distinct input vectors.
6009 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
6010 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006011
Gabor Greifba36cb52008-08-28 21:40:38 +00006012 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006013 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00006014 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00006015 VecIn2 = ExtractedFromVec;
6016 } else {
6017 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006018 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006019 break;
6020 }
6021 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006022
Chris Lattnerd7648c82006-03-28 20:28:38 +00006023 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00006024 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006025 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00006026 for (unsigned i = 0; i != NumInScalars; ++i) {
6027 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006029 continue;
6030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006031
Rafael Espindola15684b22009-04-24 12:40:33 +00006032 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00006034 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006035 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006036 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
6037 if (ExtIndex > VT.getVectorNumElements())
6038 return SDValue();
6039
6040 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006041 continue;
6042 }
6043
6044 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00006045 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00006046 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006048
Chris Lattnerd7648c82006-03-28 20:28:38 +00006049 // Add count and size info.
Chris Lattner2392ae72010-04-15 04:48:01 +00006050 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00006051 return SDValue();
6052
Dan Gohman7f321562007-06-25 16:23:39 +00006053 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00006054 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006055 Ops[0] = VecIn1;
Nate Begeman9008ca62009-04-27 18:41:29 +00006056 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
6057 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00006058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006059
Dan Gohman475871a2008-07-27 21:46:04 +00006060 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00006061}
6062
Dan Gohman475871a2008-07-27 21:46:04 +00006063SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006064 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
6065 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
6066 // inputs come from at most two distinct vectors, turn this into a shuffle
6067 // node.
6068
6069 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00006070 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00006071 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006072
Dan Gohman475871a2008-07-27 21:46:04 +00006073 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006074}
6075
Dan Gohman475871a2008-07-27 21:46:04 +00006076SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006077 return SDValue();
6078
Owen Andersone50ed302009-08-10 22:56:29 +00006079 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006080 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006081
Mon P Wangaeb06d22008-11-10 04:46:22 +00006082 SDValue N0 = N->getOperand(0);
Mon P Wangaeb06d22008-11-10 04:46:22 +00006083
6084 assert(N0.getValueType().getVectorNumElements() == NumElts &&
6085 "Vector shuffle must be normalized in DAG");
6086
Nate Begeman9008ca62009-04-27 18:41:29 +00006087 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Evan Chenge7bec0d2006-07-20 22:44:41 +00006088
Evan Cheng917ec982006-07-21 08:25:53 +00006089 // If it is a splat, check if the argument vector is a build_vector with
6090 // all scalar elements the same.
Nate Begeman9008ca62009-04-27 18:41:29 +00006091 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006092 SDNode *V = N0.getNode();
Nate Begeman9008ca62009-04-27 18:41:29 +00006093
Evan Cheng917ec982006-07-21 08:25:53 +00006094
Dan Gohman7f321562007-06-25 16:23:39 +00006095 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00006096 // not the number of vector elements, look through it. Be careful not to
6097 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00006098 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006099 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00006100 if (ConvInput.getValueType().isVector() &&
6101 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00006102 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00006103 }
6104
Dan Gohman7f321562007-06-25 16:23:39 +00006105 if (V->getOpcode() == ISD::BUILD_VECTOR) {
6106 unsigned NumElems = V->getNumOperands();
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Evan Cheng917ec982006-07-21 08:25:53 +00006108 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00006109 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00006110 bool AllSame = true;
6111 for (unsigned i = 0; i != NumElems; ++i) {
6112 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
6113 Base = V->getOperand(i);
6114 break;
6115 }
6116 }
6117 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00006118 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00006119 return N0;
6120 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00006121 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00006122 AllSame = false;
6123 break;
6124 }
6125 }
6126 // Splat of <x, x, x, x>, return <x, x, x, x>
6127 if (AllSame)
6128 return N0;
6129 }
6130 }
6131 }
Dan Gohman475871a2008-07-27 21:46:04 +00006132 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00006133}
6134
Evan Cheng44f1f092006-04-20 08:56:16 +00006135/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00006136/// an AND to a vector_shuffle with the destination vector and a zero vector.
6137/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00006138/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00006139SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00006140 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006142 SDValue LHS = N->getOperand(0);
6143 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00006144 if (N->getOpcode() == ISD::AND) {
6145 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00006146 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006147 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 SmallVector<int, 8> Indices;
6149 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00006150 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006151 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006152 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00006153 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006154 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006155 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00006156 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00006157 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00006158 else
Dan Gohman475871a2008-07-27 21:46:04 +00006159 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006160 }
6161
6162 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00006163 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006164 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00006165 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006166
Dan Gohman7f321562007-06-25 16:23:39 +00006167 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006168 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00006169 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00006170 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00006171 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
6172 RVT, &ZeroOps[0], ZeroOps.size());
6173 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
6174 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
6175 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00006176 }
6177 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006178
Dan Gohman475871a2008-07-27 21:46:04 +00006179 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00006180}
6181
Dan Gohman7f321562007-06-25 16:23:39 +00006182/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00006183SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00006184 // After legalize, the target may be depending on adds and other
6185 // binary ops to provide legal ways to construct constants or other
6186 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00006187 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00006188
Owen Andersone50ed302009-08-10 22:56:29 +00006189 EVT VT = N->getValueType(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006190 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00006191
Owen Andersone50ed302009-08-10 22:56:29 +00006192 EVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006193 SDValue LHS = N->getOperand(0);
6194 SDValue RHS = N->getOperand(1);
6195 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006196 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00006197
Dan Gohman7f321562007-06-25 16:23:39 +00006198 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00006199 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00006200 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00006201 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00006202 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00006203 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006204 SDValue LHSOp = LHS.getOperand(i);
6205 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00006206 // If these two elements can't be folded, bail out.
6207 if ((LHSOp.getOpcode() != ISD::UNDEF &&
6208 LHSOp.getOpcode() != ISD::Constant &&
6209 LHSOp.getOpcode() != ISD::ConstantFP) ||
6210 (RHSOp.getOpcode() != ISD::UNDEF &&
6211 RHSOp.getOpcode() != ISD::Constant &&
6212 RHSOp.getOpcode() != ISD::ConstantFP))
6213 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006214
Evan Cheng7b336a82006-05-31 06:08:35 +00006215 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00006216 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
6217 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00006218 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006219 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00006220 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006221 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00006222 break;
6223 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006224
Bill Wendling9729c5a2009-01-31 03:12:48 +00006225 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006226 EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00006227 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00006228 assert((Ops.back().getOpcode() == ISD::UNDEF ||
6229 Ops.back().getOpcode() == ISD::Constant ||
6230 Ops.back().getOpcode() == ISD::ConstantFP) &&
6231 "Scalar binop didn't fold!");
6232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006233
Dan Gohman7f321562007-06-25 16:23:39 +00006234 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006235 EVT VT = LHS.getValueType();
Evan Chenga87008d2009-02-25 22:49:59 +00006236 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
6237 &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00006238 }
Chris Lattneredab1b92006-04-02 03:25:57 +00006239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006240
Dan Gohman475871a2008-07-27 21:46:04 +00006241 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00006242}
6243
Bill Wendling836ca7d2009-01-30 23:59:18 +00006244SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
6245 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00006246 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006247
Bill Wendling836ca7d2009-01-30 23:59:18 +00006248 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00006249 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006250
Nate Begemanf845b452005-10-08 00:29:44 +00006251 // If we got a simplified select_cc node back from SimplifySelectCC, then
6252 // break it down into a new SETCC node, and a new SELECT node, and then return
6253 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00006254 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006255 // Check to see if we got a select_cc back (to turn into setcc/select).
6256 // Otherwise, just return whatever node we got back, like fabs.
6257 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006258 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
6259 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006260 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006261 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00006262 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006263 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
6264 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00006265 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006266
Nate Begemanf845b452005-10-08 00:29:44 +00006267 return SCC;
6268 }
Dan Gohman475871a2008-07-27 21:46:04 +00006269 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006270}
6271
Chris Lattner40c62d52005-10-18 06:04:22 +00006272/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
6273/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00006274/// select. Callers of this should assume that TheSelect is deleted if this
6275/// returns true. As such, they should return the appropriate thing (e.g. the
6276/// node) back to the top-level of the DAG combiner loop to avoid it being
6277/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00006278bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00006279 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006280
Chris Lattner40c62d52005-10-18 06:04:22 +00006281 // If this is a select from two identical things, try to pull the operation
6282 // through the select.
6283 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00006284 // If this is a load and the token chain is identical, replace the select
6285 // of two loads with a load through a select of the address to load from.
6286 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
6287 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00006288 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006289 // Do not let this transformation reduce the number of volatile loads.
6290 !cast<LoadSDNode>(LHS)->isVolatile() &&
6291 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00006292 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00006293 LHS.getOperand(0) == RHS.getOperand(0)) {
6294 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
6295 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
6296
6297 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006298 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohman75832d72009-10-31 14:14:04 +00006299 // FIXME: this discards src value information. This is
6300 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00006301 // both potential memory locations. Since we are discarding
6302 // src value info, don't do the transformation if the memory
6303 // locations are not in the default address space.
6304 unsigned LLDAddrSpace = 0, RLDAddrSpace = 0;
6305 if (const Value *LLDVal = LLD->getMemOperand()->getValue()) {
6306 if (const PointerType *PT = dyn_cast<PointerType>(LLDVal->getType()))
6307 LLDAddrSpace = PT->getAddressSpace();
6308 }
6309 if (const Value *RLDVal = RLD->getMemOperand()->getValue()) {
6310 if (const PointerType *PT = dyn_cast<PointerType>(RLDVal->getType()))
6311 RLDAddrSpace = PT->getAddressSpace();
6312 }
Dan Gohman475871a2008-07-27 21:46:04 +00006313 SDValue Addr;
Mon P Wangfe240b12010-01-11 20:12:49 +00006314 if (LLDAddrSpace == 0 && RLDAddrSpace == 0) {
6315 if (TheSelect->getOpcode() == ISD::SELECT) {
6316 // Check that the condition doesn't reach either load. If so, folding
6317 // this will induce a cycle into the DAG.
6318 if ((!LLD->hasAnyUseOfValue(1) ||
6319 !LLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) &&
6320 (!RLD->hasAnyUseOfValue(1) ||
6321 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()))) {
6322 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
6323 LLD->getBasePtr().getValueType(),
6324 TheSelect->getOperand(0), LLD->getBasePtr(),
6325 RLD->getBasePtr());
6326 }
6327 } else {
6328 // Check that the condition doesn't reach either load. If so, folding
6329 // this will induce a cycle into the DAG.
6330 if ((!LLD->hasAnyUseOfValue(1) ||
6331 (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6332 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()))) &&
6333 (!RLD->hasAnyUseOfValue(1) ||
6334 (!RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
6335 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())))) {
6336 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
6337 LLD->getBasePtr().getValueType(),
6338 TheSelect->getOperand(0),
6339 TheSelect->getOperand(1),
6340 LLD->getBasePtr(), RLD->getBasePtr(),
6341 TheSelect->getOperand(4));
6342 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006343 }
Evan Cheng466685d2006-10-09 20:57:25 +00006344 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006345
Gabor Greifba36cb52008-08-28 21:40:38 +00006346 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006347 SDValue Load;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006348 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
6349 Load = DAG.getLoad(TheSelect->getValueType(0),
6350 TheSelect->getDebugLoc(),
6351 LLD->getChain(),
Dan Gohman75832d72009-10-31 14:14:04 +00006352 Addr, 0, 0,
Scott Michelfdc40a02009-02-17 22:15:04 +00006353 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006354 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006355 LLD->getAlignment());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006356 } else {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006357 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006358 TheSelect->getDebugLoc(),
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006359 TheSelect->getValueType(0),
Dan Gohman75832d72009-10-31 14:14:04 +00006360 LLD->getChain(), Addr, 0, 0,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006361 LLD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006362 LLD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006363 LLD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006364 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006365 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006366
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006367 // Users of the select now use the result of the load.
6368 CombineTo(TheSelect, Load);
Scott Michelfdc40a02009-02-17 22:15:04 +00006369
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006370 // Users of the old loads now use the new load's chain. We know the
6371 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00006372 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
6373 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00006374 return true;
6375 }
Evan Chengc5484282006-10-04 00:56:09 +00006376 }
Chris Lattner40c62d52005-10-18 06:04:22 +00006377 }
6378 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006379
Chris Lattner40c62d52005-10-18 06:04:22 +00006380 return false;
6381}
6382
Chris Lattner600fec32009-03-11 05:08:08 +00006383/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
6384/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00006385SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00006386 SDValue N2, SDValue N3,
6387 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00006388 // (x ? y : y) -> y.
6389 if (N2 == N3) return N2;
6390
Owen Andersone50ed302009-08-10 22:56:29 +00006391 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00006392 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
6393 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
6394 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006395
6396 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00006397 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006398 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006399 if (SCC.getNode()) AddToWorkList(SCC.getNode());
6400 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006401
6402 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00006403 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006404 return N2;
6405 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00006406 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00006407 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00006408
Nate Begemanf845b452005-10-08 00:29:44 +00006409 // Check to see if we can simplify the select into an fabs node
6410 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
6411 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00006412 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00006413 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
6414 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
6415 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
6416 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006417 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006418
Nate Begemanf845b452005-10-08 00:29:44 +00006419 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
6420 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
6421 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
6422 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006423 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00006424 }
6425 }
Chris Lattner600fec32009-03-11 05:08:08 +00006426
6427 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
6428 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
6429 // in it. This is a win when the constant is not otherwise available because
6430 // it replaces two constant pool loads with one. We only do this if the FP
6431 // type is known to be legal, because if it isn't, then we are before legalize
6432 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00006433 // messing with soft float) and if the ConstantFP is not legal, because if
6434 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00006435 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
6436 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
6437 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00006438 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
6439 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00006440 // If both constants have multiple uses, then we won't need to do an
6441 // extra load, they are likely around in registers for other users.
6442 (TV->hasOneUse() || FV->hasOneUse())) {
6443 Constant *Elts[] = {
6444 const_cast<ConstantFP*>(FV->getConstantFPValue()),
6445 const_cast<ConstantFP*>(TV->getConstantFPValue())
6446 };
6447 const Type *FPTy = Elts[0]->getType();
6448 const TargetData &TD = *TLI.getTargetData();
6449
6450 // Create a ConstantArray of the two constants.
Owen Andersondebcb012009-07-29 22:17:13 +00006451 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner600fec32009-03-11 05:08:08 +00006452 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
6453 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00006454 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00006455
6456 // Get the offsets to the 0 and 1 element of the array so that we can
6457 // select between them.
6458 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00006459 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00006460 SDValue One = DAG.getIntPtrConstant(EltSize);
6461
6462 SDValue Cond = DAG.getSetCC(DL,
6463 TLI.getSetCCResultType(N0.getValueType()),
6464 N0, N1, CC);
6465 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
6466 Cond, One, Zero);
6467 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
6468 CstOffset);
6469 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
6470 PseudoSourceValue::getConstantPool(), 0, false,
David Greene1e559442010-02-15 17:00:31 +00006471 false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00006472
6473 }
6474 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006475
Nate Begemanf845b452005-10-08 00:29:44 +00006476 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00006477 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00006478 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006479 N0.getValueType().isInteger() &&
6480 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006481 (N1C->isNullValue() || // (a < 0) ? b : 0
6482 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00006483 EVT XType = N0.getValueType();
6484 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00006485 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00006486 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00006487 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00006488 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
6489 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006490 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands92abc622009-01-31 15:50:11 +00006491 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendling9729c5a2009-01-31 03:12:48 +00006492 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006493 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00006494 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006495
Duncan Sands8e4eb092008-06-08 20:54:56 +00006496 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006497 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006498 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006499 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006500
6501 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006502 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006503
Bill Wendling9729c5a2009-01-31 03:12:48 +00006504 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006505 XType, N0,
6506 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006507 getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00006508 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006509
Duncan Sands8e4eb092008-06-08 20:54:56 +00006510 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006511 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006512 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00006513 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006514
6515 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00006516 }
6517 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006518
Nate Begeman07ed4172005-10-10 21:26:48 +00006519 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00006520 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00006521 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006522
Chris Lattner1eba01e2007-04-11 06:50:51 +00006523 // If the caller doesn't want us to simplify this into a zext of a compare,
6524 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00006525 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006526 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006527
Nate Begeman07ed4172005-10-10 21:26:48 +00006528 // Get a SetCC of the condition
6529 // FIXME: Should probably make sure that setcc is legal if we ever have a
6530 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00006532 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00006533 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006534 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006535 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00006536 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00006537 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00006538 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00006539 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006540 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006541 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006542 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00006543 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006544 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00006545 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006546
Gabor Greifba36cb52008-08-28 21:40:38 +00006547 AddToWorkList(SCC.getNode());
6548 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006549
Dan Gohman002e5d02008-03-13 22:13:53 +00006550 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00006551 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00006552
Nate Begeman07ed4172005-10-10 21:26:48 +00006553 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00006554 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00006555 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands92abc622009-01-31 15:50:11 +00006556 getShiftAmountTy()));
Nate Begeman07ed4172005-10-10 21:26:48 +00006557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006558
Nate Begemanf845b452005-10-08 00:29:44 +00006559 // Check to see if this is the equivalent of setcc
6560 // FIXME: Turn all of these into setcc if setcc if setcc is legal
6561 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00006562 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006563 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00006564 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00006565 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006566 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00006567 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00006568 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00006569 return Res;
6570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006571
Bill Wendling836ca7d2009-01-30 23:59:18 +00006572 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00006573 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006574 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00006575 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006576 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006577 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006578 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands92abc622009-01-31 15:50:11 +00006579 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006580 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006581 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00006582 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00006583 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
6584 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00006585 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00006586 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006587 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00006588 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006589 getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00006590 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00006591 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00006592 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006593 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006594 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006595 getShiftAmountTy()));
Bill Wendling836ca7d2009-01-30 23:59:18 +00006596 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00006597 }
6598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006599
Nate Begemanf845b452005-10-08 00:29:44 +00006600 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
6601 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6602 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00006603 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006604 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006605 EVT XType = N0.getValueType();
Bill Wendling9729c5a2009-01-31 03:12:48 +00006606 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006607 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006608 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006609 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006610 N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006611 AddToWorkList(Shift.getNode());
6612 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006613 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00006614 }
6615 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6616 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6617 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6618 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6619 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersone50ed302009-08-10 22:56:29 +00006620 EVT XType = N0.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006621 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00006622 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendling836ca7d2009-01-30 23:59:18 +00006623 N0,
6624 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands92abc622009-01-31 15:50:11 +00006625 getShiftAmountTy()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00006626 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00006627 XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00006628 AddToWorkList(Shift.getNode());
6629 AddToWorkList(Add.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00006630 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00006631 }
6632 }
6633 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006634
Dan Gohman475871a2008-07-27 21:46:04 +00006635 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006636}
6637
Evan Chengfa1eb272007-02-08 22:13:59 +00006638/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00006639SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00006640 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006641 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006642 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00006643 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006644 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +00006645}
6646
Nate Begeman69575232005-10-20 02:15:44 +00006647/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6648/// return a DAG expression to select that will generate the same value by
6649/// multiplying by a magic number. See:
6650/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006651SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006652 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006654
Andrew Lenharth232c9102006-06-12 16:07:18 +00006655 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006656 ii != ee; ++ii)
6657 AddToWorkList(*ii);
6658 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006659}
6660
6661/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6662/// return a DAG expression to select that will generate the same value by
6663/// multiplying by a magic number. See:
6664/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00006665SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00006666 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00006667 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00006668
Andrew Lenharth232c9102006-06-12 16:07:18 +00006669 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00006670 ii != ee; ++ii)
6671 AddToWorkList(*ii);
6672 return S;
Nate Begeman69575232005-10-20 02:15:44 +00006673}
6674
Nate Begemancc66cdd2009-09-25 06:05:26 +00006675/// FindBaseOffset - Return true if base is a frame index, which is known not
6676// to alias with anything but itself. Provides base object and offset as results.
6677static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Dan Gohman46510a72010-04-15 01:51:59 +00006678 const GlobalValue *&GV, void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00006679 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006680 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00006681
Jim Laskey71382342006-10-07 23:37:56 +00006682 // If it's an adding a simple constant then integrate the offset.
6683 if (Base.getOpcode() == ISD::ADD) {
6684 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6685 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006686 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00006687 }
6688 }
Nate Begemancc66cdd2009-09-25 06:05:26 +00006689
6690 // Return the underlying GlobalValue, and update the Offset. Return false
6691 // for GlobalAddressSDNode since the same GlobalAddress may be represented
6692 // by multiple nodes with different offsets.
6693 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
6694 GV = G->getGlobal();
6695 Offset += G->getOffset();
6696 return false;
6697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006698
Nate Begemancc66cdd2009-09-25 06:05:26 +00006699 // Return the underlying Constant value, and update the Offset. Return false
6700 // for ConstantSDNodes since the same constant pool entry may be represented
6701 // by multiple nodes with different offsets.
6702 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
6703 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
6704 : (void *)C->getConstVal();
6705 Offset += C->getOffset();
6706 return false;
6707 }
Jim Laskey71382342006-10-07 23:37:56 +00006708 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00006709 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00006710}
6711
6712/// isAlias - Return true if there is any possibility that the two addresses
6713/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00006714bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00006715 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006716 unsigned SrcValueAlign1,
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006718 const Value *SrcValue2, int SrcValueOffset2,
6719 unsigned SrcValueAlign2) const {
Jim Laskey71382342006-10-07 23:37:56 +00006720 // If they are the same then they must be aliases.
6721 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006722
Jim Laskey71382342006-10-07 23:37:56 +00006723 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00006725 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00006726 const GlobalValue *GV1, *GV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00006727 void *CV1, *CV2;
6728 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
6729 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006730
Nate Begemancc66cdd2009-09-25 06:05:26 +00006731 // If they have a same base address then check to see if they overlap.
6732 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00006733 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Nate Begemancc66cdd2009-09-25 06:05:26 +00006735 // If we know what the bases are, and they aren't identical, then we know they
6736 // cannot alias.
6737 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
6738 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00006739
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006740 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
6741 // compared to the size and offset of the access, we may be able to prove they
6742 // do not alias. This check is conservative for now to catch cases created by
6743 // splitting vector types.
6744 if ((SrcValueAlign1 == SrcValueAlign2) &&
6745 (SrcValueOffset1 != SrcValueOffset2) &&
6746 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
6747 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
6748 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
6749
6750 // There is no overlap between these relatively aligned accesses of similar
6751 // size, return no alias.
6752 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
6753 return false;
6754 }
6755
Jim Laskey07a27092006-10-18 19:08:31 +00006756 if (CombinerGlobalAA) {
6757 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00006758 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6759 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6760 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00006761 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00006762 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00006763 if (AAResult == AliasAnalysis::NoAlias)
6764 return false;
6765 }
Jim Laskey096c22e2006-10-18 12:29:57 +00006766
6767 // Otherwise we have to assume they alias.
6768 return true;
Jim Laskey71382342006-10-07 23:37:56 +00006769}
6770
6771/// FindAliasInfo - Extracts the relevant alias information from the memory
6772/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00006773bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00006774 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006775 const Value *&SrcValue,
6776 int &SrcValueOffset,
6777 unsigned &SrcValueAlign) const {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006778 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6779 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006780 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006781 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006782 SrcValueOffset = LD->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006783 SrcValueAlign = LD->getOriginalAlignment();
Jim Laskey71382342006-10-07 23:37:56 +00006784 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006785 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00006786 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006787 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00006788 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00006789 SrcValueOffset = ST->getSrcValueOffset();
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006790 SrcValueAlign = ST->getOriginalAlignment();
Jim Laskey7ca56af2006-10-11 13:47:09 +00006791 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006792 llvm_unreachable("FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00006793 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006794
Jim Laskey71382342006-10-07 23:37:56 +00006795 return false;
6796}
6797
Jim Laskey6ff23e52006-10-04 16:53:27 +00006798/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6799/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006800void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6801 SmallVector<SDValue, 8> &Aliases) {
6802 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006803 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00006804
Jim Laskey279f0532006-09-25 16:29:54 +00006805 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00006806 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006807 int64_t Size;
6808 const Value *SrcValue;
6809 int SrcValueOffset;
6810 unsigned SrcValueAlign;
6811 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
6812 SrcValueAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00006813
Jim Laskey6ff23e52006-10-04 16:53:27 +00006814 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00006815 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00006816 unsigned Depth = 0;
6817
Jim Laskeybc588b82006-10-05 15:07:25 +00006818 // Look at each chain and determine if it is an alias. If so, add it to the
6819 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00006820 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00006821 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006822 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00006823 Chains.pop_back();
Nate Begeman677c89d2009-10-12 05:53:58 +00006824
6825 // For TokenFactor nodes, look at each operand and only continue up the
6826 // chain until we find two aliases. If we've seen two aliases, assume we'll
6827 // find more and revert to original chain since the xform is unlikely to be
6828 // profitable.
6829 //
6830 // FIXME: The depth check could be made to return the last non-aliasing
6831 // chain we found before we hit a tokenfactor rather than the original
6832 // chain.
6833 if (Depth > 6 || Aliases.size() == 2) {
6834 Aliases.clear();
6835 Aliases.push_back(OriginalChain);
6836 break;
6837 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006838
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006839 // Don't bother if we've been before.
6840 if (!Visited.insert(Chain.getNode()))
6841 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00006842
Jim Laskeybc588b82006-10-05 15:07:25 +00006843 switch (Chain.getOpcode()) {
6844 case ISD::EntryToken:
6845 // Entry token is ideal chain operand, but handled in FindBetterChain.
6846 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006847
Jim Laskeybc588b82006-10-05 15:07:25 +00006848 case ISD::LOAD:
6849 case ISD::STORE: {
6850 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006851 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006852 int64_t OpSize;
6853 const Value *OpSrcValue;
6854 int OpSrcValueOffset;
6855 unsigned OpSrcValueAlign;
Gabor Greifba36cb52008-08-28 21:40:38 +00006856 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006857 OpSrcValue, OpSrcValueOffset,
6858 OpSrcValueAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00006859
Jim Laskeybc588b82006-10-05 15:07:25 +00006860 // If chain is alias then stop here.
6861 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006862 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
6863 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
6864 OpSrcValueAlign)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00006865 Aliases.push_back(Chain);
6866 } else {
6867 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00006868 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00006869 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00006870 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006871 break;
6872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006873
Jim Laskeybc588b82006-10-05 15:07:25 +00006874 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006875 // We have to check each of the operands of the token factor for "small"
6876 // token factors, so we queue them up. Adding the operands to the queue
6877 // (stack) in reverse order maintains the original order and increases the
6878 // likelihood that getNode will find a matching token factor (CSE.)
6879 if (Chain.getNumOperands() > 16) {
6880 Aliases.push_back(Chain);
6881 break;
6882 }
Jim Laskeybc588b82006-10-05 15:07:25 +00006883 for (unsigned n = Chain.getNumOperands(); n;)
6884 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00006885 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00006886 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006887
Jim Laskeybc588b82006-10-05 15:07:25 +00006888 default:
6889 // For all other instructions we will just have to take what we can get.
6890 Aliases.push_back(Chain);
6891 break;
Jim Laskey279f0532006-09-25 16:29:54 +00006892 }
6893 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00006894}
6895
6896/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6897/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00006898SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6899 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00006900
Jim Laskey6ff23e52006-10-04 16:53:27 +00006901 // Accumulate all the aliases to this node.
6902 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00006903
Jim Laskey6ff23e52006-10-04 16:53:27 +00006904 if (Aliases.size() == 0) {
6905 // If no operands then chain to entry token.
6906 return DAG.getEntryNode();
6907 } else if (Aliases.size() == 1) {
6908 // If a single operand then chain to it. We don't need to revisit it.
6909 return Aliases[0];
6910 }
Nate Begeman677c89d2009-10-12 05:53:58 +00006911
Jim Laskey6ff23e52006-10-04 16:53:27 +00006912 // Construct a custom tailored token factor.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006913 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
6914 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00006915}
6916
Nate Begeman1d4d4142005-09-01 00:19:25 +00006917// SelectionDAG::Combine - This is the entry point for the file.
6918//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006919void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00006920 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00006921 /// run - This is the main entry point to this class.
6922 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00006923 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006924}