blob: fb17c93e2c480ee2686897bc0c1d02c3e4679f78 [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 Lattnerc76d4412007-05-16 06:37:59 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000031#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039using namespace llvm;
40
Chris Lattnercd3245a2006-12-19 22:41:21 +000041STATISTIC(NodesCombined , "Number of dag nodes combined");
42STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
43STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000044STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000045STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
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>
Owen Anderson0dcc8142010-09-19 21:01:26 +000049 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.
James Molloy6660c052012-02-16 09:17:04 +000067 //
68 // This has the semantics that when adding to the worklist,
69 // the item added must be next to be processed. It should
70 // also only appear once. The naive approach to this takes
71 // linear time.
72 //
73 // To reduce the insert/remove time to logarithmic, we use
74 // a set and a vector to maintain our worklist.
75 //
76 // The set contains the items on the worklist, but does not
77 // maintain the order they should be visited.
78 //
79 // The vector maintains the order nodes should be visited, but may
80 // contain duplicate or removed nodes. When choosing a node to
81 // visit, we pop off the order stack until we find an item that is
82 // also in the contents set. All operations are O(log N).
83 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000084 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
Jim Laskeyc7c3f112006-10-16 20:52:31 +000086 // AA - Used for DAG load/store alias analysis.
87 AliasAnalysis &AA;
88
Nate Begeman1d4d4142005-09-01 00:19:25 +000089 /// AddUsersToWorkList - When an instruction is simplified, add all users of
90 /// the instruction to the work lists because they might get more simplified
91 /// now.
92 ///
93 void AddUsersToWorkList(SDNode *N) {
94 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000095 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000096 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000097 }
98
Dan Gohman389079b2007-10-08 17:57:15 +000099 /// visit - call the node-specific routine that knows how to fold each
100 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000104 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000105 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000106 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000107 WorkListContents.insert(N);
108 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000111 /// removeFromWorkList - remove all instances of N from the worklist.
112 ///
113 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000114 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000115 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000118 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000121 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Dan Gohman475871a2008-07-27 21:46:04 +0000124 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000125 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000126 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000127 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000128 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000129
130 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132 private:
133
Chris Lattner012f2412006-02-17 21:58:01 +0000134 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000135 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000136 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000138 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
139 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000140 return SimplifyDemandedBits(Op, Demanded);
141 }
142
Dan Gohman475871a2008-07-27 21:46:04 +0000143 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000144
Chris Lattner448f2192006-11-11 00:39:41 +0000145 bool CombineToPreIndexedLoadStore(SDNode *N);
146 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Evan Cheng95c57ea2010-04-24 04:43:44 +0000148 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
149 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
150 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
151 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000152 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000153 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000154 SDValue PromoteExtend(SDValue Op);
155 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000157 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000158 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000159 ISD::NodeType ExtType);
160
Dan Gohman389079b2007-10-08 17:57:15 +0000161 /// combine - call the node-specific routine that knows how to fold each
162 /// particular type of node. If that doesn't do anything, try the
163 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165
166 // Visitation implementation - Implement dag node combining for different
167 // node types. The semantics are as follows:
168 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000169 // SDValue.getNode() == 0 - No change was made
170 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
171 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000172 //
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDValue visitTokenFactor(SDNode *N);
174 SDValue visitMERGE_VALUES(SDNode *N);
175 SDValue visitADD(SDNode *N);
176 SDValue visitSUB(SDNode *N);
177 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000178 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000180 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue visitMUL(SDNode *N);
182 SDValue visitSDIV(SDNode *N);
183 SDValue visitUDIV(SDNode *N);
184 SDValue visitSREM(SDNode *N);
185 SDValue visitUREM(SDNode *N);
186 SDValue visitMULHU(SDNode *N);
187 SDValue visitMULHS(SDNode *N);
188 SDValue visitSMUL_LOHI(SDNode *N);
189 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000190 SDValue visitSMULO(SDNode *N);
191 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitSDIVREM(SDNode *N);
193 SDValue visitUDIVREM(SDNode *N);
194 SDValue visitAND(SDNode *N);
195 SDValue visitOR(SDNode *N);
196 SDValue visitXOR(SDNode *N);
197 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000198 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitSHL(SDNode *N);
200 SDValue visitSRA(SDNode *N);
201 SDValue visitSRL(SDNode *N);
202 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000205 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue visitCTPOP(SDNode *N);
207 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000208 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue visitSELECT_CC(SDNode *N);
210 SDValue visitSETCC(SDNode *N);
211 SDValue visitSIGN_EXTEND(SDNode *N);
212 SDValue visitZERO_EXTEND(SDNode *N);
213 SDValue visitANY_EXTEND(SDNode *N);
214 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
215 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000216 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue visitBUILD_PAIR(SDNode *N);
218 SDValue visitFADD(SDNode *N);
219 SDValue visitFSUB(SDNode *N);
220 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000221 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue visitFDIV(SDNode *N);
223 SDValue visitFREM(SDNode *N);
224 SDValue visitFCOPYSIGN(SDNode *N);
225 SDValue visitSINT_TO_FP(SDNode *N);
226 SDValue visitUINT_TO_FP(SDNode *N);
227 SDValue visitFP_TO_SINT(SDNode *N);
228 SDValue visitFP_TO_UINT(SDNode *N);
229 SDValue visitFP_ROUND(SDNode *N);
230 SDValue visitFP_ROUND_INREG(SDNode *N);
231 SDValue visitFP_EXTEND(SDNode *N);
232 SDValue visitFNEG(SDNode *N);
233 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000234 SDValue visitFCEIL(SDNode *N);
235 SDValue visitFTRUNC(SDNode *N);
236 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000237 SDValue visitBRCOND(SDNode *N);
238 SDValue visitBR_CC(SDNode *N);
239 SDValue visitLOAD(SDNode *N);
240 SDValue visitSTORE(SDNode *N);
241 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
242 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
243 SDValue visitBUILD_VECTOR(SDNode *N);
244 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000245 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000247
Dan Gohman475871a2008-07-27 21:46:04 +0000248 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000249 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000252
Dan Gohman475871a2008-07-27 21:46:04 +0000253 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
254 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000255 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
256 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000257 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000258 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000259 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000260 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000261 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000262 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000263 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000265 SDValue BuildSDIV(SDNode *N);
266 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000267 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
268 bool DemandHighBits = true);
269 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000270 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000272 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000273 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000274 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000275 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Dan Gohman475871a2008-07-27 21:46:04 +0000277 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
280 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000281 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
282 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000283
Jim Laskey096c22e2006-10-18 12:29:57 +0000284 /// isAlias - Return true if there is any possibility that the two addresses
285 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000286 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000287 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000288 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000289 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000291 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000292 unsigned SrcValueAlign2,
293 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000294
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000295 /// isAlias - Return true if there is any possibility that the two addresses
296 /// overlap.
297 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
298
Jim Laskey7ca56af2006-10-11 13:47:09 +0000299 /// FindAliasInfo - Extracts the relevant alias information from the memory
300 /// node. Returns true if the operand was a load.
301 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000303 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000304 unsigned &SrcValueAlignment,
305 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000309 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000310
Nadav Rotemc653de62012-10-03 16:11:15 +0000311 /// Merge consecutive store operations into a wide store.
312 /// This optimization uses wide integers or vectors when possible.
313 /// \return True if some memory operations were changed.
314 bool MergeConsecutiveStores(StoreSDNode *N);
315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000317 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000318 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000320
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000322 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323
Chris Lattner2392ae72010-04-15 04:48:01 +0000324 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000325
Chris Lattner2392ae72010-04-15 04:48:01 +0000326 /// getShiftAmountTy - Returns a type large enough to hold any valid
327 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000328 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000329 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
330 if (LHSTy.isVector())
331 return LHSTy;
332 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334
Chris Lattner2392ae72010-04-15 04:48:01 +0000335 /// isTypeLegal - This method returns true if we are running before type
336 /// legalization or if the specified VT is legal.
337 bool isTypeLegal(const EVT &VT) {
338 if (!LegalTypes) return true;
339 return TLI.isTypeLegal(VT);
340 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000341
342 /// getSetCCResultType - Convenience wrapper around
343 /// TargetLowering::getSetCCResultType
344 EVT getSetCCResultType(EVT VT) const {
345 return TLI.getSetCCResultType(*DAG.getContext(), VT);
346 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000347 };
348}
349
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000350
351namespace {
352/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
353/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000354class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000355 DAGCombiner &DC;
356public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000357 explicit WorkListRemover(DAGCombiner &dc)
358 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Duncan Sandsedfcf592008-06-11 11:42:12 +0000360 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000361 DC.removeFromWorkList(N);
362 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000363};
364}
365
Chris Lattner24664722006-03-01 04:53:38 +0000366//===----------------------------------------------------------------------===//
367// TargetLowering::DAGCombinerInfo implementation
368//===----------------------------------------------------------------------===//
369
370void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
371 ((DAGCombiner*)DC)->AddToWorkList(N);
372}
373
Cameron Zwariched3caf92011-04-02 02:40:26 +0000374void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
375 ((DAGCombiner*)DC)->removeFromWorkList(N);
376}
377
Dan Gohman475871a2008-07-27 21:46:04 +0000378SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000379CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
380 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000381}
382
Dan Gohman475871a2008-07-27 21:46:04 +0000383SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000384CombineTo(SDNode *N, SDValue Res, bool AddTo) {
385 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000386}
387
388
Dan Gohman475871a2008-07-27 21:46:04 +0000389SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000390CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
391 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000392}
393
Dan Gohmane5af2d32009-01-29 01:59:02 +0000394void TargetLowering::DAGCombinerInfo::
395CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
396 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
397}
Chris Lattner24664722006-03-01 04:53:38 +0000398
Chris Lattner24664722006-03-01 04:53:38 +0000399//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000400// Helper Functions
401//===----------------------------------------------------------------------===//
402
403/// isNegatibleForFree - Return 1 if we can compute the negated form of the
404/// specified expression for the same cost as the expression itself, or 2 if we
405/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000407 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000408 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000409 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000410 // fneg is removable even if it has multiple uses.
411 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Chris Lattner29446522007-05-14 22:04:50 +0000413 // Don't allow anything with multiple uses.
414 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Chris Lattner3adf9512007-05-25 02:19:06 +0000416 // Don't recurse exponentially.
417 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000418
Chris Lattner29446522007-05-14 22:04:50 +0000419 switch (Op.getOpcode()) {
420 default: return false;
421 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000422 // Don't invert constant FP values after legalize. The negated constant
423 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000424 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000425 case ISD::FADD:
426 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000427 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000428
Owen Andersonafd3d562012-03-06 00:29:31 +0000429 // After operation legalization, it might not be legal to create new FSUBs.
430 if (LegalOperations &&
431 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
432 return 0;
433
Craig Topper956342b2012-09-09 22:58:45 +0000434 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000435 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
436 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000438 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000439 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000441 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000442 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000443 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Bill Wendlingd34470c2009-01-30 23:10:18 +0000445 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000446 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Chris Lattner29446522007-05-14 22:04:50 +0000448 case ISD::FMUL:
449 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000450 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000451
Bill Wendlingd34470c2009-01-30 23:10:18 +0000452 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000453 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
454 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000455 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Owen Andersonafd3d562012-03-06 00:29:31 +0000457 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000458 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner29446522007-05-14 22:04:50 +0000460 case ISD::FP_EXTEND:
461 case ISD::FP_ROUND:
462 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000463 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000465 }
466}
467
468/// GetNegatedExpression - If isNegatibleForFree returns true, this function
469/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000470static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000471 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000472 // fneg is removable even if it has multiple uses.
473 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000474
Chris Lattner29446522007-05-14 22:04:50 +0000475 // Don't allow anything with multiple uses.
476 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Chris Lattner3adf9512007-05-25 02:19:06 +0000478 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000479 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000480 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000481 case ISD::ConstantFP: {
482 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
483 V.changeSign();
484 return DAG.getConstantFP(V, Op.getValueType());
485 }
Chris Lattner29446522007-05-14 22:04:50 +0000486 case ISD::FADD:
487 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000488 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000489
Bill Wendlingd34470c2009-01-30 23:10:18 +0000490 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000491 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000492 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000493 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000494 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000495 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000496 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000497 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000498 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000499 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000500 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000501 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000502 Op.getOperand(0));
503 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000504 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000506
Bill Wendlingd34470c2009-01-30 23:10:18 +0000507 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000508 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000509 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000510 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000511
Bill Wendlingd34470c2009-01-30 23:10:18 +0000512 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000513 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000514 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Chris Lattner29446522007-05-14 22:04:50 +0000516 case ISD::FMUL:
517 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000518 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000519
Bill Wendlingd34470c2009-01-30 23:10:18 +0000520 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000521 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000522 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000523 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000524 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000525 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000526 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000527 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000528
Bill Wendlingd34470c2009-01-30 23:10:18 +0000529 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000530 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000531 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000532 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000533 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000534
Chris Lattner29446522007-05-14 22:04:50 +0000535 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000537 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000538 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000539 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000540 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000541 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000542 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000543 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000544 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000545 }
546}
Chris Lattner24664722006-03-01 04:53:38 +0000547
548
Nate Begeman4ebd8052005-09-01 23:24:04 +0000549// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
550// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000551// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000552// nodes based on the type of node we are checking. This simplifies life a
553// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000554static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
555 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 if (N.getOpcode() == ISD::SETCC) {
557 LHS = N.getOperand(0);
558 RHS = N.getOperand(1);
559 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000560 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000562 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563 N.getOperand(2).getOpcode() == ISD::Constant &&
564 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000565 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
567 LHS = N.getOperand(0);
568 RHS = N.getOperand(1);
569 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000571 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 return false;
573}
574
Nate Begeman99801192005-09-07 23:25:52 +0000575// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
576// one use. If this is true, it allows the users to invert the operation for
577// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000578static bool isOneUseSetCC(SDValue N) {
579 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000580 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000581 return true;
582 return false;
583}
584
Andrew Trickac6d9be2013-05-25 02:42:55 +0000585SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000586 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000587 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
589 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N0.getOperand(1)),
594 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000598 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000599 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000602 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Nate Begemancd4d58c2006-02-03 06:46:56 +0000606 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
607 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000608 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000609 SDValue OpNode =
610 DAG.FoldConstantArithmetic(Opc, VT,
611 cast<ConstantSDNode>(N1.getOperand(1)),
612 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000613 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000614 }
615 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000616 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000617 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000618 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000619 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000620 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000621 }
622 }
Bill Wendling35247c32009-01-30 00:45:56 +0000623
Dan Gohman475871a2008-07-27 21:46:04 +0000624 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000625}
626
Dan Gohman475871a2008-07-27 21:46:04 +0000627SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
628 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000629 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
630 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000631 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000632 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000633 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000634 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000635 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000636 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000637 assert((!To[i].getNode() ||
638 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000639 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000641 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000642 if (AddTo) {
643 // Push the new nodes and any users onto the worklist
644 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000645 if (To[i].getNode()) {
646 AddToWorkList(To[i].getNode());
647 AddUsersToWorkList(To[i].getNode());
648 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000649 }
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Dan Gohmandbe664a2009-01-19 21:44:21 +0000652 // Finally, if the node is now dead, remove it from the graph. The node
653 // may not be dead if the replacement process recursively simplified to
654 // something else needing this node.
655 if (N->use_empty()) {
656 // Nodes can be reintroduced into the worklist. Make sure we do not
657 // process a node that has been replaced.
658 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000659
Dan Gohmandbe664a2009-01-19 21:44:21 +0000660 // Finally, since the node is now dead, remove it from the graph.
661 DAG.DeleteNode(N);
662 }
Dan Gohman475871a2008-07-27 21:46:04 +0000663 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000664}
665
Evan Chenge5b51ac2010-04-17 06:13:15 +0000666void DAGCombiner::
667CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000668 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000669 // are deleted, make sure to remove them from our worklist.
670 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000671 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000672
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000673 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000674 AddToWorkList(TLO.New.getNode());
675 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000677 // Finally, if the node is now dead, remove it from the graph. The node
678 // may not be dead if the replacement process recursively simplified to
679 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000680 if (TLO.Old.getNode()->use_empty()) {
681 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000683 // If the operands of this node are only used by the node, they will now
684 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000685 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
686 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
687 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Gabor Greifba36cb52008-08-28 21:40:38 +0000689 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000690 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000691}
692
693/// SimplifyDemandedBits - Check the specified integer node value to see if
694/// it can be simplified or if things it uses can be simplified by bit
695/// propagation. If so, return true.
696bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000697 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000698 APInt KnownZero, KnownOne;
699 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
700 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Dan Gohmane5af2d32009-01-29 01:59:02 +0000702 // Revisit the node.
703 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000704
Dan Gohmane5af2d32009-01-29 01:59:02 +0000705 // Replace the old value with the new one.
706 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000708 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000709 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000710 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000711 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000712
Dan Gohmane5af2d32009-01-29 01:59:02 +0000713 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000714 return true;
715}
716
Evan Cheng95c57ea2010-04-24 04:43:44 +0000717void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000718 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000719 EVT VT = Load->getValueType(0);
720 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000721
Evan Cheng95c57ea2010-04-24 04:43:44 +0000722 DEBUG(dbgs() << "\nReplacing.9 ";
723 Load->dump(&DAG);
724 dbgs() << "\nWith: ";
725 Trunc.getNode()->dump(&DAG);
726 dbgs() << '\n');
727 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000728 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
729 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000730 removeFromWorkList(Load);
731 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000732 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000733}
734
735SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
736 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000737 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000738 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000739 EVT MemVT = LD->getMemoryVT();
740 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000741 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000742 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000743 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000744 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000745 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000746 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000747 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000748 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000749 LD->isNonTemporal(), LD->getAlignment());
750 }
751
Evan Cheng4c26e932010-04-19 19:29:22 +0000752 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000753 switch (Opc) {
754 default: break;
755 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000756 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000758 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000759 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000760 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000761 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000762 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000763 case ISD::Constant: {
764 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000765 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000766 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767 }
Evan Chengcaf77402010-04-23 19:10:30 +0000768 }
769
770 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000772 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000773}
774
Evan Cheng95c57ea2010-04-24 04:43:44 +0000775SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000776 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
777 return SDValue();
778 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000779 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000780 bool Replace = false;
781 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
782 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000783 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000784 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000785
786 if (Replace)
787 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
788 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000789 DAG.getValueType(OldVT));
790}
791
Evan Cheng95c57ea2010-04-24 04:43:44 +0000792SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000793 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000794 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000795 bool Replace = false;
796 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
797 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000798 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000799 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000800
801 if (Replace)
802 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
803 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804}
805
Evan Cheng64b7bf72010-04-16 06:14:10 +0000806/// PromoteIntBinOp - Promote the specified integer binary operation if the
807/// target indicates it is beneficial. e.g. On x86, it's usually better to
808/// promote i16 operations to i32 since i16 instructions are longer.
809SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
810 if (!LegalOperations)
811 return SDValue();
812
813 EVT VT = Op.getValueType();
814 if (VT.isVector() || !VT.isInteger())
815 return SDValue();
816
Evan Chenge5b51ac2010-04-17 06:13:15 +0000817 // If operation type is 'undesirable', e.g. i16 on x86, consider
818 // promoting it.
819 unsigned Opc = Op.getOpcode();
820 if (TLI.isTypeDesirableForOp(Opc, VT))
821 return SDValue();
822
Evan Cheng64b7bf72010-04-16 06:14:10 +0000823 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000824 // Consult target whether it is a good idea to promote this operation and
825 // what's the right type to promote it to.
826 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000827 assert(PVT != VT && "Don't know what type to promote to!");
828
Evan Cheng95c57ea2010-04-24 04:43:44 +0000829 bool Replace0 = false;
830 SDValue N0 = Op.getOperand(0);
831 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
832 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000833 return SDValue();
834
Evan Cheng95c57ea2010-04-24 04:43:44 +0000835 bool Replace1 = false;
836 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000837 SDValue NN1;
838 if (N0 == N1)
839 NN1 = NN0;
840 else {
841 NN1 = PromoteOperand(N1, PVT, Replace1);
842 if (NN1.getNode() == 0)
843 return SDValue();
844 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000845
Evan Cheng95c57ea2010-04-24 04:43:44 +0000846 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000847 if (NN1.getNode())
848 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000849
850 if (Replace0)
851 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
852 if (Replace1)
853 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000854
Evan Chengac7eae52010-04-27 19:48:13 +0000855 DEBUG(dbgs() << "\nPromoting ";
856 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000857 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000858 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000859 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000860 }
861 return SDValue();
862}
863
864/// PromoteIntShiftOp - Promote the specified integer shift operation if the
865/// target indicates it is beneficial. e.g. On x86, it's usually better to
866/// promote i16 operations to i32 since i16 instructions are longer.
867SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
868 if (!LegalOperations)
869 return SDValue();
870
871 EVT VT = Op.getValueType();
872 if (VT.isVector() || !VT.isInteger())
873 return SDValue();
874
875 // If operation type is 'undesirable', e.g. i16 on x86, consider
876 // promoting it.
877 unsigned Opc = Op.getOpcode();
878 if (TLI.isTypeDesirableForOp(Opc, VT))
879 return SDValue();
880
881 EVT PVT = VT;
882 // Consult target whether it is a good idea to promote this operation and
883 // what's the right type to promote it to.
884 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
885 assert(PVT != VT && "Don't know what type to promote to!");
886
Evan Cheng95c57ea2010-04-24 04:43:44 +0000887 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000888 SDValue N0 = Op.getOperand(0);
889 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000890 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000891 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000892 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000893 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000894 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000895 if (N0.getNode() == 0)
896 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000897
Evan Chenge5b51ac2010-04-17 06:13:15 +0000898 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000899 if (Replace)
900 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000901
Evan Chengac7eae52010-04-27 19:48:13 +0000902 DEBUG(dbgs() << "\nPromoting ";
903 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000904 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000905 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000906 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000907 }
908 return SDValue();
909}
910
Evan Cheng4c26e932010-04-19 19:29:22 +0000911SDValue DAGCombiner::PromoteExtend(SDValue Op) {
912 if (!LegalOperations)
913 return SDValue();
914
915 EVT VT = Op.getValueType();
916 if (VT.isVector() || !VT.isInteger())
917 return SDValue();
918
919 // If operation type is 'undesirable', e.g. i16 on x86, consider
920 // promoting it.
921 unsigned Opc = Op.getOpcode();
922 if (TLI.isTypeDesirableForOp(Opc, VT))
923 return SDValue();
924
925 EVT PVT = VT;
926 // Consult target whether it is a good idea to promote this operation and
927 // what's the right type to promote it to.
928 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
929 assert(PVT != VT && "Don't know what type to promote to!");
930 // fold (aext (aext x)) -> (aext x)
931 // fold (aext (zext x)) -> (zext x)
932 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000933 DEBUG(dbgs() << "\nPromoting ";
934 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000935 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000936 }
937 return SDValue();
938}
939
940bool DAGCombiner::PromoteLoad(SDValue Op) {
941 if (!LegalOperations)
942 return false;
943
944 EVT VT = Op.getValueType();
945 if (VT.isVector() || !VT.isInteger())
946 return false;
947
948 // If operation type is 'undesirable', e.g. i16 on x86, consider
949 // promoting it.
950 unsigned Opc = Op.getOpcode();
951 if (TLI.isTypeDesirableForOp(Opc, VT))
952 return false;
953
954 EVT PVT = VT;
955 // Consult target whether it is a good idea to promote this operation and
956 // what's the right type to promote it to.
957 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
958 assert(PVT != VT && "Don't know what type to promote to!");
959
Andrew Trickac6d9be2013-05-25 02:42:55 +0000960 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000961 SDNode *N = Op.getNode();
962 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000963 EVT MemVT = LD->getMemoryVT();
964 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000965 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000966 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000967 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000968 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000969 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000970 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000971 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000972 LD->isNonTemporal(), LD->getAlignment());
973 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
974
Evan Cheng95c57ea2010-04-24 04:43:44 +0000975 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000976 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000977 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000978 Result.getNode()->dump(&DAG);
979 dbgs() << '\n');
980 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000981 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
982 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000983 removeFromWorkList(N);
984 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000985 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000986 return true;
987 }
988 return false;
989}
990
Evan Chenge5b51ac2010-04-17 06:13:15 +0000991
Chris Lattner29446522007-05-14 22:04:50 +0000992//===----------------------------------------------------------------------===//
993// Main DAG Combiner implementation
994//===----------------------------------------------------------------------===//
995
Duncan Sands25cf2272008-11-24 14:53:14 +0000996void DAGCombiner::Run(CombineLevel AtLevel) {
997 // set the instance variables, so that the various visit routines may use it.
998 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000999 LegalOperations = Level >= AfterLegalizeVectorOps;
1000 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001001
Evan Cheng17a568b2008-08-29 22:21:44 +00001002 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001003 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1004 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001005 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001006
Evan Cheng17a568b2008-08-29 22:21:44 +00001007 // Create a dummy node (which is not added to allnodes), that adds a reference
1008 // to the root node, preventing it from being deleted, and tracking any
1009 // changes of the root.
1010 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001011
Jim Laskey26f7fa72006-10-17 19:33:52 +00001012 // The root of the dag may dangle to deleted nodes until the dag combiner is
1013 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001014 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001015
James Molloy6660c052012-02-16 09:17:04 +00001016 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001018 while (!WorkListContents.empty()) {
1019 SDNode *N;
1020 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1021 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1022 // worklist *should* contain, and check the node we want to visit is should
1023 // actually be visited.
1024 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001025 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001026 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1029 // N is deleted from the DAG, since they too may now be dead or may have a
1030 // reduced number of uses, allowing other xforms.
1031 if (N->use_empty() && N != &Dummy) {
1032 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1033 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 DAG.DeleteNode(N);
1036 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001038
Evan Cheng17a568b2008-08-29 22:21:44 +00001039 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001040
Evan Cheng17a568b2008-08-29 22:21:44 +00001041 if (RV.getNode() == 0)
1042 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001043
Evan Cheng17a568b2008-08-29 22:21:44 +00001044 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Evan Cheng17a568b2008-08-29 22:21:44 +00001046 // If we get back the same node we passed in, rather than a new node or
1047 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001049 // mechanics for us, we have no work to do in this case.
1050 if (RV.getNode() == N)
1051 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Evan Cheng17a568b2008-08-29 22:21:44 +00001053 assert(N->getOpcode() != ISD::DELETED_NODE &&
1054 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1055 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001056
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001058 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001059 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001060 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001061 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001062
Devang Patel9728ea22011-05-23 22:04:42 +00001063 // Transfer debug value.
1064 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001065 WorkListRemover DeadNodes(*this);
1066 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001067 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001068 else {
1069 assert(N->getValueType(0) == RV.getValueType() &&
1070 N->getNumValues() == 1 && "Type mismatch");
1071 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001072 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001073 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Evan Cheng17a568b2008-08-29 22:21:44 +00001075 // Push the new node and any users onto the worklist
1076 AddToWorkList(RV.getNode());
1077 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Evan Cheng17a568b2008-08-29 22:21:44 +00001079 // Add any uses of the old node to the worklist in case this node is the
1080 // last one that uses them. They may become dead after this node is
1081 // deleted.
1082 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1083 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Dan Gohmandbe664a2009-01-19 21:44:21 +00001085 // Finally, if the node is now dead, remove it from the graph. The node
1086 // may not be dead if the replacement process recursively simplified to
1087 // something else needing this node.
1088 if (N->use_empty()) {
1089 // Nodes can be reintroduced into the worklist. Make sure we do not
1090 // process a node that has been replaced.
1091 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001092
Dan Gohmandbe664a2009-01-19 21:44:21 +00001093 // Finally, since the node is now dead, remove it from the graph.
1094 DAG.DeleteNode(N);
1095 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner95038592005-10-05 06:35:28 +00001098 // If the root changed (e.g. it was a dead load, update the root).
1099 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001100 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101}
1102
Dan Gohman475871a2008-07-27 21:46:04 +00001103SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001104 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001106 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001107 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 case ISD::ADD: return visitADD(N);
1109 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001110 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001111 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001112 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001113 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 case ISD::MUL: return visitMUL(N);
1115 case ISD::SDIV: return visitSDIV(N);
1116 case ISD::UDIV: return visitUDIV(N);
1117 case ISD::SREM: return visitSREM(N);
1118 case ISD::UREM: return visitUREM(N);
1119 case ISD::MULHU: return visitMULHU(N);
1120 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001121 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1122 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001123 case ISD::SMULO: return visitSMULO(N);
1124 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001125 case ISD::SDIVREM: return visitSDIVREM(N);
1126 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 case ISD::AND: return visitAND(N);
1128 case ISD::OR: return visitOR(N);
1129 case ISD::XOR: return visitXOR(N);
1130 case ISD::SHL: return visitSHL(N);
1131 case ISD::SRA: return visitSRA(N);
1132 case ISD::SRL: return visitSRL(N);
1133 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001134 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001136 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001138 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001139 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001140 case ISD::SELECT_CC: return visitSELECT_CC(N);
1141 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1143 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001144 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001145 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1146 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001147 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001148 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001149 case ISD::FADD: return visitFADD(N);
1150 case ISD::FSUB: return visitFSUB(N);
1151 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001152 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001153 case ISD::FDIV: return visitFDIV(N);
1154 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001155 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1157 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1158 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1159 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1160 case ISD::FP_ROUND: return visitFP_ROUND(N);
1161 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1162 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1163 case ISD::FNEG: return visitFNEG(N);
1164 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001165 case ISD::FFLOOR: return visitFFLOOR(N);
1166 case ISD::FCEIL: return visitFCEIL(N);
1167 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001168 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001169 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001170 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001171 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001172 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001173 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001174 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1175 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001176 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001177 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 }
Dan Gohman475871a2008-07-27 21:46:04 +00001179 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180}
1181
Dan Gohman475871a2008-07-27 21:46:04 +00001182SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001184
1185 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001187 assert(N->getOpcode() != ISD::DELETED_NODE &&
1188 "Node was deleted but visit returned NULL!");
1189
1190 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1191 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1192
1193 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001194 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001195 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001196
1197 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1198 }
1199 }
1200
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001201 // If nothing happened still, try promoting the operation.
1202 if (RV.getNode() == 0) {
1203 switch (N->getOpcode()) {
1204 default: break;
1205 case ISD::ADD:
1206 case ISD::SUB:
1207 case ISD::MUL:
1208 case ISD::AND:
1209 case ISD::OR:
1210 case ISD::XOR:
1211 RV = PromoteIntBinOp(SDValue(N, 0));
1212 break;
1213 case ISD::SHL:
1214 case ISD::SRA:
1215 case ISD::SRL:
1216 RV = PromoteIntShiftOp(SDValue(N, 0));
1217 break;
1218 case ISD::SIGN_EXTEND:
1219 case ISD::ZERO_EXTEND:
1220 case ISD::ANY_EXTEND:
1221 RV = PromoteExtend(SDValue(N, 0));
1222 break;
1223 case ISD::LOAD:
1224 if (PromoteLoad(SDValue(N, 0)))
1225 RV = SDValue(N, 0);
1226 break;
1227 }
1228 }
1229
Scott Michelfdc40a02009-02-17 22:15:04 +00001230 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001231 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001232 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001233 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1234 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue N0 = N->getOperand(0);
1236 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001237
Evan Cheng08b11732008-03-22 01:55:50 +00001238 // Constant operands are canonicalized to RHS.
1239 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001240 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001241 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1242 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001243 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001244 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001245 }
1246 }
1247
Dan Gohman389079b2007-10-08 17:57:15 +00001248 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001249}
Dan Gohman389079b2007-10-08 17:57:15 +00001250
Chris Lattner6270f682006-10-08 22:57:01 +00001251/// getInputChainForNode - Given a node, return its input chain if it has one,
1252/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001253static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001254 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001256 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001257 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001258 return N->getOperand(NumOps-1);
1259 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001261 return N->getOperand(i);
1262 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001263 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001264}
1265
Dan Gohman475871a2008-07-27 21:46:04 +00001266SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001267 // If N has two operands, where one has an input chain equal to the other,
1268 // the 'other' chain is redundant.
1269 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001271 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001272 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001273 return N->getOperand(1);
1274 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001275
Chris Lattnerc76d4412007-05-16 06:37:59 +00001276 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001278 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001279 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Jim Laskey6ff23e52006-10-04 16:53:27 +00001281 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001282 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Jim Laskey71382342006-10-07 23:37:56 +00001284 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001285 // encountered.
1286 for (unsigned i = 0; i < TFs.size(); ++i) {
1287 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001288
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 // Check each of the operands.
1290 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001291 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Jim Laskey6ff23e52006-10-04 16:53:27 +00001293 switch (Op.getOpcode()) {
1294 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001295 // Entry tokens don't need to be added to the list. They are
1296 // rededundant.
1297 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001298 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001299
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001301 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001302 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001305 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001306 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001307 Changed = true;
1308 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001309 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Jim Laskey6ff23e52006-10-04 16:53:27 +00001312 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001313 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001314 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001315 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001316 else
1317 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001319 }
1320 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001321 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001324
1325 // If we've change things around then replace token factor.
1326 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001327 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001328 // The entry token is the only possible outcome.
1329 Result = DAG.getEntryNode();
1330 } else {
1331 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001332 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001334 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001335
Jim Laskey274062c2006-10-13 23:32:28 +00001336 // Don't add users to work list.
1337 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001339
Jim Laskey6ff23e52006-10-04 16:53:27 +00001340 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341}
1342
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001343/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001344SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001345 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001346 // Replacing results may cause a different MERGE_VALUES to suddenly
1347 // be CSE'd with N, and carry its uses with it. Iterate until no
1348 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001349 // First add the users of this node to the work list so that they
1350 // can be tried again once they have new operands.
1351 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001352 do {
1353 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001354 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001355 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001356 removeFromWorkList(N);
1357 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001358 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001359}
1360
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001361static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001362SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001363 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001364 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue N00 = N0.getOperand(0);
1366 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001367 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001368
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001370 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001371 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001372 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1373 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001374 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001375 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001376 N00.getOperand(1), N01));
1377 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001378 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001379
Dan Gohman475871a2008-07-27 21:46:04 +00001380 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001381}
1382
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue DAGCombiner::visitADD(SDNode *N) {
1384 SDValue N0 = N->getOperand(0);
1385 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1387 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001388 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001389
1390 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001391 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001394
1395 // fold (add x, 0) -> x, vector edition
1396 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1397 return N0;
1398 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1399 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001400 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001401
Dan Gohman613e0d82007-07-03 14:03:57 +00001402 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001403 if (N0.getOpcode() == ISD::UNDEF)
1404 return N0;
1405 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001406 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001409 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001410 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001411 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001412 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001416 // fold (add Sym, c) -> Sym+c
1417 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001418 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001419 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001420 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001421 GA->getOffset() +
1422 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001423 // fold ((c1-A)+c2) -> (c1+c2)-A
1424 if (N1C && N0.getOpcode() == ISD::SUB)
1425 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001426 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001427 DAG.getConstant(N1C->getAPIntValue()+
1428 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001429 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001430 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001431 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001433 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 // fold ((0-A) + B) -> B-A
1435 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1436 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (A + (0-B)) -> A-B
1439 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1440 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001441 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001442 // fold (A+(B-A)) -> B
1443 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001444 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001445 // fold ((B-A)+A) -> B
1446 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1447 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001448 // fold (A+(B-(A+C))) to (B-C)
1449 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001450 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001451 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001453 // fold (A+(B-(C+A))) to (B-C)
1454 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001455 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001456 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001457 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001458 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001459 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1460 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001461 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001462 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001463 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001464
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001465 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1466 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1467 SDValue N00 = N0.getOperand(0);
1468 SDValue N01 = N0.getOperand(1);
1469 SDValue N10 = N1.getOperand(0);
1470 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001471
1472 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001473 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1474 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1475 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001476 }
Chris Lattner947c2892006-03-13 06:51:27 +00001477
Dan Gohman475871a2008-07-27 21:46:04 +00001478 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1479 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001481 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001483 APInt LHSZero, LHSOne;
1484 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001485 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001486
Dan Gohman948d8ea2008-02-20 16:33:30 +00001487 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001488 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Chris Lattner947c2892006-03-13 06:51:27 +00001490 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1491 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001492 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001493 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001494 }
1495 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001496
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001497 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001498 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001499 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001500 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001501 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001502 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001503 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001504 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001505 }
1506
Dan Gohmancd9e1552010-01-19 23:30:49 +00001507 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1508 if (N1.getOpcode() == ISD::SHL &&
1509 N1.getOperand(0).getOpcode() == ISD::SUB)
1510 if (ConstantSDNode *C =
1511 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1512 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001513 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1514 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001515 N1.getOperand(0).getOperand(1),
1516 N1.getOperand(1)));
1517 if (N0.getOpcode() == ISD::SHL &&
1518 N0.getOperand(0).getOpcode() == ISD::SUB)
1519 if (ConstantSDNode *C =
1520 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1521 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001522 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1523 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001524 N0.getOperand(0).getOperand(1),
1525 N0.getOperand(1)));
1526
Owen Andersonbc146b02010-09-21 20:42:50 +00001527 if (N1.getOpcode() == ISD::AND) {
1528 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001530 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1531 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001532
Owen Andersonbc146b02010-09-21 20:42:50 +00001533 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1534 // and similar xforms where the inner op is either ~0 or 0.
1535 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001536 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001537 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1538 }
1539 }
1540
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001541 // add (sext i1), X -> sub X, (zext i1)
1542 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1543 N0.getOperand(0).getValueType() == MVT::i1 &&
1544 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001545 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001546 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1547 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1548 }
1549
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001550 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551}
1552
Dan Gohman475871a2008-07-27 21:46:04 +00001553SDValue DAGCombiner::visitADDC(SDNode *N) {
1554 SDValue N0 = N->getOperand(0);
1555 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001556 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001558 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattner91153682007-03-04 20:03:15 +00001560 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001561 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001562 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001564 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner91153682007-03-04 20:03:15 +00001566 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001567 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001568 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattnerb6541762007-03-04 20:40:38 +00001570 // fold (addc x, 0) -> x + no carry out
1571 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001572 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001573 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001575 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001576 APInt LHSZero, LHSOne;
1577 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001578 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001579
Dan Gohman948d8ea2008-02-20 16:33:30 +00001580 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001581 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001582
Chris Lattnerb6541762007-03-04 20:40:38 +00001583 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1584 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001585 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001586 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001587 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001588 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::visitADDE(SDNode *N) {
1595 SDValue N0 = N->getOperand(0);
1596 SDValue N1 = N->getOperand(1);
1597 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner91153682007-03-04 20:03:15 +00001601 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001602 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001603 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001604 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Chris Lattnerb6541762007-03-04 20:40:38 +00001606 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001607 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001608 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Dan Gohman475871a2008-07-27 21:46:04 +00001610 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001611}
1612
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001613// Since it may not be valid to emit a fold to zero for vector initializers
1614// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001615static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001616 SelectionDAG &DAG, bool LegalOperations) {
Stephen Linb4940152013-07-09 00:44:49 +00001617 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001618 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001619 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001620 // Produce a vector of zeros.
1621 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1622 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1623 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1624 &Ops[0], Ops.size());
1625 }
1626 return SDValue();
1627}
1628
Dan Gohman475871a2008-07-27 21:46:04 +00001629SDValue DAGCombiner::visitSUB(SDNode *N) {
1630 SDValue N0 = N->getOperand(0);
1631 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001632 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1633 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001634 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1635 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001636 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Dan Gohman7f321562007-06-25 16:23:39 +00001638 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001639 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001640 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001641 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001642
1643 // fold (sub x, 0) -> x, vector edition
1644 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1645 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001646 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001647
Chris Lattner854077d2005-10-17 01:07:11 +00001648 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001649 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001650 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001651 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001653 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001654 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001655 // fold (sub x, c) -> (add x, -c)
1656 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001657 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001658 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001659 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1660 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001661 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001662 // fold A-(A-B) -> B
1663 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1664 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001665 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001666 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001667 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001669 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001670 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001671 // fold C2-(A+C1) -> (C2-C1)-A
1672 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001673 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1674 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001675 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001676 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001677 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001678 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001679 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001680 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1681 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001682 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001683 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001684 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001685 // fold ((A+(C+B))-B) -> A+C
1686 if (N0.getOpcode() == ISD::ADD &&
1687 N0.getOperand(1).getOpcode() == ISD::ADD &&
1688 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001689 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001690 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001691 // fold ((A-(B-C))-C) -> A-B
1692 if (N0.getOpcode() == ISD::SUB &&
1693 N0.getOperand(1).getOpcode() == ISD::SUB &&
1694 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001695 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001696 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001697
Dan Gohman613e0d82007-07-03 14:03:57 +00001698 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001699 if (N0.getOpcode() == ISD::UNDEF)
1700 return N0;
1701 if (N1.getOpcode() == ISD::UNDEF)
1702 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001703
Dan Gohman6520e202008-10-18 02:06:02 +00001704 // If the relocation model supports it, consider symbol offsets.
1705 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001706 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001707 // fold (sub Sym, c) -> Sym-c
1708 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001709 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001710 GA->getOffset() -
1711 (uint64_t)N1C->getSExtValue());
1712 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1713 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1714 if (GA->getGlobal() == GB->getGlobal())
1715 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1716 VT);
1717 }
1718
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001719 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720}
1721
Craig Toppercc274522012-01-07 09:06:39 +00001722SDValue DAGCombiner::visitSUBC(SDNode *N) {
1723 SDValue N0 = N->getOperand(0);
1724 SDValue N1 = N->getOperand(1);
1725 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1726 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1727 EVT VT = N0.getValueType();
1728
1729 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001730 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001731 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1732 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001733 MVT::Glue));
1734
1735 // fold (subc x, x) -> 0 + no borrow
1736 if (N0 == N1)
1737 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001738 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001739 MVT::Glue));
1740
1741 // fold (subc x, 0) -> x + no borrow
1742 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001743 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001744 MVT::Glue));
1745
1746 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1747 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001748 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1749 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001750 MVT::Glue));
1751
1752 return SDValue();
1753}
1754
1755SDValue DAGCombiner::visitSUBE(SDNode *N) {
1756 SDValue N0 = N->getOperand(0);
1757 SDValue N1 = N->getOperand(1);
1758 SDValue CarryIn = N->getOperand(2);
1759
1760 // fold (sube x, y, false) -> (subc x, y)
1761 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001762 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001763
1764 return SDValue();
1765}
1766
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001767/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1768/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001769static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1770 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1771 if (!C)
1772 return false;
1773
1774 APInt SplatUndef;
1775 unsigned SplatBitSize;
1776 bool HasAnyUndefs;
1777 EVT EltVT = N->getValueType(0).getVectorElementType();
1778 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1779 HasAnyUndefs) &&
1780 EltVT.getSizeInBits() >= SplatBitSize);
1781}
1782
Dan Gohman475871a2008-07-27 21:46:04 +00001783SDValue DAGCombiner::visitMUL(SDNode *N) {
1784 SDValue N0 = N->getOperand(0);
1785 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001786 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001787
Dan Gohman613e0d82007-07-03 14:03:57 +00001788 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001789 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001790 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001791
1792 bool N0IsConst = false;
1793 bool N1IsConst = false;
1794 APInt ConstValue0, ConstValue1;
1795 // fold vector ops
1796 if (VT.isVector()) {
1797 SDValue FoldedVOp = SimplifyVBinOp(N);
1798 if (FoldedVOp.getNode()) return FoldedVOp;
1799
1800 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1801 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1802 } else {
1803 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1804 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1805 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1806 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1807 }
1808
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001810 if (N0IsConst && N1IsConst)
1811 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1812
Nate Begeman99801192005-09-07 23:25:52 +00001813 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001814 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001815 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001817 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818 return N1;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001819 // fold (mul x, 1) -> x
1820 if (N1IsConst && ConstValue1 == 1)
1821 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001823 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001824 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001825 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 // fold (mul x, (1 << c)) -> x << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001827 if (N1IsConst && ConstValue1.isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001828 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001829 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001830 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001831 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001832 if (N1IsConst && (-ConstValue1).isPowerOf2()) {
1833 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001834 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001835 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001836 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001837 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001838 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001839 DAG.getConstant(Log2Val,
1840 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001841 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001842
1843 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001844 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001845 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001846 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1847 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001848 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001849 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001850 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001851 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001852 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001853 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001855 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1856 // use.
1857 {
Dan Gohman475871a2008-07-27 21:46:04 +00001858 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001859 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001860 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001861 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1862 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001864 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001866 isa<ConstantSDNode>(N1.getOperand(1)) &&
1867 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001868 Sh = N1; Y = N0;
1869 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001870
Gabor Greifba36cb52008-08-28 21:40:38 +00001871 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001872 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001873 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001874 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001875 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001876 }
1877 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001878
Chris Lattnera1deca32006-03-04 23:33:26 +00001879 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001880 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1881 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1882 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001883 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1884 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001885 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001887 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Nate Begemancd4d58c2006-02-03 06:46:56 +00001889 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001890 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001891 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001892 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001893
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001894 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895}
1896
Dan Gohman475871a2008-07-27 21:46:04 +00001897SDValue DAGCombiner::visitSDIV(SDNode *N) {
1898 SDValue N0 = N->getOperand(0);
1899 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001900 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001902 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903
Dan Gohman7f321562007-06-25 16:23:39 +00001904 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001905 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001907 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001908 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001909
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001911 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001912 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001913 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001914 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001915 return N0;
1916 // fold (sdiv X, -1) -> 0-X
1917 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001918 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001919 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001920 // If we know the sign bits of both operands are zero, strength reduce to a
1921 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001922 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001923 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001924 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001925 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001926 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001927 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001928 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001929 (N1C->getAPIntValue().isPowerOf2() ||
1930 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001931 // If dividing by powers of two is cheap, then don't perform the following
1932 // fold.
1933 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001934 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001935
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001936 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001937
Chris Lattner8f4880b2006-02-16 08:02:36 +00001938 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001939 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001940 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001941 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001943
Chris Lattner8f4880b2006-02-16 08:02:36 +00001944 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001945 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001946 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001947 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001948 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001949 AddToWorkList(SRL.getNode());
1950 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001951 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001952 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001953
Nate Begeman405e3ec2005-10-21 00:02:42 +00001954 // If we're dividing by a positive value, we're done. Otherwise, we must
1955 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001956 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001957 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001958
Gabor Greifba36cb52008-08-28 21:40:38 +00001959 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001960 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001961 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001962 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001963
Nate Begeman69575232005-10-20 02:15:44 +00001964 // if integer divide is expensive and we satisfy the requirements, emit an
1965 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001966 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001968 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001969 }
Dan Gohman7f321562007-06-25 16:23:39 +00001970
Dan Gohman613e0d82007-07-03 14:03:57 +00001971 // undef / X -> 0
1972 if (N0.getOpcode() == ISD::UNDEF)
1973 return DAG.getConstant(0, VT);
1974 // X / undef -> undef
1975 if (N1.getOpcode() == ISD::UNDEF)
1976 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001977
Dan Gohman475871a2008-07-27 21:46:04 +00001978 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001979}
1980
Dan Gohman475871a2008-07-27 21:46:04 +00001981SDValue DAGCombiner::visitUDIV(SDNode *N) {
1982 SDValue N0 = N->getOperand(0);
1983 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001984 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1985 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001987
Dan Gohman7f321562007-06-25 16:23:39 +00001988 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001990 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001992 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001993
Nate Begeman1d4d4142005-09-01 00:19:25 +00001994 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001995 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001996 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001997 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001998 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001999 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002000 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002001 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002002 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002003 if (N1.getOpcode() == ISD::SHL) {
2004 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002005 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002006 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002007 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002008 N1.getOperand(1),
2009 DAG.getConstant(SHC->getAPIntValue()
2010 .logBase2(),
2011 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002012 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002013 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002014 }
2015 }
2016 }
Nate Begeman69575232005-10-20 02:15:44 +00002017 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002018 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002020 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002021 }
Dan Gohman7f321562007-06-25 16:23:39 +00002022
Dan Gohman613e0d82007-07-03 14:03:57 +00002023 // undef / X -> 0
2024 if (N0.getOpcode() == ISD::UNDEF)
2025 return DAG.getConstant(0, VT);
2026 // X / undef -> undef
2027 if (N1.getOpcode() == ISD::UNDEF)
2028 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002029
Dan Gohman475871a2008-07-27 21:46:04 +00002030 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002031}
2032
Dan Gohman475871a2008-07-27 21:46:04 +00002033SDValue DAGCombiner::visitSREM(SDNode *N) {
2034 SDValue N0 = N->getOperand(0);
2035 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002036 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2037 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002038 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002039
Nate Begeman1d4d4142005-09-01 00:19:25 +00002040 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002041 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002042 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002043 // If we know the sign bits of both operands are zero, strength reduce to a
2044 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002045 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002046 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002047 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Dan Gohman77003042007-11-26 23:46:11 +00002050 // If X/C can be simplified by the division-by-constant logic, lower
2051 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002052 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002053 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002054 AddToWorkList(Div.getNode());
2055 SDValue OptimizedDiv = combine(Div.getNode());
2056 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002057 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002058 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002059 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002060 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002061 return Sub;
2062 }
Chris Lattner26d29902006-10-12 20:58:32 +00002063 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002064
Dan Gohman613e0d82007-07-03 14:03:57 +00002065 // undef % X -> 0
2066 if (N0.getOpcode() == ISD::UNDEF)
2067 return DAG.getConstant(0, VT);
2068 // X % undef -> undef
2069 if (N1.getOpcode() == ISD::UNDEF)
2070 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002071
Dan Gohman475871a2008-07-27 21:46:04 +00002072 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002073}
2074
Dan Gohman475871a2008-07-27 21:46:04 +00002075SDValue DAGCombiner::visitUREM(SDNode *N) {
2076 SDValue N0 = N->getOperand(0);
2077 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002078 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2079 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002080 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002081
Nate Begeman1d4d4142005-09-01 00:19:25 +00002082 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002083 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002084 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002085 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002086 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002087 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002088 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002089 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2090 if (N1.getOpcode() == ISD::SHL) {
2091 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002092 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002094 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002095 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002096 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002097 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002098 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002099 }
2100 }
2101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002102
Dan Gohman77003042007-11-26 23:46:11 +00002103 // If X/C can be simplified by the division-by-constant logic, lower
2104 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002105 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002106 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002107 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002108 SDValue OptimizedDiv = combine(Div.getNode());
2109 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002110 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002111 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002112 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002113 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002114 return Sub;
2115 }
Chris Lattner26d29902006-10-12 20:58:32 +00002116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Dan Gohman613e0d82007-07-03 14:03:57 +00002118 // undef % X -> 0
2119 if (N0.getOpcode() == ISD::UNDEF)
2120 return DAG.getConstant(0, VT);
2121 // X % undef -> undef
2122 if (N1.getOpcode() == ISD::UNDEF)
2123 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002124
Dan Gohman475871a2008-07-27 21:46:04 +00002125 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126}
2127
Dan Gohman475871a2008-07-27 21:46:04 +00002128SDValue DAGCombiner::visitMULHS(SDNode *N) {
2129 SDValue N0 = N->getOperand(0);
2130 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002131 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002132 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002133 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Nate Begeman1d4d4142005-09-01 00:19:25 +00002135 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002136 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002137 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002138 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002139 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002140 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002141 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002142 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002143 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002144 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002145 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002146
Chris Lattnerde1c3602010-12-13 08:39:01 +00002147 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2148 // plus a shift.
2149 if (VT.isSimple() && !VT.isVector()) {
2150 MVT Simple = VT.getSimpleVT();
2151 unsigned SimpleSize = Simple.getSizeInBits();
2152 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2153 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2154 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2155 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2156 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002157 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002158 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002159 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2160 }
2161 }
Owen Anderson95771af2011-02-25 21:41:48 +00002162
Dan Gohman475871a2008-07-27 21:46:04 +00002163 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164}
2165
Dan Gohman475871a2008-07-27 21:46:04 +00002166SDValue DAGCombiner::visitMULHU(SDNode *N) {
2167 SDValue N0 = N->getOperand(0);
2168 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002170 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002171 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002174 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002175 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002177 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002178 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002179 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002180 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002181 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002182
Chris Lattnerde1c3602010-12-13 08:39:01 +00002183 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2184 // plus a shift.
2185 if (VT.isSimple() && !VT.isVector()) {
2186 MVT Simple = VT.getSimpleVT();
2187 unsigned SimpleSize = Simple.getSizeInBits();
2188 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2189 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2190 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2191 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2192 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2193 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002194 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002195 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2196 }
2197 }
Owen Anderson95771af2011-02-25 21:41:48 +00002198
Dan Gohman475871a2008-07-27 21:46:04 +00002199 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002200}
2201
Dan Gohman389079b2007-10-08 17:57:15 +00002202/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2203/// compute two values. LoOp and HiOp give the opcodes for the two computations
2204/// that are being performed. Return true if a simplification was made.
2205///
Scott Michelfdc40a02009-02-17 22:15:04 +00002206SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002207 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002208 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002209 bool HiExists = N->hasAnyUseOfValue(1);
2210 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002211 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002212 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002213 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002214 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002215 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002216 }
2217
2218 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002219 bool LoExists = N->hasAnyUseOfValue(0);
2220 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002221 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002222 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002223 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002224 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002225 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002226 }
2227
Evan Cheng44711942007-11-08 09:25:29 +00002228 // If both halves are used, return as it is.
2229 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002230 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002231
2232 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002233 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002234 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002235 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 AddToWorkList(Lo.getNode());
2237 SDValue LoOpt = combine(Lo.getNode());
2238 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002239 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002240 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002241 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002242 }
2243
Evan Cheng44711942007-11-08 09:25:29 +00002244 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002245 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002246 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002247 AddToWorkList(Hi.getNode());
2248 SDValue HiOpt = combine(Hi.getNode());
2249 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002250 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002251 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002252 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002253 }
Bill Wendling826d1142009-01-30 03:08:40 +00002254
Dan Gohman475871a2008-07-27 21:46:04 +00002255 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002256}
2257
Dan Gohman475871a2008-07-27 21:46:04 +00002258SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2259 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002260 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002261
Chris Lattner33e77d32010-12-15 06:04:19 +00002262 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002263 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002264
2265 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2266 // plus a shift.
2267 if (VT.isSimple() && !VT.isVector()) {
2268 MVT Simple = VT.getSimpleVT();
2269 unsigned SimpleSize = Simple.getSizeInBits();
2270 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2271 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2272 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2273 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2274 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2275 // Compute the high part as N1.
2276 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002277 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002278 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2279 // Compute the low part as N0.
2280 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2281 return CombineTo(N, Lo, Hi);
2282 }
2283 }
Owen Anderson95771af2011-02-25 21:41:48 +00002284
Dan Gohman475871a2008-07-27 21:46:04 +00002285 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002286}
2287
Dan Gohman475871a2008-07-27 21:46:04 +00002288SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2289 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002290 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002291
Chris Lattner33e77d32010-12-15 06:04:19 +00002292 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002293 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002294
Chris Lattner33e77d32010-12-15 06:04:19 +00002295 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2296 // plus a shift.
2297 if (VT.isSimple() && !VT.isVector()) {
2298 MVT Simple = VT.getSimpleVT();
2299 unsigned SimpleSize = Simple.getSizeInBits();
2300 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2301 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2302 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2303 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2304 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2305 // Compute the high part as N1.
2306 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002307 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002308 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2309 // Compute the low part as N0.
2310 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2311 return CombineTo(N, Lo, Hi);
2312 }
2313 }
Owen Anderson95771af2011-02-25 21:41:48 +00002314
Dan Gohman475871a2008-07-27 21:46:04 +00002315 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002316}
2317
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002318SDValue DAGCombiner::visitSMULO(SDNode *N) {
2319 // (smulo x, 2) -> (saddo x, x)
2320 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2321 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002322 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002323 N->getOperand(0), N->getOperand(0));
2324
2325 return SDValue();
2326}
2327
2328SDValue DAGCombiner::visitUMULO(SDNode *N) {
2329 // (umulo x, 2) -> (uaddo x, x)
2330 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2331 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002332 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002333 N->getOperand(0), N->getOperand(0));
2334
2335 return SDValue();
2336}
2337
Dan Gohman475871a2008-07-27 21:46:04 +00002338SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2339 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002340 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002341
Dan Gohman475871a2008-07-27 21:46:04 +00002342 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002343}
2344
Dan Gohman475871a2008-07-27 21:46:04 +00002345SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2346 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002347 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002348
Dan Gohman475871a2008-07-27 21:46:04 +00002349 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002350}
2351
Chris Lattner35e5c142006-05-05 05:51:50 +00002352/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2353/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002354SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2355 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002357 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Dan Gohmanff00a552010-01-14 03:08:49 +00002359 // Bail early if none of these transforms apply.
2360 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2361
Chris Lattner540121f2006-05-05 06:31:05 +00002362 // For each of OP in AND/OR/XOR:
2363 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2364 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2365 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002366 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002367 //
2368 // do not sink logical op inside of a vector extend, since it may combine
2369 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002370 EVT Op0VT = N0.getOperand(0).getValueType();
2371 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002372 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002373 // Avoid infinite looping with PromoteIntBinOp.
2374 (N0.getOpcode() == ISD::ANY_EXTEND &&
2375 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002376 (N0.getOpcode() == ISD::TRUNCATE &&
2377 (!TLI.isZExtFree(VT, Op0VT) ||
2378 !TLI.isTruncateFree(Op0VT, VT)) &&
2379 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002380 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002381 Op0VT == N1.getOperand(0).getValueType() &&
2382 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002383 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002384 N0.getOperand(0).getValueType(),
2385 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002386 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002387 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002389
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002390 // For each of OP in SHL/SRL/SRA/AND...
2391 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2392 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2393 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002394 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002395 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002396 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002397 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002398 N0.getOperand(0).getValueType(),
2399 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002400 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002401 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002402 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002403 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002404
Nadav Rotem4ac90812012-04-01 19:31:22 +00002405 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2406 // Only perform this optimization after type legalization and before
2407 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2408 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2409 // we don't want to undo this promotion.
2410 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2411 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002412 if ((N0.getOpcode() == ISD::BITCAST ||
2413 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2414 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002415 SDValue In0 = N0.getOperand(0);
2416 SDValue In1 = N1.getOperand(0);
2417 EVT In0Ty = In0.getValueType();
2418 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002419 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002420 // If both incoming values are integers, and the original types are the
2421 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002422 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002423 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2424 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002425 AddToWorkList(Op.getNode());
2426 return BC;
2427 }
2428 }
2429
2430 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2431 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2432 // If both shuffles use the same mask, and both shuffle within a single
2433 // vector, then it is worthwhile to move the swizzle after the operation.
2434 // The type-legalizer generates this pattern when loading illegal
2435 // vector types from memory. In many cases this allows additional shuffle
2436 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002437 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2438 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2439 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002440 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2441 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002442
2443 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2444 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002445
2446 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002447
2448 // Check that both shuffles use the same mask. The masks are known to be of
2449 // the same length because the result vector type is the same.
2450 bool SameMask = true;
2451 for (unsigned i = 0; i != NumElts; ++i) {
2452 int Idx0 = SVN0->getMaskElt(i);
2453 int Idx1 = SVN1->getMaskElt(i);
2454 if (Idx0 != Idx1) {
2455 SameMask = false;
2456 break;
2457 }
2458 }
2459
Craig Topperf9204232012-04-09 07:19:09 +00002460 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002461 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002462 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002463 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002464 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002465 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002466 }
2467 }
Craig Topperf9204232012-04-09 07:19:09 +00002468
Dan Gohman475871a2008-07-27 21:46:04 +00002469 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002470}
2471
Dan Gohman475871a2008-07-27 21:46:04 +00002472SDValue DAGCombiner::visitAND(SDNode *N) {
2473 SDValue N0 = N->getOperand(0);
2474 SDValue N1 = N->getOperand(1);
2475 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002476 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2477 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002478 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002479 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002480
Dan Gohman7f321562007-06-25 16:23:39 +00002481 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002482 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002483 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002484 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002485
2486 // fold (and x, 0) -> 0, vector edition
2487 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2488 return N0;
2489 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2490 return N1;
2491
2492 // fold (and x, -1) -> x, vector edition
2493 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2494 return N1;
2495 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2496 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002497 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002498
Dan Gohman613e0d82007-07-03 14:03:57 +00002499 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002500 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002501 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002503 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002504 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002505 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002506 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002507 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002509 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510 return N0;
2511 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002512 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002513 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002514 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002515 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002516 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002517 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002518 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002519 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002520 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002521 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002522 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002523 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002524 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2525 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002526 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002527 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002528 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002529 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002530 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002531 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Chris Lattner1ec05d12006-03-01 21:47:21 +00002533 // Replace uses of the AND with uses of the Zero extend node.
2534 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002535
Chris Lattner3603cd62006-02-02 07:17:31 +00002536 // We actually want to replace all uses of the any_extend with the
2537 // zero_extend, to avoid duplicating things. This will later cause this
2538 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002539 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002540 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002541 }
2542 }
Stephen Lin155615d2013-07-08 00:37:03 +00002543 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002544 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2545 // already be zero by virtue of the width of the base type of the load.
2546 //
2547 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2548 // more cases.
2549 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2550 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2551 N0.getOpcode() == ISD::LOAD) {
2552 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2553 N0 : N0.getOperand(0) );
2554
2555 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2556 // This can be a pure constant or a vector splat, in which case we treat the
2557 // vector as a scalar and use the splat value.
2558 APInt Constant = APInt::getNullValue(1);
2559 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2560 Constant = C->getAPIntValue();
2561 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2562 APInt SplatValue, SplatUndef;
2563 unsigned SplatBitSize;
2564 bool HasAnyUndefs;
2565 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2566 SplatBitSize, HasAnyUndefs);
2567 if (IsSplat) {
2568 // Undef bits can contribute to a possible optimisation if set, so
2569 // set them.
2570 SplatValue |= SplatUndef;
2571
2572 // The splat value may be something like "0x00FFFFFF", which means 0 for
2573 // the first vector value and FF for the rest, repeating. We need a mask
2574 // that will apply equally to all members of the vector, so AND all the
2575 // lanes of the constant together.
2576 EVT VT = Vector->getValueType(0);
2577 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002578
2579 // If the splat value has been compressed to a bitlength lower
2580 // than the size of the vector lane, we need to re-expand it to
2581 // the lane size.
2582 if (BitWidth > SplatBitSize)
2583 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2584 SplatBitSize < BitWidth;
2585 SplatBitSize = SplatBitSize * 2)
2586 SplatValue |= SplatValue.shl(SplatBitSize);
2587
James Molloy6259dcd2012-02-20 12:02:38 +00002588 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002589 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002590 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2591 }
2592 }
2593
2594 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2595 // actually legal and isn't going to get expanded, else this is a false
2596 // optimisation.
2597 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2598 Load->getMemoryVT());
2599
2600 // Resize the constant to the same size as the original memory access before
2601 // extension. If it is still the AllOnesValue then this AND is completely
2602 // unneeded.
2603 Constant =
2604 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2605
2606 bool B;
2607 switch (Load->getExtensionType()) {
2608 default: B = false; break;
2609 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2610 case ISD::ZEXTLOAD:
2611 case ISD::NON_EXTLOAD: B = true; break;
2612 }
2613
2614 if (B && Constant.isAllOnesValue()) {
2615 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2616 // preserve semantics once we get rid of the AND.
2617 SDValue NewLoad(Load, 0);
2618 if (Load->getExtensionType() == ISD::EXTLOAD) {
2619 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002620 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002621 Load->getChain(), Load->getBasePtr(),
2622 Load->getOffset(), Load->getMemoryVT(),
2623 Load->getMemOperand());
2624 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002625 if (Load->getNumValues() == 3) {
2626 // PRE/POST_INC loads have 3 values.
2627 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2628 NewLoad.getValue(2) };
2629 CombineTo(Load, To, 3, true);
2630 } else {
2631 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2632 }
James Molloy6259dcd2012-02-20 12:02:38 +00002633 }
2634
2635 // Fold the AND away, taking care not to fold to the old load node if we
2636 // replaced it.
2637 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2638
2639 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2640 }
2641 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002642 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2643 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2644 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2645 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002646
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002647 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002648 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002649 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002650 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002651 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002652 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002653 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002654 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002655 }
Bill Wendling2627a882009-01-30 20:43:18 +00002656 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002657 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002658 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002659 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002660 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002661 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002662 }
Bill Wendling2627a882009-01-30 20:43:18 +00002663 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002664 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002665 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002666 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002667 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002668 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002669 }
2670 }
2671 // canonicalize equivalent to ll == rl
2672 if (LL == RR && LR == RL) {
2673 Op1 = ISD::getSetCCSwappedOperands(Op1);
2674 std::swap(RL, RR);
2675 }
2676 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002678 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002679 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002680 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002681 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2682 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002683 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002684 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002685 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002686 }
2687 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002688
Bill Wendling2627a882009-01-30 20:43:18 +00002689 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002690 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002691 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002692 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002693 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002694
Nate Begemande996292006-02-03 22:24:05 +00002695 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2696 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002697 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002698 SimplifyDemandedBits(SDValue(N, 0)))
2699 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002700
Nate Begemanded49632005-10-13 03:11:28 +00002701 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002702 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002703 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002704 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002705 // If we zero all the possible extended bits, then we can turn this into
2706 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002707 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002708 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002709 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002710 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002711 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002712 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002713 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002714 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002715 LN0->isVolatile(), LN0->isNonTemporal(),
2716 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002717 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002718 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002719 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002720 }
2721 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002722 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002723 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002724 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002725 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002726 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002727 // If we zero all the possible extended bits, then we can turn this into
2728 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002729 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002730 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002731 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002732 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002733 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002734 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002735 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002736 LN0->getBasePtr(), LN0->getPointerInfo(),
2737 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002738 LN0->isVolatile(), LN0->isNonTemporal(),
2739 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002740 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002741 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002742 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002743 }
2744 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002745
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002746 // fold (and (load x), 255) -> (zextload x, i8)
2747 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002748 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2749 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2750 (N0.getOpcode() == ISD::ANY_EXTEND &&
2751 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2752 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2753 LoadSDNode *LN0 = HasAnyExt
2754 ? cast<LoadSDNode>(N0.getOperand(0))
2755 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002756 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002757 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002758 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002759 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2760 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2761 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002762
Evan Chengd40d03e2010-01-06 19:38:29 +00002763 if (ExtVT == LoadedVT &&
2764 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002765 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002766
2767 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002768 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002769 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002770 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002771 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2772 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002773 AddToWorkList(N);
2774 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2775 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2776 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002777
Chris Lattneref7634c2010-01-07 21:53:27 +00002778 // Do not change the width of a volatile load.
2779 // Do not generate loads of non-round integer types since these can
2780 // be expensive (and would be wrong if the type is not byte sized).
2781 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2782 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2783 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002784
Chris Lattneref7634c2010-01-07 21:53:27 +00002785 unsigned Alignment = LN0->getAlignment();
2786 SDValue NewPtr = LN0->getBasePtr();
2787
2788 // For big endian targets, we need to add an offset to the pointer
2789 // to load the correct bytes. For little endian systems, we merely
2790 // need to read fewer bytes from the same pointer.
2791 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002792 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2793 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2794 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002795 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002796 NewPtr, DAG.getConstant(PtrOff, PtrType));
2797 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002798 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002799
2800 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002801
Chris Lattneref7634c2010-01-07 21:53:27 +00002802 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2803 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002804 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002805 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002806 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002807 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2808 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002809 AddToWorkList(N);
2810 CombineTo(LN0, Load, Load.getValue(1));
2811 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002812 }
Evan Cheng466685d2006-10-09 20:57:25 +00002813 }
Chris Lattner15045b62006-02-28 06:35:35 +00002814 }
2815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002816
Evan Chenga9e13ba2012-07-17 18:54:11 +00002817 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2818 VT.getSizeInBits() <= 64) {
2819 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2820 APInt ADDC = ADDI->getAPIntValue();
2821 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2822 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2823 // immediate for an add, but it is legal if its top c2 bits are set,
2824 // transform the ADD so the immediate doesn't need to be materialized
2825 // in a register.
2826 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2827 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2828 SRLI->getZExtValue());
2829 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2830 ADDC |= Mask;
2831 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2832 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002833 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002834 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2835 CombineTo(N0.getNode(), NewAdd);
2836 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2837 }
2838 }
2839 }
2840 }
2841 }
2842 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002843
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002844 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002845}
2846
Evan Cheng9568e5c2011-06-21 06:01:08 +00002847/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2848///
2849SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2850 bool DemandHighBits) {
2851 if (!LegalOperations)
2852 return SDValue();
2853
2854 EVT VT = N->getValueType(0);
2855 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2856 return SDValue();
2857 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2858 return SDValue();
2859
2860 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2861 bool LookPassAnd0 = false;
2862 bool LookPassAnd1 = false;
2863 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2864 std::swap(N0, N1);
2865 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2866 std::swap(N0, N1);
2867 if (N0.getOpcode() == ISD::AND) {
2868 if (!N0.getNode()->hasOneUse())
2869 return SDValue();
2870 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2871 if (!N01C || N01C->getZExtValue() != 0xFF00)
2872 return SDValue();
2873 N0 = N0.getOperand(0);
2874 LookPassAnd0 = true;
2875 }
2876
2877 if (N1.getOpcode() == ISD::AND) {
2878 if (!N1.getNode()->hasOneUse())
2879 return SDValue();
2880 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2881 if (!N11C || N11C->getZExtValue() != 0xFF)
2882 return SDValue();
2883 N1 = N1.getOperand(0);
2884 LookPassAnd1 = true;
2885 }
2886
2887 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2888 std::swap(N0, N1);
2889 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2890 return SDValue();
2891 if (!N0.getNode()->hasOneUse() ||
2892 !N1.getNode()->hasOneUse())
2893 return SDValue();
2894
2895 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2896 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2897 if (!N01C || !N11C)
2898 return SDValue();
2899 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2900 return SDValue();
2901
2902 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2903 SDValue N00 = N0->getOperand(0);
2904 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2905 if (!N00.getNode()->hasOneUse())
2906 return SDValue();
2907 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2908 if (!N001C || N001C->getZExtValue() != 0xFF)
2909 return SDValue();
2910 N00 = N00.getOperand(0);
2911 LookPassAnd0 = true;
2912 }
2913
2914 SDValue N10 = N1->getOperand(0);
2915 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2916 if (!N10.getNode()->hasOneUse())
2917 return SDValue();
2918 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2919 if (!N101C || N101C->getZExtValue() != 0xFF00)
2920 return SDValue();
2921 N10 = N10.getOperand(0);
2922 LookPassAnd1 = true;
2923 }
2924
2925 if (N00 != N10)
2926 return SDValue();
2927
2928 // Make sure everything beyond the low halfword is zero since the SRL 16
2929 // will clear the top bits.
2930 unsigned OpSizeInBits = VT.getSizeInBits();
2931 if (DemandHighBits && OpSizeInBits > 16 &&
2932 (!LookPassAnd0 || !LookPassAnd1) &&
2933 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2934 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002935
Andrew Trickac6d9be2013-05-25 02:42:55 +00002936 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002937 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002938 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002939 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2940 return Res;
2941}
2942
2943/// isBSwapHWordElement - Return true if the specified node is an element
2944/// that makes up a 32-bit packed halfword byteswap. i.e.
2945/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2946static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2947 if (!N.getNode()->hasOneUse())
2948 return false;
2949
2950 unsigned Opc = N.getOpcode();
2951 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2952 return false;
2953
2954 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2955 if (!N1C)
2956 return false;
2957
2958 unsigned Num;
2959 switch (N1C->getZExtValue()) {
2960 default:
2961 return false;
2962 case 0xFF: Num = 0; break;
2963 case 0xFF00: Num = 1; break;
2964 case 0xFF0000: Num = 2; break;
2965 case 0xFF000000: Num = 3; break;
2966 }
2967
2968 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2969 SDValue N0 = N.getOperand(0);
2970 if (Opc == ISD::AND) {
2971 if (Num == 0 || Num == 2) {
2972 // (x >> 8) & 0xff
2973 // (x >> 8) & 0xff0000
2974 if (N0.getOpcode() != ISD::SRL)
2975 return false;
2976 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2977 if (!C || C->getZExtValue() != 8)
2978 return false;
2979 } else {
2980 // (x << 8) & 0xff00
2981 // (x << 8) & 0xff000000
2982 if (N0.getOpcode() != ISD::SHL)
2983 return false;
2984 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2985 if (!C || C->getZExtValue() != 8)
2986 return false;
2987 }
2988 } else if (Opc == ISD::SHL) {
2989 // (x & 0xff) << 8
2990 // (x & 0xff0000) << 8
2991 if (Num != 0 && Num != 2)
2992 return false;
2993 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2994 if (!C || C->getZExtValue() != 8)
2995 return false;
2996 } else { // Opc == ISD::SRL
2997 // (x & 0xff00) >> 8
2998 // (x & 0xff000000) >> 8
2999 if (Num != 1 && Num != 3)
3000 return false;
3001 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3002 if (!C || C->getZExtValue() != 8)
3003 return false;
3004 }
3005
3006 if (Parts[Num])
3007 return false;
3008
3009 Parts[Num] = N0.getOperand(0).getNode();
3010 return true;
3011}
3012
3013/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3014/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3015/// => (rotl (bswap x), 16)
3016SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3017 if (!LegalOperations)
3018 return SDValue();
3019
3020 EVT VT = N->getValueType(0);
3021 if (VT != MVT::i32)
3022 return SDValue();
3023 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3024 return SDValue();
3025
3026 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3027 // Look for either
3028 // (or (or (and), (and)), (or (and), (and)))
3029 // (or (or (or (and), (and)), (and)), (and))
3030 if (N0.getOpcode() != ISD::OR)
3031 return SDValue();
3032 SDValue N00 = N0.getOperand(0);
3033 SDValue N01 = N0.getOperand(1);
3034
Evan Cheng9a65a012012-12-13 01:34:32 +00003035 if (N1.getOpcode() == ISD::OR &&
3036 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003037 // (or (or (and), (and)), (or (and), (and)))
3038 SDValue N000 = N00.getOperand(0);
3039 if (!isBSwapHWordElement(N000, Parts))
3040 return SDValue();
3041
3042 SDValue N001 = N00.getOperand(1);
3043 if (!isBSwapHWordElement(N001, Parts))
3044 return SDValue();
3045 SDValue N010 = N01.getOperand(0);
3046 if (!isBSwapHWordElement(N010, Parts))
3047 return SDValue();
3048 SDValue N011 = N01.getOperand(1);
3049 if (!isBSwapHWordElement(N011, Parts))
3050 return SDValue();
3051 } else {
3052 // (or (or (or (and), (and)), (and)), (and))
3053 if (!isBSwapHWordElement(N1, Parts))
3054 return SDValue();
3055 if (!isBSwapHWordElement(N01, Parts))
3056 return SDValue();
3057 if (N00.getOpcode() != ISD::OR)
3058 return SDValue();
3059 SDValue N000 = N00.getOperand(0);
3060 if (!isBSwapHWordElement(N000, Parts))
3061 return SDValue();
3062 SDValue N001 = N00.getOperand(1);
3063 if (!isBSwapHWordElement(N001, Parts))
3064 return SDValue();
3065 }
3066
3067 // Make sure the parts are all coming from the same node.
3068 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3069 return SDValue();
3070
Andrew Trickac6d9be2013-05-25 02:42:55 +00003071 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003072 SDValue(Parts[0],0));
3073
3074 // Result of the bswap should be rotated by 16. If it's not legal, than
3075 // do (x << 16) | (x >> 16).
3076 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3077 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003078 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003079 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003080 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3081 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3082 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3083 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003084}
3085
Dan Gohman475871a2008-07-27 21:46:04 +00003086SDValue DAGCombiner::visitOR(SDNode *N) {
3087 SDValue N0 = N->getOperand(0);
3088 SDValue N1 = N->getOperand(1);
3089 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003092 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003093
Dan Gohman7f321562007-06-25 16:23:39 +00003094 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003097 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003098
3099 // fold (or x, 0) -> x, vector edition
3100 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3101 return N1;
3102 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3103 return N0;
3104
3105 // fold (or x, -1) -> -1, vector edition
3106 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3107 return N0;
3108 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3109 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003111
Dan Gohman613e0d82007-07-03 14:03:57 +00003112 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003113 if (!LegalOperations &&
3114 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003115 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3116 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3117 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003118 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003119 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003120 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003121 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003122 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003123 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003124 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003125 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003126 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003127 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003128 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003129 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003130 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003131 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003132 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003133
3134 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3135 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3136 if (BSwap.getNode() != 0)
3137 return BSwap;
3138 BSwap = MatchBSwapHWordLow(N, N0, N1);
3139 if (BSwap.getNode() != 0)
3140 return BSwap;
3141
Nate Begemancd4d58c2006-02-03 06:46:56 +00003142 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003143 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003144 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003145 return ROR;
3146 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003147 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003148 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003149 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003150 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003151 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003152 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3153 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003154 N0.getOperand(0), N1),
3155 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003156 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003157 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3158 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3159 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3160 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003161
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003162 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003163 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003164 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3165 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003166 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003167 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003168 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003169 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003170 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003171 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003172 }
Bill Wendling09025642009-01-30 20:59:34 +00003173 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3174 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003175 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003176 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003177 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003178 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003179 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003180 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003181 }
3182 }
3183 // canonicalize equivalent to ll == rl
3184 if (LL == RR && LR == RL) {
3185 Op1 = ISD::getSetCCSwappedOperands(Op1);
3186 std::swap(RL, RR);
3187 }
3188 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003189 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003190 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003191 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003192 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003193 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3194 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003195 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003196 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003197 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003198 }
3199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003200
Bill Wendling09025642009-01-30 20:59:34 +00003201 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003202 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003203 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003204 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003206
Bill Wendling09025642009-01-30 20:59:34 +00003207 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003208 if (N0.getOpcode() == ISD::AND &&
3209 N1.getOpcode() == ISD::AND &&
3210 N0.getOperand(1).getOpcode() == ISD::Constant &&
3211 N1.getOperand(1).getOpcode() == ISD::Constant &&
3212 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003213 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003214 // We can only do this xform if we know that bits from X that are set in C2
3215 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003216 const APInt &LHSMask =
3217 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3218 const APInt &RHSMask =
3219 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003220
Dan Gohmanea859be2007-06-22 14:59:07 +00003221 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3222 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003223 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003224 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003225 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003226 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003227 }
3228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003229
Chris Lattner516b9622006-09-14 20:50:57 +00003230 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003231 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003232 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003233
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003234 // Simplify the operands using demanded-bits information.
3235 if (!VT.isVector() &&
3236 SimplifyDemandedBits(SDValue(N, 0)))
3237 return SDValue(N, 0);
3238
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003239 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003240}
3241
Chris Lattner516b9622006-09-14 20:50:57 +00003242/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003243static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003244 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003245 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003246 Mask = Op.getOperand(1);
3247 Op = Op.getOperand(0);
3248 } else {
3249 return false;
3250 }
3251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003252
Chris Lattner516b9622006-09-14 20:50:57 +00003253 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3254 Shift = Op;
3255 return true;
3256 }
Bill Wendling09025642009-01-30 20:59:34 +00003257
Scott Michelfdc40a02009-02-17 22:15:04 +00003258 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003259}
3260
Chris Lattner516b9622006-09-14 20:50:57 +00003261// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3262// idioms for rotate, and if the target supports rotation instructions, generate
3263// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003264SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003265 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003266 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003267 if (!TLI.isTypeLegal(VT)) return 0;
3268
3269 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003270 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3271 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003272 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003273
Chris Lattner516b9622006-09-14 20:50:57 +00003274 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003275 SDValue LHSShift; // The shift.
3276 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003277 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3278 return 0; // Not part of a rotate.
3279
Dan Gohman475871a2008-07-27 21:46:04 +00003280 SDValue RHSShift; // The shift.
3281 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003282 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3283 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003284
Chris Lattner516b9622006-09-14 20:50:57 +00003285 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3286 return 0; // Not shifting the same value.
3287
3288 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3289 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003290
Chris Lattner516b9622006-09-14 20:50:57 +00003291 // Canonicalize shl to left side in a shl/srl pair.
3292 if (RHSShift.getOpcode() == ISD::SHL) {
3293 std::swap(LHS, RHS);
3294 std::swap(LHSShift, RHSShift);
3295 std::swap(LHSMask , RHSMask );
3296 }
3297
Duncan Sands83ec4b62008-06-06 12:08:01 +00003298 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SDValue LHSShiftArg = LHSShift.getOperand(0);
3300 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3301 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003302
3303 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3304 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003305 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3306 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003307 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3308 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003309 if ((LShVal + RShVal) != OpSizeInBits)
3310 return 0;
3311
Craig Topper32b73432012-09-29 06:54:22 +00003312 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3313 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003314
Chris Lattner516b9622006-09-14 20:50:57 +00003315 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003316 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003317 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003318
Gabor Greifba36cb52008-08-28 21:40:38 +00003319 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003320 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3321 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003322 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003323 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003324 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3325 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003326 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003327
Bill Wendling317bd702009-01-30 21:14:50 +00003328 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003330
Gabor Greifba36cb52008-08-28 21:40:38 +00003331 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003332 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003333
Chris Lattner516b9622006-09-14 20:50:57 +00003334 // If there is a mask here, and we have a variable shift, we can't be sure
3335 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003336 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003337 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003338
Chris Lattner516b9622006-09-14 20:50:57 +00003339 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3340 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003341 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3342 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003343 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003344 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Stephen Linb4940152013-07-09 00:44:49 +00003345 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003346 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3347 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003348 }
3349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003350
Chris Lattner516b9622006-09-14 20:50:57 +00003351 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3352 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003353 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
Stephen Linb4940152013-07-09 00:44:49 +00003354 RHSShiftAmt == LHSShiftAmt.getOperand(1))
Scott Michelfdc40a02009-02-17 22:15:04 +00003355 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003356 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0)))
3357 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003358 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3359 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003360
Dan Gohman74feef22008-10-17 01:23:35 +00003361 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003362 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3363 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3364 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3365 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3366 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3367 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3368 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3369 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003370 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3371 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003372 if (RExtOp0.getOpcode() == ISD::SUB &&
3373 RExtOp0.getOperand(1) == LExtOp0) {
3374 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003375 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003376 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003377 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003378 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003379 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0)))
3380 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003381 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3382 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003383 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003384 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3385 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003386 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003387 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003388 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003389 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003390 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003391 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0)))
3392 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003393 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3394 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003395 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003396 }
3397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003398
Chris Lattner516b9622006-09-14 20:50:57 +00003399 return 0;
3400}
3401
Dan Gohman475871a2008-07-27 21:46:04 +00003402SDValue DAGCombiner::visitXOR(SDNode *N) {
3403 SDValue N0 = N->getOperand(0);
3404 SDValue N1 = N->getOperand(1);
3405 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003406 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3407 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003408 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003409
Dan Gohman7f321562007-06-25 16:23:39 +00003410 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003411 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003412 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003413 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003414
3415 // fold (xor x, 0) -> x, vector edition
3416 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3417 return N1;
3418 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3419 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003421
Evan Cheng26471c42008-03-25 20:08:07 +00003422 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3423 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3424 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003425 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003426 if (N0.getOpcode() == ISD::UNDEF)
3427 return N0;
3428 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003429 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003430 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003431 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003432 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003433 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003434 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003435 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003436 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003437 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003438 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003439 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003440 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003441 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003442 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003443
Nate Begeman1d4d4142005-09-01 00:19:25 +00003444 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003445 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003446 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003447 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3448 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003449
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003450 if (!LegalOperations ||
3451 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003452 switch (N0.getOpcode()) {
3453 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003454 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003455 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003456 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003457 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003458 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003459 N0.getOperand(3), NotCC);
3460 }
3461 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003462 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003463
Chris Lattner61c5ff42007-09-10 21:39:07 +00003464 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003465 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003466 N0.getNode()->hasOneUse() &&
3467 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003469 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003470 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003471 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003472 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003474
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003475 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003477 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003478 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003479 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3480 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003481 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3482 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003483 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003484 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003485 }
3486 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003487 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003488 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003489 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003490 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003491 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3492 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003493 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3494 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003495 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003496 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003497 }
3498 }
David Majnemer363160a2013-05-08 06:44:42 +00003499 // fold (xor (and x, y), y) -> (and (not x), y)
3500 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3501 N0->getOperand(1) == N1) {
3502 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003503 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003504 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003505 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003506 }
Bill Wendling317bd702009-01-30 21:14:50 +00003507 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003508 if (N1C && N0.getOpcode() == ISD::XOR) {
3509 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3510 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3511 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003512 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003513 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003514 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003515 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003516 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003517 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003518 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003519 }
3520 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003521 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003522 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003523
Chris Lattner35e5c142006-05-05 05:51:50 +00003524 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3525 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003527 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003529
Chris Lattner3e104b12006-04-08 04:15:24 +00003530 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003532 SimplifyDemandedBits(SDValue(N, 0)))
3533 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003534
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003535 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003536}
3537
Chris Lattnere70da202007-12-06 07:33:36 +00003538/// visitShiftByConstant - Handle transforms common to the three shifts, when
3539/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003540SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003541 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003542 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003543
Chris Lattnere70da202007-12-06 07:33:36 +00003544 // We want to pull some binops through shifts, so that we have (and (shift))
3545 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3546 // thing happens with address calculations, so it's important to canonicalize
3547 // it.
3548 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003549
Chris Lattnere70da202007-12-06 07:33:36 +00003550 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003551 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003552 case ISD::OR:
3553 case ISD::XOR:
3554 HighBitSet = false; // We can only transform sra if the high bit is clear.
3555 break;
3556 case ISD::AND:
3557 HighBitSet = true; // We can only transform sra if the high bit is set.
3558 break;
3559 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003560 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003561 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003562 HighBitSet = false; // We can only transform sra if the high bit is clear.
3563 break;
3564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003565
Chris Lattnere70da202007-12-06 07:33:36 +00003566 // We require the RHS of the binop to be a constant as well.
3567 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003568 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003569
3570 // FIXME: disable this unless the input to the binop is a shift by a constant.
3571 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003572 //
Bill Wendling88103372009-01-30 21:37:17 +00003573 // void foo(int *X, int i) { X[i & 1235] = 1; }
3574 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003576 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003577 BinOpLHSVal->getOpcode() != ISD::SRA &&
3578 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3579 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003580 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003581
Owen Andersone50ed302009-08-10 22:56:29 +00003582 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003583
Bill Wendling88103372009-01-30 21:37:17 +00003584 // If this is a signed shift right, and the high bit is modified by the
3585 // logical operation, do not perform the transformation. The highBitSet
3586 // boolean indicates the value of the high bit of the constant which would
3587 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003588 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003589 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3590 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003591 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003593
Chris Lattnere70da202007-12-06 07:33:36 +00003594 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003595 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003596 N->getValueType(0),
3597 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003598
3599 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003600 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003601 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003602 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003603
3604 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003605 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003606}
3607
Dan Gohman475871a2008-07-27 21:46:04 +00003608SDValue DAGCombiner::visitSHL(SDNode *N) {
3609 SDValue N0 = N->getOperand(0);
3610 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003611 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3612 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003613 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003614 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003615
Nate Begeman1d4d4142005-09-01 00:19:25 +00003616 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003617 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003618 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003619 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003620 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003621 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003622 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003623 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003624 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003625 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003626 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003627 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003628 // fold (shl undef, x) -> 0
3629 if (N0.getOpcode() == ISD::UNDEF)
3630 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003631 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003632 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003633 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003634 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003635 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003636 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003637 N1.getOperand(0).getOpcode() == ISD::AND &&
3638 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003639 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003640 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003641 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003642 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003643 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003644 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003645 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3646 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003647 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003648 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003649 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003650 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003651 }
3652 }
3653
Dan Gohman475871a2008-07-27 21:46:04 +00003654 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3655 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003656
3657 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003658 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003659 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003660 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3661 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003662 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003663 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003664 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003665 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003666 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003667
3668 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3669 // For this to be valid, the second form must not preserve any of the bits
3670 // that are shifted out by the inner shift in the first form. This means
3671 // the outer shift size must be >= the number of bits added by the ext.
3672 // As a corollary, we don't care what kind of ext it is.
3673 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3674 N0.getOpcode() == ISD::ANY_EXTEND ||
3675 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3676 N0.getOperand(0).getOpcode() == ISD::SHL &&
3677 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003678 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003679 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3680 uint64_t c2 = N1C->getZExtValue();
3681 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3682 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3683 if (c2 >= OpSizeInBits - InnerShiftSize) {
3684 if (c1 + c2 >= OpSizeInBits)
3685 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003686 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3687 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003688 N0.getOperand(0)->getOperand(0)),
3689 DAG.getConstant(c1 + c2, N1.getValueType()));
3690 }
3691 }
3692
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003693 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3694 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003695 // Only fold this if the inner shift has no other uses -- if it does, folding
3696 // this will increase the total number of instructions.
3697 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003698 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003699 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003700 if (c1 < VT.getSizeInBits()) {
3701 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003702 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3703 VT.getSizeInBits() - c1);
3704 SDValue Shift;
3705 if (c2 > c1) {
3706 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003707 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003708 DAG.getConstant(c2-c1, N1.getValueType()));
3709 } else {
3710 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003711 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003712 DAG.getConstant(c1-c2, N1.getValueType()));
3713 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003714 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003715 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003716 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003717 }
Bill Wendling88103372009-01-30 21:37:17 +00003718 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003719 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3720 SDValue HiBitsMask =
3721 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3722 VT.getSizeInBits() -
3723 N1C->getZExtValue()),
3724 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003725 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003726 HiBitsMask);
3727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003728
Evan Chenge5b51ac2010-04-17 06:13:15 +00003729 if (N1C) {
3730 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3731 if (NewSHL.getNode())
3732 return NewSHL;
3733 }
3734
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003735 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736}
3737
Dan Gohman475871a2008-07-27 21:46:04 +00003738SDValue DAGCombiner::visitSRA(SDNode *N) {
3739 SDValue N0 = N->getOperand(0);
3740 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003741 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3742 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003743 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003744 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003745
Bill Wendling88103372009-01-30 21:37:17 +00003746 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003747 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003748 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003749 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003750 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003751 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003752 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003753 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003754 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003755 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003756 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003757 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003758 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003759 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003760 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003761 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3762 // sext_inreg.
3763 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003764 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003765 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3766 if (VT.isVector())
3767 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3768 ExtVT, VT.getVectorNumElements());
3769 if ((!LegalOperations ||
3770 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003771 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003772 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003773 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003774
Bill Wendling88103372009-01-30 21:37:17 +00003775 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003776 if (N1C && N0.getOpcode() == ISD::SRA) {
3777 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003778 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003779 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003780 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003781 DAG.getConstant(Sum, N1C->getValueType(0)));
3782 }
3783 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003784
Bill Wendling88103372009-01-30 21:37:17 +00003785 // fold (sra (shl X, m), (sub result_size, n))
3786 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003787 // result_size - n != m.
3788 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003789 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003790 if (N0.getOpcode() == ISD::SHL) {
3791 // Get the two constanst of the shifts, CN0 = m, CN = n.
3792 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3793 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003794 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003795 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003796 EVT::getIntegerVT(*DAG.getContext(),
3797 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003798 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003799 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003800
Scott Michelfdc40a02009-02-17 22:15:04 +00003801 // If the shift is not a no-op (in which case this should be just a sign
3802 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003803 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003804 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003805 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003806 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3807 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003808 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003809
Owen Anderson95771af2011-02-25 21:41:48 +00003810 SDValue Amt = DAG.getConstant(ShiftAmt,
3811 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003812 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003813 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003814 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003815 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003816 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003817 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003818 }
3819 }
3820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003821
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003822 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003823 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003824 N1.getOperand(0).getOpcode() == ISD::AND &&
3825 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003826 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003827 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003828 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003829 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003830 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003831 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003832 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3833 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003834 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003835 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003836 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003837 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003838 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003839 }
3840 }
3841
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003842 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3843 // if c1 is equal to the number of bits the trunc removes
3844 if (N0.getOpcode() == ISD::TRUNCATE &&
3845 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3846 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3847 N0.getOperand(0).hasOneUse() &&
3848 N0.getOperand(0).getOperand(1).hasOneUse() &&
3849 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3850 EVT LargeVT = N0.getOperand(0).getValueType();
3851 ConstantSDNode *LargeShiftAmt =
3852 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3853
3854 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3855 LargeShiftAmt->getZExtValue()) {
3856 SDValue Amt =
3857 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003858 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003859 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003860 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003861 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003862 }
3863 }
3864
Scott Michelfdc40a02009-02-17 22:15:04 +00003865 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003866 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3867 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003868
3869
Nate Begeman1d4d4142005-09-01 00:19:25 +00003870 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003871 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003872 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003873
Evan Chenge5b51ac2010-04-17 06:13:15 +00003874 if (N1C) {
3875 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3876 if (NewSRA.getNode())
3877 return NewSRA;
3878 }
3879
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003880 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003881}
3882
Dan Gohman475871a2008-07-27 21:46:04 +00003883SDValue DAGCombiner::visitSRL(SDNode *N) {
3884 SDValue N0 = N->getOperand(0);
3885 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003886 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3887 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003888 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003889 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003890
Nate Begeman1d4d4142005-09-01 00:19:25 +00003891 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003892 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003893 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003894 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003895 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003896 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003897 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003898 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003899 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003900 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003901 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003902 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003903 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003904 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003905 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003906 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Bill Wendling88103372009-01-30 21:37:17 +00003908 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003909 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003910 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003911 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3912 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003913 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003914 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003915 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003916 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003917 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003918
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003919 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003920 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3921 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003922 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003923 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003924 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3925 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003926 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3927 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003928 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003929 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003930 if (c1 + OpSizeInBits == InnerShiftSize) {
3931 if (c1 + c2 >= InnerShiftSize)
3932 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003933 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
3934 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003935 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003936 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003937 }
3938 }
3939
Chris Lattnerefcddc32010-04-15 05:28:43 +00003940 // fold (srl (shl x, c), c) -> (and x, cst2)
3941 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3942 N0.getValueSizeInBits() <= 64) {
3943 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003944 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00003945 DAG.getConstant(~0ULL >> ShAmt, VT));
3946 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003947
Michael Liao2da86392013-06-21 18:45:27 +00003948 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00003949 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3950 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003951 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003952 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003953 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003954
Evan Chenge5b51ac2010-04-17 06:13:15 +00003955 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003956 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003957 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003958 N0.getOperand(0),
3959 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003960 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00003961 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
3962 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3963 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
3964 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003965 }
Chris Lattner06afe072006-05-05 22:53:17 +00003966 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003967
Chris Lattner3657ffe2006-10-12 20:23:19 +00003968 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3969 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003970 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003971 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003972 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003973 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003974
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003975 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003976 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003977 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003978 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003979 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003980
Chris Lattner350bec02006-04-02 06:11:11 +00003981 // If any of the input bits are KnownOne, then the input couldn't be all
3982 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003983 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003984
Chris Lattner350bec02006-04-02 06:11:11 +00003985 // If all of the bits input the to ctlz node are known to be zero, then
3986 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003987 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003988 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003989
Chris Lattner350bec02006-04-02 06:11:11 +00003990 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003991 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003992 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003993 // could be set on input to the CTLZ node. If this bit is set, the SRL
3994 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3995 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003996 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003997 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003998
Chris Lattner350bec02006-04-02 06:11:11 +00003999 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004000 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004001 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004002 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004003 }
Bill Wendling88103372009-01-30 21:37:17 +00004004
Andrew Trickac6d9be2013-05-25 02:42:55 +00004005 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004006 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004007 }
4008 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004009
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004010 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004011 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004012 N1.getOperand(0).getOpcode() == ISD::AND &&
4013 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004014 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004015 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004016 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004017 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004018 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004019 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004020 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4021 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004022 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004023 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004024 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004025 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004026 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004027 }
4028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004029
Chris Lattner61a4c072007-04-18 03:06:49 +00004030 // fold operands of srl based on knowledge that the low bits are not
4031 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004032 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4033 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004034
Evan Cheng9ab2b982009-12-18 21:31:31 +00004035 if (N1C) {
4036 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4037 if (NewSRL.getNode())
4038 return NewSRL;
4039 }
4040
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004041 // Attempt to convert a srl of a load into a narrower zero-extending load.
4042 SDValue NarrowLoad = ReduceLoadWidth(N);
4043 if (NarrowLoad.getNode())
4044 return NarrowLoad;
4045
Evan Cheng9ab2b982009-12-18 21:31:31 +00004046 // Here is a common situation. We want to optimize:
4047 //
4048 // %a = ...
4049 // %b = and i32 %a, 2
4050 // %c = srl i32 %b, 1
4051 // brcond i32 %c ...
4052 //
4053 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004054 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004055 // %a = ...
4056 // %b = and %a, 2
4057 // %c = setcc eq %b, 0
4058 // brcond %c ...
4059 //
4060 // However when after the source operand of SRL is optimized into AND, the SRL
4061 // itself may not be optimized further. Look for it and add the BRCOND into
4062 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004063 if (N->hasOneUse()) {
4064 SDNode *Use = *N->use_begin();
4065 if (Use->getOpcode() == ISD::BRCOND)
4066 AddToWorkList(Use);
4067 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4068 // Also look pass the truncate.
4069 Use = *Use->use_begin();
4070 if (Use->getOpcode() == ISD::BRCOND)
4071 AddToWorkList(Use);
4072 }
4073 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004074
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004075 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004076}
4077
Dan Gohman475871a2008-07-27 21:46:04 +00004078SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4079 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004080 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004081
4082 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004083 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004084 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004085 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004086}
4087
Chandler Carruth63974b22011-12-13 01:56:10 +00004088SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4089 SDValue N0 = N->getOperand(0);
4090 EVT VT = N->getValueType(0);
4091
4092 // fold (ctlz_zero_undef c1) -> c2
4093 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004094 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004095 return SDValue();
4096}
4097
Dan Gohman475871a2008-07-27 21:46:04 +00004098SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4099 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004100 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004101
Nate Begeman1d4d4142005-09-01 00:19:25 +00004102 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004103 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004104 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004105 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004106}
4107
Chandler Carruth63974b22011-12-13 01:56:10 +00004108SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4109 SDValue N0 = N->getOperand(0);
4110 EVT VT = N->getValueType(0);
4111
4112 // fold (cttz_zero_undef c1) -> c2
4113 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004114 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004115 return SDValue();
4116}
4117
Dan Gohman475871a2008-07-27 21:46:04 +00004118SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4119 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004120 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004121
Nate Begeman1d4d4142005-09-01 00:19:25 +00004122 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004123 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004124 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004125 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004126}
4127
Dan Gohman475871a2008-07-27 21:46:04 +00004128SDValue DAGCombiner::visitSELECT(SDNode *N) {
4129 SDValue N0 = N->getOperand(0);
4130 SDValue N1 = N->getOperand(1);
4131 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4134 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004135 EVT VT = N->getValueType(0);
4136 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004137
Bill Wendling34584e62009-01-30 22:02:18 +00004138 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004139 if (N1 == N2)
4140 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004141 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004142 if (N0C && !N0C->isNullValue())
4143 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004144 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004145 if (N0C && N0C->isNullValue())
4146 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004147 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004149 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004150 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004151 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004153 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004154 TLI.getBooleanContents(false) ==
4155 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004156 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004157 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004158 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004159 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004160 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004161 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004162 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004163 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004164 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004165 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4166 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004167 }
Bill Wendling34584e62009-01-30 22:02:18 +00004168 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004170 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004171 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004172 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004173 }
Bill Wendling34584e62009-01-30 22:02:18 +00004174 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004176 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004177 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004178 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004179 }
Bill Wendling34584e62009-01-30 22:02:18 +00004180 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004182 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004183 // fold (select X, X, Y) -> (or X, Y)
4184 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004186 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004187 // fold (select X, Y, X) -> (and X, Y)
4188 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004189 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004190 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004191
Chris Lattner40c62d52005-10-18 06:04:22 +00004192 // If we can fold this based on the true/false value, do so.
4193 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004194 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004195
Nate Begeman44728a72005-09-19 22:34:01 +00004196 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004197 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004198 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004199 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004200 // having to say they don't support SELECT_CC on every type the DAG knows
4201 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004202 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004203 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004204 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004205 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004206 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004207 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004208 }
Bill Wendling34584e62009-01-30 22:02:18 +00004209
Dan Gohman475871a2008-07-27 21:46:04 +00004210 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004211}
4212
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004213SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4214 SDValue N0 = N->getOperand(0);
4215 SDValue N1 = N->getOperand(1);
4216 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004217 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004218
4219 // Canonicalize integer abs.
4220 // vselect (setg[te] X, 0), X, -X ->
4221 // vselect (setgt X, -1), X, -X ->
4222 // vselect (setl[te] X, 0), -X, X ->
4223 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4224 if (N0.getOpcode() == ISD::SETCC) {
4225 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4226 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4227 bool isAbs = false;
4228 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4229
4230 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4231 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4232 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4233 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4234 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4235 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4236 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4237
4238 if (isAbs) {
4239 EVT VT = LHS.getValueType();
4240 SDValue Shift = DAG.getNode(
4241 ISD::SRA, DL, VT, LHS,
4242 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4243 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4244 AddToWorkList(Shift.getNode());
4245 AddToWorkList(Add.getNode());
4246 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4247 }
4248 }
4249
4250 return SDValue();
4251}
4252
Dan Gohman475871a2008-07-27 21:46:04 +00004253SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4254 SDValue N0 = N->getOperand(0);
4255 SDValue N1 = N->getOperand(1);
4256 SDValue N2 = N->getOperand(2);
4257 SDValue N3 = N->getOperand(3);
4258 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004259 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004260
Nate Begeman44728a72005-09-19 22:34:01 +00004261 // fold select_cc lhs, rhs, x, x, cc -> x
4262 if (N2 == N3)
4263 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004264
Chris Lattner5f42a242006-09-20 06:19:26 +00004265 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004266 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004267 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004268 if (SCC.getNode()) {
4269 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004270
Stephen Lin7e6d6202013-06-15 04:03:33 +00004271 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4272 if (!SCCC->isNullValue())
4273 return N2; // cond always true -> true val
4274 else
4275 return N3; // cond always false -> false val
4276 }
4277
4278 // Fold to a simpler select_cc
4279 if (SCC.getOpcode() == ISD::SETCC)
4280 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4281 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4282 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004284
Chris Lattner40c62d52005-10-18 06:04:22 +00004285 // If we can fold this based on the true/false value, do so.
4286 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004287 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004288
Nate Begeman44728a72005-09-19 22:34:01 +00004289 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004290 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004291}
4292
Dan Gohman475871a2008-07-27 21:46:04 +00004293SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004294 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004295 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004296 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004297}
4298
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004299// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004300// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004301// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004302// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004303static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004304 unsigned ExtOpc,
4305 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004306 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004307 bool HasCopyToRegUses = false;
4308 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004309 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4310 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004311 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004312 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004313 if (User == N)
4314 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004315 if (UI.getUse().getResNo() != N0.getResNo())
4316 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004317 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004318 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004319 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4320 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4321 // Sign bits will be lost after a zext.
4322 return false;
4323 bool Add = false;
4324 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004326 if (UseOp == N0)
4327 continue;
4328 if (!isa<ConstantSDNode>(UseOp))
4329 return false;
4330 Add = true;
4331 }
4332 if (Add)
4333 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004334 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004335 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004336 // If truncates aren't free and there are users we can't
4337 // extend, it isn't worthwhile.
4338 if (!isTruncFree)
4339 return false;
4340 // Remember if this value is live-out.
4341 if (User->getOpcode() == ISD::CopyToReg)
4342 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004343 }
4344
4345 if (HasCopyToRegUses) {
4346 bool BothLiveOut = false;
4347 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4348 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004349 SDUse &Use = UI.getUse();
4350 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4351 BothLiveOut = true;
4352 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004353 }
4354 }
4355 if (BothLiveOut)
4356 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004357 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004358 return ExtendNodes.size();
4359 }
4360 return true;
4361}
4362
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004363void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004364 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004365 ISD::NodeType ExtType) {
4366 // Extend SetCC uses if necessary.
4367 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4368 SDNode *SetCC = SetCCs[i];
4369 SmallVector<SDValue, 4> Ops;
4370
4371 for (unsigned j = 0; j != 2; ++j) {
4372 SDValue SOp = SetCC->getOperand(j);
4373 if (SOp == Trunc)
4374 Ops.push_back(ExtLoad);
4375 else
4376 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4377 }
4378
4379 Ops.push_back(SetCC->getOperand(2));
4380 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4381 &Ops[0], Ops.size()));
4382 }
4383}
4384
Dan Gohman475871a2008-07-27 21:46:04 +00004385SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4386 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004387 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004388
Nate Begeman1d4d4142005-09-01 00:19:25 +00004389 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004390 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004391 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004392
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004393 // fold (sext (sext x)) -> (sext x)
4394 // fold (sext (aext x)) -> (sext x)
4395 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004396 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004397 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004398
Chris Lattner22558872007-02-26 03:13:59 +00004399 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004400 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4401 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004402 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4403 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004404 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4405 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004406 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004407 // CombineTo deleted the truncate, if needed, but not what's under it.
4408 AddToWorkList(oye);
4409 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004410 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004411 }
Evan Chengc88138f2007-03-22 01:54:19 +00004412
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004413 // See if the value being truncated is already sign extended. If so, just
4414 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004415 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004416 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4417 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4418 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004419 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004420
Chris Lattner22558872007-02-26 03:13:59 +00004421 if (OpBits == DestBits) {
4422 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4423 // bits, it is already ready.
4424 if (NumSignBits > DestBits-MidBits)
4425 return Op;
4426 } else if (OpBits < DestBits) {
4427 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4428 // bits, just sext from i32.
4429 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004430 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004431 } else {
4432 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4433 // bits, just truncate to i32.
4434 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004435 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner22558872007-02-26 03:13:59 +00004438 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004439 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4440 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004441 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004442 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004443 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004444 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4445 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004446 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004447 }
Chris Lattner6007b842006-09-21 06:00:20 +00004448 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004449
Evan Cheng110dec22005-12-14 02:19:23 +00004450 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004451 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004452 // on vectors in one instruction. We only perform this transformation on
4453 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004454 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004455 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004456 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004457 bool DoXform = true;
4458 SmallVector<SDNode*, 4> SetCCs;
4459 if (!N0.hasOneUse())
4460 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4461 if (DoXform) {
4462 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004463 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004464 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004465 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004466 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004467 LN0->isVolatile(), LN0->isNonTemporal(),
4468 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004469 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004470 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004471 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004472 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004473 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004474 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004475 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004476 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004477 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004478
4479 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4480 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004481 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4482 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004483 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004484 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004485 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004486 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004487 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004488 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004489 LN0->getBasePtr(), LN0->getPointerInfo(),
4490 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004491 LN0->isVolatile(), LN0->isNonTemporal(),
4492 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004493 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004494 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004495 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004496 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004497 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004498 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004499 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004500 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004501
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004502 // fold (sext (and/or/xor (load x), cst)) ->
4503 // (and/or/xor (sextload x), (sext cst))
4504 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4505 N0.getOpcode() == ISD::XOR) &&
4506 isa<LoadSDNode>(N0.getOperand(0)) &&
4507 N0.getOperand(1).getOpcode() == ISD::Constant &&
4508 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4509 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4510 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4511 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4512 bool DoXform = true;
4513 SmallVector<SDNode*, 4> SetCCs;
4514 if (!N0.hasOneUse())
4515 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4516 SetCCs, TLI);
4517 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004518 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004519 LN0->getChain(), LN0->getBasePtr(),
4520 LN0->getPointerInfo(),
4521 LN0->getMemoryVT(),
4522 LN0->isVolatile(),
4523 LN0->isNonTemporal(),
4524 LN0->getAlignment());
4525 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4526 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004527 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004528 ExtLoad, DAG.getConstant(Mask, VT));
4529 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004530 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004531 N0.getOperand(0).getValueType(), ExtLoad);
4532 CombineTo(N, And);
4533 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004534 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004535 ISD::SIGN_EXTEND);
4536 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4537 }
4538 }
4539 }
4540
Chris Lattner20a35c32007-04-11 05:32:27 +00004541 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004542 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004543 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004544 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004545 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004546 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004547 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004548 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4549 // of the same size as the compared operands. Only optimize sext(setcc())
4550 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004551 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004552
4553 // We know that the # elements of the results is the same as the
4554 // # elements of the compare (and the # elements of the compare result
4555 // for that matter). Check to see that they are the same size. If so,
4556 // we know that the element size of the sext'd result matches the
4557 // element size of the compare operands.
4558 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004559 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004560 N0.getOperand(1),
4561 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004562
Dan Gohman3ce89f42010-04-30 17:19:19 +00004563 // If the desired elements are smaller or larger than the source
4564 // elements we can use a matching integer vector type and then
4565 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004566 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004567 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004568 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004569 N0.getOperand(0), N0.getOperand(1),
4570 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004571 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004572 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004573 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004574
Chris Lattner2b7a2712009-07-08 00:31:33 +00004575 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004576 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004577 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004578 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004579 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004580 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004581 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004582 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004583 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004584 if (!VT.isVector() &&
4585 (!LegalOperations ||
4586 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4587 return DAG.getSelect(SDLoc(N), VT,
4588 DAG.getSetCC(SDLoc(N),
4589 getSetCCResultType(VT),
4590 N0.getOperand(0), N0.getOperand(1),
4591 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4592 NegOne, DAG.getConstant(0, VT));
4593 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004595
Dan Gohman8f0ad582008-04-28 16:58:24 +00004596 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004597 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004598 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004599 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004600
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004601 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004602}
4603
Rafael Espindoladecbc432012-04-09 16:06:03 +00004604// isTruncateOf - If N is a truncate of some other value, return true, record
4605// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4606// This function computes KnownZero to avoid a duplicated call to
4607// ComputeMaskedBits in the caller.
4608static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4609 APInt &KnownZero) {
4610 APInt KnownOne;
4611 if (N->getOpcode() == ISD::TRUNCATE) {
4612 Op = N->getOperand(0);
4613 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4614 return true;
4615 }
4616
4617 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4618 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4619 return false;
4620
4621 SDValue Op0 = N->getOperand(0);
4622 SDValue Op1 = N->getOperand(1);
4623 assert(Op0.getValueType() == Op1.getValueType());
4624
4625 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4626 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004627 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004628 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004629 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004630 Op = Op0;
4631 else
4632 return false;
4633
4634 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4635
4636 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4637 return false;
4638
4639 return true;
4640}
4641
Dan Gohman475871a2008-07-27 21:46:04 +00004642SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4643 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004644 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004645
Nate Begeman1d4d4142005-09-01 00:19:25 +00004646 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004647 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004648 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004649 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004650 // fold (zext (aext x)) -> (zext x)
4651 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004652 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004653 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004654
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004655 // fold (zext (truncate x)) -> (zext x) or
4656 // (zext (truncate x)) -> (truncate x)
4657 // This is valid when the truncated bits of x are already zero.
4658 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004659 SDValue Op;
4660 APInt KnownZero;
4661 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4662 APInt TruncatedBits =
4663 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4664 APInt(Op.getValueSizeInBits(), 0) :
4665 APInt::getBitsSet(Op.getValueSizeInBits(),
4666 N0.getValueSizeInBits(),
4667 std::min(Op.getValueSizeInBits(),
4668 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004669 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004670 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004671 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004672 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004673 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004674
4675 return Op;
4676 }
4677 }
4678
Evan Chengc88138f2007-03-22 01:54:19 +00004679 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4680 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004681 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004682 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4683 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004684 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4685 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004686 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004687 // CombineTo deleted the truncate, if needed, but not what's under it.
4688 AddToWorkList(oye);
4689 }
Eli Friedmane545d382011-04-16 23:25:34 +00004690 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004691 }
Evan Chengc88138f2007-03-22 01:54:19 +00004692 }
4693
Chris Lattner6007b842006-09-21 06:00:20 +00004694 // fold (zext (truncate x)) -> (and x, mask)
4695 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004696 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004697
4698 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4699 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4700 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4701 if (NarrowLoad.getNode()) {
4702 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4703 if (NarrowLoad.getNode() != N0.getNode()) {
4704 CombineTo(N0.getNode(), NarrowLoad);
4705 // CombineTo deleted the truncate, if needed, but not what's under it.
4706 AddToWorkList(oye);
4707 }
4708 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4709 }
4710
Dan Gohman475871a2008-07-27 21:46:04 +00004711 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004712 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004713 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004714 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004715 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004716 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004717 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004718 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004719 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004720 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004722
Dan Gohman97121ba2009-04-08 00:15:30 +00004723 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4724 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004725 if (N0.getOpcode() == ISD::AND &&
4726 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004727 N0.getOperand(1).getOpcode() == ISD::Constant &&
4728 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4729 N0.getValueType()) ||
4730 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004731 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004732 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004733 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004734 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004735 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004736 }
Dan Gohman220a8232008-03-03 23:51:38 +00004737 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004738 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004739 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004740 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004741 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004742
Evan Cheng110dec22005-12-14 02:19:23 +00004743 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004744 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004745 // on vectors in one instruction. We only perform this transformation on
4746 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004747 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004748 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004749 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004750 bool DoXform = true;
4751 SmallVector<SDNode*, 4> SetCCs;
4752 if (!N0.hasOneUse())
4753 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4754 if (DoXform) {
4755 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004756 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004757 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004758 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004759 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004760 LN0->isVolatile(), LN0->isNonTemporal(),
4761 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004762 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004763 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004764 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004765 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004766
Andrew Trickac6d9be2013-05-25 02:42:55 +00004767 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004768 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004769 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004770 }
Evan Cheng110dec22005-12-14 02:19:23 +00004771 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004772
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004773 // fold (zext (and/or/xor (load x), cst)) ->
4774 // (and/or/xor (zextload x), (zext cst))
4775 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4776 N0.getOpcode() == ISD::XOR) &&
4777 isa<LoadSDNode>(N0.getOperand(0)) &&
4778 N0.getOperand(1).getOpcode() == ISD::Constant &&
4779 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4780 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4781 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4782 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4783 bool DoXform = true;
4784 SmallVector<SDNode*, 4> SetCCs;
4785 if (!N0.hasOneUse())
4786 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4787 SetCCs, TLI);
4788 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004789 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004790 LN0->getChain(), LN0->getBasePtr(),
4791 LN0->getPointerInfo(),
4792 LN0->getMemoryVT(),
4793 LN0->isVolatile(),
4794 LN0->isNonTemporal(),
4795 LN0->getAlignment());
4796 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4797 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004798 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004799 ExtLoad, DAG.getConstant(Mask, VT));
4800 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004801 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004802 N0.getOperand(0).getValueType(), ExtLoad);
4803 CombineTo(N, And);
4804 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004805 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004806 ISD::ZERO_EXTEND);
4807 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4808 }
4809 }
4810 }
4811
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004812 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4813 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004814 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4815 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004816 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004817 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004818 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004819 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004820 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004821 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004822 LN0->getBasePtr(), LN0->getPointerInfo(),
4823 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004824 LN0->isVolatile(), LN0->isNonTemporal(),
4825 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004826 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004827 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004828 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004829 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004830 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004831 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004832 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004833 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004834
Chris Lattner20a35c32007-04-11 05:32:27 +00004835 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004836 if (!LegalOperations && VT.isVector()) {
4837 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4838 // Only do this before legalize for now.
4839 EVT N0VT = N0.getOperand(0).getValueType();
4840 EVT EltVT = VT.getVectorElementType();
4841 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4842 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004843 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004844 // We know that the # elements of the results is the same as the
4845 // # elements of the compare (and the # elements of the compare result
4846 // for that matter). Check to see that they are the same size. If so,
4847 // we know that the element size of the sext'd result matches the
4848 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004849 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4850 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004851 N0.getOperand(1),
4852 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004853 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004854 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004855
4856 // If the desired elements are smaller or larger than the source
4857 // elements we can use a matching integer vector type and then
4858 // truncate/sign extend
4859 EVT MatchingElementType =
4860 EVT::getIntegerVT(*DAG.getContext(),
4861 N0VT.getScalarType().getSizeInBits());
4862 EVT MatchingVectorType =
4863 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4864 N0VT.getVectorNumElements());
4865 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004866 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004867 N0.getOperand(1),
4868 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004869 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4870 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4871 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004872 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004873 }
4874
4875 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004876 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004877 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004878 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004879 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004880 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004881 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004882
Evan Cheng9818c042009-12-15 03:00:32 +00004883 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004884 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004885 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004886 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4887 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004888 SDValue ShAmt = N0.getOperand(1);
4889 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004890 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004891 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004892 // If the original shl may be shifting out bits, do not perform this
4893 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004894 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4895 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4896 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004897 return SDValue();
4898 }
Chris Lattnere0751182011-02-13 19:09:16 +00004899
Andrew Trickac6d9be2013-05-25 02:42:55 +00004900 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004901
4902 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004903 if (VT.getSizeInBits() >= 256)
4904 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004905
Chris Lattnere0751182011-02-13 19:09:16 +00004906 return DAG.getNode(N0.getOpcode(), DL, VT,
4907 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4908 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004909 }
4910
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004911 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004912}
4913
Dan Gohman475871a2008-07-27 21:46:04 +00004914SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4915 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004916 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004917
Chris Lattner5ffc0662006-05-05 05:58:59 +00004918 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004919 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004920 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004921 // fold (aext (aext x)) -> (aext x)
4922 // fold (aext (zext x)) -> (zext x)
4923 // fold (aext (sext x)) -> (sext x)
4924 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4925 N0.getOpcode() == ISD::ZERO_EXTEND ||
4926 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004927 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004928
Evan Chengc88138f2007-03-22 01:54:19 +00004929 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4930 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4931 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004932 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4933 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004934 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4935 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004936 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004937 // CombineTo deleted the truncate, if needed, but not what's under it.
4938 AddToWorkList(oye);
4939 }
Eli Friedmane545d382011-04-16 23:25:34 +00004940 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004941 }
Evan Chengc88138f2007-03-22 01:54:19 +00004942 }
4943
Chris Lattner84750582006-09-20 06:29:17 +00004944 // fold (aext (truncate x))
4945 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004946 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004947 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004948 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004949 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004950 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
4951 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004953
Dan Gohman97121ba2009-04-08 00:15:30 +00004954 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4955 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004956 if (N0.getOpcode() == ISD::AND &&
4957 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004958 N0.getOperand(1).getOpcode() == ISD::Constant &&
4959 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4960 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004961 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004962 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004963 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004964 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004965 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004966 }
Dan Gohman220a8232008-03-03 23:51:38 +00004967 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004968 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004969 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00004970 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004972
Chris Lattner5ffc0662006-05-05 05:58:59 +00004973 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004974 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004975 // on vectors in one instruction. We only perform this transformation on
4976 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004977 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004978 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004979 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004980 bool DoXform = true;
4981 SmallVector<SDNode*, 4> SetCCs;
4982 if (!N0.hasOneUse())
4983 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4984 if (DoXform) {
4985 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004986 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004987 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004988 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004989 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004990 LN0->isVolatile(), LN0->isNonTemporal(),
4991 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004992 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004993 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004994 N0.getValueType(), ExtLoad);
4995 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004996 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004997 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004998 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4999 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005000 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005001
Chris Lattner5ffc0662006-05-05 05:58:59 +00005002 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5003 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5004 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005005 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005006 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005007 N0.hasOneUse()) {
5008 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005009 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005010 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005011 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005012 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005013 LN0->isVolatile(), LN0->isNonTemporal(),
5014 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005015 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005016 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005017 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005018 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005019 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005020 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005021 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005022
Chris Lattner20a35c32007-04-11 05:32:27 +00005023 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005024 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5025 // Only do this before legalize for now.
5026 if (VT.isVector() && !LegalOperations) {
5027 EVT N0VT = N0.getOperand(0).getValueType();
5028 // We know that the # elements of the results is the same as the
5029 // # elements of the compare (and the # elements of the compare result
5030 // for that matter). Check to see that they are the same size. If so,
5031 // we know that the element size of the sext'd result matches the
5032 // element size of the compare operands.
5033 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005034 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005035 N0.getOperand(1),
5036 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005037 // If the desired elements are smaller or larger than the source
5038 // elements we can use a matching integer vector type and then
5039 // truncate/sign extend
5040 else {
Duncan Sands34727662010-07-12 08:16:59 +00005041 EVT MatchingElementType =
5042 EVT::getIntegerVT(*DAG.getContext(),
5043 N0VT.getScalarType().getSizeInBits());
5044 EVT MatchingVectorType =
5045 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5046 N0VT.getVectorNumElements());
5047 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005048 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005049 N0.getOperand(1),
5050 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005051 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005052 }
5053 }
5054
5055 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005056 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005057 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005058 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005059 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005060 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005061 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005062 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005063
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005064 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005065}
5066
Chris Lattner2b4c2792007-10-13 06:35:54 +00005067/// GetDemandedBits - See if the specified operand can be simplified with the
5068/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005069/// simpler operand, otherwise return a null SDValue.
5070SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005071 switch (V.getOpcode()) {
5072 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005073 case ISD::Constant: {
5074 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5075 assert(CV != 0 && "Const value should be ConstSDNode.");
5076 const APInt &CVal = CV->getAPIntValue();
5077 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005078 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005079 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005080 break;
5081 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005082 case ISD::OR:
5083 case ISD::XOR:
5084 // If the LHS or RHS don't contribute bits to the or, drop them.
5085 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5086 return V.getOperand(1);
5087 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5088 return V.getOperand(0);
5089 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005090 case ISD::SRL:
5091 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005092 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005093 break;
5094 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5095 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005096 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005097
Dan Gohmancc91d632009-01-03 19:22:06 +00005098 // Watch out for shift count overflow though.
5099 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005100 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005101 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005102 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005103 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005104 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005105 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005106 }
Dan Gohman475871a2008-07-27 21:46:04 +00005107 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005108}
5109
Evan Chengc88138f2007-03-22 01:54:19 +00005110/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5111/// bits and then truncated to a narrower type and where N is a multiple
5112/// of number of bits of the narrower type, transform it to a narrower load
5113/// from address + N / num of bits of new type. If the result is to be
5114/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005115SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005116 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005117
Evan Chengc88138f2007-03-22 01:54:19 +00005118 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005119 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005120 EVT VT = N->getValueType(0);
5121 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005122
Dan Gohman7f8613e2008-08-14 20:04:46 +00005123 // This transformation isn't valid for vector loads.
5124 if (VT.isVector())
5125 return SDValue();
5126
Dan Gohmand1996362010-01-09 02:13:55 +00005127 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005128 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005129 if (Opc == ISD::SIGN_EXTEND_INREG) {
5130 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005131 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005132 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005133 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005134 ExtType = ISD::ZEXTLOAD;
5135 N0 = SDValue(N, 0);
5136 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5137 if (!N01) return SDValue();
5138 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5139 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005140 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005141 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5142 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005143
Owen Andersone50ed302009-08-10 22:56:29 +00005144 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005145
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005146 // Do not generate loads of non-round integer types since these can
5147 // be expensive (and would be wrong if the type is not byte sized).
5148 if (!ExtVT.isRound())
5149 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005150
Evan Chengc88138f2007-03-22 01:54:19 +00005151 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005152 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005153 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005154 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005155 // Is the shift amount a multiple of size of VT?
5156 if ((ShAmt & (EVTBits-1)) == 0) {
5157 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005158 // Is the load width a multiple of size of VT?
5159 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005160 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005161 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005162
Chris Lattnercbf68df2010-12-22 08:02:57 +00005163 // At this point, we must have a load or else we can't do the transform.
5164 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005165
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005166 // Because a SRL must be assumed to *need* to zero-extend the high bits
5167 // (as opposed to anyext the high bits), we can't combine the zextload
5168 // lowering of SRL and an sextload.
5169 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5170 return SDValue();
5171
Chris Lattner2831a192010-10-01 05:36:09 +00005172 // If the shift amount is larger than the input type then we're not
5173 // accessing any of the loaded bytes. If the load was a zextload/extload
5174 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005175 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005176 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005177 }
5178 }
5179
Dan Gohman394d6292010-11-03 01:47:46 +00005180 // If the load is shifted left (and the result isn't shifted back right),
5181 // we can fold the truncate through the shift.
5182 unsigned ShLeftAmt = 0;
5183 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005184 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005185 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5186 ShLeftAmt = N01->getZExtValue();
5187 N0 = N0.getOperand(0);
5188 }
5189 }
Owen Anderson95771af2011-02-25 21:41:48 +00005190
Chris Lattner4c32bc22010-12-22 07:36:50 +00005191 // If we haven't found a load, we can't narrow it. Don't transform one with
5192 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005193 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5194 return SDValue();
5195
5196 // Don't change the width of a volatile load.
5197 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5198 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005199 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005200
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005201 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005202 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005203 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005204
Bill Schmidt89e88e32013-01-14 22:04:38 +00005205 // For the transform to be legal, the load must produce only two values
5206 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005207 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005208 // transformation is not equivalent, and the downstream logic to replace
5209 // uses gets things wrong.
5210 if (LN0->getNumValues() > 2)
5211 return SDValue();
5212
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005213 // If the load that we're shrinking is an extload and we're not just
5214 // discarding the extension we can't simply shrink the load. Bail.
5215 // TODO: It would be possible to merge the extensions in some cases.
5216 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5217 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5218 return SDValue();
5219
Chris Lattner4c32bc22010-12-22 07:36:50 +00005220 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005221
Evan Cheng16436df2012-06-26 01:19:33 +00005222 if (PtrType == MVT::Untyped || PtrType.isExtended())
5223 // It's not possible to generate a constant of extended or untyped type.
5224 return SDValue();
5225
Chris Lattner4c32bc22010-12-22 07:36:50 +00005226 // For big endian targets, we need to adjust the offset to the pointer to
5227 // load the correct bytes.
5228 if (TLI.isBigEndian()) {
5229 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5230 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5231 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005232 }
5233
Chris Lattner4c32bc22010-12-22 07:36:50 +00005234 uint64_t PtrOff = ShAmt / 8;
5235 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005236 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005237 PtrType, LN0->getBasePtr(),
5238 DAG.getConstant(PtrOff, PtrType));
5239 AddToWorkList(NewPtr.getNode());
5240
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005241 SDValue Load;
5242 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005243 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005244 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005245 LN0->isVolatile(), LN0->isNonTemporal(),
5246 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005247 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005248 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005249 LN0->getPointerInfo().getWithOffset(PtrOff),
5250 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5251 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005252
5253 // Replace the old load's chain with the new load's chain.
5254 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005255 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005256
5257 // Shift the result left, if we've swallowed a left shift.
5258 SDValue Result = Load;
5259 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005260 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005261 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5262 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005263 // If the shift amount is as large as the result size (but, presumably,
5264 // no larger than the source) then the useful bits of the result are
5265 // zero; we can't simply return the shortened shift, because the result
5266 // of that operation is undefined.
5267 if (ShLeftAmt >= VT.getSizeInBits())
5268 Result = DAG.getConstant(0, VT);
5269 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005270 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005271 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005272 }
5273
5274 // Return the new loaded value.
5275 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005276}
5277
Dan Gohman475871a2008-07-27 21:46:04 +00005278SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5279 SDValue N0 = N->getOperand(0);
5280 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005281 EVT VT = N->getValueType(0);
5282 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005283 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005284 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005285
Nate Begeman1d4d4142005-09-01 00:19:25 +00005286 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005287 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005288 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005289
Chris Lattner541a24f2006-05-06 22:43:44 +00005290 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005291 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005292 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005293
Nate Begeman646d7e22005-09-02 21:18:40 +00005294 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5295 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005296 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005297 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005298 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005299
Dan Gohman75dcf082008-07-31 00:50:31 +00005300 // fold (sext_in_reg (sext x)) -> (sext x)
5301 // fold (sext_in_reg (aext x)) -> (sext x)
5302 // if x is small enough.
5303 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5304 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005305 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5306 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005307 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005308 }
5309
Chris Lattner95a5e052007-04-17 19:03:21 +00005310 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005311 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005312 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005313
Chris Lattner95a5e052007-04-17 19:03:21 +00005314 // fold operands of sext_in_reg based on knowledge that the top bits are not
5315 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005316 if (SimplifyDemandedBits(SDValue(N, 0)))
5317 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005318
Evan Chengc88138f2007-03-22 01:54:19 +00005319 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5320 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005321 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005322 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005323 return NarrowLoad;
5324
Bill Wendling8509c902009-01-30 22:33:24 +00005325 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005326 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005327 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5328 if (N0.getOpcode() == ISD::SRL) {
5329 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005330 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005331 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005332 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005333 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005334 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005335 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005336 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005337 }
5338 }
Evan Chengc88138f2007-03-22 01:54:19 +00005339
Nate Begemanded49632005-10-13 03:11:28 +00005340 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005341 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005342 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005343 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005344 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005345 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005346 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005347 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005348 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005349 LN0->getBasePtr(), LN0->getPointerInfo(),
5350 EVT,
David Greene1e559442010-02-15 17:00:31 +00005351 LN0->isVolatile(), LN0->isNonTemporal(),
5352 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005353 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005354 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005355 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005356 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005357 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005358 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005359 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005360 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005361 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005362 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005363 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005364 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005365 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005366 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005367 LN0->getBasePtr(), LN0->getPointerInfo(),
5368 EVT,
David Greene1e559442010-02-15 17:00:31 +00005369 LN0->isVolatile(), LN0->isNonTemporal(),
5370 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005371 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005372 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005373 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005374 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005375
5376 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5377 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5378 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5379 N0.getOperand(1), false);
5380 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005381 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005382 BSwap, N1);
5383 }
5384
Dan Gohman475871a2008-07-27 21:46:04 +00005385 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005386}
5387
Dan Gohman475871a2008-07-27 21:46:04 +00005388SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5389 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005390 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005391 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005392
5393 // noop truncate
5394 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005395 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005396 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005397 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005398 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005399 // fold (truncate (truncate x)) -> (truncate x)
5400 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005401 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005402 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005403 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5404 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005405 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005406 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005407 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005408 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005409 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005410 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005411 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005412 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005413 // if the source and dest are the same type, we can drop both the extend
5414 // and the truncate.
5415 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005416 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005417
Nadav Rotemcc870a82012-02-05 11:39:23 +00005418 // Fold extract-and-trunc into a narrow extract. For example:
5419 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5420 // i32 y = TRUNCATE(i64 x)
5421 // -- becomes --
5422 // v16i8 b = BITCAST (v2i64 val)
5423 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5424 //
5425 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005426 // creates this pattern) and before operation legalization after which
5427 // we need to be more careful about the vector instructions that we generate.
5428 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5429 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5430
5431 EVT VecTy = N0.getOperand(0).getValueType();
5432 EVT ExTy = N0.getValueType();
5433 EVT TrTy = N->getValueType(0);
5434
5435 unsigned NumElem = VecTy.getVectorNumElements();
5436 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5437
5438 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5439 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5440
5441 SDValue EltNo = N0->getOperand(1);
5442 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5443 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005444 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005445 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5446
Andrew Trickac6d9be2013-05-25 02:42:55 +00005447 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005448 NVT, N0.getOperand(0));
5449
5450 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005451 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005452 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005453 }
5454 }
5455
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005456 // Fold a series of buildvector, bitcast, and truncate if possible.
5457 // For example fold
5458 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5459 // (2xi32 (buildvector x, y)).
5460 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5461 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5462 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5463 N0.getOperand(0).hasOneUse()) {
5464
5465 SDValue BuildVect = N0.getOperand(0);
5466 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5467 EVT TruncVecEltTy = VT.getVectorElementType();
5468
5469 // Check that the element types match.
5470 if (BuildVectEltTy == TruncVecEltTy) {
5471 // Now we only need to compute the offset of the truncated elements.
5472 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5473 unsigned TruncVecNumElts = VT.getVectorNumElements();
5474 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5475
5476 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5477 "Invalid number of elements");
5478
5479 SmallVector<SDValue, 8> Opnds;
5480 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5481 Opnds.push_back(BuildVect.getOperand(i));
5482
Andrew Trickac6d9be2013-05-25 02:42:55 +00005483 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005484 Opnds.size());
5485 }
5486 }
5487
Chris Lattner2b4c2792007-10-13 06:35:54 +00005488 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005489 // only the low bits are being used.
5490 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005491 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005492 // may have different active low bits.
5493 if (!VT.isVector()) {
5494 SDValue Shorter =
5495 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5496 VT.getSizeInBits()));
5497 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005498 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005499 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005500 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005501 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005502 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5503 SDValue Reduced = ReduceLoadWidth(N);
5504 if (Reduced.getNode())
5505 return Reduced;
5506 }
Michael Liao07edaf32012-10-17 23:45:54 +00005507 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5508 // where ... are all 'undef'.
5509 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5510 SmallVector<EVT, 8> VTs;
5511 SDValue V;
5512 unsigned Idx = 0;
5513 unsigned NumDefs = 0;
5514
5515 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5516 SDValue X = N0.getOperand(i);
5517 if (X.getOpcode() != ISD::UNDEF) {
5518 V = X;
5519 Idx = i;
5520 NumDefs++;
5521 }
5522 // Stop if more than one members are non-undef.
5523 if (NumDefs > 1)
5524 break;
5525 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5526 VT.getVectorElementType(),
5527 X.getValueType().getVectorNumElements()));
5528 }
5529
5530 if (NumDefs == 0)
5531 return DAG.getUNDEF(VT);
5532
5533 if (NumDefs == 1) {
5534 assert(V.getNode() && "The single defined operand is empty!");
5535 SmallVector<SDValue, 8> Opnds;
5536 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5537 if (i != Idx) {
5538 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5539 continue;
5540 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005541 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005542 AddToWorkList(NV.getNode());
5543 Opnds.push_back(NV);
5544 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005545 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005546 &Opnds[0], Opnds.size());
5547 }
5548 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005549
5550 // Simplify the operands using demanded-bits information.
5551 if (!VT.isVector() &&
5552 SimplifyDemandedBits(SDValue(N, 0)))
5553 return SDValue(N, 0);
5554
Evan Chenge5b51ac2010-04-17 06:13:15 +00005555 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005556}
5557
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005558static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005560 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005561 return Elt.getNode();
5562 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005563}
5564
5565/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005566/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005567SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005568 assert(N->getOpcode() == ISD::BUILD_PAIR);
5569
Nate Begemanabc01992009-06-05 21:37:30 +00005570 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5571 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005572 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5573 LD1->getPointerInfo().getAddrSpace() !=
5574 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005575 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005576 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005577
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005578 if (ISD::isNON_EXTLoad(LD2) &&
5579 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005580 // If both are volatile this would reduce the number of volatile loads.
5581 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005582 !LD1->isVolatile() &&
5583 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005584 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005585 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005586 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005587 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005588
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005589 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005590 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005591 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005592 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005593 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005594 }
Bill Wendling67a67682009-01-30 22:44:24 +00005595
Dan Gohman475871a2008-07-27 21:46:04 +00005596 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005597}
5598
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005599SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005600 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005601 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005602
Dan Gohman7f321562007-06-25 16:23:39 +00005603 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5604 // Only do this before legalize, since afterward the target may be depending
5605 // on the bitconvert.
5606 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005607 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005608 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005609 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005610 bool isSimple = true;
5611 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5612 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5613 N0.getOperand(i).getOpcode() != ISD::Constant &&
5614 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005615 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005616 break;
5617 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005618
Owen Andersone50ed302009-08-10 22:56:29 +00005619 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005620 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005621 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005622 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005623 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005625
Dan Gohman3dd168d2008-09-05 01:58:21 +00005626 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005627 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005628 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005629 if (Res.getNode() != N) {
5630 if (!LegalOperations ||
5631 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5632 return Res;
5633
5634 // Folding it resulted in an illegal node, and it's too late to
5635 // do that. Clean up the old node and forego the transformation.
5636 // Ideally this won't happen very often, because instcombine
5637 // and the earlier dagcombine runs (where illegal nodes are
5638 // permitted) should have folded most of them already.
5639 DAG.DeleteNode(Res.getNode());
5640 }
Chris Lattner94683772005-12-23 05:30:37 +00005641 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005642
Bill Wendling67a67682009-01-30 22:44:24 +00005643 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005644 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005645 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005646 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005647
Chris Lattner57104102005-12-23 05:44:41 +00005648 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005649 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005650 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005651 // Do not change the width of a volatile load.
5652 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005653 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005654 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005655 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005656 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005657 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005658
Evan Cheng59d5b682007-05-07 21:27:48 +00005659 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005660 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005661 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005662 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005663 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005664 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005665 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005666 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005667 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005668 Load.getValue(1));
5669 return Load;
5670 }
Chris Lattner57104102005-12-23 05:44:41 +00005671 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005672
Bill Wendling67a67682009-01-30 22:44:24 +00005673 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5674 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005675 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005676 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5677 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005678 N0.getNode()->hasOneUse() && VT.isInteger() &&
5679 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005680 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005681 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005682 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005683
Duncan Sands83ec4b62008-06-06 12:08:01 +00005684 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005685 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005686 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005687 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005688 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005689 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005690 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005692
Bill Wendling67a67682009-01-30 22:44:24 +00005693 // fold (bitconvert (fcopysign cst, x)) ->
5694 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5695 // Note that we don't handle (copysign x, cst) because this can always be
5696 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005697 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005698 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005699 VT.isInteger() && !VT.isVector()) {
5700 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005701 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005702 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005703 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005704 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005705 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005706
Duncan Sands25cf2272008-11-24 14:53:14 +00005707 // If X has a different width than the result/lhs, sext it or truncate it.
5708 unsigned VTWidth = VT.getSizeInBits();
5709 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005710 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005711 AddToWorkList(X.getNode());
5712 } else if (OrigXWidth > VTWidth) {
5713 // To get the sign bit in the right place, we have to shift it right
5714 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005715 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005716 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005717 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5718 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005719 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005720 AddToWorkList(X.getNode());
5721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005722
Duncan Sands25cf2272008-11-24 14:53:14 +00005723 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005724 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005725 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005726 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005727
Andrew Trickac6d9be2013-05-25 02:42:55 +00005728 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005729 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005730 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005731 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005732 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005733
Andrew Trickac6d9be2013-05-25 02:42:55 +00005734 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005735 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005736 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005737
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005738 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005739 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005740 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5741 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005742 return CombineLD;
5743 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005744
Dan Gohman475871a2008-07-27 21:46:04 +00005745 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005746}
5747
Dan Gohman475871a2008-07-27 21:46:04 +00005748SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005749 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005750 return CombineConsecutiveLoads(N, VT);
5751}
5752
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005753/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005754/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005755/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005756SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005757ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005758 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005759
Chris Lattner6258fb22006-04-02 02:53:43 +00005760 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005761 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005762
Duncan Sands83ec4b62008-06-06 12:08:01 +00005763 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5764 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005765
Chris Lattner6258fb22006-04-02 02:53:43 +00005766 // If this is a conversion of N elements of one type to N elements of another
5767 // type, convert each element. This handles FP<->INT cases.
5768 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005769 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5770 BV->getValueType(0).getVectorNumElements());
5771
5772 // Due to the FP element handling below calling this routine recursively,
5773 // we can end up with a scalar-to-vector node here.
5774 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005775 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5776 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005777 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005778
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005780 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005781 SDValue Op = BV->getOperand(i);
5782 // If the vector element type is not legal, the BUILD_VECTOR operands
5783 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005784 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005785 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5786 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005787 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005788 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005789 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005790 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005791 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005793
Chris Lattner6258fb22006-04-02 02:53:43 +00005794 // Otherwise, we're growing or shrinking the elements. To avoid having to
5795 // handle annoying details of growing/shrinking FP values, we convert them to
5796 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005797 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005798 // Convert the input float vector to a int vector where the elements are the
5799 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005800 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005801 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005802 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005803 SrcEltVT = IntVT;
5804 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005805
Chris Lattner6258fb22006-04-02 02:53:43 +00005806 // Now we know the input is an integer vector. If the output is a FP type,
5807 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005808 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005810 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005811 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005812
Chris Lattner6258fb22006-04-02 02:53:43 +00005813 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005814 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005816
Chris Lattner6258fb22006-04-02 02:53:43 +00005817 // Okay, we know the src/dst types are both integers of differing types.
5818 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005819 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005820 if (SrcBitSize < DstBitSize) {
5821 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005822
Dan Gohman475871a2008-07-27 21:46:04 +00005823 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005824 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005825 i += NumInputsPerOutput) {
5826 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005827 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005828 bool EltIsUndef = true;
5829 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5830 // Shift the previously computed bits over.
5831 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005832 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005833 if (Op.getOpcode() == ISD::UNDEF) continue;
5834 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005835
Jay Foad40f8f622010-12-07 08:25:19 +00005836 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005837 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005838 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005839
Chris Lattner6258fb22006-04-02 02:53:43 +00005840 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005841 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005842 else
5843 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5844 }
5845
Owen Anderson23b9b192009-08-12 00:36:31 +00005846 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005847 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005848 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005849 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005850
Chris Lattner6258fb22006-04-02 02:53:43 +00005851 // Finally, this must be the case where we are shrinking elements: each input
5852 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005853 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005854 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005855 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5856 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005857 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005858
Dan Gohman7f321562007-06-25 16:23:39 +00005859 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005860 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5861 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005862 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005863 continue;
5864 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005865
Jay Foad40f8f622010-12-07 08:25:19 +00005866 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5867 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005868
Chris Lattner6258fb22006-04-02 02:53:43 +00005869 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005870 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005871 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005872 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005873 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005874 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005875 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005876 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005877 }
5878
5879 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005880 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005881 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5882 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005883
Andrew Trickac6d9be2013-05-25 02:42:55 +00005884 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005885 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005886}
5887
Dan Gohman475871a2008-07-27 21:46:04 +00005888SDValue DAGCombiner::visitFADD(SDNode *N) {
5889 SDValue N0 = N->getOperand(0);
5890 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005891 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5892 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005893 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005894
Dan Gohman7f321562007-06-25 16:23:39 +00005895 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005896 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005897 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005898 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005899 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005900
Lang Hames01806942012-06-14 20:37:15 +00005901 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005902 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005903 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005904 // canonicalize constant to RHS
5905 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005906 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005907 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005908 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5909 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005910 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005911 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005912 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005913 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005914 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005915 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005916 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005917 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005918 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005919 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005920 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005921
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005922 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005923 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5924 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5925 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005926 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
5927 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005928 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005929
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005930 // No FP constant should be created after legalization as Instruction
5931 // Selection pass has hard time in dealing with FP constant.
5932 //
5933 // We don't need test this condition for transformation like following, as
5934 // the DAG being transformed implies it is legal to take FP constant as
5935 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00005936 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005937 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00005938 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005939 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5940
Owen Anderson607ebde2012-11-01 02:00:53 +00005941 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005942 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005943 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00005944 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005945
5946 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005947 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005948 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00005949 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005950
Owen Anderson43da6c72012-08-30 23:35:16 +00005951 // In unsafe math mode, we can fold chains of FADD's of the same value
5952 // into multiplications. This transform is not safe in general because
5953 // we are reducing the number of rounding steps.
5954 if (DAG.getTarget().Options.UnsafeFPMath &&
5955 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5956 !N0CFP && !N1CFP) {
5957 if (N0.getOpcode() == ISD::FMUL) {
5958 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5959 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5960
Stephen Lin38103d12013-06-14 18:17:35 +00005961 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005962 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005963 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005964 SDValue(CFP00, 0),
5965 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005966 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005967 N1, NewCFP);
5968 }
5969
Stephen Lin38103d12013-06-14 18:17:35 +00005970 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005971 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005972 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005973 SDValue(CFP01, 0),
5974 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005975 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005976 N1, NewCFP);
5977 }
5978
Stephen Lin38103d12013-06-14 18:17:35 +00005979 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00005980 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5981 N1.getOperand(0) == N1.getOperand(1) &&
5982 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005983 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005984 SDValue(CFP00, 0),
5985 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005986 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005987 N0.getOperand(1), NewCFP);
5988 }
5989
Stephen Lin38103d12013-06-14 18:17:35 +00005990 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00005991 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5992 N1.getOperand(0) == N1.getOperand(1) &&
5993 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005994 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005995 SDValue(CFP01, 0),
5996 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005997 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005998 N0.getOperand(0), NewCFP);
5999 }
6000 }
6001
6002 if (N1.getOpcode() == ISD::FMUL) {
6003 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6004 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6005
Stephen Lin38103d12013-06-14 18:17:35 +00006006 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006007 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006008 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006009 SDValue(CFP10, 0),
6010 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006011 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006012 N0, NewCFP);
6013 }
6014
Stephen Lin38103d12013-06-14 18:17:35 +00006015 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006016 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006017 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006018 SDValue(CFP11, 0),
6019 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006020 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006021 N0, NewCFP);
6022 }
6023
Owen Anderson43da6c72012-08-30 23:35:16 +00006024
Stephen Lin38103d12013-06-14 18:17:35 +00006025 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6026 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6027 N0.getOperand(0) == N0.getOperand(1) &&
6028 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006029 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006030 SDValue(CFP10, 0),
6031 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006032 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006033 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006034 }
6035
Stephen Lin38103d12013-06-14 18:17:35 +00006036 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6037 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6038 N0.getOperand(0) == N0.getOperand(1) &&
6039 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006040 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006041 SDValue(CFP11, 0),
6042 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006043 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006044 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006045 }
6046 }
6047
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006048 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006049 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006050 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006051 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006052 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006053 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006054 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006055 }
6056
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006057 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006058 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006059 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006060 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006061 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006062 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006063 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006064 }
6065
Stephen Lina553bed2013-06-14 21:33:58 +00006066 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006067 if (AllowNewFpConst &&
6068 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006069 N0.getOperand(0) == N0.getOperand(1) &&
6070 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006071 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006072 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006073 N0.getOperand(0),
6074 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006075 }
6076
Lang Hamesd693caf2012-06-19 22:51:23 +00006077 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006078 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006079 DAG.getTarget().Options.UnsafeFPMath) &&
6080 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006081 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006082
6083 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006084 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006085 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006086 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006087
Michael Liaob79bff52012-09-01 04:09:16 +00006088 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006089 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006090 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006091 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006092 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006093 }
6094
Dan Gohman475871a2008-07-27 21:46:04 +00006095 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006096}
6097
Dan Gohman475871a2008-07-27 21:46:04 +00006098SDValue DAGCombiner::visitFSUB(SDNode *N) {
6099 SDValue N0 = N->getOperand(0);
6100 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006101 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6102 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006103 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006104 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006105
Dan Gohman7f321562007-06-25 16:23:39 +00006106 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006107 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006108 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006109 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006111
Nate Begemana0e221d2005-10-18 00:28:13 +00006112 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006113 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006114 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006115 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006116 if (DAG.getTarget().Options.UnsafeFPMath &&
6117 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006118 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006119 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006120 if (DAG.getTarget().Options.UnsafeFPMath &&
6121 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006122 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006123 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006124 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006125 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006126 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006127 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006128 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006129 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006130 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006131
Bill Wendling5a894342012-03-15 05:12:00 +00006132 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006133 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006134 // (fsub x, (fadd x, y)) -> (fneg y) &
6135 // (fsub x, (fadd y, x)) -> (fneg y)
6136 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006137 if (N0 == N1)
6138 return DAG.getConstantFP(0.0f, VT);
6139
Bill Wendling5a894342012-03-15 05:12:00 +00006140 if (N1.getOpcode() == ISD::FADD) {
6141 SDValue N10 = N1->getOperand(0);
6142 SDValue N11 = N1->getOperand(1);
6143
6144 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6145 &DAG.getTarget().Options))
6146 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Linb4940152013-07-09 00:44:49 +00006147
6148 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6149 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006150 return GetNegatedExpression(N10, DAG, LegalOperations);
6151 }
6152 }
6153
Lang Hamesd693caf2012-06-19 22:51:23 +00006154 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006155 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006156 DAG.getTarget().Options.UnsafeFPMath) &&
6157 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006158 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006159
6160 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006161 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006162 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006163 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006164 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006165
6166 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6167 // Note: Commutes FSUB operands.
Stephen Linb4940152013-07-09 00:44:49 +00006168 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006169 return DAG.getNode(ISD::FMA, dl, VT,
6170 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006171 N1.getOperand(0)),
6172 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006173
Stephen Linb4940152013-07-09 00:44:49 +00006174 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006175 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006176 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6177 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6178 SDValue N00 = N0.getOperand(0).getOperand(0);
6179 SDValue N01 = N0.getOperand(0).getOperand(1);
6180 return DAG.getNode(ISD::FMA, dl, VT,
6181 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6182 DAG.getNode(ISD::FNEG, dl, VT, N1));
6183 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006184 }
6185
Dan Gohman475871a2008-07-27 21:46:04 +00006186 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006187}
6188
Dan Gohman475871a2008-07-27 21:46:04 +00006189SDValue DAGCombiner::visitFMUL(SDNode *N) {
6190 SDValue N0 = N->getOperand(0);
6191 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006192 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6193 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006194 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006195 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006196
Dan Gohman7f321562007-06-25 16:23:39 +00006197 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006198 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006199 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006200 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006202
Nate Begeman11af4ea2005-10-17 20:40:11 +00006203 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006204 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006205 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006206 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006207 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006208 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006209 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006210 if (DAG.getTarget().Options.UnsafeFPMath &&
6211 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006212 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006213 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006214 if (DAG.getTarget().Options.UnsafeFPMath &&
6215 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006216 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006217 // fold (fmul A, 1.0) -> A
6218 if (N1CFP && N1CFP->isExactlyValue(1.0))
6219 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006220 // fold (fmul X, 2.0) -> (fadd X, X)
6221 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006222 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006223 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006224 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006225 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006226 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006227
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006228 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006229 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006230 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006231 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006232 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006233 // Both can be negated for free, check to see if at least one is cheaper
6234 // negated.
6235 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006236 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006237 GetNegatedExpression(N0, DAG, LegalOperations),
6238 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006239 }
6240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006241
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006242 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006243 if (DAG.getTarget().Options.UnsafeFPMath &&
6244 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006245 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006246 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6247 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006248 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006249
Dan Gohman475871a2008-07-27 21:46:04 +00006250 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006251}
6252
Owen Anderson062c0a52012-05-02 22:17:40 +00006253SDValue DAGCombiner::visitFMA(SDNode *N) {
6254 SDValue N0 = N->getOperand(0);
6255 SDValue N1 = N->getOperand(1);
6256 SDValue N2 = N->getOperand(2);
6257 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6258 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6259 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006260 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006261
Owen Anderson607ebde2012-11-01 02:00:53 +00006262 if (DAG.getTarget().Options.UnsafeFPMath) {
6263 if (N0CFP && N0CFP->isZero())
6264 return N2;
6265 if (N1CFP && N1CFP->isZero())
6266 return N2;
6267 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006268 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006269 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006270 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006271 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006272
Owen Anderson85ef6f42012-05-30 18:50:39 +00006273 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006274 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006275 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006276
Owen Anderson58d57292012-09-01 06:04:27 +00006277 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6278 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6279 N2.getOpcode() == ISD::FMUL &&
6280 N0 == N2.getOperand(0) &&
6281 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6282 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6283 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6284 }
6285
6286
6287 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6288 if (DAG.getTarget().Options.UnsafeFPMath &&
6289 N0.getOpcode() == ISD::FMUL && N1CFP &&
6290 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6291 return DAG.getNode(ISD::FMA, dl, VT,
6292 N0.getOperand(0),
6293 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6294 N2);
6295 }
6296
6297 // (fma x, 1, y) -> (fadd x, y)
6298 // (fma x, -1, y) -> (fadd (fneg x), y)
6299 if (N1CFP) {
6300 if (N1CFP->isExactlyValue(1.0))
6301 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6302
6303 if (N1CFP->isExactlyValue(-1.0) &&
6304 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6305 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6306 AddToWorkList(RHSNeg.getNode());
6307 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6308 }
6309 }
6310
6311 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006312 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6313 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006314 DAG.getNode(ISD::FADD, dl, VT,
6315 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006316
6317 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6318 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006319 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6320 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006321 DAG.getNode(ISD::FADD, dl, VT,
6322 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006323
6324
Owen Anderson062c0a52012-05-02 22:17:40 +00006325 return SDValue();
6326}
6327
Dan Gohman475871a2008-07-27 21:46:04 +00006328SDValue DAGCombiner::visitFDIV(SDNode *N) {
6329 SDValue N0 = N->getOperand(0);
6330 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006331 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6332 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006333 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006334 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006335
Dan Gohman7f321562007-06-25 16:23:39 +00006336 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006337 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006338 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006339 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006341
Nate Begemana148d982006-01-18 22:35:16 +00006342 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006343 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006344 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006345
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006346 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006347 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006348 // Compute the reciprocal 1.0 / c2.
6349 APFloat N1APF = N1CFP->getValueAPF();
6350 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6351 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006352 // Only do the transform if the reciprocal is a legal fp immediate that
6353 // isn't too nasty (eg NaN, denormal, ...).
6354 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006355 (!LegalOperations ||
6356 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6357 // backend)... we should handle this gracefully after Legalize.
6358 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6359 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6360 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006361 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006362 DAG.getConstantFP(Recip, VT));
6363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006365 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006366 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006367 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006368 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006369 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006370 // Both can be negated for free, check to see if at least one is cheaper
6371 // negated.
6372 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006373 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006374 GetNegatedExpression(N0, DAG, LegalOperations),
6375 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006376 }
6377 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006378
Dan Gohman475871a2008-07-27 21:46:04 +00006379 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006380}
6381
Dan Gohman475871a2008-07-27 21:46:04 +00006382SDValue DAGCombiner::visitFREM(SDNode *N) {
6383 SDValue N0 = N->getOperand(0);
6384 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006385 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6386 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006387 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006388
Nate Begemana148d982006-01-18 22:35:16 +00006389 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006390 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006391 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006392
Dan Gohman475871a2008-07-27 21:46:04 +00006393 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006394}
6395
Dan Gohman475871a2008-07-27 21:46:04 +00006396SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6397 SDValue N0 = N->getOperand(0);
6398 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006399 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6400 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006401 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006402
Ulrich Weigande669c932012-10-29 18:35:49 +00006403 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006404 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006405
Chris Lattner12d83032006-03-05 05:30:57 +00006406 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006407 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006408 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6409 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006410 if (!V.isNegative()) {
6411 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006412 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006413 } else {
6414 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006415 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6416 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006417 }
Chris Lattner12d83032006-03-05 05:30:57 +00006418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006419
Chris Lattner12d83032006-03-05 05:30:57 +00006420 // copysign(fabs(x), y) -> copysign(x, y)
6421 // copysign(fneg(x), y) -> copysign(x, y)
6422 // copysign(copysign(x,z), y) -> copysign(x, y)
6423 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6424 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006425 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006426 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006427
6428 // copysign(x, abs(y)) -> abs(x)
6429 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006430 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006431
Chris Lattner12d83032006-03-05 05:30:57 +00006432 // copysign(x, copysign(y,z)) -> copysign(x, z)
6433 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006434 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006435 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006436
Chris Lattner12d83032006-03-05 05:30:57 +00006437 // copysign(x, fp_extend(y)) -> copysign(x, y)
6438 // copysign(x, fp_round(y)) -> copysign(x, y)
6439 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006440 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006441 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006442
Dan Gohman475871a2008-07-27 21:46:04 +00006443 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006444}
6445
Dan Gohman475871a2008-07-27 21:46:04 +00006446SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6447 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006448 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006449 EVT VT = N->getValueType(0);
6450 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006451
Nate Begeman1d4d4142005-09-01 00:19:25 +00006452 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006453 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006454 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006455 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006456 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006457 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006458
Chris Lattnercda88752008-06-26 00:16:49 +00006459 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6460 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006461 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6462 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006463 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006464 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006465 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006466 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006467
Nadav Rotemed1a3352012-07-23 07:59:50 +00006468 // The next optimizations are desireable only if SELECT_CC can be lowered.
6469 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6470 // having to say they don't support SELECT_CC on every type the DAG knows
6471 // about, since there is no way to mark an opcode illegal at all value types
6472 // (See also visitSELECT)
6473 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6474 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6475 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6476 !VT.isVector() &&
6477 (!LegalOperations ||
6478 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6479 SDValue Ops[] =
6480 { N0.getOperand(0), N0.getOperand(1),
6481 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6482 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006483 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006484 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006485
Nadav Rotemed1a3352012-07-23 07:59:50 +00006486 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6487 // (select_cc x, y, 1.0, 0.0,, cc)
6488 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6489 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6490 (!LegalOperations ||
6491 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6492 SDValue Ops[] =
6493 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6494 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6495 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006496 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006497 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006498 }
6499
Dan Gohman475871a2008-07-27 21:46:04 +00006500 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006501}
6502
Dan Gohman475871a2008-07-27 21:46:04 +00006503SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6504 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006505 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006506 EVT VT = N->getValueType(0);
6507 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006508
Nate Begeman1d4d4142005-09-01 00:19:25 +00006509 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006510 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006511 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006512 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006513 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006514 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006515
Chris Lattnercda88752008-06-26 00:16:49 +00006516 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6517 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006518 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6519 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006520 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006521 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006522 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006524
Nadav Rotemed1a3352012-07-23 07:59:50 +00006525 // The next optimizations are desireable only if SELECT_CC can be lowered.
6526 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6527 // having to say they don't support SELECT_CC on every type the DAG knows
6528 // about, since there is no way to mark an opcode illegal at all value types
6529 // (See also visitSELECT)
6530 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6531 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006532
Nadav Rotemed1a3352012-07-23 07:59:50 +00006533 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6534 (!LegalOperations ||
6535 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6536 SDValue Ops[] =
6537 { N0.getOperand(0), N0.getOperand(1),
6538 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6539 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006540 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006541 }
6542 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006543
Dan Gohman475871a2008-07-27 21:46:04 +00006544 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006545}
6546
Dan Gohman475871a2008-07-27 21:46:04 +00006547SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6548 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006549 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006550 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006551
Nate Begeman1d4d4142005-09-01 00:19:25 +00006552 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006553 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006554 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006555
Dan Gohman475871a2008-07-27 21:46:04 +00006556 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006557}
6558
Dan Gohman475871a2008-07-27 21:46:04 +00006559SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6560 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006561 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006562 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006563
Nate Begeman1d4d4142005-09-01 00:19:25 +00006564 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006565 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006566 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006567
Dan Gohman475871a2008-07-27 21:46:04 +00006568 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006569}
6570
Dan Gohman475871a2008-07-27 21:46:04 +00006571SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6572 SDValue N0 = N->getOperand(0);
6573 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006574 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006575 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006576
Nate Begeman1d4d4142005-09-01 00:19:25 +00006577 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006578 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006579 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006580
Chris Lattner79dbea52006-03-13 06:26:26 +00006581 // fold (fp_round (fp_extend x)) -> x
6582 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6583 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006584
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006585 // fold (fp_round (fp_round x)) -> (fp_round x)
6586 if (N0.getOpcode() == ISD::FP_ROUND) {
6587 // This is a value preserving truncation if both round's are.
6588 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006589 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006590 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006591 DAG.getIntPtrConstant(IsTrunc));
6592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006593
Chris Lattner79dbea52006-03-13 06:26:26 +00006594 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006595 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006596 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006597 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006598 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006599 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006600 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006602
Dan Gohman475871a2008-07-27 21:46:04 +00006603 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006604}
6605
Dan Gohman475871a2008-07-27 21:46:04 +00006606SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6607 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006608 EVT VT = N->getValueType(0);
6609 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006610 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006611
Nate Begeman1d4d4142005-09-01 00:19:25 +00006612 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006613 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006614 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006615 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006616 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006617
Dan Gohman475871a2008-07-27 21:46:04 +00006618 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006619}
6620
Dan Gohman475871a2008-07-27 21:46:04 +00006621SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6622 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006623 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006624 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006625
Chris Lattner5938bef2007-12-29 06:55:23 +00006626 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006627 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006628 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006629 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006630
Nate Begeman1d4d4142005-09-01 00:19:25 +00006631 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006632 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006633 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006634
6635 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6636 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006637 if (N0.getOpcode() == ISD::FP_ROUND
6638 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006640 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006641 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006642 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006643 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006644 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006645 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006646
Chris Lattner0bd48932008-01-17 07:00:52 +00006647 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006648 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006649 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006650 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006651 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006652 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006653 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006654 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006655 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006656 LN0->isVolatile(), LN0->isNonTemporal(),
6657 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006658 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006659 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006660 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006661 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006662 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006663 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006664 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006665
Dan Gohman475871a2008-07-27 21:46:04 +00006666 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006667}
6668
Dan Gohman475871a2008-07-27 21:46:04 +00006669SDValue DAGCombiner::visitFNEG(SDNode *N) {
6670 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006671 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006672
Craig Topperdd201ff2012-09-11 01:45:21 +00006673 if (VT.isVector()) {
6674 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6675 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006676 }
6677
Owen Andersonafd3d562012-03-06 00:29:31 +00006678 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6679 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006680 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006681
Chris Lattner3bd39d42008-01-27 17:42:27 +00006682 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6683 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006684 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006685 !VT.isVector() &&
6686 N0.getNode()->hasOneUse() &&
6687 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006689 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006690 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006691 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006692 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006693 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006694 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006695 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006696 }
6697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006698
Owen Anderson58d57292012-09-01 06:04:27 +00006699 // (fneg (fmul c, x)) -> (fmul -c, x)
6700 if (N0.getOpcode() == ISD::FMUL) {
6701 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006702 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006703 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006704 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006705 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006706 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006707 }
6708
Dan Gohman475871a2008-07-27 21:46:04 +00006709 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006710}
6711
Owen Anderson7c626d32012-08-13 23:32:49 +00006712SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6713 SDValue N0 = N->getOperand(0);
6714 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6715 EVT VT = N->getValueType(0);
6716
6717 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006718 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006719 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006720
6721 return SDValue();
6722}
6723
6724SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6725 SDValue N0 = N->getOperand(0);
6726 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6727 EVT VT = N->getValueType(0);
6728
6729 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006730 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006731 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006732
6733 return SDValue();
6734}
6735
6736SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6737 SDValue N0 = N->getOperand(0);
6738 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6739 EVT VT = N->getValueType(0);
6740
6741 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006742 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006743 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006744
6745 return SDValue();
6746}
6747
Dan Gohman475871a2008-07-27 21:46:04 +00006748SDValue DAGCombiner::visitFABS(SDNode *N) {
6749 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006750 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006751 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006752
Craig Topperdd201ff2012-09-11 01:45:21 +00006753 if (VT.isVector()) {
6754 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6755 if (FoldedVOp.getNode()) return FoldedVOp;
6756 }
6757
Nate Begeman1d4d4142005-09-01 00:19:25 +00006758 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006759 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006760 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006761 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006762 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006763 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006764 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006765 // fold (fabs (fcopysign x, y)) -> (fabs x)
6766 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006767 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006768
Chris Lattner3bd39d42008-01-27 17:42:27 +00006769 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6770 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006771 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006772 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006773 N0.getOperand(0).getValueType().isInteger() &&
6774 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006775 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006776 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006777 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006778 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006779 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006780 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006781 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006782 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006783 }
6784 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006785
Dan Gohman475871a2008-07-27 21:46:04 +00006786 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006787}
6788
Dan Gohman475871a2008-07-27 21:46:04 +00006789SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6790 SDValue Chain = N->getOperand(0);
6791 SDValue N1 = N->getOperand(1);
6792 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006793
Dan Gohmane0f06c72009-11-17 00:47:23 +00006794 // If N is a constant we could fold this into a fallthrough or unconditional
6795 // branch. However that doesn't happen very often in normal code, because
6796 // Instcombine/SimplifyCFG should have handled the available opportunities.
6797 // If we did this folding here, it would be necessary to update the
6798 // MachineBasicBlock CFG, which is awkward.
6799
Nate Begeman750ac1b2006-02-01 07:19:44 +00006800 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6801 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006802 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006803 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6804 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006805 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006806 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006807 N1.getOperand(0), N1.getOperand(1), N2);
6808 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006809
Evan Cheng2a135ae2010-10-04 22:41:01 +00006810 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6811 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6812 (N1.getOperand(0).hasOneUse() &&
6813 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6814 SDNode *Trunc = 0;
6815 if (N1.getOpcode() == ISD::TRUNCATE) {
6816 // Look pass the truncate.
6817 Trunc = N1.getNode();
6818 N1 = N1.getOperand(0);
6819 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006820
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006821 // Match this pattern so that we can generate simpler code:
6822 //
6823 // %a = ...
6824 // %b = and i32 %a, 2
6825 // %c = srl i32 %b, 1
6826 // brcond i32 %c ...
6827 //
6828 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006829 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006830 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006831 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006832 // %c = setcc eq %b, 0
6833 // brcond %c ...
6834 //
6835 // This applies only when the AND constant value has one bit set and the
6836 // SRL constant is equal to the log2 of the AND constant. The back-end is
6837 // smart enough to convert the result into a TEST/JMP sequence.
6838 SDValue Op0 = N1.getOperand(0);
6839 SDValue Op1 = N1.getOperand(1);
6840
6841 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006842 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006843 SDValue AndOp1 = Op0.getOperand(1);
6844
6845 if (AndOp1.getOpcode() == ISD::Constant) {
6846 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6847
6848 if (AndConst.isPowerOf2() &&
6849 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6850 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006851 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006852 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006853 Op0, DAG.getConstant(0, Op0.getValueType()),
6854 ISD::SETNE);
6855
Andrew Trickac6d9be2013-05-25 02:42:55 +00006856 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006857 MVT::Other, Chain, SetCC, N2);
6858 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6859 // will convert it back to (X & C1) >> C2.
6860 CombineTo(N, NewBRCond, false);
6861 // Truncate is dead.
6862 if (Trunc) {
6863 removeFromWorkList(Trunc);
6864 DAG.DeleteNode(Trunc);
6865 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006866 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006867 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006868 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006869 removeFromWorkList(N1.getNode());
6870 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006871 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006872 }
6873 }
6874 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006875
6876 if (Trunc)
6877 // Restore N1 if the above transformation doesn't match.
6878 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006879 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006880
Evan Cheng2c755ba2010-02-27 07:36:59 +00006881 // Transform br(xor(x, y)) -> br(x != y)
6882 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6883 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6884 SDNode *TheXor = N1.getNode();
6885 SDValue Op0 = TheXor->getOperand(0);
6886 SDValue Op1 = TheXor->getOperand(1);
6887 if (Op0.getOpcode() == Op1.getOpcode()) {
6888 // Avoid missing important xor optimizations.
6889 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006890 if (Tmp.getNode()) {
6891 if (Tmp.getNode() != TheXor) {
6892 DEBUG(dbgs() << "\nReplacing.8 ";
6893 TheXor->dump(&DAG);
6894 dbgs() << "\nWith: ";
6895 Tmp.getNode()->dump(&DAG);
6896 dbgs() << '\n');
6897 WorkListRemover DeadNodes(*this);
6898 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6899 removeFromWorkList(TheXor);
6900 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006901 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006902 MVT::Other, Chain, Tmp, N2);
6903 }
6904
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006905 // visitXOR has changed XOR's operands or replaced the XOR completely,
6906 // bail out.
6907 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006908 }
6909 }
6910
6911 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6912 bool Equal = false;
6913 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6914 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6915 Op0.getOpcode() == ISD::XOR) {
6916 TheXor = Op0.getNode();
6917 Equal = true;
6918 }
6919
Evan Cheng2a135ae2010-10-04 22:41:01 +00006920 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006921 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00006922 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006923 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006924 SetCCVT,
6925 Op0, Op1,
6926 Equal ? ISD::SETEQ : ISD::SETNE);
6927 // Replace the uses of XOR with SETCC
6928 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006929 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006930 removeFromWorkList(N1.getNode());
6931 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006932 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006933 MVT::Other, Chain, SetCC, N2);
6934 }
6935 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006936
Dan Gohman475871a2008-07-27 21:46:04 +00006937 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006938}
6939
Chris Lattner3ea0b472005-10-05 06:47:48 +00006940// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6941//
Dan Gohman475871a2008-07-27 21:46:04 +00006942SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006943 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006944 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006945
Dan Gohmane0f06c72009-11-17 00:47:23 +00006946 // If N is a constant we could fold this into a fallthrough or unconditional
6947 // branch. However that doesn't happen very often in normal code, because
6948 // Instcombine/SimplifyCFG should have handled the available opportunities.
6949 // If we did this folding here, it would be necessary to update the
6950 // MachineBasicBlock CFG, which is awkward.
6951
Duncan Sands8eab8a22008-06-09 11:32:28 +00006952 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00006953 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006954 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006955 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006956 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006957
Nate Begemane17daeb2005-10-05 21:43:42 +00006958 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006959 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006960 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006961 N->getOperand(0), Simp.getOperand(2),
6962 Simp.getOperand(0), Simp.getOperand(1),
6963 N->getOperand(4));
6964
Dan Gohman475871a2008-07-27 21:46:04 +00006965 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006966}
6967
Evan Chengc4b527a2012-01-13 01:37:24 +00006968/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6969/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006970/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006971static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6972 SelectionDAG &DAG,
6973 const TargetLowering &TLI) {
6974 EVT VT;
6975 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6976 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6977 return false;
6978 VT = Use->getValueType(0);
6979 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6980 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6981 return false;
6982 VT = ST->getValue().getValueType();
6983 } else
6984 return false;
6985
Chandler Carruth56d433d2013-01-07 15:14:13 +00006986 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00006987 if (N->getOpcode() == ISD::ADD) {
6988 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6989 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006990 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006991 AM.BaseOffs = Offset->getSExtValue();
6992 else
Evan Cheng03be3622012-03-06 23:33:32 +00006993 // [reg +/- reg]
6994 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006995 } else if (N->getOpcode() == ISD::SUB) {
6996 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6997 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006998 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006999 AM.BaseOffs = -Offset->getSExtValue();
7000 else
Evan Cheng03be3622012-03-06 23:33:32 +00007001 // [reg +/- reg]
7002 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007003 } else
7004 return false;
7005
7006 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7007}
7008
Duncan Sandsec87aa82008-06-15 20:12:31 +00007009/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7010/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007011/// and it has other uses besides the load / store. After the
7012/// transformation, the new indexed load / store has effectively folded
7013/// the add / subtract in and all of its other uses are redirected to the
7014/// new load / store.
7015bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007016 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007017 return false;
7018
7019 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007020 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007021 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007022 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007023 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007024 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007025 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007026 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007027 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7028 return false;
7029 Ptr = LD->getBasePtr();
7030 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007031 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007032 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007033 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007034 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7035 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7036 return false;
7037 Ptr = ST->getBasePtr();
7038 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007039 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007040 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007041 }
Chris Lattner448f2192006-11-11 00:39:41 +00007042
Chris Lattner9f1794e2006-11-11 00:56:29 +00007043 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7044 // out. There is no reason to make this a preinc/predec.
7045 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007046 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007047 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007048
Chris Lattner9f1794e2006-11-11 00:56:29 +00007049 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007050 SDValue BasePtr;
7051 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007052 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7053 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7054 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007055
7056 // Backends without true r+i pre-indexed forms may need to pass a
7057 // constant base with a variable offset so that constant coercion
7058 // will work with the patterns in canonical form.
7059 bool Swapped = false;
7060 if (isa<ConstantSDNode>(BasePtr)) {
7061 std::swap(BasePtr, Offset);
7062 Swapped = true;
7063 }
7064
Evan Chenga7d4a042007-05-03 23:52:19 +00007065 // Don't create a indexed load / store with zero offset.
7066 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007067 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007068 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007069
Chris Lattner41e53fd2006-11-11 01:00:15 +00007070 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007071 // 1) The new base ptr is a frame index.
7072 // 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 +00007073 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007074 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007075 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007076 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007077
Chris Lattner41e53fd2006-11-11 01:00:15 +00007078 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7079 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007080 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007081 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007082
Chris Lattner41e53fd2006-11-11 01:00:15 +00007083 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007084 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007085 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007086 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007087 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007088 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007089
Hal Finkel089a5f82013-02-08 21:35:47 +00007090 // If the offset is a constant, there may be other adds of constants that
7091 // can be folded with this one. We should do this to avoid having to keep
7092 // a copy of the original base pointer.
7093 SmallVector<SDNode *, 16> OtherUses;
7094 if (isa<ConstantSDNode>(Offset))
7095 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7096 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7097 SDNode *Use = *I;
7098 if (Use == Ptr.getNode())
7099 continue;
7100
7101 if (Use->isPredecessorOf(N))
7102 continue;
7103
7104 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7105 OtherUses.clear();
7106 break;
7107 }
7108
7109 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7110 if (Op1.getNode() == BasePtr.getNode())
7111 std::swap(Op0, Op1);
7112 assert(Op0.getNode() == BasePtr.getNode() &&
7113 "Use of ADD/SUB but not an operand");
7114
7115 if (!isa<ConstantSDNode>(Op1)) {
7116 OtherUses.clear();
7117 break;
7118 }
7119
7120 // FIXME: In some cases, we can be smarter about this.
7121 if (Op1.getValueType() != Offset.getValueType()) {
7122 OtherUses.clear();
7123 break;
7124 }
7125
7126 OtherUses.push_back(Use);
7127 }
7128
7129 if (Swapped)
7130 std::swap(BasePtr, Offset);
7131
Evan Chengc843abe2007-05-24 02:35:39 +00007132 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007133 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007134
7135 // Caches for hasPredecessorHelper
7136 SmallPtrSet<const SDNode *, 32> Visited;
7137 SmallVector<const SDNode *, 16> Worklist;
7138
Gabor Greifba36cb52008-08-28 21:40:38 +00007139 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7140 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007141 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007142 if (Use == N)
7143 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007144 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007145 return false;
7146
Evan Chengc4b527a2012-01-13 01:37:24 +00007147 // If Ptr may be folded in addressing mode of other use, then it's
7148 // not profitable to do this transformation.
7149 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007150 RealUse = true;
7151 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007152
Chris Lattner9f1794e2006-11-11 00:56:29 +00007153 if (!RealUse)
7154 return false;
7155
Dan Gohman475871a2008-07-27 21:46:04 +00007156 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007157 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007158 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007159 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007160 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007161 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007162 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007163 ++PreIndexedNodes;
7164 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007165 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007166 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007167 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007168 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007169 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007170 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007171 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007172 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7173 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007174 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007175 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007176 }
7177
Chris Lattner9f1794e2006-11-11 00:56:29 +00007178 // Finally, since the node is now dead, remove it from the graph.
7179 DAG.DeleteNode(N);
7180
Hal Finkel089a5f82013-02-08 21:35:47 +00007181 if (Swapped)
7182 std::swap(BasePtr, Offset);
7183
7184 // Replace other uses of BasePtr that can be updated to use Ptr
7185 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7186 unsigned OffsetIdx = 1;
7187 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7188 OffsetIdx = 0;
7189 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7190 BasePtr.getNode() && "Expected BasePtr operand");
7191
Silviu Baranga730a5702013-04-26 15:52:24 +00007192 // We need to replace ptr0 in the following expression:
7193 // x0 * offset0 + y0 * ptr0 = t0
7194 // knowing that
7195 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007196 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007197 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7198 // indexed load/store and the expresion that needs to be re-written.
7199 //
7200 // Therefore, we have:
7201 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007202
7203 ConstantSDNode *CN =
7204 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007205 int X0, X1, Y0, Y1;
7206 APInt Offset0 = CN->getAPIntValue();
7207 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007208
Silviu Baranga730a5702013-04-26 15:52:24 +00007209 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7210 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7211 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7212 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007213
Silviu Baranga730a5702013-04-26 15:52:24 +00007214 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7215
7216 APInt CNV = Offset0;
7217 if (X0 < 0) CNV = -CNV;
7218 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7219 else CNV = CNV - Offset1;
7220
7221 // We can now generate the new expression.
7222 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7223 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7224
7225 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007226 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007227 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7228 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7229 removeFromWorkList(OtherUses[i]);
7230 DAG.DeleteNode(OtherUses[i]);
7231 }
7232
Chris Lattner9f1794e2006-11-11 00:56:29 +00007233 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007234 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007235 removeFromWorkList(Ptr.getNode());
7236 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007237
7238 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007239}
7240
Duncan Sandsec87aa82008-06-15 20:12:31 +00007241/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007242/// add / sub of the base pointer node into a post-indexed load / store.
7243/// The transformation folded the add / subtract into the new indexed
7244/// load / store effectively and all of its uses are redirected to the
7245/// new load / store.
7246bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007247 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007248 return false;
7249
7250 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007251 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007252 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007253 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007254 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007255 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007256 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007257 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7258 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7259 return false;
7260 Ptr = LD->getBasePtr();
7261 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007262 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007263 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007264 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007265 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7266 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7267 return false;
7268 Ptr = ST->getBasePtr();
7269 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007270 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007271 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007272 }
Chris Lattner448f2192006-11-11 00:39:41 +00007273
Gabor Greifba36cb52008-08-28 21:40:38 +00007274 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007275 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007276
Gabor Greifba36cb52008-08-28 21:40:38 +00007277 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7278 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007279 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007280 if (Op == N ||
7281 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7282 continue;
7283
Dan Gohman475871a2008-07-27 21:46:04 +00007284 SDValue BasePtr;
7285 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007286 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7287 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007288 // Don't create a indexed load / store with zero offset.
7289 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007290 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007291 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007292
Chris Lattner9f1794e2006-11-11 00:56:29 +00007293 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007294 // 1) All uses are load / store ops that use it as base ptr (and
7295 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007296 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7297 // nor a successor of N. Otherwise, if Op is folded that would
7298 // create a cycle.
7299
Evan Chengcaab1292009-05-06 18:25:01 +00007300 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7301 continue;
7302
Chris Lattner9f1794e2006-11-11 00:56:29 +00007303 // Check for #1.
7304 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007305 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7306 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007307 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007308 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007309 continue;
7310
Chris Lattner9f1794e2006-11-11 00:56:29 +00007311 // If all the uses are load / store addresses, then don't do the
7312 // transformation.
7313 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7314 bool RealUse = false;
7315 for (SDNode::use_iterator III = Use->use_begin(),
7316 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007317 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007318 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007319 RealUse = true;
7320 }
Chris Lattner448f2192006-11-11 00:39:41 +00007321
Chris Lattner9f1794e2006-11-11 00:56:29 +00007322 if (!RealUse) {
7323 TryNext = true;
7324 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007325 }
7326 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007327 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007328
Chris Lattner9f1794e2006-11-11 00:56:29 +00007329 if (TryNext)
7330 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007331
Chris Lattner9f1794e2006-11-11 00:56:29 +00007332 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007333 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007334 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007335 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007336 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007337 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007338 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007339 ++PostIndexedNodes;
7340 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007341 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007342 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007343 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007344 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007345 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007346 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007347 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007348 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7349 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007350 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007351 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007352 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007353
Chris Lattner9f1794e2006-11-11 00:56:29 +00007354 // Finally, since the node is now dead, remove it from the graph.
7355 DAG.DeleteNode(N);
7356
7357 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007358 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007359 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007360 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007361 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007362 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007363 }
7364 }
7365 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007366
Chris Lattner448f2192006-11-11 00:39:41 +00007367 return false;
7368}
7369
Dan Gohman475871a2008-07-27 21:46:04 +00007370SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007371 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007372 SDValue Chain = LD->getChain();
7373 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007374
Evan Cheng45a7ca92007-05-01 00:38:21 +00007375 // If load is not volatile and there are no uses of the loaded value (and
7376 // the updated indexed value in case of indexed loads), change uses of the
7377 // chain value into uses of the chain input (i.e. delete the dead load).
7378 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007379 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007380 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007381 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007382 // It's not safe to use the two value CombineTo variant here. e.g.
7383 // v1, chain2 = load chain1, loc
7384 // v2, chain3 = load chain2, loc
7385 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007386 // Now we replace use of chain2 with chain1. This makes the second load
7387 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007388 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007389 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007390 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007391 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007392 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007393 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007394 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007395
Chris Lattner125991a2008-01-24 07:57:06 +00007396 if (N->use_empty()) {
7397 removeFromWorkList(N);
7398 DAG.DeleteNode(N);
7399 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007400
Dan Gohman475871a2008-07-27 21:46:04 +00007401 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007402 }
Evan Cheng498f5592007-05-01 08:53:39 +00007403 } else {
7404 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007405 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007406 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007407 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007408 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007409 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007410 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007411 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007412 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007413 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007414 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007415 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007416 DAG.getUNDEF(N->getValueType(1)));
7417 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007418 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007419 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007420 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007421 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007422 }
7423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007424
Chris Lattner01a22022005-10-10 22:04:48 +00007425 // If this load is directly stored, replace the load value with the stored
7426 // value.
7427 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007428 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007429 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007430 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007431 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7432 if (PrevST->getBasePtr() == Ptr &&
7433 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007434 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007435 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007437
Evan Cheng255f20f2010-04-01 06:04:33 +00007438 // Try to infer better alignment information than the load already has.
7439 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007440 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007441 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7442 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007443 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007444 LD->getValueType(0),
7445 Chain, Ptr, LD->getPointerInfo(),
7446 LD->getMemoryVT(),
7447 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007448 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7449 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007450 }
7451 }
7452
Jim Laskey7ca56af2006-10-11 13:47:09 +00007453 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007454 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007455 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007456
Jim Laskey6ff23e52006-10-04 16:53:27 +00007457 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007458 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007459 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007460
Jim Laskey279f0532006-09-25 16:29:54 +00007461 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007462 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007463 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007464 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007465 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007466 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007467 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007468 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007469 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007470 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007471 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007472 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007473 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007474 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007475 }
Jim Laskey279f0532006-09-25 16:29:54 +00007476
Jim Laskey6ff23e52006-10-04 16:53:27 +00007477 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007478 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007480
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007481 // Make sure the new and old chains are cleaned up.
7482 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007483
Jim Laskey274062c2006-10-13 23:32:28 +00007484 // Replace uses with load result and token factor. Don't add users
7485 // to work list.
7486 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007487 }
7488 }
7489
Evan Cheng7fc033a2006-11-03 03:06:21 +00007490 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007491 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007492 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007493
Dan Gohman475871a2008-07-27 21:46:04 +00007494 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007495}
7496
Chris Lattner2392ae72010-04-15 04:48:01 +00007497/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7498/// load is having specific bytes cleared out. If so, return the byte size
7499/// being masked out and the shift amount.
7500static std::pair<unsigned, unsigned>
7501CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7502 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007503
Chris Lattner2392ae72010-04-15 04:48:01 +00007504 // Check for the structure we're looking for.
7505 if (V->getOpcode() != ISD::AND ||
7506 !isa<ConstantSDNode>(V->getOperand(1)) ||
7507 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7508 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007509
Chris Lattnere6987582010-04-15 06:10:49 +00007510 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007511 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007512 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007513
Chris Lattnere6987582010-04-15 06:10:49 +00007514 // The store should be chained directly to the load or be an operand of a
7515 // tokenfactor.
7516 if (LD == Chain.getNode())
7517 ; // ok.
7518 else if (Chain->getOpcode() != ISD::TokenFactor)
7519 return Result; // Fail.
7520 else {
7521 bool isOk = false;
7522 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7523 if (Chain->getOperand(i).getNode() == LD) {
7524 isOk = true;
7525 break;
7526 }
7527 if (!isOk) return Result;
7528 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007529
Chris Lattner2392ae72010-04-15 04:48:01 +00007530 // This only handles simple types.
7531 if (V.getValueType() != MVT::i16 &&
7532 V.getValueType() != MVT::i32 &&
7533 V.getValueType() != MVT::i64)
7534 return Result;
7535
7536 // Check the constant mask. Invert it so that the bits being masked out are
7537 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7538 // follow the sign bit for uniformity.
7539 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007540 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007541 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007542 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007543 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7544 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007545
Chris Lattner2392ae72010-04-15 04:48:01 +00007546 // See if we have a continuous run of bits. If so, we have 0*1+0*
7547 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7548 return Result;
7549
7550 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7551 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7552 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007553
Chris Lattner2392ae72010-04-15 04:48:01 +00007554 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7555 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007556 case 1:
7557 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007558 case 4: break;
7559 default: return Result; // All one mask, or 5-byte mask.
7560 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007561
Chris Lattner2392ae72010-04-15 04:48:01 +00007562 // Verify that the first bit starts at a multiple of mask so that the access
7563 // is aligned the same as the access width.
7564 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007565
Chris Lattner2392ae72010-04-15 04:48:01 +00007566 Result.first = MaskedBytes;
7567 Result.second = NotMaskTZ/8;
7568 return Result;
7569}
7570
7571
7572/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7573/// provides a value as specified by MaskInfo. If so, replace the specified
7574/// store with a narrower store of truncated IVal.
7575static SDNode *
7576ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7577 SDValue IVal, StoreSDNode *St,
7578 DAGCombiner *DC) {
7579 unsigned NumBytes = MaskInfo.first;
7580 unsigned ByteShift = MaskInfo.second;
7581 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007582
Chris Lattner2392ae72010-04-15 04:48:01 +00007583 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7584 // that uses this. If not, this is not a replacement.
7585 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7586 ByteShift*8, (ByteShift+NumBytes)*8);
7587 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007588
Chris Lattner2392ae72010-04-15 04:48:01 +00007589 // Check that it is legal on the target to do this. It is legal if the new
7590 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7591 // legalization.
7592 MVT VT = MVT::getIntegerVT(NumBytes*8);
7593 if (!DC->isTypeLegal(VT))
7594 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007595
Chris Lattner2392ae72010-04-15 04:48:01 +00007596 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7597 // shifted by ByteShift and truncated down to NumBytes.
7598 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007599 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007600 DAG.getConstant(ByteShift*8,
7601 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007602
7603 // Figure out the offset for the store and the alignment of the access.
7604 unsigned StOffset;
7605 unsigned NewAlign = St->getAlignment();
7606
7607 if (DAG.getTargetLoweringInfo().isLittleEndian())
7608 StOffset = ByteShift;
7609 else
7610 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007611
Chris Lattner2392ae72010-04-15 04:48:01 +00007612 SDValue Ptr = St->getBasePtr();
7613 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007614 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007615 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7616 NewAlign = MinAlign(NewAlign, StOffset);
7617 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007618
Chris Lattner2392ae72010-04-15 04:48:01 +00007619 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007620 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007621
Chris Lattner2392ae72010-04-15 04:48:01 +00007622 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007623 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007624 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007625 false, false, NewAlign).getNode();
7626}
7627
Evan Cheng8b944d32009-05-28 00:35:15 +00007628
7629/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7630/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7631/// of the loaded bits, try narrowing the load and store if it would end up
7632/// being a win for performance or code size.
7633SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7634 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007635 if (ST->isVolatile())
7636 return SDValue();
7637
Evan Cheng8b944d32009-05-28 00:35:15 +00007638 SDValue Chain = ST->getChain();
7639 SDValue Value = ST->getValue();
7640 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007641 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007642
7643 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007644 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007645
7646 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007647
Chris Lattner2392ae72010-04-15 04:48:01 +00007648 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7649 // is a byte mask indicating a consecutive number of bytes, check to see if
7650 // Y is known to provide just those bytes. If so, we try to replace the
7651 // load + replace + store sequence with a single (narrower) store, which makes
7652 // the load dead.
7653 if (Opc == ISD::OR) {
7654 std::pair<unsigned, unsigned> MaskedLoad;
7655 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7656 if (MaskedLoad.first)
7657 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7658 Value.getOperand(1), ST,this))
7659 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007660
Chris Lattner2392ae72010-04-15 04:48:01 +00007661 // Or is commutative, so try swapping X and Y.
7662 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7663 if (MaskedLoad.first)
7664 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7665 Value.getOperand(0), ST,this))
7666 return SDValue(NewST, 0);
7667 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007668
Evan Cheng8b944d32009-05-28 00:35:15 +00007669 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7670 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007671 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007672
7673 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007674 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7675 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007676 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007677 if (LD->getBasePtr() != Ptr ||
7678 LD->getPointerInfo().getAddrSpace() !=
7679 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007680 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007681
7682 // Find the type to narrow it the load / op / store to.
7683 SDValue N1 = Value.getOperand(1);
7684 unsigned BitWidth = N1.getValueSizeInBits();
7685 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7686 if (Opc == ISD::AND)
7687 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007688 if (Imm == 0 || Imm.isAllOnesValue())
7689 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007690 unsigned ShAmt = Imm.countTrailingZeros();
7691 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7692 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007693 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007694 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007695 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007696 TLI.isNarrowingProfitable(VT, NewVT))) {
7697 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007698 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007699 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007700 if (NewBW >= BitWidth)
7701 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007702
7703 // If the lsb changed does not start at the type bitwidth boundary,
7704 // start at the previous one.
7705 if (ShAmt % NewBW)
7706 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007707 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7708 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007709 if ((Imm & Mask) == Imm) {
7710 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7711 if (Opc == ISD::AND)
7712 NewImm ^= APInt::getAllOnesValue(NewBW);
7713 uint64_t PtrOff = ShAmt / 8;
7714 // For big endian targets, we need to adjust the offset to the pointer to
7715 // load the correct bytes.
7716 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007717 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007718
7719 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007720 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007721 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007722 return SDValue();
7723
Andrew Trickac6d9be2013-05-25 02:42:55 +00007724 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007725 Ptr.getValueType(), Ptr,
7726 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007727 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007728 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007729 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007730 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007731 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007732 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007733 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007734 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007735 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007736 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007737 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007738
7739 AddToWorkList(NewPtr.getNode());
7740 AddToWorkList(NewLD.getNode());
7741 AddToWorkList(NewVal.getNode());
7742 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007743 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007744 ++OpsNarrowed;
7745 return NewST;
7746 }
7747 }
7748
Evan Chengcdcecc02009-05-28 18:41:02 +00007749 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007750}
7751
Evan Cheng31959b12011-02-02 01:06:55 +00007752/// TransformFPLoadStorePair - For a given floating point load / store pair,
7753/// if the load value isn't used by any other operations, then consider
7754/// transforming the pair to integer load / store operations if the target
7755/// deems the transformation profitable.
7756SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7757 StoreSDNode *ST = cast<StoreSDNode>(N);
7758 SDValue Chain = ST->getChain();
7759 SDValue Value = ST->getValue();
7760 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7761 Value.hasOneUse() &&
7762 Chain == SDValue(Value.getNode(), 1)) {
7763 LoadSDNode *LD = cast<LoadSDNode>(Value);
7764 EVT VT = LD->getMemoryVT();
7765 if (!VT.isFloatingPoint() ||
7766 VT != ST->getMemoryVT() ||
7767 LD->isNonTemporal() ||
7768 ST->isNonTemporal() ||
7769 LD->getPointerInfo().getAddrSpace() != 0 ||
7770 ST->getPointerInfo().getAddrSpace() != 0)
7771 return SDValue();
7772
7773 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7774 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7775 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7776 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7777 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7778 return SDValue();
7779
7780 unsigned LDAlign = LD->getAlignment();
7781 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007782 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007783 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007784 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7785 return SDValue();
7786
Andrew Trickac6d9be2013-05-25 02:42:55 +00007787 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007788 LD->getChain(), LD->getBasePtr(),
7789 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007790 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007791
Andrew Trickac6d9be2013-05-25 02:42:55 +00007792 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007793 NewLD, ST->getBasePtr(),
7794 ST->getPointerInfo(),
7795 false, false, STAlign);
7796
7797 AddToWorkList(NewLD.getNode());
7798 AddToWorkList(NewST.getNode());
7799 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007800 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007801 ++LdStFP2Int;
7802 return NewST;
7803 }
7804
7805 return SDValue();
7806}
7807
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007808/// Helper struct to parse and store a memory address as base + index + offset.
7809/// We ignore sign extensions when it is safe to do so.
7810/// The following two expressions are not equivalent. To differentiate we need
7811/// to store whether there was a sign extension involved in the index
7812/// computation.
7813/// (load (i64 add (i64 copyfromreg %c)
7814/// (i64 signextend (add (i8 load %index)
7815/// (i8 1))))
7816/// vs
7817///
7818/// (load (i64 add (i64 copyfromreg %c)
7819/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7820/// (i32 1)))))
7821struct BaseIndexOffset {
7822 SDValue Base;
7823 SDValue Index;
7824 int64_t Offset;
7825 bool IsIndexSignExt;
7826
7827 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7828
7829 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7830 bool IsIndexSignExt) :
7831 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7832
7833 bool equalBaseIndex(const BaseIndexOffset &Other) {
7834 return Other.Base == Base && Other.Index == Index &&
7835 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007836 }
7837
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007838 /// Parses tree in Ptr for base, index, offset addresses.
7839 static BaseIndexOffset match(SDValue Ptr) {
7840 bool IsIndexSignExt = false;
7841
7842 // Just Base or possibly anything else.
7843 if (Ptr->getOpcode() != ISD::ADD)
7844 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7845
7846 // Base + offset.
7847 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7848 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7849 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7850 IsIndexSignExt);
7851 }
7852
7853 // Look at Base + Index + Offset cases.
7854 SDValue Base = Ptr->getOperand(0);
7855 SDValue IndexOffset = Ptr->getOperand(1);
7856
7857 // Skip signextends.
7858 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7859 IndexOffset = IndexOffset->getOperand(0);
7860 IsIndexSignExt = true;
7861 }
7862
7863 // Either the case of Base + Index (no offset) or something else.
7864 if (IndexOffset->getOpcode() != ISD::ADD)
7865 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7866
7867 // Now we have the case of Base + Index + offset.
7868 SDValue Index = IndexOffset->getOperand(0);
7869 SDValue Offset = IndexOffset->getOperand(1);
7870
7871 if (!isa<ConstantSDNode>(Offset))
7872 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7873
7874 // Ignore signextends.
7875 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7876 Index = Index->getOperand(0);
7877 IsIndexSignExt = true;
7878 } else IsIndexSignExt = false;
7879
7880 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7881 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7882 }
7883};
Nadav Rotemc653de62012-10-03 16:11:15 +00007884
7885/// Holds a pointer to an LSBaseSDNode as well as information on where it
7886/// is located in a sequence of memory operations connected by a chain.
7887struct MemOpLink {
7888 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7889 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7890 // Ptr to the mem node.
7891 LSBaseSDNode *MemNode;
7892 // Offset from the base ptr.
7893 int64_t OffsetFromBase;
7894 // What is the sequence number of this mem node.
7895 // Lowest mem operand in the DAG starts at zero.
7896 unsigned SequenceNum;
7897};
7898
7899/// Sorts store nodes in a link according to their offset from a shared
7900// base ptr.
7901struct ConsecutiveMemoryChainSorter {
7902 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7903 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7904 }
7905};
7906
7907bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7908 EVT MemVT = St->getMemoryVT();
7909 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007910 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7911 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007912
7913 // Don't merge vectors into wider inputs.
7914 if (MemVT.isVector() || !MemVT.isSimple())
7915 return false;
7916
7917 // Perform an early exit check. Do not bother looking at stored values that
7918 // are not constants or loads.
7919 SDValue StoredVal = St->getValue();
7920 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7921 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7922 !IsLoadSrc)
7923 return false;
7924
7925 // Only look at ends of store sequences.
7926 SDValue Chain = SDValue(St, 1);
7927 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7928 return false;
7929
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007930 // This holds the base pointer, index, and the offset in bytes from the base
7931 // pointer.
7932 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007933
7934 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007935 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007936 return false;
7937
7938 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007939 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007940 return false;
7941
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007942 // Save the LoadSDNodes that we find in the chain.
7943 // We need to make sure that these nodes do not interfere with
7944 // any of the store nodes.
7945 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7946
7947 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00007948 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007949
Nadav Rotemc653de62012-10-03 16:11:15 +00007950 // Walk up the chain and look for nodes with offsets from the same
7951 // base pointer. Stop when reaching an instruction with a different kind
7952 // or instruction which has a different base pointer.
7953 unsigned Seq = 0;
7954 StoreSDNode *Index = St;
7955 while (Index) {
7956 // If the chain has more than one use, then we can't reorder the mem ops.
7957 if (Index != St && !SDValue(Index, 1)->hasOneUse())
7958 break;
7959
7960 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007961 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007962
7963 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007964 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00007965 break;
7966
7967 // Check that the alignment is the same.
7968 if (Index->getAlignment() != St->getAlignment())
7969 break;
7970
7971 // The memory operands must not be volatile.
7972 if (Index->isVolatile() || Index->isIndexed())
7973 break;
7974
7975 // No truncation.
7976 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
7977 if (St->isTruncatingStore())
7978 break;
7979
7980 // The stored memory type must be the same.
7981 if (Index->getMemoryVT() != MemVT)
7982 break;
7983
7984 // We do not allow unaligned stores because we want to prevent overriding
7985 // stores.
7986 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
7987 break;
7988
7989 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007990 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00007991
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007992 // Find the next memory operand in the chain. If the next operand in the
7993 // chain is a store then move up and continue the scan with the next
7994 // memory operand. If the next operand is a load save it and use alias
7995 // information to check if it interferes with anything.
7996 SDNode *NextInChain = Index->getChain().getNode();
7997 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00007998 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007999 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008000 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008001 break;
8002 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8003 // Save the load node for later. Continue the scan.
8004 AliasLoadNodes.push_back(Ldn);
8005 NextInChain = Ldn->getChain().getNode();
8006 continue;
8007 } else {
8008 Index = NULL;
8009 break;
8010 }
8011 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008012 }
8013
8014 // Check if there is anything to merge.
8015 if (StoreNodes.size() < 2)
8016 return false;
8017
8018 // Sort the memory operands according to their distance from the base pointer.
8019 std::sort(StoreNodes.begin(), StoreNodes.end(),
8020 ConsecutiveMemoryChainSorter());
8021
8022 // Scan the memory operations on the chain and find the first non-consecutive
8023 // store memory address.
8024 unsigned LastConsecutiveStore = 0;
8025 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008026 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8027
8028 // Check that the addresses are consecutive starting from the second
8029 // element in the list of stores.
8030 if (i > 0) {
8031 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8032 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8033 break;
8034 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008035
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008036 bool Alias = false;
8037 // Check if this store interferes with any of the loads that we found.
8038 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8039 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8040 Alias = true;
8041 break;
8042 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008043 // We found a load that alias with this store. Stop the sequence.
8044 if (Alias)
8045 break;
8046
Nadav Rotemc653de62012-10-03 16:11:15 +00008047 // Mark this node as useful.
8048 LastConsecutiveStore = i;
8049 }
8050
8051 // The node with the lowest store address.
8052 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8053
8054 // Store the constants into memory as one consecutive store.
8055 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008056 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008057 unsigned LastLegalVectorType = 0;
8058 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008059 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8060 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8061 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008062
8063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008064 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008065 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008066 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008067 } else {
8068 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008069 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008070 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008071
Nadav Rotemc653de62012-10-03 16:11:15 +00008072 // Find a legal type for the constant store.
8073 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8074 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8075 if (TLI.isTypeLegal(StoreTy))
8076 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008077 // Or check whether a truncstore is legal.
8078 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8079 TargetLowering::TypePromoteInteger) {
8080 EVT LegalizedStoredValueTy =
8081 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8082 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8083 LastLegalType = i+1;
8084 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008085
8086 // Find a legal type for the vector store.
8087 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8088 if (TLI.isTypeLegal(Ty))
8089 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008090 }
8091
Bob Wilson99d8e762012-12-20 01:36:20 +00008092 // We only use vectors if the constant is known to be zero and the
8093 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008094 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008095 LastLegalVectorType = 0;
8096
Nadav Rotemc653de62012-10-03 16:11:15 +00008097 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008098 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008099 return false;
8100
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008101 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008102 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8103
8104 // Make sure we have something to merge.
8105 if (NumElem < 2)
8106 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008107
8108 unsigned EarliestNodeUsed = 0;
8109 for (unsigned i=0; i < NumElem; ++i) {
8110 // Find a chain for the new wide-store operand. Notice that some
8111 // of the store nodes that we found may not be selected for inclusion
8112 // in the wide store. The chain we use needs to be the chain of the
8113 // earliest store node which is *used* and replaced by the wide store.
8114 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8115 EarliestNodeUsed = i;
8116 }
8117
8118 // The earliest Node in the DAG.
8119 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008120 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008121
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008122 SDValue StoredVal;
8123 if (UseVector) {
8124 // Find a legal type for the vector store.
8125 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8126 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8127 StoredVal = DAG.getConstant(0, Ty);
8128 } else {
8129 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8130 APInt StoreInt(StoreBW, 0);
8131
8132 // Construct a single integer constant which is made of the smaller
8133 // constant inputs.
8134 bool IsLE = TLI.isLittleEndian();
8135 for (unsigned i = 0; i < NumElem ; ++i) {
8136 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8137 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8138 SDValue Val = St->getValue();
8139 StoreInt<<=ElementSizeBytes*8;
8140 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8141 StoreInt|=C->getAPIntValue().zext(StoreBW);
8142 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8143 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8144 } else {
8145 assert(false && "Invalid constant element type");
8146 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008147 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008148
8149 // Create the new Load and Store operations.
8150 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8151 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008152 }
8153
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008154 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008155 FirstInChain->getBasePtr(),
8156 FirstInChain->getPointerInfo(),
8157 false, false,
8158 FirstInChain->getAlignment());
8159
8160 // Replace the first store with the new store
8161 CombineTo(EarliestOp, NewStore);
8162 // Erase all other stores.
8163 for (unsigned i = 0; i < NumElem ; ++i) {
8164 if (StoreNodes[i].MemNode == EarliestOp)
8165 continue;
8166 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008167 // ReplaceAllUsesWith will replace all uses that existed when it was
8168 // called, but graph optimizations may cause new ones to appear. For
8169 // example, the case in pr14333 looks like
8170 //
8171 // St's chain -> St -> another store -> X
8172 //
8173 // And the only difference from St to the other store is the chain.
8174 // When we change it's chain to be St's chain they become identical,
8175 // get CSEed and the net result is that X is now a use of St.
8176 // Since we know that St is redundant, just iterate.
8177 while (!St->use_empty())
8178 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008179 removeFromWorkList(St);
8180 DAG.DeleteNode(St);
8181 }
8182
8183 return true;
8184 }
8185
8186 // Below we handle the case of multiple consecutive stores that
8187 // come from multiple consecutive loads. We merge them into a single
8188 // wide load and a single wide store.
8189
8190 // Look for load nodes which are used by the stored values.
8191 SmallVector<MemOpLink, 8> LoadNodes;
8192
8193 // Find acceptable loads. Loads need to have the same chain (token factor),
8194 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008195 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008196 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8197 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8198 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8199 if (!Ld) break;
8200
8201 // Loads must only have one use.
8202 if (!Ld->hasNUsesOfValue(1, 0))
8203 break;
8204
8205 // Check that the alignment is the same as the stores.
8206 if (Ld->getAlignment() != St->getAlignment())
8207 break;
8208
8209 // The memory operands must not be volatile.
8210 if (Ld->isVolatile() || Ld->isIndexed())
8211 break;
8212
8213 // We do not accept ext loads.
8214 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8215 break;
8216
8217 // The stored memory type must be the same.
8218 if (Ld->getMemoryVT() != MemVT)
8219 break;
8220
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008221 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008222 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008223 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008224 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008225 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008226 break;
8227 } else {
8228 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008229 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008230 }
8231
8232 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008233 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008234 }
8235
8236 if (LoadNodes.size() < 2)
8237 return false;
8238
8239 // Scan the memory operations on the chain and find the first non-consecutive
8240 // load memory address. These variables hold the index in the store node
8241 // array.
8242 unsigned LastConsecutiveLoad = 0;
8243 // This variable refers to the size and not index in the array.
8244 unsigned LastLegalVectorType = 0;
8245 unsigned LastLegalIntegerType = 0;
8246 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008247 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8248 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8249 // All loads much share the same chain.
8250 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8251 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008252
Nadav Rotemc653de62012-10-03 16:11:15 +00008253 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8254 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8255 break;
8256 LastConsecutiveLoad = i;
8257
8258 // Find a legal type for the vector store.
8259 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8260 if (TLI.isTypeLegal(StoreTy))
8261 LastLegalVectorType = i + 1;
8262
8263 // Find a legal type for the integer store.
8264 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8265 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8266 if (TLI.isTypeLegal(StoreTy))
8267 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008268 // Or check whether a truncstore and extload is legal.
8269 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8270 TargetLowering::TypePromoteInteger) {
8271 EVT LegalizedStoredValueTy =
8272 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8273 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8274 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8275 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8276 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8277 LastLegalIntegerType = i+1;
8278 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008279 }
8280
8281 // Only use vector types if the vector type is larger than the integer type.
8282 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008283 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008284 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8285
8286 // We add +1 here because the LastXXX variables refer to location while
8287 // the NumElem refers to array/index size.
8288 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8289 NumElem = std::min(LastLegalType, NumElem);
8290
8291 if (NumElem < 2)
8292 return false;
8293
8294 // The earliest Node in the DAG.
8295 unsigned EarliestNodeUsed = 0;
8296 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8297 for (unsigned i=1; i<NumElem; ++i) {
8298 // Find a chain for the new wide-store operand. Notice that some
8299 // of the store nodes that we found may not be selected for inclusion
8300 // in the wide store. The chain we use needs to be the chain of the
8301 // earliest store node which is *used* and replaced by the wide store.
8302 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8303 EarliestNodeUsed = i;
8304 }
8305
8306 // Find if it is better to use vectors or integers to load and store
8307 // to memory.
8308 EVT JointMemOpVT;
8309 if (UseVectorTy) {
8310 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8311 } else {
8312 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8313 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8314 }
8315
Andrew Trickac6d9be2013-05-25 02:42:55 +00008316 SDLoc LoadDL(LoadNodes[0].MemNode);
8317 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008318
8319 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8320 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8321 FirstLoad->getChain(),
8322 FirstLoad->getBasePtr(),
8323 FirstLoad->getPointerInfo(),
8324 false, false, false,
8325 FirstLoad->getAlignment());
8326
8327 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8328 FirstInChain->getBasePtr(),
8329 FirstInChain->getPointerInfo(), false, false,
8330 FirstInChain->getAlignment());
8331
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008332 // Replace one of the loads with the new load.
8333 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8334 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8335 SDValue(NewLoad.getNode(), 1));
8336
8337 // Remove the rest of the load chains.
8338 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008339 // Replace all chain users of the old load nodes with the chain of the new
8340 // load node.
8341 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008342 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8343 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008344
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008345 // Replace the first store with the new store.
8346 CombineTo(EarliestOp, NewStore);
8347 // Erase all other stores.
8348 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008349 // Remove all Store nodes.
8350 if (StoreNodes[i].MemNode == EarliestOp)
8351 continue;
8352 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8353 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8354 removeFromWorkList(St);
8355 DAG.DeleteNode(St);
8356 }
8357
8358 return true;
8359}
8360
Dan Gohman475871a2008-07-27 21:46:04 +00008361SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008362 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008363 SDValue Chain = ST->getChain();
8364 SDValue Value = ST->getValue();
8365 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008366
Evan Cheng59d5b682007-05-07 21:27:48 +00008367 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008368 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008369 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008370 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008371 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008372 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008373 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008374 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008375 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008376 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008377 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008378 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008379 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008380 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008381 }
Owen Andersona34d9362011-04-14 17:30:49 +00008382
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008383 // Turn 'store undef, Ptr' -> nothing.
8384 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8385 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008386
Nate Begeman2cbba892006-12-11 02:23:46 +00008387 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008388 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008389 // NOTE: If the original store is volatile, this transform must not increase
8390 // the number of stores. For example, on x86-32 an f64 can be stored in one
8391 // processor operation but an i64 (which is not legal) requires two. So the
8392 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008393 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008394 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008395 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008396 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008397 case MVT::f16: // We don't do this for these yet.
8398 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008399 case MVT::f128:
8400 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008401 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008402 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008403 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008404 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008405 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008406 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008407 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008408 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008409 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008410 }
8411 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008412 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008413 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008414 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008415 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008416 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008417 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008418 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008419 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008420 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008421 }
Owen Andersona34d9362011-04-14 17:30:49 +00008422
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008423 if (!ST->isVolatile() &&
8424 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008425 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008426 // argument passing. Since this is so common, custom legalize the
8427 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008428 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008429 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8430 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008431 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008432
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008433 unsigned Alignment = ST->getAlignment();
8434 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008435 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008436
Andrew Trickac6d9be2013-05-25 02:42:55 +00008437 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008438 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008439 isVolatile, isNonTemporal,
8440 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008441 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008442 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008443 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008444 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008445 Ptr, ST->getPointerInfo().getWithOffset(4),
8446 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008447 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008448 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008449 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008450 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008451
Chris Lattner62be1a72006-12-12 04:16:14 +00008452 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008453 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008454 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008455 }
8456
Evan Cheng255f20f2010-04-01 06:04:33 +00008457 // Try to infer better alignment information than the store already has.
8458 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008459 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8460 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008461 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008462 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8463 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008464 }
8465 }
8466
Evan Cheng31959b12011-02-02 01:06:55 +00008467 // Try transforming a pair floating point load / store ops to integer
8468 // load / store ops.
8469 SDValue NewST = TransformFPLoadStorePair(N);
8470 if (NewST.getNode())
8471 return NewST;
8472
Scott Michelfdc40a02009-02-17 22:15:04 +00008473 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008474 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008475 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008476
Jim Laskey6ff23e52006-10-04 16:53:27 +00008477 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008478 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008479 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008480
8481 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008482 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008483 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008484 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008485 ST->getMemoryVT(), ST->isVolatile(),
8486 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008487 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008488 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008489 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008490 ST->isVolatile(), ST->isNonTemporal(),
8491 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008492 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008493
Jim Laskey279f0532006-09-25 16:29:54 +00008494 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008495 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008497
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008498 // Make sure the new and old chains are cleaned up.
8499 AddToWorkList(Token.getNode());
8500
Jim Laskey274062c2006-10-13 23:32:28 +00008501 // Don't add users to work list.
8502 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008503 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008505
Evan Cheng33dbedc2006-11-05 09:31:14 +00008506 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008507 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008508 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008509
Chris Lattner3c872852007-12-29 06:26:16 +00008510 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008511 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008512 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008513 // See if we can simplify the input to this truncstore with knowledge that
8514 // only the low bits are being used. For example:
8515 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008516 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008517 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008518 APInt::getLowBitsSet(
8519 Value.getValueType().getScalarType().getSizeInBits(),
8520 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008521 AddToWorkList(Value.getNode());
8522 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008523 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008524 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008525 ST->isVolatile(), ST->isNonTemporal(),
8526 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008527
Chris Lattnere33544c2007-10-13 06:58:48 +00008528 // Otherwise, see if we can simplify the operation with
8529 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008530 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008531 APInt::getLowBitsSet(
8532 Value.getValueType().getScalarType().getSizeInBits(),
8533 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008534 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008536
Chris Lattner3c872852007-12-29 06:26:16 +00008537 // If this is a load followed by a store to the same location, then the store
8538 // is dead/noop.
8539 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008540 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008541 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008542 // There can't be any side effects between the load and store, such as
8543 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008544 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008545 // The store is dead, remove it.
8546 return Chain;
8547 }
8548 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008549
Chris Lattnerddf89562008-01-17 19:59:44 +00008550 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8551 // truncating store. We can do this even if this is already a truncstore.
8552 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008553 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008554 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008555 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008556 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008557 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008558 ST->isVolatile(), ST->isNonTemporal(),
8559 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008560 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008561
Nadav Rotemc653de62012-10-03 16:11:15 +00008562 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008563 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008564 if (!LegalTypes) {
8565 bool EverChanged = false;
8566
8567 do {
8568 // There can be multiple store sequences on the same chain.
8569 // Keep trying to merge store sequences until we are unable to do so
8570 // or until we merge the last store on the chain.
8571 bool Changed = MergeConsecutiveStores(ST);
8572 EverChanged |= Changed;
8573 if (!Changed) break;
8574 } while (ST->getOpcode() != ISD::DELETED_NODE);
8575
8576 if (EverChanged)
8577 return SDValue(N, 0);
8578 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008579
Evan Cheng8b944d32009-05-28 00:35:15 +00008580 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008581}
8582
Dan Gohman475871a2008-07-27 21:46:04 +00008583SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8584 SDValue InVec = N->getOperand(0);
8585 SDValue InVal = N->getOperand(1);
8586 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008587 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008588
Bob Wilson492fd452010-05-19 23:42:58 +00008589 // If the inserted element is an UNDEF, just use the input vector.
8590 if (InVal.getOpcode() == ISD::UNDEF)
8591 return InVec;
8592
Nadav Rotem609d54e2011-02-12 14:40:33 +00008593 EVT VT = InVec.getValueType();
8594
Owen Anderson95771af2011-02-25 21:41:48 +00008595 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008596 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8597 return SDValue();
8598
Eli Friedman9db817f2011-09-09 21:04:06 +00008599 // Check that we know which element is being inserted
8600 if (!isa<ConstantSDNode>(EltNo))
8601 return SDValue();
8602 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008603
Eli Friedman9db817f2011-09-09 21:04:06 +00008604 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8605 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8606 // vector elements.
8607 SmallVector<SDValue, 8> Ops;
8608 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
8609 Ops.append(InVec.getNode()->op_begin(),
8610 InVec.getNode()->op_end());
8611 } else if (InVec.getOpcode() == ISD::UNDEF) {
8612 unsigned NElts = VT.getVectorNumElements();
8613 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8614 } else {
8615 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008616 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008617
8618 // Insert the element
8619 if (Elt < Ops.size()) {
8620 // All the operands of BUILD_VECTOR must have the same type;
8621 // we enforce that here.
8622 EVT OpVT = Ops[0].getValueType();
8623 if (InVal.getValueType() != OpVT)
8624 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8625 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8626 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8627 Ops[Elt] = InVal;
8628 }
8629
8630 // Return the new vector
8631 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8632 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008633}
8634
Dan Gohman475871a2008-07-27 21:46:04 +00008635SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008636 // (vextract (scalar_to_vector val, 0) -> val
8637 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008638 EVT VT = InVec.getValueType();
8639 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008640
Duncan Sandsc356f332011-05-09 08:03:33 +00008641 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8642 // Check if the result type doesn't match the inserted element type. A
8643 // SCALAR_TO_VECTOR may truncate the inserted element and the
8644 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8645 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008646 if (InOp.getValueType() != NVT) {
8647 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008648 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008649 }
8650 return InOp;
8651 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008652
Nadav Rotemba05c912012-01-17 21:44:01 +00008653 SDValue EltNo = N->getOperand(1);
8654 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8655
8656 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8657 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008658 // we may introduce new vector instructions which are not backed by TD
8659 // patterns. For example on AVX, extracting elements from a wide vector
8660 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008661 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8662 && ConstEltNo && !LegalOperations) {
8663 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8664 int NumElem = VT.getVectorNumElements();
8665 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8666 // Find the new index to extract from.
8667 int OrigElt = SVOp->getMaskElt(Elt);
8668
8669 // Extracting an undef index is undef.
8670 if (OrigElt == -1)
8671 return DAG.getUNDEF(NVT);
8672
8673 // Select the right vector half to extract from.
8674 if (OrigElt < NumElem) {
8675 InVec = InVec->getOperand(0);
8676 } else {
8677 InVec = InVec->getOperand(1);
8678 OrigElt -= NumElem;
8679 }
8680
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008681 EVT IndexTy = N->getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008682 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008683 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008684 }
8685
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008686 // Perform only after legalization to ensure build_vector / vector_shuffle
8687 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008688 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008689
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008690 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8691 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8692 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008693
Nadav Rotemba05c912012-01-17 21:44:01 +00008694 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008695 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008696 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008697 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008698 EVT ExtVT = VT.getVectorElementType();
8699 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008700
Evan Cheng84387ea2012-03-13 22:00:52 +00008701 // If the result of load has to be truncated, then it's not necessarily
8702 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008703 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008704 return SDValue();
8705
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008706 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008707 // Don't duplicate a load with other uses.
8708 if (!InVec.hasOneUse())
8709 return SDValue();
8710
Owen Andersone50ed302009-08-10 22:56:29 +00008711 EVT BCVT = InVec.getOperand(0).getValueType();
8712 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008713 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008714 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8715 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008716 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008717 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008718 NewLoad = true;
8719 }
Evan Cheng513da432007-10-06 08:19:55 +00008720
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008721 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008722 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008723 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008724 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008725 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008726 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008727 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008728 // Don't duplicate a load with other uses.
8729 if (!InVec.hasOneUse())
8730 return SDValue();
8731
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008732 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008733 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008734 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8735 // =>
8736 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008737
Eli Friedmand6e25602011-12-26 22:49:32 +00008738 // Don't duplicate a load with other uses.
8739 if (!InVec.hasOneUse())
8740 return SDValue();
8741
Mon P Wanga60b5232008-12-11 00:26:16 +00008742 // If the bit convert changed the number of elements, it is unsafe
8743 // to examine the mask.
8744 if (BCNumEltsChanged)
8745 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008746
8747 // Select the input vector, guarding against out of range extract vector.
8748 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008749 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008750 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8751
Eli Friedmand6e25602011-12-26 22:49:32 +00008752 if (InVec.getOpcode() == ISD::BITCAST) {
8753 // Don't duplicate a load with other uses.
8754 if (!InVec.hasOneUse())
8755 return SDValue();
8756
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008757 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008758 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008759 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008760 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008761 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008762 }
8763 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008764
Eli Friedmand6e25602011-12-26 22:49:32 +00008765 // Make sure we found a non-volatile load and the extractelement is
8766 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008767 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008768 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008769
Eric Christopherd81f17a2010-11-03 20:44:42 +00008770 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8771 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008772 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008773
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008774 unsigned Align = LN0->getAlignment();
8775 if (NewLoad) {
8776 // Check the resultant load doesn't need a higher alignment than the
8777 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008778 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008779 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008780 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008781
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008782 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008783 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008784
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008785 Align = NewAlign;
8786 }
8787
Dan Gohman475871a2008-07-27 21:46:04 +00008788 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008789 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008790
Eric Christopherd81f17a2010-11-03 20:44:42 +00008791 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008792 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008793 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008794 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008795 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008796 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008797 DAG.getConstant(PtrOff, PtrType));
8798 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008799
Eli Friedman4db4add2011-11-16 23:50:22 +00008800 // The replacement we need to do here is a little tricky: we need to
8801 // replace an extractelement of a load with a load.
8802 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008803 // Note that this replacement assumes that the extractvalue is the only
8804 // use of the load; that's okay because we don't want to perform this
8805 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008806 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008807 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008808 if (NVT.bitsGT(LVT)) {
8809 // If the result type of vextract is wider than the load, then issue an
8810 // extending load instead.
8811 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8812 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008813 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008814 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8815 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008816 Chain = Load.getValue(1);
8817 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008818 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008819 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00008820 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008821 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008822 Chain = Load.getValue(1);
8823 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008824 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008825 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008826 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008827 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008828 WorkListRemover DeadNodes(*this);
8829 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008830 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008831 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008832 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8833 // worklist explicitly as well.
8834 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008835 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008836 // Make sure to revisit this node to clean it up; it will usually be dead.
8837 AddToWorkList(N);
8838 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008839 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008840
Dan Gohman475871a2008-07-27 21:46:04 +00008841 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008842}
Evan Cheng513da432007-10-06 08:19:55 +00008843
Michael Liaofac14ab2012-10-23 23:06:52 +00008844// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8845SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8846 // We perform this optimization post type-legalization because
8847 // the type-legalizer often scalarizes integer-promoted vectors.
8848 // Performing this optimization before may create bit-casts which
8849 // will be type-legalized to complex code sequences.
8850 // We perform this optimization only before the operation legalizer because we
8851 // may introduce illegal operations.
8852 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8853 return SDValue();
8854
Dan Gohman7f321562007-06-25 16:23:39 +00008855 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008856 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008857 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008858
Nadav Rotemb00418a2011-10-29 21:23:04 +00008859 // Check to see if this is a BUILD_VECTOR of a bunch of values
8860 // which come from any_extend or zero_extend nodes. If so, we can create
8861 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008862 // optimizations. We do not handle sign-extend because we can't fill the sign
8863 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008864 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008865 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008866
Craig Topperd3b58892012-01-17 09:09:48 +00008867 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008868 SDValue In = N->getOperand(i);
8869 // Ignore undef inputs.
8870 if (In.getOpcode() == ISD::UNDEF) continue;
8871
8872 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8873 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8874
Nadav Rotemf47368b2011-10-31 20:08:25 +00008875 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008876 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008877 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008878 break;
8879 }
8880
8881 // The input is a ZeroExt or AnyExt. Check the original type.
8882 EVT InTy = In.getOperand(0).getValueType();
8883
8884 // Check that all of the widened source types are the same.
8885 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008886 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008887 SourceType = InTy;
8888 else if (InTy != SourceType) {
8889 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008890 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008891 break;
8892 }
8893
8894 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008895 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008896 }
8897
Nadav Rotemf47368b2011-10-31 20:08:25 +00008898 // In order to have valid types, all of the inputs must be extended from the
8899 // same source type and all of the inputs must be any or zero extend.
8900 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008901 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008902 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008903 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8904 isPowerOf2_32(SourceType.getSizeInBits());
8905
Nadav Rotem6431ff92012-03-15 08:49:06 +00008906 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8907 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008908 if (!ValidTypes)
8909 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008910
Michael Liaofac14ab2012-10-23 23:06:52 +00008911 bool isLE = TLI.isLittleEndian();
8912 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8913 assert(ElemRatio > 1 && "Invalid element size ratio");
8914 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8915 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008916
Michael Liaofac14ab2012-10-23 23:06:52 +00008917 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8918 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008919
Michael Liaofac14ab2012-10-23 23:06:52 +00008920 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008921 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008922 SDValue Cast = N->getOperand(i);
8923 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8924 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8925 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8926 SDValue In;
8927 if (Cast.getOpcode() == ISD::UNDEF)
8928 In = DAG.getUNDEF(SourceType);
8929 else
8930 In = Cast->getOperand(0);
8931 unsigned Index = isLE ? (i * ElemRatio) :
8932 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008933
Michael Liaofac14ab2012-10-23 23:06:52 +00008934 assert(Index < Ops.size() && "Invalid index");
8935 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008936 }
Chris Lattnerca242442006-03-19 01:27:56 +00008937
Michael Liaofac14ab2012-10-23 23:06:52 +00008938 // The type of the new BUILD_VECTOR node.
8939 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8940 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8941 "Invalid vector size");
8942 // Check if the new vector type is legal.
8943 if (!isTypeLegal(VecVT)) return SDValue();
8944
8945 // Make the new BUILD_VECTOR.
8946 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
8947
8948 // The new BUILD_VECTOR node has the potential to be further optimized.
8949 AddToWorkList(BV.getNode());
8950 // Bitcast to the desired type.
8951 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8952}
8953
Michael Liao1a5cc712012-10-24 04:14:18 +00008954SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
8955 EVT VT = N->getValueType(0);
8956
8957 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008958 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00008959
8960 EVT SrcVT = MVT::Other;
8961 unsigned Opcode = ISD::DELETED_NODE;
8962 unsigned NumDefs = 0;
8963
8964 for (unsigned i = 0; i != NumInScalars; ++i) {
8965 SDValue In = N->getOperand(i);
8966 unsigned Opc = In.getOpcode();
8967
8968 if (Opc == ISD::UNDEF)
8969 continue;
8970
8971 // If all scalar values are floats and converted from integers.
8972 if (Opcode == ISD::DELETED_NODE &&
8973 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
8974 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00008975 }
Tom Stellardd40758b2013-01-02 22:13:01 +00008976
Michael Liao1a5cc712012-10-24 04:14:18 +00008977 if (Opc != Opcode)
8978 return SDValue();
8979
8980 EVT InVT = In.getOperand(0).getValueType();
8981
8982 // If all scalar values are typed differently, bail out. It's chosen to
8983 // simplify BUILD_VECTOR of integer types.
8984 if (SrcVT == MVT::Other)
8985 SrcVT = InVT;
8986 if (SrcVT != InVT)
8987 return SDValue();
8988 NumDefs++;
8989 }
8990
8991 // If the vector has just one element defined, it's not worth to fold it into
8992 // a vectorized one.
8993 if (NumDefs < 2)
8994 return SDValue();
8995
8996 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
8997 && "Should only handle conversion from integer to float.");
8998 assert(SrcVT != MVT::Other && "Cannot determine source type!");
8999
9000 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009001
9002 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9003 return SDValue();
9004
Michael Liao1a5cc712012-10-24 04:14:18 +00009005 SmallVector<SDValue, 8> Opnds;
9006 for (unsigned i = 0; i != NumInScalars; ++i) {
9007 SDValue In = N->getOperand(i);
9008
9009 if (In.getOpcode() == ISD::UNDEF)
9010 Opnds.push_back(DAG.getUNDEF(SrcVT));
9011 else
9012 Opnds.push_back(In.getOperand(0));
9013 }
9014 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9015 &Opnds[0], Opnds.size());
9016 AddToWorkList(BV.getNode());
9017
9018 return DAG.getNode(Opcode, dl, VT, BV);
9019}
9020
Michael Liaofac14ab2012-10-23 23:06:52 +00009021SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9022 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009023 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009024 EVT VT = N->getValueType(0);
9025
9026 // A vector built entirely of undefs is undef.
9027 if (ISD::allOperandsUndef(N))
9028 return DAG.getUNDEF(VT);
9029
9030 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9031 if (V.getNode())
9032 return V;
9033
Michael Liao1a5cc712012-10-24 04:14:18 +00009034 V = reduceBuildVecConvertToConvertBuildVec(N);
9035 if (V.getNode())
9036 return V;
9037
Dan Gohman7f321562007-06-25 16:23:39 +00009038 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9039 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9040 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009041
9042 // May only combine to shuffle after legalize if shuffle is legal.
9043 if (LegalOperations &&
9044 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9045 return SDValue();
9046
Dan Gohman475871a2008-07-27 21:46:04 +00009047 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009048 for (unsigned i = 0; i != NumInScalars; ++i) {
9049 // Ignore undef inputs.
9050 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009051
Dan Gohman7f321562007-06-25 16:23:39 +00009052 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009053 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009054 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009055 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009056 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009057 break;
9058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009059
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009060 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009061 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009062 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9063 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009064
Gabor Greifba36cb52008-08-28 21:40:38 +00009065 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009066 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009067 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009068 VecIn2 = ExtractedFromVec;
9069 } else {
9070 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009071 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009072 break;
9073 }
9074 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009075
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009076 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009077 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009078 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009079 for (unsigned i = 0; i != NumInScalars; ++i) {
9080 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009081 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009082 continue;
9083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009084
Rafael Espindola15684b22009-04-24 12:40:33 +00009085 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009086 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009087 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009088 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009089 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9090 if (ExtIndex > VT.getVectorNumElements())
9091 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009092
Nate Begeman5a5ca152009-04-29 05:20:52 +00009093 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009094 continue;
9095 }
9096
9097 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009098 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009099 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009100 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009101
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009102 // We can't generate a shuffle node with mismatched input and output types.
9103 // Attempt to transform a single input vector to the correct type.
9104 if ((VT != VecIn1.getValueType())) {
9105 // We don't support shuffeling between TWO values of different types.
9106 if (VecIn2.getNode() != 0)
9107 return SDValue();
9108
9109 // We only support widening of vectors which are half the size of the
9110 // output registers. For example XMM->YMM widening on X86 with AVX.
9111 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9112 return SDValue();
9113
James Molloy8cd08bf2012-09-10 14:01:21 +00009114 // If the input vector type has a different base type to the output
9115 // vector type, bail out.
9116 if (VecIn1.getValueType().getVectorElementType() !=
9117 VT.getVectorElementType())
9118 return SDValue();
9119
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009120 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009121 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009122 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009123 }
9124
9125 // If VecIn2 is unused then change it to undef.
9126 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9127
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009128 // Check that we were able to transform all incoming values to the same
9129 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009130 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9131 VecIn1.getValueType() != VT)
9132 return SDValue();
9133
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009134 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009135 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009136 return SDValue();
9137
Dan Gohman7f321562007-06-25 16:23:39 +00009138 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009139 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009140 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009141 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009142 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009143 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009144
Dan Gohman475871a2008-07-27 21:46:04 +00009145 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009146}
9147
Dan Gohman475871a2008-07-27 21:46:04 +00009148SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009149 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9150 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9151 // inputs come from at most two distinct vectors, turn this into a shuffle
9152 // node.
9153
9154 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009155 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009156 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009157
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009158 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009159 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009160 return DAG.getUNDEF(N->getValueType(0));
9161
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009162 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9163 // nodes often generate nop CONCAT_VECTOR nodes.
9164 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9165 // place the incoming vectors at the exact same location.
9166 SDValue SingleSource = SDValue();
9167 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9168
9169 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9170 SDValue Op = N->getOperand(i);
9171
9172 if (Op.getOpcode() == ISD::UNDEF)
9173 continue;
9174
9175 // Check if this is the identity extract:
9176 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9177 return SDValue();
9178
9179 // Find the single incoming vector for the extract_subvector.
9180 if (SingleSource.getNode()) {
9181 if (Op.getOperand(0) != SingleSource)
9182 return SDValue();
9183 } else {
9184 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009185
9186 // Check the source type is the same as the type of the result.
9187 // If not, this concat may extend the vector, so we can not
9188 // optimize it away.
9189 if (SingleSource.getValueType() != N->getValueType(0))
9190 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009191 }
9192
9193 unsigned IdentityIndex = i * PartNumElem;
9194 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9195 // The extract index must be constant.
9196 if (!CS)
9197 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009198
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009199 // Check that we are reading from the identity index.
9200 if (CS->getZExtValue() != IdentityIndex)
9201 return SDValue();
9202 }
9203
9204 if (SingleSource.getNode())
9205 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009206
Dan Gohman475871a2008-07-27 21:46:04 +00009207 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009208}
9209
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009210SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9211 EVT NVT = N->getValueType(0);
9212 SDValue V = N->getOperand(0);
9213
Michael Liao13429e22012-10-17 20:48:33 +00009214 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9215 // Combine:
9216 // (extract_subvec (concat V1, V2, ...), i)
9217 // Into:
9218 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009219 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9220 if (V->getOperand(0).getValueType() != NVT)
9221 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009222 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9223 unsigned NumElems = NVT.getVectorNumElements();
9224 assert((Idx % NumElems) == 0 &&
9225 "IDX in concat is not a multiple of the result vector length.");
9226 return V->getOperand(Idx / NumElems);
9227 }
9228
Michael Liaob4f98ea2013-03-25 23:47:35 +00009229 // Skip bitcasting
9230 if (V->getOpcode() == ISD::BITCAST)
9231 V = V.getOperand(0);
9232
9233 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009234 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009235 // Handle only simple case where vector being inserted and vector
9236 // being extracted are of same type, and are half size of larger vectors.
9237 EVT BigVT = V->getOperand(0).getValueType();
9238 EVT SmallVT = V->getOperand(1).getValueType();
9239 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9240 return SDValue();
9241
9242 // Only handle cases where both indexes are constants with the same type.
9243 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9244 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9245
9246 if (InsIdx && ExtIdx &&
9247 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9248 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9249 // Combine:
9250 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9251 // Into:
9252 // indices are equal or bit offsets are equal => V1
9253 // otherwise => (extract_subvec V1, ExtIdx)
9254 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9255 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9256 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9257 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9258 DAG.getNode(ISD::BITCAST, dl,
9259 N->getOperand(0).getValueType(),
9260 V->getOperand(0)), N->getOperand(1));
9261 }
9262 }
9263
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009264 return SDValue();
9265}
9266
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009267// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9268static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9269 EVT VT = N->getValueType(0);
9270 unsigned NumElts = VT.getVectorNumElements();
9271
9272 SDValue N0 = N->getOperand(0);
9273 SDValue N1 = N->getOperand(1);
9274 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9275
9276 SmallVector<SDValue, 4> Ops;
9277 EVT ConcatVT = N0.getOperand(0).getValueType();
9278 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9279 unsigned NumConcats = NumElts / NumElemsPerConcat;
9280
9281 // Look at every vector that's inserted. We're looking for exact
9282 // subvector-sized copies from a concatenated vector
9283 for (unsigned I = 0; I != NumConcats; ++I) {
9284 // Make sure we're dealing with a copy.
9285 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009286 bool AllUndef = true, NoUndef = true;
9287 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9288 if (SVN->getMaskElt(J) >= 0)
9289 AllUndef = false;
9290 else
9291 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009292 }
9293
Hao Liu3778c042013-05-13 02:07:05 +00009294 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009295 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9296 return SDValue();
9297
9298 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9299 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9300 return SDValue();
9301
9302 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9303 if (FirstElt < N0.getNumOperands())
9304 Ops.push_back(N0.getOperand(FirstElt));
9305 else
9306 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9307
9308 } else if (AllUndef) {
9309 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9310 } else { // Mixed with general masks and undefs, can't do optimization.
9311 return SDValue();
9312 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009313 }
9314
Andrew Trickac6d9be2013-05-25 02:42:55 +00009315 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009316 Ops.size());
9317}
9318
Dan Gohman475871a2008-07-27 21:46:04 +00009319SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009320 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009321 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009322
Mon P Wangaeb06d22008-11-10 04:46:22 +00009323 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009324 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009325
Craig Topperae1bec52012-04-09 05:16:56 +00009326 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009327
Craig Topper481b79c2012-01-04 08:07:43 +00009328 // Canonicalize shuffle undef, undef -> undef
9329 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9330 return DAG.getUNDEF(VT);
9331
9332 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9333
9334 // Canonicalize shuffle v, v -> v, undef
9335 if (N0 == N1) {
9336 SmallVector<int, 8> NewMask;
9337 for (unsigned i = 0; i != NumElts; ++i) {
9338 int Idx = SVN->getMaskElt(i);
9339 if (Idx >= (int)NumElts) Idx -= NumElts;
9340 NewMask.push_back(Idx);
9341 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009342 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009343 &NewMask[0]);
9344 }
9345
9346 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9347 if (N0.getOpcode() == ISD::UNDEF) {
9348 SmallVector<int, 8> NewMask;
9349 for (unsigned i = 0; i != NumElts; ++i) {
9350 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009351 if (Idx >= 0) {
9352 if (Idx < (int)NumElts)
9353 Idx += NumElts;
9354 else
9355 Idx -= NumElts;
9356 }
9357 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009358 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009359 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009360 &NewMask[0]);
9361 }
9362
9363 // Remove references to rhs if it is undef
9364 if (N1.getOpcode() == ISD::UNDEF) {
9365 bool Changed = false;
9366 SmallVector<int, 8> NewMask;
9367 for (unsigned i = 0; i != NumElts; ++i) {
9368 int Idx = SVN->getMaskElt(i);
9369 if (Idx >= (int)NumElts) {
9370 Idx = -1;
9371 Changed = true;
9372 }
9373 NewMask.push_back(Idx);
9374 }
9375 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009376 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009377 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009378
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009379 // If it is a splat, check if the argument vector is another splat or a
9380 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009381 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009382 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009383
Dan Gohman7f321562007-06-25 16:23:39 +00009384 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009385 // not the number of vector elements, look through it. Be careful not to
9386 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009387 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009388 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009389 if (ConvInput.getValueType().isVector() &&
9390 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009391 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009392 }
9393
Dan Gohman7f321562007-06-25 16:23:39 +00009394 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009395 assert(V->getNumOperands() == NumElts &&
9396 "BUILD_VECTOR has wrong number of operands");
9397 SDValue Base;
9398 bool AllSame = true;
9399 for (unsigned i = 0; i != NumElts; ++i) {
9400 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9401 Base = V->getOperand(i);
9402 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009403 }
Evan Cheng917ec982006-07-21 08:25:53 +00009404 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009405 // Splat of <u, u, u, u>, return <u, u, u, u>
9406 if (!Base.getNode())
9407 return N0;
9408 for (unsigned i = 0; i != NumElts; ++i) {
9409 if (V->getOperand(i) != Base) {
9410 AllSame = false;
9411 break;
9412 }
9413 }
9414 // Splat of <x, x, x, x>, return <x, x, x, x>
9415 if (AllSame)
9416 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009417 }
9418 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009419
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009420 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9421 Level < AfterLegalizeVectorOps &&
9422 (N1.getOpcode() == ISD::UNDEF ||
9423 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9424 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9425 SDValue V = partitionShuffleOfConcats(N, DAG);
9426
9427 if (V.getNode())
9428 return V;
9429 }
9430
Nadav Rotem4ac90812012-04-01 19:31:22 +00009431 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009432 // and it reverses the swizzle of the previous shuffle then we can
9433 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009434 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9435 N1.getOpcode() == ISD::UNDEF) {
9436
Nadav Rotem4ac90812012-04-01 19:31:22 +00009437 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9438
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009439 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9440 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9441 return SDValue();
9442
Craig Topperae1bec52012-04-09 05:16:56 +00009443 // The incoming shuffle must be of the same type as the result of the
9444 // current shuffle.
9445 assert(OtherSV->getOperand(0).getValueType() == VT &&
9446 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009447
9448 for (unsigned i = 0; i != NumElts; ++i) {
9449 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009450 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009451 // Next, this index comes from the first value, which is the incoming
9452 // shuffle. Adopt the incoming index.
9453 if (Idx >= 0)
9454 Idx = OtherSV->getMaskElt(Idx);
9455
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009456 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009457 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009458 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009459 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009460
9461 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009462 }
9463
Dan Gohman475871a2008-07-27 21:46:04 +00009464 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009465}
9466
Evan Cheng44f1f092006-04-20 08:56:16 +00009467/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009468/// an AND to a vector_shuffle with the destination vector and a zero vector.
9469/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009470/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009471SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009472 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009473 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009474 SDValue LHS = N->getOperand(0);
9475 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009476 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009477 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009478 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009479 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009480 SmallVector<int, 8> Indices;
9481 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009482 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009483 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009484 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009485 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009486
9487 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009488 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009489 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009490 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009491 else
Dan Gohman475871a2008-07-27 21:46:04 +00009492 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009493 }
9494
9495 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009496 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009497 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009498 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009499
Dan Gohman7f321562007-06-25 16:23:39 +00009500 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009501 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009502 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009503 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009504 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009505 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009506 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009507 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009508 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009509 }
9510 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009511
Dan Gohman475871a2008-07-27 21:46:04 +00009512 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009513}
9514
Dan Gohman7f321562007-06-25 16:23:39 +00009515/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009516SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009517 assert(N->getValueType(0).isVector() &&
9518 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009519
Dan Gohman475871a2008-07-27 21:46:04 +00009520 SDValue LHS = N->getOperand(0);
9521 SDValue RHS = N->getOperand(1);
9522 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009523 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009524
Dan Gohman7f321562007-06-25 16:23:39 +00009525 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009526 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009527 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009528 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009529 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009530 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009531 SDValue LHSOp = LHS.getOperand(i);
9532 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009533 // If these two elements can't be folded, bail out.
9534 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9535 LHSOp.getOpcode() != ISD::Constant &&
9536 LHSOp.getOpcode() != ISD::ConstantFP) ||
9537 (RHSOp.getOpcode() != ISD::UNDEF &&
9538 RHSOp.getOpcode() != ISD::Constant &&
9539 RHSOp.getOpcode() != ISD::ConstantFP))
9540 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009541
Evan Cheng7b336a82006-05-31 06:08:35 +00009542 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009543 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9544 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009545 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009546 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009547 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009548 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009549 break;
9550 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009551
Bob Wilsond7273432010-12-17 23:06:49 +00009552 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009553 EVT RVT = RHSOp.getValueType();
9554 if (RVT != VT) {
9555 // Integer BUILD_VECTOR operands may have types larger than the element
9556 // size (e.g., when the element type is not legal). Prior to type
9557 // legalization, the types may not match between the two BUILD_VECTORS.
9558 // Truncate one of the operands to make them match.
9559 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009560 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009561 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009562 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009563 VT = RVT;
9564 }
9565 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009566 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009567 LHSOp, RHSOp);
9568 if (FoldOp.getOpcode() != ISD::UNDEF &&
9569 FoldOp.getOpcode() != ISD::Constant &&
9570 FoldOp.getOpcode() != ISD::ConstantFP)
9571 break;
9572 Ops.push_back(FoldOp);
9573 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009575
Bob Wilsond7273432010-12-17 23:06:49 +00009576 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009577 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009578 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009580
Dan Gohman475871a2008-07-27 21:46:04 +00009581 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009582}
9583
Craig Topperdd201ff2012-09-11 01:45:21 +00009584/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9585SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009586 assert(N->getValueType(0).isVector() &&
9587 "SimplifyVUnaryOp only works on vectors!");
9588
9589 SDValue N0 = N->getOperand(0);
9590
9591 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9592 return SDValue();
9593
9594 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9595 SmallVector<SDValue, 8> Ops;
9596 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9597 SDValue Op = N0.getOperand(i);
9598 if (Op.getOpcode() != ISD::UNDEF &&
9599 Op.getOpcode() != ISD::ConstantFP)
9600 break;
9601 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009602 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009603 if (FoldOp.getOpcode() != ISD::UNDEF &&
9604 FoldOp.getOpcode() != ISD::ConstantFP)
9605 break;
9606 Ops.push_back(FoldOp);
9607 AddToWorkList(FoldOp.getNode());
9608 }
9609
9610 if (Ops.size() != N0.getNumOperands())
9611 return SDValue();
9612
Andrew Trickac6d9be2013-05-25 02:42:55 +00009613 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009614 N0.getValueType(), &Ops[0], Ops.size());
9615}
9616
Andrew Trickac6d9be2013-05-25 02:42:55 +00009617SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009618 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009619 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009620
Bill Wendling836ca7d2009-01-30 23:59:18 +00009621 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009622 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009623
Nate Begemanf845b452005-10-08 00:29:44 +00009624 // If we got a simplified select_cc node back from SimplifySelectCC, then
9625 // break it down into a new SETCC node, and a new SELECT node, and then return
9626 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009627 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009628 // Check to see if we got a select_cc back (to turn into setcc/select).
9629 // Otherwise, just return whatever node we got back, like fabs.
9630 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009631 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009632 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009633 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009634 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009635 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009636 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9637 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009638 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009639
Nate Begemanf845b452005-10-08 00:29:44 +00009640 return SCC;
9641 }
Dan Gohman475871a2008-07-27 21:46:04 +00009642 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009643}
9644
Chris Lattner40c62d52005-10-18 06:04:22 +00009645/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9646/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009647/// select. Callers of this should assume that TheSelect is deleted if this
9648/// returns true. As such, they should return the appropriate thing (e.g. the
9649/// node) back to the top-level of the DAG combiner loop to avoid it being
9650/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009651bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009652 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009653
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009654 // Cannot simplify select with vector condition
9655 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9656
Chris Lattner40c62d52005-10-18 06:04:22 +00009657 // If this is a select from two identical things, try to pull the operation
9658 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009659 if (LHS.getOpcode() != RHS.getOpcode() ||
9660 !LHS.hasOneUse() || !RHS.hasOneUse())
9661 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009662
Chris Lattner18061612010-09-21 15:46:59 +00009663 // If this is a load and the token chain is identical, replace the select
9664 // of two loads with a load through a select of the address to load from.
9665 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9666 // constants have been dropped into the constant pool.
9667 if (LHS.getOpcode() == ISD::LOAD) {
9668 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9669 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009670
Chris Lattner18061612010-09-21 15:46:59 +00009671 // Token chains must be identical.
9672 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009673 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009674 LLD->isVolatile() || RLD->isVolatile() ||
9675 // If this is an EXTLOAD, the VT's must match.
9676 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009677 // If this is an EXTLOAD, the kind of extension must match.
9678 (LLD->getExtensionType() != RLD->getExtensionType() &&
9679 // The only exception is if one of the extensions is anyext.
9680 LLD->getExtensionType() != ISD::EXTLOAD &&
9681 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009682 // FIXME: this discards src value information. This is
9683 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009684 // both potential memory locations. Since we are discarding
9685 // src value info, don't do the transformation if the memory
9686 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009687 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009688 RLD->getPointerInfo().getAddrSpace() != 0 ||
9689 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9690 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009691 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009692
Chris Lattnerf1658062010-09-21 15:58:55 +00009693 // Check that the select condition doesn't reach either load. If so,
9694 // folding this will induce a cycle into the DAG. If not, this is safe to
9695 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009696 SDValue Addr;
9697 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009698 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9699 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9700 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9701 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009702 // The loads must not depend on one another.
9703 if (LLD->isPredecessorOf(RLD) ||
9704 RLD->isPredecessorOf(LLD))
9705 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009706 Addr = DAG.getSelect(SDLoc(TheSelect),
9707 LLD->getBasePtr().getValueType(),
9708 TheSelect->getOperand(0), LLD->getBasePtr(),
9709 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009710 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009711 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9712 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9713
9714 if ((LLD->hasAnyUseOfValue(1) &&
9715 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009716 (RLD->hasAnyUseOfValue(1) &&
9717 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009718 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009719
Andrew Trickac6d9be2013-05-25 02:42:55 +00009720 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009721 LLD->getBasePtr().getValueType(),
9722 TheSelect->getOperand(0),
9723 TheSelect->getOperand(1),
9724 LLD->getBasePtr(), RLD->getBasePtr(),
9725 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009726 }
9727
Chris Lattnerf1658062010-09-21 15:58:55 +00009728 SDValue Load;
9729 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9730 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009731 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009732 // FIXME: Discards pointer info.
9733 LLD->getChain(), Addr, MachinePointerInfo(),
9734 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009735 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009736 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009737 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9738 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009739 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009740 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009741 // FIXME: Discards pointer info.
9742 LLD->getChain(), Addr, MachinePointerInfo(),
9743 LLD->getMemoryVT(), LLD->isVolatile(),
9744 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009745 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009746
9747 // Users of the select now use the result of the load.
9748 CombineTo(TheSelect, Load);
9749
9750 // Users of the old loads now use the new load's chain. We know the
9751 // old-load value is dead now.
9752 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9753 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9754 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009755 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Chris Lattner40c62d52005-10-18 06:04:22 +00009757 return false;
9758}
9759
Chris Lattner600fec32009-03-11 05:08:08 +00009760/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9761/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009762SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009763 SDValue N2, SDValue N3,
9764 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009765 // (x ? y : y) -> y.
9766 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009767
Owen Andersone50ed302009-08-10 22:56:29 +00009768 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009769 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9770 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9771 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009772
9773 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009774 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009775 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009776 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9777 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009778
9779 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009780 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009781 return N2;
9782 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009783 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009784 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Nate Begemanf845b452005-10-08 00:29:44 +00009786 // Check to see if we can simplify the select into an fabs node
9787 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9788 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009789 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009790 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9791 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9792 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9793 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009794 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009795
Nate Begemanf845b452005-10-08 00:29:44 +00009796 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9797 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9798 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9799 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009800 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009801 }
9802 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009803
Chris Lattner600fec32009-03-11 05:08:08 +00009804 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9805 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9806 // in it. This is a win when the constant is not otherwise available because
9807 // it replaces two constant pool loads with one. We only do this if the FP
9808 // type is known to be legal, because if it isn't, then we are before legalize
9809 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009810 // messing with soft float) and if the ConstantFP is not legal, because if
9811 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009812 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9813 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9814 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009815 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9816 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009817 // If both constants have multiple uses, then we won't need to do an
9818 // extra load, they are likely around in registers for other users.
9819 (TV->hasOneUse() || FV->hasOneUse())) {
9820 Constant *Elts[] = {
9821 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9822 const_cast<ConstantFP*>(TV->getConstantFPValue())
9823 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009824 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009825 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009826
Chris Lattner600fec32009-03-11 05:08:08 +00009827 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009828 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009829 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9830 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009831 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009832
9833 // Get the offsets to the 0 and 1 element of the array so that we can
9834 // select between them.
9835 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009836 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009837 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009838
Chris Lattner600fec32009-03-11 05:08:08 +00009839 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009840 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009841 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009842 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009843 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9844 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009845 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009846 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
9847 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009848 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009849 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009850 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009851 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009852
9853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009854 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009855
Nate Begemanf845b452005-10-08 00:29:44 +00009856 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009857 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009858 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009859 (N1C->isNullValue() || // (a < 0) ? b : 0
9860 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009861 EVT XType = N0.getValueType();
9862 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009863 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009864 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009865 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009866 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9867 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009868 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009869 SDValue ShCt = DAG.getConstant(ShCtV,
9870 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009871 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009872 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009873 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009874
Duncan Sands8e4eb092008-06-08 20:54:56 +00009875 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009876 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009877 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009878 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009879
9880 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009881 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009882
Andrew Trickac6d9be2013-05-25 02:42:55 +00009883 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009884 XType, N0,
9885 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009886 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009887 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009888
Duncan Sands8e4eb092008-06-08 20:54:56 +00009889 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009890 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009891 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009892 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009893
9894 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009895 }
9896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009897
Owen Andersoned1088a2010-09-22 22:58:22 +00009898 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9899 // where y is has a single bit set.
9900 // A plaintext description would be, we can turn the SELECT_CC into an AND
9901 // when the condition can be materialized as an all-ones register. Any
9902 // single bit-test can be materialized as an all-ones register with
9903 // shift-left and shift-right-arith.
9904 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9905 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009906 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009907 N2C && N2C->isNullValue()) {
9908 SDValue AndLHS = N0->getOperand(0);
9909 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9910 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9911 // Shift the tested bit over the sign bit.
9912 APInt AndMask = ConstAndRHS->getAPIntValue();
9913 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009914 DAG.getConstant(AndMask.countLeadingZeros(),
9915 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009916 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009917
Owen Andersoned1088a2010-09-22 22:58:22 +00009918 // Now arithmetic right shift it all the way over, so the result is either
9919 // all-ones, or zero.
9920 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009921 DAG.getConstant(AndMask.getBitWidth()-1,
9922 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009923 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009924
Owen Andersoned1088a2010-09-22 22:58:22 +00009925 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9926 }
9927 }
9928
Nate Begeman07ed4172005-10-10 21:26:48 +00009929 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009930 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009931 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9932 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009933
Chris Lattner1eba01e2007-04-11 06:50:51 +00009934 // If the caller doesn't want us to simplify this into a zext of a compare,
9935 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009936 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009937 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009938
Nate Begeman07ed4172005-10-10 21:26:48 +00009939 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009940 // NOTE: Don't create a SETCC if it's not legal on this target.
9941 if (!LegalOperations ||
9942 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00009943 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009944 SDValue Temp, SCC;
9945 // cast from setcc result type to select result type
9946 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +00009947 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009948 N0, N1, CC);
9949 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009950 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009951 N2.getValueType());
9952 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009953 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009954 N2.getValueType(), SCC);
9955 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009956 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
9957 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009958 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009959 }
9960
9961 AddToWorkList(SCC.getNode());
9962 AddToWorkList(Temp.getNode());
9963
9964 if (N2C->getAPIntValue() == 1)
9965 return Temp;
9966
9967 // shl setcc result by log2 n2c
9968 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
9969 DAG.getConstant(N2C->getAPIntValue().logBase2(),
9970 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +00009971 }
Nate Begeman07ed4172005-10-10 21:26:48 +00009972 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009973
Nate Begemanf845b452005-10-08 00:29:44 +00009974 // Check to see if this is the equivalent of setcc
9975 // FIXME: Turn all of these into setcc if setcc if setcc is legal
9976 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00009977 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00009978 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00009979 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +00009980 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
9981 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00009982 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009983 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00009984 return Res;
9985 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009986
Bill Wendling836ca7d2009-01-30 23:59:18 +00009987 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00009988 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009989 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00009990 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009991 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009992 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009993 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00009994 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00009995 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009996 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00009997 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009998 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009999 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010000 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010001 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010002 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010003 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010004 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010005 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010006 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010007 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010008 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010009 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010010 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010011 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010012 }
10013 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010014
Benjamin Kramercde51102010-07-08 12:09:56 +000010015 // Check to see if this is an integer abs.
10016 // select_cc setg[te] X, 0, X, -X ->
10017 // select_cc setgt X, -1, X, -X ->
10018 // select_cc setl[te] X, 0, -X, X ->
10019 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010020 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010021 if (N1C) {
10022 ConstantSDNode *SubC = NULL;
10023 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10024 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10025 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10026 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10027 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10028 (N1C->isOne() && CC == ISD::SETLT)) &&
10029 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10030 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10031
Owen Andersone50ed302009-08-10 22:56:29 +000010032 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010033 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010034 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010035 N0,
10036 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010037 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010038 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010039 XType, N0, Shift);
10040 AddToWorkList(Shift.getNode());
10041 AddToWorkList(Add.getNode());
10042 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010043 }
10044 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010045
Dan Gohman475871a2008-07-27 21:46:04 +000010046 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010047}
10048
Evan Chengfa1eb272007-02-08 22:13:59 +000010049/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010050SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010051 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010052 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010053 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010054 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010055 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010056}
10057
Nate Begeman69575232005-10-20 02:15:44 +000010058/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10059/// return a DAG expression to select that will generate the same value by
10060/// multiplying by a magic number. See:
10061/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010062SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010063 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010064 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010065
Andrew Lenharth232c9102006-06-12 16:07:18 +000010066 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010067 ii != ee; ++ii)
10068 AddToWorkList(*ii);
10069 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010070}
10071
10072/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10073/// return a DAG expression to select that will generate the same value by
10074/// multiplying by a magic number. See:
10075/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010076SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010077 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010078 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010079
Andrew Lenharth232c9102006-06-12 16:07:18 +000010080 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010081 ii != ee; ++ii)
10082 AddToWorkList(*ii);
10083 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010084}
10085
Nate Begemancc66cdd2009-09-25 06:05:26 +000010086/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010087// to alias with anything but itself. Provides base object and offset as
10088// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010089static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010090 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010091 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010092 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010093
Jim Laskey71382342006-10-07 23:37:56 +000010094 // If it's an adding a simple constant then integrate the offset.
10095 if (Base.getOpcode() == ISD::ADD) {
10096 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10097 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010098 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010099 }
10100 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010101
Nate Begemancc66cdd2009-09-25 06:05:26 +000010102 // Return the underlying GlobalValue, and update the Offset. Return false
10103 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10104 // by multiple nodes with different offsets.
10105 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10106 GV = G->getGlobal();
10107 Offset += G->getOffset();
10108 return false;
10109 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010110
Nate Begemancc66cdd2009-09-25 06:05:26 +000010111 // Return the underlying Constant value, and update the Offset. Return false
10112 // for ConstantSDNodes since the same constant pool entry may be represented
10113 // by multiple nodes with different offsets.
10114 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010115 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10116 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010117 Offset += C->getOffset();
10118 return false;
10119 }
Jim Laskey71382342006-10-07 23:37:56 +000010120 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010121 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010122}
10123
10124/// isAlias - Return true if there is any possibility that the two addresses
10125/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010126bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010127 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010128 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010129 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010130 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010131 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010132 unsigned SrcValueAlign2,
10133 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010134 // If they are the same then they must be aliases.
10135 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010136
Jim Laskey71382342006-10-07 23:37:56 +000010137 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010138 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010139 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010140 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010141 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010142 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10143 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010144
Nate Begemancc66cdd2009-09-25 06:05:26 +000010145 // If they have a same base address then check to see if they overlap.
10146 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010147 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010148
Owen Anderson4a9f1502010-09-20 20:39:59 +000010149 // It is possible for different frame indices to alias each other, mostly
10150 // when tail call optimization reuses return address slots for arguments.
10151 // To catch this case, look up the actual index of frame indices to compute
10152 // the real alias relationship.
10153 if (isFrameIndex1 && isFrameIndex2) {
10154 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10155 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10156 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10157 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10158 }
10159
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010160 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010161 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010162 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10163 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010164
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010165 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10166 // compared to the size and offset of the access, we may be able to prove they
10167 // do not alias. This check is conservative for now to catch cases created by
10168 // splitting vector types.
10169 if ((SrcValueAlign1 == SrcValueAlign2) &&
10170 (SrcValueOffset1 != SrcValueOffset2) &&
10171 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10172 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10173 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010174
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010175 // There is no overlap between these relatively aligned accesses of similar
10176 // size, return no alias.
10177 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10178 return false;
10179 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010180
Jim Laskey07a27092006-10-18 19:08:31 +000010181 if (CombinerGlobalAA) {
10182 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010183 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10184 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10185 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010186 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010187 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10188 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010189 if (AAResult == AliasAnalysis::NoAlias)
10190 return false;
10191 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010192
10193 // Otherwise we have to assume they alias.
10194 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010195}
10196
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010197bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10198 SDValue Ptr0, Ptr1;
10199 int64_t Size0, Size1;
10200 const Value *SrcValue0, *SrcValue1;
10201 int SrcValueOffset0, SrcValueOffset1;
10202 unsigned SrcValueAlign0, SrcValueAlign1;
10203 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10204 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10205 SrcValueAlign0, SrcTBAAInfo0);
10206 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10207 SrcValueAlign1, SrcTBAAInfo1);
10208 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010209 SrcValueAlign0, SrcTBAAInfo0,
10210 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10211 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010212}
10213
Jim Laskey71382342006-10-07 23:37:56 +000010214/// FindAliasInfo - Extracts the relevant alias information from the memory
10215/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010216bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010217 SDValue &Ptr, int64_t &Size,
10218 const Value *&SrcValue,
10219 int &SrcValueOffset,
10220 unsigned &SrcValueAlign,
10221 const MDNode *&TBAAInfo) const {
10222 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10223
10224 Ptr = LS->getBasePtr();
10225 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10226 SrcValue = LS->getSrcValue();
10227 SrcValueOffset = LS->getSrcValueOffset();
10228 SrcValueAlign = LS->getOriginalAlignment();
10229 TBAAInfo = LS->getTBAAInfo();
10230 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010231}
10232
Jim Laskey6ff23e52006-10-04 16:53:27 +000010233/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10234/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010235void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
10236 SmallVector<SDValue, 8> &Aliases) {
10237 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010238 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010239
Jim Laskey279f0532006-09-25 16:29:54 +000010240 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010241 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010242 int64_t Size;
10243 const Value *SrcValue;
10244 int SrcValueOffset;
10245 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010246 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010247 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010248 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010249
Jim Laskey6ff23e52006-10-04 16:53:27 +000010250 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010251 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010252 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010253
Jim Laskeybc588b82006-10-05 15:07:25 +000010254 // Look at each chain and determine if it is an alias. If so, add it to the
10255 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010256 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010257 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010258 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010259 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010260
10261 // For TokenFactor nodes, look at each operand and only continue up the
10262 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010263 // find more and revert to original chain since the xform is unlikely to be
10264 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010265 //
10266 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010267 // chain we found before we hit a tokenfactor rather than the original
10268 // chain.
10269 if (Depth > 6 || Aliases.size() == 2) {
10270 Aliases.clear();
10271 Aliases.push_back(OriginalChain);
10272 break;
10273 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010274
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010275 // Don't bother if we've been before.
10276 if (!Visited.insert(Chain.getNode()))
10277 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010278
Jim Laskeybc588b82006-10-05 15:07:25 +000010279 switch (Chain.getOpcode()) {
10280 case ISD::EntryToken:
10281 // Entry token is ideal chain operand, but handled in FindBetterChain.
10282 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010283
Jim Laskeybc588b82006-10-05 15:07:25 +000010284 case ISD::LOAD:
10285 case ISD::STORE: {
10286 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010287 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010288 int64_t OpSize;
10289 const Value *OpSrcValue;
10290 int OpSrcValueOffset;
10291 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010292 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010293 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010294 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010295 OpSrcValueAlign,
10296 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010297
Jim Laskeybc588b82006-10-05 15:07:25 +000010298 // If chain is alias then stop here.
10299 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010300 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010301 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010302 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010303 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010304 Aliases.push_back(Chain);
10305 } else {
10306 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010307 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010308 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010309 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010310 break;
10311 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010312
Jim Laskeybc588b82006-10-05 15:07:25 +000010313 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010314 // We have to check each of the operands of the token factor for "small"
10315 // token factors, so we queue them up. Adding the operands to the queue
10316 // (stack) in reverse order maintains the original order and increases the
10317 // likelihood that getNode will find a matching token factor (CSE.)
10318 if (Chain.getNumOperands() > 16) {
10319 Aliases.push_back(Chain);
10320 break;
10321 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010322 for (unsigned n = Chain.getNumOperands(); n;)
10323 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010324 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010325 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010326
Jim Laskeybc588b82006-10-05 15:07:25 +000010327 default:
10328 // For all other instructions we will just have to take what we can get.
10329 Aliases.push_back(Chain);
10330 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010331 }
10332 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010333}
10334
10335/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10336/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010337SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10338 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010339
Jim Laskey6ff23e52006-10-04 16:53:27 +000010340 // Accumulate all the aliases to this node.
10341 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010342
Dan Gohman71dc7c92011-05-17 22:20:36 +000010343 // If no operands then chain to entry token.
10344 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010345 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010346
10347 // If a single operand then chain to it. We don't need to revisit it.
10348 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010349 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010350
Jim Laskey6ff23e52006-10-04 16:53:27 +000010351 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010352 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010353 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010354}
10355
Nate Begeman1d4d4142005-09-01 00:19:25 +000010356// SelectionDAG::Combine - This is the entry point for the file.
10357//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010358void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010359 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010360 /// run - This is the main entry point to this class.
10361 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010362 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010363}