blob: 92b0004c89bb40605b1bc0329e611b78dfa42f69 [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) {
329 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000330 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331
Chris Lattner2392ae72010-04-15 04:48:01 +0000332 /// isTypeLegal - This method returns true if we are running before type
333 /// legalization or if the specified VT is legal.
334 bool isTypeLegal(const EVT &VT) {
335 if (!LegalTypes) return true;
336 return TLI.isTypeLegal(VT);
337 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000338
339 /// getSetCCResultType - Convenience wrapper around
340 /// TargetLowering::getSetCCResultType
341 EVT getSetCCResultType(EVT VT) const {
342 return TLI.getSetCCResultType(*DAG.getContext(), VT);
343 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000344 };
345}
346
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000347
348namespace {
349/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
350/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000351class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000352 DAGCombiner &DC;
353public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000354 explicit WorkListRemover(DAGCombiner &dc)
355 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Duncan Sandsedfcf592008-06-11 11:42:12 +0000357 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000358 DC.removeFromWorkList(N);
359 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000360};
361}
362
Chris Lattner24664722006-03-01 04:53:38 +0000363//===----------------------------------------------------------------------===//
364// TargetLowering::DAGCombinerInfo implementation
365//===----------------------------------------------------------------------===//
366
367void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
368 ((DAGCombiner*)DC)->AddToWorkList(N);
369}
370
Cameron Zwariched3caf92011-04-02 02:40:26 +0000371void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
372 ((DAGCombiner*)DC)->removeFromWorkList(N);
373}
374
Dan Gohman475871a2008-07-27 21:46:04 +0000375SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000376CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
377 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000378}
379
Dan Gohman475871a2008-07-27 21:46:04 +0000380SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000381CombineTo(SDNode *N, SDValue Res, bool AddTo) {
382 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000383}
384
385
Dan Gohman475871a2008-07-27 21:46:04 +0000386SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000387CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
388 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000389}
390
Dan Gohmane5af2d32009-01-29 01:59:02 +0000391void TargetLowering::DAGCombinerInfo::
392CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
393 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
394}
Chris Lattner24664722006-03-01 04:53:38 +0000395
Chris Lattner24664722006-03-01 04:53:38 +0000396//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000397// Helper Functions
398//===----------------------------------------------------------------------===//
399
400/// isNegatibleForFree - Return 1 if we can compute the negated form of the
401/// specified expression for the same cost as the expression itself, or 2 if we
402/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000403static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000404 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000405 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000406 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000407 // fneg is removable even if it has multiple uses.
408 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000409
Chris Lattner29446522007-05-14 22:04:50 +0000410 // Don't allow anything with multiple uses.
411 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Chris Lattner3adf9512007-05-25 02:19:06 +0000413 // Don't recurse exponentially.
414 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Chris Lattner29446522007-05-14 22:04:50 +0000416 switch (Op.getOpcode()) {
417 default: return false;
418 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000419 // Don't invert constant FP values after legalize. The negated constant
420 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000421 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000422 case ISD::FADD:
423 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000424 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000425
Owen Andersonafd3d562012-03-06 00:29:31 +0000426 // After operation legalization, it might not be legal to create new FSUBs.
427 if (LegalOperations &&
428 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
429 return 0;
430
Craig Topper956342b2012-09-09 22:58:45 +0000431 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000432 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
433 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000434 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000435 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000436 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000437 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000438 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000439 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Bill Wendlingd34470c2009-01-30 23:10:18 +0000442 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000443 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Chris Lattner29446522007-05-14 22:04:50 +0000445 case ISD::FMUL:
446 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000447 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Bill Wendlingd34470c2009-01-30 23:10:18 +0000449 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000450 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
451 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000452 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000453
Owen Andersonafd3d562012-03-06 00:29:31 +0000454 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000455 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Chris Lattner29446522007-05-14 22:04:50 +0000457 case ISD::FP_EXTEND:
458 case ISD::FP_ROUND:
459 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000460 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000461 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000462 }
463}
464
465/// GetNegatedExpression - If isNegatibleForFree returns true, this function
466/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000467static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000468 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000469 // fneg is removable even if it has multiple uses.
470 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Chris Lattner29446522007-05-14 22:04:50 +0000472 // Don't allow anything with multiple uses.
473 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000474
Chris Lattner3adf9512007-05-25 02:19:06 +0000475 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000476 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000477 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000478 case ISD::ConstantFP: {
479 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
480 V.changeSign();
481 return DAG.getConstantFP(V, Op.getValueType());
482 }
Chris Lattner29446522007-05-14 22:04:50 +0000483 case ISD::FADD:
484 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000485 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000486
Bill Wendlingd34470c2009-01-30 23:10:18 +0000487 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000488 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000489 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000490 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000491 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000492 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000493 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000494 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000495 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000496 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000497 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000498 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000499 Op.getOperand(0));
500 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000501 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000502 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000503
Bill Wendlingd34470c2009-01-30 23:10:18 +0000504 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000505 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000506 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000507 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000508
Bill Wendlingd34470c2009-01-30 23:10:18 +0000509 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000510 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000511 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000512
Chris Lattner29446522007-05-14 22:04:50 +0000513 case ISD::FMUL:
514 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000515 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Bill Wendlingd34470c2009-01-30 23:10:18 +0000517 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000518 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000519 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000520 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000521 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000522 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000523 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000524 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000525
Bill Wendlingd34470c2009-01-30 23:10:18 +0000526 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000527 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000528 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000529 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000530 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000531
Chris Lattner29446522007-05-14 22:04:50 +0000532 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000533 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000534 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000535 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000536 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000537 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000538 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000539 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000540 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000541 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000542 }
543}
Chris Lattner24664722006-03-01 04:53:38 +0000544
545
Nate Begeman4ebd8052005-09-01 23:24:04 +0000546// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
547// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000548// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000549// nodes based on the type of node we are checking. This simplifies life a
550// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000551static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
552 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 if (N.getOpcode() == ISD::SETCC) {
554 LHS = N.getOperand(0);
555 RHS = N.getOperand(1);
556 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000557 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000558 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000559 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 N.getOperand(2).getOpcode() == ISD::Constant &&
561 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000562 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
564 LHS = N.getOperand(0);
565 RHS = N.getOperand(1);
566 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000567 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000568 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 return false;
570}
571
Nate Begeman99801192005-09-07 23:25:52 +0000572// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
573// one use. If this is true, it allows the users to invert the operation for
574// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000575static bool isOneUseSetCC(SDValue N) {
576 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000577 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000578 return true;
579 return false;
580}
581
Andrew Trickac6d9be2013-05-25 02:42:55 +0000582SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000583 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000584 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000585 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
586 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000587 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000588 SDValue OpNode =
589 DAG.FoldConstantArithmetic(Opc, VT,
590 cast<ConstantSDNode>(N0.getOperand(1)),
591 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000592 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000593 }
594 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000595 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000596 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000597 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000598 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000599 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000600 }
601 }
Bill Wendling35247c32009-01-30 00:45:56 +0000602
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
604 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000605 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000606 SDValue OpNode =
607 DAG.FoldConstantArithmetic(Opc, VT,
608 cast<ConstantSDNode>(N1.getOperand(1)),
609 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000610 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000611 }
612 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000613 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000614 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000615 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000616 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000617 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000618 }
619 }
Bill Wendling35247c32009-01-30 00:45:56 +0000620
Dan Gohman475871a2008-07-27 21:46:04 +0000621 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000622}
623
Dan Gohman475871a2008-07-27 21:46:04 +0000624SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
625 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000626 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
627 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000628 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000629 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000630 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000631 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000632 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000633 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000634 assert((!To[i].getNode() ||
635 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000636 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000637 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000638 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000639 if (AddTo) {
640 // Push the new nodes and any users onto the worklist
641 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000642 if (To[i].getNode()) {
643 AddToWorkList(To[i].getNode());
644 AddUsersToWorkList(To[i].getNode());
645 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646 }
647 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000648
Dan Gohmandbe664a2009-01-19 21:44:21 +0000649 // Finally, if the node is now dead, remove it from the graph. The node
650 // may not be dead if the replacement process recursively simplified to
651 // something else needing this node.
652 if (N->use_empty()) {
653 // Nodes can be reintroduced into the worklist. Make sure we do not
654 // process a node that has been replaced.
655 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000656
Dan Gohmandbe664a2009-01-19 21:44:21 +0000657 // Finally, since the node is now dead, remove it from the graph.
658 DAG.DeleteNode(N);
659 }
Dan Gohman475871a2008-07-27 21:46:04 +0000660 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000661}
662
Evan Chenge5b51ac2010-04-17 06:13:15 +0000663void DAGCombiner::
664CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000665 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000666 // are deleted, make sure to remove them from our worklist.
667 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000668 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000669
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000671 AddToWorkList(TLO.New.getNode());
672 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000673
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000674 // Finally, if the node is now dead, remove it from the graph. The node
675 // may not be dead if the replacement process recursively simplified to
676 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000677 if (TLO.Old.getNode()->use_empty()) {
678 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000679
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000680 // If the operands of this node are only used by the node, they will now
681 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000682 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
683 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
684 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000685
Gabor Greifba36cb52008-08-28 21:40:38 +0000686 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000687 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000688}
689
690/// SimplifyDemandedBits - Check the specified integer node value to see if
691/// it can be simplified or if things it uses can be simplified by bit
692/// propagation. If so, return true.
693bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000694 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000695 APInt KnownZero, KnownOne;
696 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
697 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000698
Dan Gohmane5af2d32009-01-29 01:59:02 +0000699 // Revisit the node.
700 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Dan Gohmane5af2d32009-01-29 01:59:02 +0000702 // Replace the old value with the new one.
703 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000704 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000705 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000706 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000707 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000708 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000709
Dan Gohmane5af2d32009-01-29 01:59:02 +0000710 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000711 return true;
712}
713
Evan Cheng95c57ea2010-04-24 04:43:44 +0000714void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000715 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000716 EVT VT = Load->getValueType(0);
717 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000718
Evan Cheng95c57ea2010-04-24 04:43:44 +0000719 DEBUG(dbgs() << "\nReplacing.9 ";
720 Load->dump(&DAG);
721 dbgs() << "\nWith: ";
722 Trunc.getNode()->dump(&DAG);
723 dbgs() << '\n');
724 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000725 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
726 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000727 removeFromWorkList(Load);
728 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000729 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000730}
731
732SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
733 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000734 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000735 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000736 EVT MemVT = LD->getMemoryVT();
737 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000738 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000739 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000740 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000741 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000742 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000743 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000744 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000745 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000746 LD->isNonTemporal(), LD->getAlignment());
747 }
748
Evan Cheng4c26e932010-04-19 19:29:22 +0000749 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000750 switch (Opc) {
751 default: break;
752 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000753 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000754 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000755 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000756 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000757 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000758 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000759 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000760 case ISD::Constant: {
761 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000762 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000763 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764 }
Evan Chengcaf77402010-04-23 19:10:30 +0000765 }
766
767 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000768 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000769 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000770}
771
Evan Cheng95c57ea2010-04-24 04:43:44 +0000772SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000773 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
774 return SDValue();
775 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000776 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000777 bool Replace = false;
778 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
779 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000781 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782
783 if (Replace)
784 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
785 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000786 DAG.getValueType(OldVT));
787}
788
Evan Cheng95c57ea2010-04-24 04:43:44 +0000789SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000790 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000791 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000792 bool Replace = false;
793 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
794 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000795 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000796 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000797
798 if (Replace)
799 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
800 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000801}
802
Evan Cheng64b7bf72010-04-16 06:14:10 +0000803/// PromoteIntBinOp - Promote the specified integer binary operation if the
804/// target indicates it is beneficial. e.g. On x86, it's usually better to
805/// promote i16 operations to i32 since i16 instructions are longer.
806SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
807 if (!LegalOperations)
808 return SDValue();
809
810 EVT VT = Op.getValueType();
811 if (VT.isVector() || !VT.isInteger())
812 return SDValue();
813
Evan Chenge5b51ac2010-04-17 06:13:15 +0000814 // If operation type is 'undesirable', e.g. i16 on x86, consider
815 // promoting it.
816 unsigned Opc = Op.getOpcode();
817 if (TLI.isTypeDesirableForOp(Opc, VT))
818 return SDValue();
819
Evan Cheng64b7bf72010-04-16 06:14:10 +0000820 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000821 // Consult target whether it is a good idea to promote this operation and
822 // what's the right type to promote it to.
823 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000824 assert(PVT != VT && "Don't know what type to promote to!");
825
Evan Cheng95c57ea2010-04-24 04:43:44 +0000826 bool Replace0 = false;
827 SDValue N0 = Op.getOperand(0);
828 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
829 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000830 return SDValue();
831
Evan Cheng95c57ea2010-04-24 04:43:44 +0000832 bool Replace1 = false;
833 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000834 SDValue NN1;
835 if (N0 == N1)
836 NN1 = NN0;
837 else {
838 NN1 = PromoteOperand(N1, PVT, Replace1);
839 if (NN1.getNode() == 0)
840 return SDValue();
841 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000842
Evan Cheng95c57ea2010-04-24 04:43:44 +0000843 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000844 if (NN1.getNode())
845 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000846
847 if (Replace0)
848 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
849 if (Replace1)
850 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000851
Evan Chengac7eae52010-04-27 19:48:13 +0000852 DEBUG(dbgs() << "\nPromoting ";
853 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000854 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000855 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000856 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000857 }
858 return SDValue();
859}
860
861/// PromoteIntShiftOp - Promote the specified integer shift operation if the
862/// target indicates it is beneficial. e.g. On x86, it's usually better to
863/// promote i16 operations to i32 since i16 instructions are longer.
864SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
865 if (!LegalOperations)
866 return SDValue();
867
868 EVT VT = Op.getValueType();
869 if (VT.isVector() || !VT.isInteger())
870 return SDValue();
871
872 // If operation type is 'undesirable', e.g. i16 on x86, consider
873 // promoting it.
874 unsigned Opc = Op.getOpcode();
875 if (TLI.isTypeDesirableForOp(Opc, VT))
876 return SDValue();
877
878 EVT PVT = VT;
879 // Consult target whether it is a good idea to promote this operation and
880 // what's the right type to promote it to.
881 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
882 assert(PVT != VT && "Don't know what type to promote to!");
883
Evan Cheng95c57ea2010-04-24 04:43:44 +0000884 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000885 SDValue N0 = Op.getOperand(0);
886 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000887 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000888 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000889 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000890 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000891 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000892 if (N0.getNode() == 0)
893 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000894
Evan Chenge5b51ac2010-04-17 06:13:15 +0000895 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000896 if (Replace)
897 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000898
Evan Chengac7eae52010-04-27 19:48:13 +0000899 DEBUG(dbgs() << "\nPromoting ";
900 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000901 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000902 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000903 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000904 }
905 return SDValue();
906}
907
Evan Cheng4c26e932010-04-19 19:29:22 +0000908SDValue DAGCombiner::PromoteExtend(SDValue Op) {
909 if (!LegalOperations)
910 return SDValue();
911
912 EVT VT = Op.getValueType();
913 if (VT.isVector() || !VT.isInteger())
914 return SDValue();
915
916 // If operation type is 'undesirable', e.g. i16 on x86, consider
917 // promoting it.
918 unsigned Opc = Op.getOpcode();
919 if (TLI.isTypeDesirableForOp(Opc, VT))
920 return SDValue();
921
922 EVT PVT = VT;
923 // Consult target whether it is a good idea to promote this operation and
924 // what's the right type to promote it to.
925 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
926 assert(PVT != VT && "Don't know what type to promote to!");
927 // fold (aext (aext x)) -> (aext x)
928 // fold (aext (zext x)) -> (zext x)
929 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000930 DEBUG(dbgs() << "\nPromoting ";
931 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000932 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000933 }
934 return SDValue();
935}
936
937bool DAGCombiner::PromoteLoad(SDValue Op) {
938 if (!LegalOperations)
939 return false;
940
941 EVT VT = Op.getValueType();
942 if (VT.isVector() || !VT.isInteger())
943 return false;
944
945 // If operation type is 'undesirable', e.g. i16 on x86, consider
946 // promoting it.
947 unsigned Opc = Op.getOpcode();
948 if (TLI.isTypeDesirableForOp(Opc, VT))
949 return false;
950
951 EVT PVT = VT;
952 // Consult target whether it is a good idea to promote this operation and
953 // what's the right type to promote it to.
954 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
955 assert(PVT != VT && "Don't know what type to promote to!");
956
Andrew Trickac6d9be2013-05-25 02:42:55 +0000957 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000958 SDNode *N = Op.getNode();
959 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000960 EVT MemVT = LD->getMemoryVT();
961 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000962 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000963 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000964 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000965 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000966 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000967 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000968 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000969 LD->isNonTemporal(), LD->getAlignment());
970 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
971
Evan Cheng95c57ea2010-04-24 04:43:44 +0000972 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000973 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000974 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000975 Result.getNode()->dump(&DAG);
976 dbgs() << '\n');
977 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000978 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
979 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000980 removeFromWorkList(N);
981 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000982 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000983 return true;
984 }
985 return false;
986}
987
Evan Chenge5b51ac2010-04-17 06:13:15 +0000988
Chris Lattner29446522007-05-14 22:04:50 +0000989//===----------------------------------------------------------------------===//
990// Main DAG Combiner implementation
991//===----------------------------------------------------------------------===//
992
Duncan Sands25cf2272008-11-24 14:53:14 +0000993void DAGCombiner::Run(CombineLevel AtLevel) {
994 // set the instance variables, so that the various visit routines may use it.
995 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000996 LegalOperations = Level >= AfterLegalizeVectorOps;
997 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000998
Evan Cheng17a568b2008-08-29 22:21:44 +0000999 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001000 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1001 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001002 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001003
Evan Cheng17a568b2008-08-29 22:21:44 +00001004 // Create a dummy node (which is not added to allnodes), that adds a reference
1005 // to the root node, preventing it from being deleted, and tracking any
1006 // changes of the root.
1007 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001008
Jim Laskey26f7fa72006-10-17 19:33:52 +00001009 // The root of the dag may dangle to deleted nodes until the dag combiner is
1010 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001011 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001012
James Molloy6660c052012-02-16 09:17:04 +00001013 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001014 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001015 while (!WorkListContents.empty()) {
1016 SDNode *N;
1017 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1018 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1019 // worklist *should* contain, and check the node we want to visit is should
1020 // actually be visited.
1021 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001022 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001023 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001024
Evan Cheng17a568b2008-08-29 22:21:44 +00001025 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1026 // N is deleted from the DAG, since they too may now be dead or may have a
1027 // reduced number of uses, allowing other xforms.
1028 if (N->use_empty() && N != &Dummy) {
1029 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1030 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Evan Cheng17a568b2008-08-29 22:21:44 +00001032 DAG.DeleteNode(N);
1033 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001035
Evan Cheng17a568b2008-08-29 22:21:44 +00001036 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001037
Evan Cheng17a568b2008-08-29 22:21:44 +00001038 if (RV.getNode() == 0)
1039 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001040
Evan Cheng17a568b2008-08-29 22:21:44 +00001041 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001042
Evan Cheng17a568b2008-08-29 22:21:44 +00001043 // If we get back the same node we passed in, rather than a new node or
1044 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001045 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001046 // mechanics for us, we have no work to do in this case.
1047 if (RV.getNode() == N)
1048 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001049
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 assert(N->getOpcode() != ISD::DELETED_NODE &&
1051 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1052 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001053
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001054 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001055 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001056 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001057 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001058 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001059
Devang Patel9728ea22011-05-23 22:04:42 +00001060 // Transfer debug value.
1061 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001062 WorkListRemover DeadNodes(*this);
1063 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001064 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001065 else {
1066 assert(N->getValueType(0) == RV.getValueType() &&
1067 N->getNumValues() == 1 && "Type mismatch");
1068 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001069 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001070 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Evan Cheng17a568b2008-08-29 22:21:44 +00001072 // Push the new node and any users onto the worklist
1073 AddToWorkList(RV.getNode());
1074 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001075
Evan Cheng17a568b2008-08-29 22:21:44 +00001076 // Add any uses of the old node to the worklist in case this node is the
1077 // last one that uses them. They may become dead after this node is
1078 // deleted.
1079 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1080 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001081
Dan Gohmandbe664a2009-01-19 21:44:21 +00001082 // Finally, if the node is now dead, remove it from the graph. The node
1083 // may not be dead if the replacement process recursively simplified to
1084 // something else needing this node.
1085 if (N->use_empty()) {
1086 // Nodes can be reintroduced into the worklist. Make sure we do not
1087 // process a node that has been replaced.
1088 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001089
Dan Gohmandbe664a2009-01-19 21:44:21 +00001090 // Finally, since the node is now dead, remove it from the graph.
1091 DAG.DeleteNode(N);
1092 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001094
Chris Lattner95038592005-10-05 06:35:28 +00001095 // If the root changed (e.g. it was a dead load, update the root).
1096 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001097 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098}
1099
Dan Gohman475871a2008-07-27 21:46:04 +00001100SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001101 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001103 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001104 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 case ISD::ADD: return visitADD(N);
1106 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001107 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001108 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001109 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001110 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 case ISD::MUL: return visitMUL(N);
1112 case ISD::SDIV: return visitSDIV(N);
1113 case ISD::UDIV: return visitUDIV(N);
1114 case ISD::SREM: return visitSREM(N);
1115 case ISD::UREM: return visitUREM(N);
1116 case ISD::MULHU: return visitMULHU(N);
1117 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001118 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1119 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001120 case ISD::SMULO: return visitSMULO(N);
1121 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001122 case ISD::SDIVREM: return visitSDIVREM(N);
1123 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001124 case ISD::AND: return visitAND(N);
1125 case ISD::OR: return visitOR(N);
1126 case ISD::XOR: return visitXOR(N);
1127 case ISD::SHL: return visitSHL(N);
1128 case ISD::SRA: return visitSRA(N);
1129 case ISD::SRL: return visitSRL(N);
1130 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001131 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001133 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001134 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001135 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001136 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001137 case ISD::SELECT_CC: return visitSELECT_CC(N);
1138 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001139 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1140 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001141 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1143 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001144 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001145 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001146 case ISD::FADD: return visitFADD(N);
1147 case ISD::FSUB: return visitFSUB(N);
1148 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001149 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001150 case ISD::FDIV: return visitFDIV(N);
1151 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001152 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001153 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1154 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1155 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1156 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1157 case ISD::FP_ROUND: return visitFP_ROUND(N);
1158 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1159 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1160 case ISD::FNEG: return visitFNEG(N);
1161 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001162 case ISD::FFLOOR: return visitFFLOOR(N);
1163 case ISD::FCEIL: return visitFCEIL(N);
1164 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001165 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001166 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001167 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001168 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001169 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001170 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001171 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1172 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001173 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001174 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 }
Dan Gohman475871a2008-07-27 21:46:04 +00001176 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177}
1178
Dan Gohman475871a2008-07-27 21:46:04 +00001179SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001180 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001181
1182 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001183 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001184 assert(N->getOpcode() != ISD::DELETED_NODE &&
1185 "Node was deleted but visit returned NULL!");
1186
1187 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1188 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1189
1190 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001191 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001192 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001193
1194 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1195 }
1196 }
1197
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001198 // If nothing happened still, try promoting the operation.
1199 if (RV.getNode() == 0) {
1200 switch (N->getOpcode()) {
1201 default: break;
1202 case ISD::ADD:
1203 case ISD::SUB:
1204 case ISD::MUL:
1205 case ISD::AND:
1206 case ISD::OR:
1207 case ISD::XOR:
1208 RV = PromoteIntBinOp(SDValue(N, 0));
1209 break;
1210 case ISD::SHL:
1211 case ISD::SRA:
1212 case ISD::SRL:
1213 RV = PromoteIntShiftOp(SDValue(N, 0));
1214 break;
1215 case ISD::SIGN_EXTEND:
1216 case ISD::ZERO_EXTEND:
1217 case ISD::ANY_EXTEND:
1218 RV = PromoteExtend(SDValue(N, 0));
1219 break;
1220 case ISD::LOAD:
1221 if (PromoteLoad(SDValue(N, 0)))
1222 RV = SDValue(N, 0);
1223 break;
1224 }
1225 }
1226
Scott Michelfdc40a02009-02-17 22:15:04 +00001227 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001228 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001229 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001230 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1231 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SDValue N0 = N->getOperand(0);
1233 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001234
Evan Cheng08b11732008-03-22 01:55:50 +00001235 // Constant operands are canonicalized to RHS.
1236 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001237 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001238 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1239 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001240 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001241 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001242 }
1243 }
1244
Dan Gohman389079b2007-10-08 17:57:15 +00001245 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001246}
Dan Gohman389079b2007-10-08 17:57:15 +00001247
Chris Lattner6270f682006-10-08 22:57:01 +00001248/// getInputChainForNode - Given a node, return its input chain if it has one,
1249/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001250static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001251 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001253 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001255 return N->getOperand(NumOps-1);
1256 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001258 return N->getOperand(i);
1259 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001260 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001261}
1262
Dan Gohman475871a2008-07-27 21:46:04 +00001263SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001264 // If N has two operands, where one has an input chain equal to the other,
1265 // the 'other' chain is redundant.
1266 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001267 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001268 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001270 return N->getOperand(1);
1271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001272
Chris Lattnerc76d4412007-05-16 06:37:59 +00001273 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001275 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001276 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001277
Jim Laskey6ff23e52006-10-04 16:53:27 +00001278 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001279 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Jim Laskey71382342006-10-07 23:37:56 +00001281 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001282 // encountered.
1283 for (unsigned i = 0; i < TFs.size(); ++i) {
1284 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001285
Jim Laskey6ff23e52006-10-04 16:53:27 +00001286 // Check each of the operands.
1287 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001288 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001289
Jim Laskey6ff23e52006-10-04 16:53:27 +00001290 switch (Op.getOpcode()) {
1291 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001292 // Entry tokens don't need to be added to the list. They are
1293 // rededundant.
1294 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001295 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001296
Jim Laskey6ff23e52006-10-04 16:53:27 +00001297 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001298 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001299 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001301 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001302 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001304 Changed = true;
1305 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001306 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001307 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Jim Laskey6ff23e52006-10-04 16:53:27 +00001309 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001310 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001311 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001312 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001313 else
1314 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001315 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001316 }
1317 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001319
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001321
1322 // If we've change things around then replace token factor.
1323 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001324 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325 // The entry token is the only possible outcome.
1326 Result = DAG.getEntryNode();
1327 } else {
1328 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001329 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001330 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001331 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001332
Jim Laskey274062c2006-10-13 23:32:28 +00001333 // Don't add users to work list.
1334 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Jim Laskey6ff23e52006-10-04 16:53:27 +00001337 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338}
1339
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001340/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001341SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001342 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001343 // Replacing results may cause a different MERGE_VALUES to suddenly
1344 // be CSE'd with N, and carry its uses with it. Iterate until no
1345 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001346 // First add the users of this node to the work list so that they
1347 // can be tried again once they have new operands.
1348 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001349 do {
1350 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001351 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001352 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001353 removeFromWorkList(N);
1354 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001355 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001356}
1357
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001358static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001359SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001360 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue N00 = N0.getOperand(0);
1363 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001364 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001365
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001367 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001368 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001369 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1370 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001371 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001372 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001373 N00.getOperand(1), N01));
1374 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001375 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001376
Dan Gohman475871a2008-07-27 21:46:04 +00001377 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001378}
1379
Dan Gohman475871a2008-07-27 21:46:04 +00001380SDValue DAGCombiner::visitADD(SDNode *N) {
1381 SDValue N0 = N->getOperand(0);
1382 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1384 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001385 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001386
1387 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001388 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001390 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001391
1392 // fold (add x, 0) -> x, vector edition
1393 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1394 return N0;
1395 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1396 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001397 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001398
Dan Gohman613e0d82007-07-03 14:03:57 +00001399 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001400 if (N0.getOpcode() == ISD::UNDEF)
1401 return N0;
1402 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001403 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001406 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001407 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001408 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001409 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001412 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001413 // fold (add Sym, c) -> Sym+c
1414 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001415 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001416 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001417 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001418 GA->getOffset() +
1419 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001420 // fold ((c1-A)+c2) -> (c1+c2)-A
1421 if (N1C && N0.getOpcode() == ISD::SUB)
1422 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001423 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001424 DAG.getConstant(N1C->getAPIntValue()+
1425 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001426 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001427 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001428 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001429 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001430 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 // fold ((0-A) + B) -> B-A
1432 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1433 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001434 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold (A + (0-B)) -> A-B
1436 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1437 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001438 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001439 // fold (A+(B-A)) -> B
1440 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001441 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001442 // fold ((B-A)+A) -> B
1443 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1444 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001445 // fold (A+(B-(A+C))) to (B-C)
1446 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001447 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001448 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001449 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001450 // fold (A+(B-(C+A))) to (B-C)
1451 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001452 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001453 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001454 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001455 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001456 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1457 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001458 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001460 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001461
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001462 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1463 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1464 SDValue N00 = N0.getOperand(0);
1465 SDValue N01 = N0.getOperand(1);
1466 SDValue N10 = N1.getOperand(0);
1467 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001468
1469 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001470 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1471 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1472 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001473 }
Chris Lattner947c2892006-03-13 06:51:27 +00001474
Dan Gohman475871a2008-07-27 21:46:04 +00001475 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1476 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001477
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001478 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001479 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001480 APInt LHSZero, LHSOne;
1481 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001482 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001483
Dan Gohman948d8ea2008-02-20 16:33:30 +00001484 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001485 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Chris Lattner947c2892006-03-13 06:51:27 +00001487 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1488 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001489 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001490 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001491 }
1492 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001493
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001494 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001495 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001496 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001497 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001498 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001500 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001501 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001502 }
1503
Dan Gohmancd9e1552010-01-19 23:30:49 +00001504 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1505 if (N1.getOpcode() == ISD::SHL &&
1506 N1.getOperand(0).getOpcode() == ISD::SUB)
1507 if (ConstantSDNode *C =
1508 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1509 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001510 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1511 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001512 N1.getOperand(0).getOperand(1),
1513 N1.getOperand(1)));
1514 if (N0.getOpcode() == ISD::SHL &&
1515 N0.getOperand(0).getOpcode() == ISD::SUB)
1516 if (ConstantSDNode *C =
1517 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1518 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001519 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1520 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001521 N0.getOperand(0).getOperand(1),
1522 N0.getOperand(1)));
1523
Owen Andersonbc146b02010-09-21 20:42:50 +00001524 if (N1.getOpcode() == ISD::AND) {
1525 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001526 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001527 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1528 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529
Owen Andersonbc146b02010-09-21 20:42:50 +00001530 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1531 // and similar xforms where the inner op is either ~0 or 0.
1532 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001533 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001534 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1535 }
1536 }
1537
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001538 // add (sext i1), X -> sub X, (zext i1)
1539 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1540 N0.getOperand(0).getValueType() == MVT::i1 &&
1541 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001542 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001543 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1544 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1545 }
1546
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001547 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548}
1549
Dan Gohman475871a2008-07-27 21:46:04 +00001550SDValue DAGCombiner::visitADDC(SDNode *N) {
1551 SDValue N0 = N->getOperand(0);
1552 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001555 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001556
Chris Lattner91153682007-03-04 20:03:15 +00001557 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001558 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001559 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001560 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001561 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001562
Chris Lattner91153682007-03-04 20:03:15 +00001563 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001564 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001565 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Chris Lattnerb6541762007-03-04 20:40:38 +00001567 // fold (addc x, 0) -> x + no carry out
1568 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001569 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001570 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001571
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001572 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001573 APInt LHSZero, LHSOne;
1574 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001575 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001576
Dan Gohman948d8ea2008-02-20 16:33:30 +00001577 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001578 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001579
Chris Lattnerb6541762007-03-04 20:40:38 +00001580 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1581 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001582 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001583 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001584 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001585 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Dan Gohman475871a2008-07-27 21:46:04 +00001588 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001589}
1590
Dan Gohman475871a2008-07-27 21:46:04 +00001591SDValue DAGCombiner::visitADDE(SDNode *N) {
1592 SDValue N0 = N->getOperand(0);
1593 SDValue N1 = N->getOperand(1);
1594 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001595 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1596 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001597
Chris Lattner91153682007-03-04 20:03:15 +00001598 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001599 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001600 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001601 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattnerb6541762007-03-04 20:40:38 +00001603 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001604 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001605 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Dan Gohman475871a2008-07-27 21:46:04 +00001607 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001608}
1609
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001610// Since it may not be valid to emit a fold to zero for vector initializers
1611// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001612static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001613 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001614 if (!VT.isVector()) {
1615 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001616 }
1617 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001618 // Produce a vector of zeros.
1619 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1620 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1621 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1622 &Ops[0], Ops.size());
1623 }
1624 return SDValue();
1625}
1626
Dan Gohman475871a2008-07-27 21:46:04 +00001627SDValue DAGCombiner::visitSUB(SDNode *N) {
1628 SDValue N0 = N->getOperand(0);
1629 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001630 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1631 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001632 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1633 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001634 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001635
Dan Gohman7f321562007-06-25 16:23:39 +00001636 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001637 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001638 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001640
1641 // fold (sub x, 0) -> x, vector edition
1642 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1643 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001644 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001645
Chris Lattner854077d2005-10-17 01:07:11 +00001646 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001647 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001648 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001649 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001650 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001651 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001652 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001653 // fold (sub x, c) -> (add x, -c)
1654 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001655 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001656 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001657 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1658 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001659 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001660 // fold A-(A-B) -> B
1661 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1662 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001663 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001664 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001665 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001667 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001668 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001669 // fold C2-(A+C1) -> (C2-C1)-A
1670 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001671 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1672 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001673 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001674 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001675 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001676 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001677 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001678 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1679 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001680 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001681 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001682 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001683 // fold ((A+(C+B))-B) -> A+C
1684 if (N0.getOpcode() == ISD::ADD &&
1685 N0.getOperand(1).getOpcode() == ISD::ADD &&
1686 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001687 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001688 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001689 // fold ((A-(B-C))-C) -> A-B
1690 if (N0.getOpcode() == ISD::SUB &&
1691 N0.getOperand(1).getOpcode() == ISD::SUB &&
1692 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001693 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001694 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001695
Dan Gohman613e0d82007-07-03 14:03:57 +00001696 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001697 if (N0.getOpcode() == ISD::UNDEF)
1698 return N0;
1699 if (N1.getOpcode() == ISD::UNDEF)
1700 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001701
Dan Gohman6520e202008-10-18 02:06:02 +00001702 // If the relocation model supports it, consider symbol offsets.
1703 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001704 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001705 // fold (sub Sym, c) -> Sym-c
1706 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001707 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001708 GA->getOffset() -
1709 (uint64_t)N1C->getSExtValue());
1710 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1711 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1712 if (GA->getGlobal() == GB->getGlobal())
1713 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1714 VT);
1715 }
1716
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001717 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001718}
1719
Craig Toppercc274522012-01-07 09:06:39 +00001720SDValue DAGCombiner::visitSUBC(SDNode *N) {
1721 SDValue N0 = N->getOperand(0);
1722 SDValue N1 = N->getOperand(1);
1723 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1724 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1725 EVT VT = N0.getValueType();
1726
1727 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001728 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001729 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1730 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001731 MVT::Glue));
1732
1733 // fold (subc x, x) -> 0 + no borrow
1734 if (N0 == N1)
1735 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001736 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001737 MVT::Glue));
1738
1739 // fold (subc x, 0) -> x + no borrow
1740 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001741 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001742 MVT::Glue));
1743
1744 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1745 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001746 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1747 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001748 MVT::Glue));
1749
1750 return SDValue();
1751}
1752
1753SDValue DAGCombiner::visitSUBE(SDNode *N) {
1754 SDValue N0 = N->getOperand(0);
1755 SDValue N1 = N->getOperand(1);
1756 SDValue CarryIn = N->getOperand(2);
1757
1758 // fold (sube x, y, false) -> (subc x, y)
1759 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001760 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001761
1762 return SDValue();
1763}
1764
Dan Gohman475871a2008-07-27 21:46:04 +00001765SDValue DAGCombiner::visitMUL(SDNode *N) {
1766 SDValue N0 = N->getOperand(0);
1767 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001768 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1769 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001770 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Dan Gohman7f321562007-06-25 16:23:39 +00001772 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001773 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001774 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001775 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001777
Dan Gohman613e0d82007-07-03 14:03:57 +00001778 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001779 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001780 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001782 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001783 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001784 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001785 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001786 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001788 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001789 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001791 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001792 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001793 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001795 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001796 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001797 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001798 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001799 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001800 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1801 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001803 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001804 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001805 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001806 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001807 DAG.getConstant(Log2Val,
1808 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001809 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001810 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001811 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001812 isa<ConstantSDNode>(N0.getOperand(1))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001813 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001814 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001815 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001816 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001817 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001818 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001819
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001820 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1821 // use.
1822 {
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001824 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1825 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001826 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001827 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001828 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001829 isa<ConstantSDNode>(N1.getOperand(1)) &&
1830 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001831 Sh = N1; Y = N0;
1832 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001833
Gabor Greifba36cb52008-08-28 21:40:38 +00001834 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001835 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001836 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001837 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001838 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001839 }
1840 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001841
Chris Lattnera1deca32006-03-04 23:33:26 +00001842 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001843 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001844 isa<ConstantSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001845 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1846 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001847 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001848 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001849 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001850
Nate Begemancd4d58c2006-02-03 06:46:56 +00001851 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001852 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001853 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001854 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001855
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001856 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857}
1858
Dan Gohman475871a2008-07-27 21:46:04 +00001859SDValue DAGCombiner::visitSDIV(SDNode *N) {
1860 SDValue N0 = N->getOperand(0);
1861 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001862 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1863 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001864 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865
Dan Gohman7f321562007-06-25 16:23:39 +00001866 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001867 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001868 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Nate Begeman1d4d4142005-09-01 00:19:25 +00001872 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001873 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001874 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001875 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001876 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001877 return N0;
1878 // fold (sdiv X, -1) -> 0-X
1879 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001880 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001881 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001882 // If we know the sign bits of both operands are zero, strength reduce to a
1883 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001884 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001885 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001887 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001888 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001889 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001890 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001891 (N1C->getAPIntValue().isPowerOf2() ||
1892 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001893 // If dividing by powers of two is cheap, then don't perform the following
1894 // fold.
1895 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001896 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001897
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001898 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001899
Chris Lattner8f4880b2006-02-16 08:02:36 +00001900 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001901 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001902 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001903 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001905
Chris Lattner8f4880b2006-02-16 08:02:36 +00001906 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001907 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001908 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001909 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001910 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001911 AddToWorkList(SRL.getNode());
1912 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001913 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001914 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001915
Nate Begeman405e3ec2005-10-21 00:02:42 +00001916 // If we're dividing by a positive value, we're done. Otherwise, we must
1917 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001918 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001919 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001920
Gabor Greifba36cb52008-08-28 21:40:38 +00001921 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001922 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001923 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001924 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001925
Nate Begeman69575232005-10-20 02:15:44 +00001926 // if integer divide is expensive and we satisfy the requirements, emit an
1927 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001928 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001930 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001931 }
Dan Gohman7f321562007-06-25 16:23:39 +00001932
Dan Gohman613e0d82007-07-03 14:03:57 +00001933 // undef / X -> 0
1934 if (N0.getOpcode() == ISD::UNDEF)
1935 return DAG.getConstant(0, VT);
1936 // X / undef -> undef
1937 if (N1.getOpcode() == ISD::UNDEF)
1938 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001939
Dan Gohman475871a2008-07-27 21:46:04 +00001940 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941}
1942
Dan Gohman475871a2008-07-27 21:46:04 +00001943SDValue DAGCombiner::visitUDIV(SDNode *N) {
1944 SDValue N0 = N->getOperand(0);
1945 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001946 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001948 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001949
Dan Gohman7f321562007-06-25 16:23:39 +00001950 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001951 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001952 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001953 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001954 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001955
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001957 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001958 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001960 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001961 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001962 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001963 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001964 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001965 if (N1.getOpcode() == ISD::SHL) {
1966 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001967 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001968 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001969 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00001970 N1.getOperand(1),
1971 DAG.getConstant(SHC->getAPIntValue()
1972 .logBase2(),
1973 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001974 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001975 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001976 }
1977 }
1978 }
Nate Begeman69575232005-10-20 02:15:44 +00001979 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001980 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001982 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001983 }
Dan Gohman7f321562007-06-25 16:23:39 +00001984
Dan Gohman613e0d82007-07-03 14:03:57 +00001985 // undef / X -> 0
1986 if (N0.getOpcode() == ISD::UNDEF)
1987 return DAG.getConstant(0, VT);
1988 // X / undef -> undef
1989 if (N1.getOpcode() == ISD::UNDEF)
1990 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001991
Dan Gohman475871a2008-07-27 21:46:04 +00001992 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993}
1994
Dan Gohman475871a2008-07-27 21:46:04 +00001995SDValue DAGCombiner::visitSREM(SDNode *N) {
1996 SDValue N0 = N->getOperand(0);
1997 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001998 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002000 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002001
Nate Begeman1d4d4142005-09-01 00:19:25 +00002002 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002003 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002004 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002005 // If we know the sign bits of both operands are zero, strength reduce to a
2006 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002007 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002008 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002009 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002010 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Dan Gohman77003042007-11-26 23:46:11 +00002012 // If X/C can be simplified by the division-by-constant logic, lower
2013 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002014 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002015 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002016 AddToWorkList(Div.getNode());
2017 SDValue OptimizedDiv = combine(Div.getNode());
2018 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002019 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002020 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002021 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002022 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002023 return Sub;
2024 }
Chris Lattner26d29902006-10-12 20:58:32 +00002025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002026
Dan Gohman613e0d82007-07-03 14:03:57 +00002027 // undef % X -> 0
2028 if (N0.getOpcode() == ISD::UNDEF)
2029 return DAG.getConstant(0, VT);
2030 // X % undef -> undef
2031 if (N1.getOpcode() == ISD::UNDEF)
2032 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002033
Dan Gohman475871a2008-07-27 21:46:04 +00002034 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002035}
2036
Dan Gohman475871a2008-07-27 21:46:04 +00002037SDValue DAGCombiner::visitUREM(SDNode *N) {
2038 SDValue N0 = N->getOperand(0);
2039 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002040 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2041 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002042 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002043
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002045 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002046 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002047 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002048 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002049 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002050 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002051 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2052 if (N1.getOpcode() == ISD::SHL) {
2053 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002054 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002055 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002056 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002058 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002059 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002060 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002061 }
2062 }
2063 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002064
Dan Gohman77003042007-11-26 23:46:11 +00002065 // If X/C can be simplified by the division-by-constant logic, lower
2066 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002067 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002068 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002069 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 SDValue OptimizedDiv = combine(Div.getNode());
2071 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002072 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002073 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002074 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002075 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002076 return Sub;
2077 }
Chris Lattner26d29902006-10-12 20:58:32 +00002078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002079
Dan Gohman613e0d82007-07-03 14:03:57 +00002080 // undef % X -> 0
2081 if (N0.getOpcode() == ISD::UNDEF)
2082 return DAG.getConstant(0, VT);
2083 // X % undef -> undef
2084 if (N1.getOpcode() == ISD::UNDEF)
2085 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002086
Dan Gohman475871a2008-07-27 21:46:04 +00002087 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088}
2089
Dan Gohman475871a2008-07-27 21:46:04 +00002090SDValue DAGCombiner::visitMULHS(SDNode *N) {
2091 SDValue N0 = N->getOperand(0);
2092 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002093 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002094 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002095 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002098 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002099 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002101 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002102 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002103 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002104 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002105 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002106 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002107 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002108
Chris Lattnerde1c3602010-12-13 08:39:01 +00002109 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2110 // plus a shift.
2111 if (VT.isSimple() && !VT.isVector()) {
2112 MVT Simple = VT.getSimpleVT();
2113 unsigned SimpleSize = Simple.getSizeInBits();
2114 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2115 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2116 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2117 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2118 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002119 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002120 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002121 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2122 }
2123 }
Owen Anderson95771af2011-02-25 21:41:48 +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::visitMULHU(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 (mulhu 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 (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002139 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002140 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002141 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002142 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002143 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002144
Chris Lattnerde1c3602010-12-13 08:39:01 +00002145 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2146 // plus a shift.
2147 if (VT.isSimple() && !VT.isVector()) {
2148 MVT Simple = VT.getSimpleVT();
2149 unsigned SimpleSize = Simple.getSizeInBits();
2150 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2151 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2152 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2153 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2154 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2155 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002156 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002157 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2158 }
2159 }
Owen Anderson95771af2011-02-25 21:41:48 +00002160
Dan Gohman475871a2008-07-27 21:46:04 +00002161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162}
2163
Dan Gohman389079b2007-10-08 17:57:15 +00002164/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2165/// compute two values. LoOp and HiOp give the opcodes for the two computations
2166/// that are being performed. Return true if a simplification was made.
2167///
Scott Michelfdc40a02009-02-17 22:15:04 +00002168SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002169 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002170 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002171 bool HiExists = N->hasAnyUseOfValue(1);
2172 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002173 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002174 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002175 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002176 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002177 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002178 }
2179
2180 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002181 bool LoExists = N->hasAnyUseOfValue(0);
2182 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002183 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002184 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002185 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002186 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002187 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002188 }
2189
Evan Cheng44711942007-11-08 09:25:29 +00002190 // If both halves are used, return as it is.
2191 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002192 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002193
2194 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002195 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002196 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002197 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002198 AddToWorkList(Lo.getNode());
2199 SDValue LoOpt = combine(Lo.getNode());
2200 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002201 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002202 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002203 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002204 }
2205
Evan Cheng44711942007-11-08 09:25:29 +00002206 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002207 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002208 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 AddToWorkList(Hi.getNode());
2210 SDValue HiOpt = combine(Hi.getNode());
2211 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002212 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002213 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002214 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002215 }
Bill Wendling826d1142009-01-30 03:08:40 +00002216
Dan Gohman475871a2008-07-27 21:46:04 +00002217 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002218}
2219
Dan Gohman475871a2008-07-27 21:46:04 +00002220SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2221 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002222 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002223
Chris Lattner33e77d32010-12-15 06:04:19 +00002224 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002225 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002226
2227 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2228 // plus a shift.
2229 if (VT.isSimple() && !VT.isVector()) {
2230 MVT Simple = VT.getSimpleVT();
2231 unsigned SimpleSize = Simple.getSizeInBits();
2232 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2233 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2234 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2235 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2236 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2237 // Compute the high part as N1.
2238 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002239 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002240 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2241 // Compute the low part as N0.
2242 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2243 return CombineTo(N, Lo, Hi);
2244 }
2245 }
Owen Anderson95771af2011-02-25 21:41:48 +00002246
Dan Gohman475871a2008-07-27 21:46:04 +00002247 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002248}
2249
Dan Gohman475871a2008-07-27 21:46:04 +00002250SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2251 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002252 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002253
Chris Lattner33e77d32010-12-15 06:04:19 +00002254 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002255 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002256
Chris Lattner33e77d32010-12-15 06:04:19 +00002257 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2258 // plus a shift.
2259 if (VT.isSimple() && !VT.isVector()) {
2260 MVT Simple = VT.getSimpleVT();
2261 unsigned SimpleSize = Simple.getSizeInBits();
2262 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2263 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2264 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2265 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2266 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2267 // Compute the high part as N1.
2268 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002269 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002270 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2271 // Compute the low part as N0.
2272 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2273 return CombineTo(N, Lo, Hi);
2274 }
2275 }
Owen Anderson95771af2011-02-25 21:41:48 +00002276
Dan Gohman475871a2008-07-27 21:46:04 +00002277 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002278}
2279
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002280SDValue DAGCombiner::visitSMULO(SDNode *N) {
2281 // (smulo x, 2) -> (saddo x, x)
2282 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2283 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002284 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002285 N->getOperand(0), N->getOperand(0));
2286
2287 return SDValue();
2288}
2289
2290SDValue DAGCombiner::visitUMULO(SDNode *N) {
2291 // (umulo x, 2) -> (uaddo x, x)
2292 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2293 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002294 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002295 N->getOperand(0), N->getOperand(0));
2296
2297 return SDValue();
2298}
2299
Dan Gohman475871a2008-07-27 21:46:04 +00002300SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2301 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002302 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002303
Dan Gohman475871a2008-07-27 21:46:04 +00002304 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002305}
2306
Dan Gohman475871a2008-07-27 21:46:04 +00002307SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2308 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002309 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002310
Dan Gohman475871a2008-07-27 21:46:04 +00002311 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002312}
2313
Chris Lattner35e5c142006-05-05 05:51:50 +00002314/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2315/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002316SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2317 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002318 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002319 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Dan Gohmanff00a552010-01-14 03:08:49 +00002321 // Bail early if none of these transforms apply.
2322 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2323
Chris Lattner540121f2006-05-05 06:31:05 +00002324 // For each of OP in AND/OR/XOR:
2325 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2326 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2327 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002328 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002329 //
2330 // do not sink logical op inside of a vector extend, since it may combine
2331 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002332 EVT Op0VT = N0.getOperand(0).getValueType();
2333 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002334 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002335 // Avoid infinite looping with PromoteIntBinOp.
2336 (N0.getOpcode() == ISD::ANY_EXTEND &&
2337 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002338 (N0.getOpcode() == ISD::TRUNCATE &&
2339 (!TLI.isZExtFree(VT, Op0VT) ||
2340 !TLI.isTruncateFree(Op0VT, VT)) &&
2341 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002342 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002343 Op0VT == N1.getOperand(0).getValueType() &&
2344 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002345 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002346 N0.getOperand(0).getValueType(),
2347 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002349 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002351
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002352 // For each of OP in SHL/SRL/SRA/AND...
2353 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2354 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2355 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002356 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002357 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002358 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002359 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002360 N0.getOperand(0).getValueType(),
2361 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002362 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002363 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002364 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002365 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Nadav Rotem4ac90812012-04-01 19:31:22 +00002367 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2368 // Only perform this optimization after type legalization and before
2369 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2370 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2371 // we don't want to undo this promotion.
2372 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2373 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002374 if ((N0.getOpcode() == ISD::BITCAST ||
2375 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2376 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002377 SDValue In0 = N0.getOperand(0);
2378 SDValue In1 = N1.getOperand(0);
2379 EVT In0Ty = In0.getValueType();
2380 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002381 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002382 // If both incoming values are integers, and the original types are the
2383 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002384 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002385 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2386 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002387 AddToWorkList(Op.getNode());
2388 return BC;
2389 }
2390 }
2391
2392 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2393 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2394 // If both shuffles use the same mask, and both shuffle within a single
2395 // vector, then it is worthwhile to move the swizzle after the operation.
2396 // The type-legalizer generates this pattern when loading illegal
2397 // vector types from memory. In many cases this allows additional shuffle
2398 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002399 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2400 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2401 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002402 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2403 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002404
2405 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2406 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002407
2408 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002409
2410 // Check that both shuffles use the same mask. The masks are known to be of
2411 // the same length because the result vector type is the same.
2412 bool SameMask = true;
2413 for (unsigned i = 0; i != NumElts; ++i) {
2414 int Idx0 = SVN0->getMaskElt(i);
2415 int Idx1 = SVN1->getMaskElt(i);
2416 if (Idx0 != Idx1) {
2417 SameMask = false;
2418 break;
2419 }
2420 }
2421
Craig Topperf9204232012-04-09 07:19:09 +00002422 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002423 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002424 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002425 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002426 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002427 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002428 }
2429 }
Craig Topperf9204232012-04-09 07:19:09 +00002430
Dan Gohman475871a2008-07-27 21:46:04 +00002431 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002432}
2433
Dan Gohman475871a2008-07-27 21:46:04 +00002434SDValue DAGCombiner::visitAND(SDNode *N) {
2435 SDValue N0 = N->getOperand(0);
2436 SDValue N1 = N->getOperand(1);
2437 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002438 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002440 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002441 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002442
Dan Gohman7f321562007-06-25 16:23:39 +00002443 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002444 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002445 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002446 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002447
2448 // fold (and x, 0) -> 0, vector edition
2449 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2450 return N0;
2451 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2452 return N1;
2453
2454 // fold (and x, -1) -> x, vector edition
2455 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2456 return N1;
2457 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2458 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Dan Gohman613e0d82007-07-03 14:03:57 +00002461 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002462 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002463 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002466 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002467 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002468 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002469 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002470 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002471 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002472 return N0;
2473 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002474 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002475 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002476 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002477 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002478 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002479 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002480 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002481 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002482 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002484 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002486 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2487 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002488 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002489 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002490 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002491 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002492 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002493 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002494
Chris Lattner1ec05d12006-03-01 21:47:21 +00002495 // Replace uses of the AND with uses of the Zero extend node.
2496 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Chris Lattner3603cd62006-02-02 07:17:31 +00002498 // We actually want to replace all uses of the any_extend with the
2499 // zero_extend, to avoid duplicating things. This will later cause this
2500 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002501 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002502 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002503 }
2504 }
James Molloy6259dcd2012-02-20 12:02:38 +00002505 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2506 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2507 // already be zero by virtue of the width of the base type of the load.
2508 //
2509 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2510 // more cases.
2511 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2512 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2513 N0.getOpcode() == ISD::LOAD) {
2514 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2515 N0 : N0.getOperand(0) );
2516
2517 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2518 // This can be a pure constant or a vector splat, in which case we treat the
2519 // vector as a scalar and use the splat value.
2520 APInt Constant = APInt::getNullValue(1);
2521 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2522 Constant = C->getAPIntValue();
2523 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2524 APInt SplatValue, SplatUndef;
2525 unsigned SplatBitSize;
2526 bool HasAnyUndefs;
2527 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2528 SplatBitSize, HasAnyUndefs);
2529 if (IsSplat) {
2530 // Undef bits can contribute to a possible optimisation if set, so
2531 // set them.
2532 SplatValue |= SplatUndef;
2533
2534 // The splat value may be something like "0x00FFFFFF", which means 0 for
2535 // the first vector value and FF for the rest, repeating. We need a mask
2536 // that will apply equally to all members of the vector, so AND all the
2537 // lanes of the constant together.
2538 EVT VT = Vector->getValueType(0);
2539 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002540
2541 // If the splat value has been compressed to a bitlength lower
2542 // than the size of the vector lane, we need to re-expand it to
2543 // the lane size.
2544 if (BitWidth > SplatBitSize)
2545 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2546 SplatBitSize < BitWidth;
2547 SplatBitSize = SplatBitSize * 2)
2548 SplatValue |= SplatValue.shl(SplatBitSize);
2549
James Molloy6259dcd2012-02-20 12:02:38 +00002550 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002551 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002552 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2553 }
2554 }
2555
2556 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2557 // actually legal and isn't going to get expanded, else this is a false
2558 // optimisation.
2559 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2560 Load->getMemoryVT());
2561
2562 // Resize the constant to the same size as the original memory access before
2563 // extension. If it is still the AllOnesValue then this AND is completely
2564 // unneeded.
2565 Constant =
2566 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2567
2568 bool B;
2569 switch (Load->getExtensionType()) {
2570 default: B = false; break;
2571 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2572 case ISD::ZEXTLOAD:
2573 case ISD::NON_EXTLOAD: B = true; break;
2574 }
2575
2576 if (B && Constant.isAllOnesValue()) {
2577 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2578 // preserve semantics once we get rid of the AND.
2579 SDValue NewLoad(Load, 0);
2580 if (Load->getExtensionType() == ISD::EXTLOAD) {
2581 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002582 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002583 Load->getChain(), Load->getBasePtr(),
2584 Load->getOffset(), Load->getMemoryVT(),
2585 Load->getMemOperand());
2586 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002587 if (Load->getNumValues() == 3) {
2588 // PRE/POST_INC loads have 3 values.
2589 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2590 NewLoad.getValue(2) };
2591 CombineTo(Load, To, 3, true);
2592 } else {
2593 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2594 }
James Molloy6259dcd2012-02-20 12:02:38 +00002595 }
2596
2597 // Fold the AND away, taking care not to fold to the old load node if we
2598 // replaced it.
2599 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2600
2601 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2602 }
2603 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002604 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2605 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2606 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2607 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002608
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002609 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002611 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002612 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002613 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002614 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002615 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002616 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002617 }
Bill Wendling2627a882009-01-30 20:43:18 +00002618 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002619 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002620 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002621 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002622 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002623 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002624 }
Bill Wendling2627a882009-01-30 20:43:18 +00002625 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002626 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002627 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002628 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002629 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002630 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002631 }
2632 }
2633 // canonicalize equivalent to ll == rl
2634 if (LL == RR && LR == RL) {
2635 Op1 = ISD::getSetCCSwappedOperands(Op1);
2636 std::swap(RL, RR);
2637 }
2638 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002639 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002640 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002641 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002642 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002643 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2644 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002645 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002646 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002647 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002648 }
2649 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002650
Bill Wendling2627a882009-01-30 20:43:18 +00002651 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002652 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002653 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002656
Nate Begemande996292006-02-03 22:24:05 +00002657 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2658 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002660 SimplifyDemandedBits(SDValue(N, 0)))
2661 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002662
Nate Begemanded49632005-10-13 03:11:28 +00002663 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002664 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002665 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002666 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002667 // If we zero all the possible extended bits, then we can turn this into
2668 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002669 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002670 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002671 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002672 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002673 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002674 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002675 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002676 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002677 LN0->isVolatile(), LN0->isNonTemporal(),
2678 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002679 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002680 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002681 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002682 }
2683 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002684 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002685 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002686 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002687 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002688 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002689 // If we zero all the possible extended bits, then we can turn this into
2690 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002691 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002692 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002693 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002694 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002695 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002696 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002697 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002698 LN0->getBasePtr(), LN0->getPointerInfo(),
2699 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002700 LN0->isVolatile(), LN0->isNonTemporal(),
2701 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002702 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002703 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002704 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002705 }
2706 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002707
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002708 // fold (and (load x), 255) -> (zextload x, i8)
2709 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002710 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2711 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2712 (N0.getOpcode() == ISD::ANY_EXTEND &&
2713 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2714 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2715 LoadSDNode *LN0 = HasAnyExt
2716 ? cast<LoadSDNode>(N0.getOperand(0))
2717 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002718 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002719 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002720 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002721 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2722 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2723 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002724
Evan Chengd40d03e2010-01-06 19:38:29 +00002725 if (ExtVT == LoadedVT &&
2726 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002727 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002728
2729 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002730 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002731 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002732 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002733 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2734 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002735 AddToWorkList(N);
2736 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2737 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2738 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002739
Chris Lattneref7634c2010-01-07 21:53:27 +00002740 // Do not change the width of a volatile load.
2741 // Do not generate loads of non-round integer types since these can
2742 // be expensive (and would be wrong if the type is not byte sized).
2743 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2744 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2745 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002746
Chris Lattneref7634c2010-01-07 21:53:27 +00002747 unsigned Alignment = LN0->getAlignment();
2748 SDValue NewPtr = LN0->getBasePtr();
2749
2750 // For big endian targets, we need to add an offset to the pointer
2751 // to load the correct bytes. For little endian systems, we merely
2752 // need to read fewer bytes from the same pointer.
2753 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002754 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2755 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2756 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002757 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002758 NewPtr, DAG.getConstant(PtrOff, PtrType));
2759 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002760 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002761
2762 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002763
Chris Lattneref7634c2010-01-07 21:53:27 +00002764 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2765 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002766 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002767 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002768 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002769 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2770 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002771 AddToWorkList(N);
2772 CombineTo(LN0, Load, Load.getValue(1));
2773 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002774 }
Evan Cheng466685d2006-10-09 20:57:25 +00002775 }
Chris Lattner15045b62006-02-28 06:35:35 +00002776 }
2777 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002778
Evan Chenga9e13ba2012-07-17 18:54:11 +00002779 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2780 VT.getSizeInBits() <= 64) {
2781 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2782 APInt ADDC = ADDI->getAPIntValue();
2783 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2784 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2785 // immediate for an add, but it is legal if its top c2 bits are set,
2786 // transform the ADD so the immediate doesn't need to be materialized
2787 // in a register.
2788 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2789 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2790 SRLI->getZExtValue());
2791 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2792 ADDC |= Mask;
2793 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2794 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002795 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002796 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2797 CombineTo(N0.getNode(), NewAdd);
2798 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2799 }
2800 }
2801 }
2802 }
2803 }
2804 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002805
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002806 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002807}
2808
Evan Cheng9568e5c2011-06-21 06:01:08 +00002809/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2810///
2811SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2812 bool DemandHighBits) {
2813 if (!LegalOperations)
2814 return SDValue();
2815
2816 EVT VT = N->getValueType(0);
2817 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2818 return SDValue();
2819 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2820 return SDValue();
2821
2822 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2823 bool LookPassAnd0 = false;
2824 bool LookPassAnd1 = false;
2825 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2826 std::swap(N0, N1);
2827 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2828 std::swap(N0, N1);
2829 if (N0.getOpcode() == ISD::AND) {
2830 if (!N0.getNode()->hasOneUse())
2831 return SDValue();
2832 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2833 if (!N01C || N01C->getZExtValue() != 0xFF00)
2834 return SDValue();
2835 N0 = N0.getOperand(0);
2836 LookPassAnd0 = true;
2837 }
2838
2839 if (N1.getOpcode() == ISD::AND) {
2840 if (!N1.getNode()->hasOneUse())
2841 return SDValue();
2842 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2843 if (!N11C || N11C->getZExtValue() != 0xFF)
2844 return SDValue();
2845 N1 = N1.getOperand(0);
2846 LookPassAnd1 = true;
2847 }
2848
2849 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2850 std::swap(N0, N1);
2851 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2852 return SDValue();
2853 if (!N0.getNode()->hasOneUse() ||
2854 !N1.getNode()->hasOneUse())
2855 return SDValue();
2856
2857 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2858 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2859 if (!N01C || !N11C)
2860 return SDValue();
2861 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2862 return SDValue();
2863
2864 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2865 SDValue N00 = N0->getOperand(0);
2866 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2867 if (!N00.getNode()->hasOneUse())
2868 return SDValue();
2869 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2870 if (!N001C || N001C->getZExtValue() != 0xFF)
2871 return SDValue();
2872 N00 = N00.getOperand(0);
2873 LookPassAnd0 = true;
2874 }
2875
2876 SDValue N10 = N1->getOperand(0);
2877 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2878 if (!N10.getNode()->hasOneUse())
2879 return SDValue();
2880 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2881 if (!N101C || N101C->getZExtValue() != 0xFF00)
2882 return SDValue();
2883 N10 = N10.getOperand(0);
2884 LookPassAnd1 = true;
2885 }
2886
2887 if (N00 != N10)
2888 return SDValue();
2889
2890 // Make sure everything beyond the low halfword is zero since the SRL 16
2891 // will clear the top bits.
2892 unsigned OpSizeInBits = VT.getSizeInBits();
2893 if (DemandHighBits && OpSizeInBits > 16 &&
2894 (!LookPassAnd0 || !LookPassAnd1) &&
2895 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2896 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002897
Andrew Trickac6d9be2013-05-25 02:42:55 +00002898 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002899 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002900 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002901 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2902 return Res;
2903}
2904
2905/// isBSwapHWordElement - Return true if the specified node is an element
2906/// that makes up a 32-bit packed halfword byteswap. i.e.
2907/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2908static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2909 if (!N.getNode()->hasOneUse())
2910 return false;
2911
2912 unsigned Opc = N.getOpcode();
2913 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2914 return false;
2915
2916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2917 if (!N1C)
2918 return false;
2919
2920 unsigned Num;
2921 switch (N1C->getZExtValue()) {
2922 default:
2923 return false;
2924 case 0xFF: Num = 0; break;
2925 case 0xFF00: Num = 1; break;
2926 case 0xFF0000: Num = 2; break;
2927 case 0xFF000000: Num = 3; break;
2928 }
2929
2930 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2931 SDValue N0 = N.getOperand(0);
2932 if (Opc == ISD::AND) {
2933 if (Num == 0 || Num == 2) {
2934 // (x >> 8) & 0xff
2935 // (x >> 8) & 0xff0000
2936 if (N0.getOpcode() != ISD::SRL)
2937 return false;
2938 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2939 if (!C || C->getZExtValue() != 8)
2940 return false;
2941 } else {
2942 // (x << 8) & 0xff00
2943 // (x << 8) & 0xff000000
2944 if (N0.getOpcode() != ISD::SHL)
2945 return false;
2946 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2947 if (!C || C->getZExtValue() != 8)
2948 return false;
2949 }
2950 } else if (Opc == ISD::SHL) {
2951 // (x & 0xff) << 8
2952 // (x & 0xff0000) << 8
2953 if (Num != 0 && Num != 2)
2954 return false;
2955 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2956 if (!C || C->getZExtValue() != 8)
2957 return false;
2958 } else { // Opc == ISD::SRL
2959 // (x & 0xff00) >> 8
2960 // (x & 0xff000000) >> 8
2961 if (Num != 1 && Num != 3)
2962 return false;
2963 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2964 if (!C || C->getZExtValue() != 8)
2965 return false;
2966 }
2967
2968 if (Parts[Num])
2969 return false;
2970
2971 Parts[Num] = N0.getOperand(0).getNode();
2972 return true;
2973}
2974
2975/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2976/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2977/// => (rotl (bswap x), 16)
2978SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2979 if (!LegalOperations)
2980 return SDValue();
2981
2982 EVT VT = N->getValueType(0);
2983 if (VT != MVT::i32)
2984 return SDValue();
2985 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2986 return SDValue();
2987
2988 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2989 // Look for either
2990 // (or (or (and), (and)), (or (and), (and)))
2991 // (or (or (or (and), (and)), (and)), (and))
2992 if (N0.getOpcode() != ISD::OR)
2993 return SDValue();
2994 SDValue N00 = N0.getOperand(0);
2995 SDValue N01 = N0.getOperand(1);
2996
Evan Cheng9a65a012012-12-13 01:34:32 +00002997 if (N1.getOpcode() == ISD::OR &&
2998 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002999 // (or (or (and), (and)), (or (and), (and)))
3000 SDValue N000 = N00.getOperand(0);
3001 if (!isBSwapHWordElement(N000, Parts))
3002 return SDValue();
3003
3004 SDValue N001 = N00.getOperand(1);
3005 if (!isBSwapHWordElement(N001, Parts))
3006 return SDValue();
3007 SDValue N010 = N01.getOperand(0);
3008 if (!isBSwapHWordElement(N010, Parts))
3009 return SDValue();
3010 SDValue N011 = N01.getOperand(1);
3011 if (!isBSwapHWordElement(N011, Parts))
3012 return SDValue();
3013 } else {
3014 // (or (or (or (and), (and)), (and)), (and))
3015 if (!isBSwapHWordElement(N1, Parts))
3016 return SDValue();
3017 if (!isBSwapHWordElement(N01, Parts))
3018 return SDValue();
3019 if (N00.getOpcode() != ISD::OR)
3020 return SDValue();
3021 SDValue N000 = N00.getOperand(0);
3022 if (!isBSwapHWordElement(N000, Parts))
3023 return SDValue();
3024 SDValue N001 = N00.getOperand(1);
3025 if (!isBSwapHWordElement(N001, Parts))
3026 return SDValue();
3027 }
3028
3029 // Make sure the parts are all coming from the same node.
3030 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3031 return SDValue();
3032
Andrew Trickac6d9be2013-05-25 02:42:55 +00003033 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003034 SDValue(Parts[0],0));
3035
3036 // Result of the bswap should be rotated by 16. If it's not legal, than
3037 // do (x << 16) | (x >> 16).
3038 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3039 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003040 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003041 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003042 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3043 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3044 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3045 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003046}
3047
Dan Gohman475871a2008-07-27 21:46:04 +00003048SDValue DAGCombiner::visitOR(SDNode *N) {
3049 SDValue N0 = N->getOperand(0);
3050 SDValue N1 = N->getOperand(1);
3051 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003052 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3053 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003054 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003055
Dan Gohman7f321562007-06-25 16:23:39 +00003056 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003057 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003058 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003059 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003060
3061 // fold (or x, 0) -> x, vector edition
3062 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3063 return N1;
3064 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3065 return N0;
3066
3067 // fold (or x, -1) -> -1, vector edition
3068 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3069 return N0;
3070 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3071 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003073
Dan Gohman613e0d82007-07-03 14:03:57 +00003074 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003075 if (!LegalOperations &&
3076 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003077 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3078 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3079 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003080 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003081 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003082 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003083 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003084 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003085 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003086 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003087 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003088 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003089 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003090 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003091 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003092 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003093 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003094 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003095
3096 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3097 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3098 if (BSwap.getNode() != 0)
3099 return BSwap;
3100 BSwap = MatchBSwapHWordLow(N, N0, N1);
3101 if (BSwap.getNode() != 0)
3102 return BSwap;
3103
Nate Begemancd4d58c2006-02-03 06:46:56 +00003104 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003105 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003106 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003107 return ROR;
3108 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003109 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003110 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003111 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003112 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003113 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003114 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3115 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003116 N0.getOperand(0), N1),
3117 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003118 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003119 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3120 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3121 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3122 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003123
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003124 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003125 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003126 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3127 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003128 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003129 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003130 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003131 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003132 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003133 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003134 }
Bill Wendling09025642009-01-30 20:59:34 +00003135 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3136 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003137 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003138 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003139 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003140 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003141 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003142 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003143 }
3144 }
3145 // canonicalize equivalent to ll == rl
3146 if (LL == RR && LR == RL) {
3147 Op1 = ISD::getSetCCSwappedOperands(Op1);
3148 std::swap(RL, RR);
3149 }
3150 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003152 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003153 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003154 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003155 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3156 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003157 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003158 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003159 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003160 }
3161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003162
Bill Wendling09025642009-01-30 20:59:34 +00003163 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003164 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003165 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003166 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Bill Wendling09025642009-01-30 20:59:34 +00003169 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003170 if (N0.getOpcode() == ISD::AND &&
3171 N1.getOpcode() == ISD::AND &&
3172 N0.getOperand(1).getOpcode() == ISD::Constant &&
3173 N1.getOperand(1).getOpcode() == ISD::Constant &&
3174 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003175 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003176 // We can only do this xform if we know that bits from X that are set in C2
3177 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003178 const APInt &LHSMask =
3179 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3180 const APInt &RHSMask =
3181 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003182
Dan Gohmanea859be2007-06-22 14:59:07 +00003183 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3184 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003185 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003186 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003187 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003188 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003189 }
3190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003191
Chris Lattner516b9622006-09-14 20:50:57 +00003192 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003193 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003194 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003195
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003196 // Simplify the operands using demanded-bits information.
3197 if (!VT.isVector() &&
3198 SimplifyDemandedBits(SDValue(N, 0)))
3199 return SDValue(N, 0);
3200
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003201 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003202}
3203
Chris Lattner516b9622006-09-14 20:50:57 +00003204/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003205static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003206 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003207 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003208 Mask = Op.getOperand(1);
3209 Op = Op.getOperand(0);
3210 } else {
3211 return false;
3212 }
3213 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003214
Chris Lattner516b9622006-09-14 20:50:57 +00003215 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3216 Shift = Op;
3217 return true;
3218 }
Bill Wendling09025642009-01-30 20:59:34 +00003219
Scott Michelfdc40a02009-02-17 22:15:04 +00003220 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003221}
3222
Chris Lattner516b9622006-09-14 20:50:57 +00003223// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3224// idioms for rotate, and if the target supports rotation instructions, generate
3225// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003226SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003227 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003228 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003229 if (!TLI.isTypeLegal(VT)) return 0;
3230
3231 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003232 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3233 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003234 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003235
Chris Lattner516b9622006-09-14 20:50:57 +00003236 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003237 SDValue LHSShift; // The shift.
3238 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003239 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3240 return 0; // Not part of a rotate.
3241
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue RHSShift; // The shift.
3243 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003244 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3245 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003246
Chris Lattner516b9622006-09-14 20:50:57 +00003247 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3248 return 0; // Not shifting the same value.
3249
3250 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3251 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003252
Chris Lattner516b9622006-09-14 20:50:57 +00003253 // Canonicalize shl to left side in a shl/srl pair.
3254 if (RHSShift.getOpcode() == ISD::SHL) {
3255 std::swap(LHS, RHS);
3256 std::swap(LHSShift, RHSShift);
3257 std::swap(LHSMask , RHSMask );
3258 }
3259
Duncan Sands83ec4b62008-06-06 12:08:01 +00003260 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003261 SDValue LHSShiftArg = LHSShift.getOperand(0);
3262 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3263 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003264
3265 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3266 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003267 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3268 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003269 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3270 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003271 if ((LShVal + RShVal) != OpSizeInBits)
3272 return 0;
3273
Craig Topper32b73432012-09-29 06:54:22 +00003274 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3275 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003276
Chris Lattner516b9622006-09-14 20:50:57 +00003277 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003278 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003279 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003280
Gabor Greifba36cb52008-08-28 21:40:38 +00003281 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003282 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3283 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003284 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003285 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003286 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3287 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003289
Bill Wendling317bd702009-01-30 21:14:50 +00003290 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003291 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003292
Gabor Greifba36cb52008-08-28 21:40:38 +00003293 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003295
Chris Lattner516b9622006-09-14 20:50:57 +00003296 // If there is a mask here, and we have a variable shift, we can't be sure
3297 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003298 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003299 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003300
Chris Lattner516b9622006-09-14 20:50:57 +00003301 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3302 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003303 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3304 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003305 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003306 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003307 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003308 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3309 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003310 }
Chris Lattner516b9622006-09-14 20:50:57 +00003311 }
3312 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003313
Chris Lattner516b9622006-09-14 20:50:57 +00003314 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3315 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003316 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3317 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003318 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003319 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003320 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003321 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3322 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003323 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003324 }
3325 }
3326
Dan Gohman74feef22008-10-17 01:23:35 +00003327 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003328 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3329 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3330 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3331 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3332 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3333 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3334 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3335 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003336 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3337 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003338 if (RExtOp0.getOpcode() == ISD::SUB &&
3339 RExtOp0.getOperand(1) == LExtOp0) {
3340 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003341 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003342 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003343 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003344 if (ConstantSDNode *SUBC =
3345 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003346 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003347 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3348 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003349 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003350 }
3351 }
3352 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3353 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003354 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003355 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003356 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003357 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003358 if (ConstantSDNode *SUBC =
3359 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003360 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003361 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3362 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003363 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003364 }
3365 }
Chris Lattner516b9622006-09-14 20:50:57 +00003366 }
3367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003368
Chris Lattner516b9622006-09-14 20:50:57 +00003369 return 0;
3370}
3371
Dan Gohman475871a2008-07-27 21:46:04 +00003372SDValue DAGCombiner::visitXOR(SDNode *N) {
3373 SDValue N0 = N->getOperand(0);
3374 SDValue N1 = N->getOperand(1);
3375 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003376 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3377 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003378 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003379
Dan Gohman7f321562007-06-25 16:23:39 +00003380 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003381 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003383 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003384
3385 // fold (xor x, 0) -> x, vector edition
3386 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3387 return N1;
3388 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3389 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003390 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003391
Evan Cheng26471c42008-03-25 20:08:07 +00003392 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3393 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3394 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003395 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003396 if (N0.getOpcode() == ISD::UNDEF)
3397 return N0;
3398 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003399 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003401 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003402 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003403 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003404 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003405 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003406 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003407 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003408 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003409 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003410 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003411 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003412 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003413
Nate Begeman1d4d4142005-09-01 00:19:25 +00003414 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003415 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003416 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003417 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3418 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003419
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003420 if (!LegalOperations ||
3421 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003422 switch (N0.getOpcode()) {
3423 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003424 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003425 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003426 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003427 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003428 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003429 N0.getOperand(3), NotCC);
3430 }
3431 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003432 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003433
Chris Lattner61c5ff42007-09-10 21:39:07 +00003434 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003435 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003436 N0.getNode()->hasOneUse() &&
3437 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003438 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003439 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003440 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003441 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003442 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003443 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003444
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003445 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003447 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003448 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003449 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3450 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003451 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3452 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003453 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003454 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003455 }
3456 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003457 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003458 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003459 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003460 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003461 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3462 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003463 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3464 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003465 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003466 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003467 }
3468 }
David Majnemer363160a2013-05-08 06:44:42 +00003469 // fold (xor (and x, y), y) -> (and (not x), y)
3470 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3471 N0->getOperand(1) == N1) {
3472 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003473 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003474 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003475 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003476 }
Bill Wendling317bd702009-01-30 21:14:50 +00003477 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003478 if (N1C && N0.getOpcode() == ISD::XOR) {
3479 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3480 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3481 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003482 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003483 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003484 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003485 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003486 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003487 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003488 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003489 }
3490 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003491 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003492 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003493
Chris Lattner35e5c142006-05-05 05:51:50 +00003494 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3495 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003496 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003497 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003498 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003499
Chris Lattner3e104b12006-04-08 04:15:24 +00003500 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003501 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003502 SimplifyDemandedBits(SDValue(N, 0)))
3503 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003504
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003505 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003506}
3507
Chris Lattnere70da202007-12-06 07:33:36 +00003508/// visitShiftByConstant - Handle transforms common to the three shifts, when
3509/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003510SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003511 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003512 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003513
Chris Lattnere70da202007-12-06 07:33:36 +00003514 // We want to pull some binops through shifts, so that we have (and (shift))
3515 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3516 // thing happens with address calculations, so it's important to canonicalize
3517 // it.
3518 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003519
Chris Lattnere70da202007-12-06 07:33:36 +00003520 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003521 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003522 case ISD::OR:
3523 case ISD::XOR:
3524 HighBitSet = false; // We can only transform sra if the high bit is clear.
3525 break;
3526 case ISD::AND:
3527 HighBitSet = true; // We can only transform sra if the high bit is set.
3528 break;
3529 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003530 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003531 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003532 HighBitSet = false; // We can only transform sra if the high bit is clear.
3533 break;
3534 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003535
Chris Lattnere70da202007-12-06 07:33:36 +00003536 // We require the RHS of the binop to be a constant as well.
3537 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003538 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003539
3540 // FIXME: disable this unless the input to the binop is a shift by a constant.
3541 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003542 //
Bill Wendling88103372009-01-30 21:37:17 +00003543 // void foo(int *X, int i) { X[i & 1235] = 1; }
3544 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003545 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003546 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003547 BinOpLHSVal->getOpcode() != ISD::SRA &&
3548 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3549 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003550 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003551
Owen Andersone50ed302009-08-10 22:56:29 +00003552 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003553
Bill Wendling88103372009-01-30 21:37:17 +00003554 // If this is a signed shift right, and the high bit is modified by the
3555 // logical operation, do not perform the transformation. The highBitSet
3556 // boolean indicates the value of the high bit of the constant which would
3557 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003558 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003559 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3560 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003561 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003563
Chris Lattnere70da202007-12-06 07:33:36 +00003564 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003565 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003566 N->getValueType(0),
3567 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003568
3569 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003570 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003571 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003572 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003573
3574 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003575 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003576}
3577
Dan Gohman475871a2008-07-27 21:46:04 +00003578SDValue DAGCombiner::visitSHL(SDNode *N) {
3579 SDValue N0 = N->getOperand(0);
3580 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003581 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3582 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003583 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003584 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003585
Nate Begeman1d4d4142005-09-01 00:19:25 +00003586 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003587 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003588 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003589 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003590 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003591 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003592 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003593 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003594 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003595 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003596 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003597 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003598 // fold (shl undef, x) -> 0
3599 if (N0.getOpcode() == ISD::UNDEF)
3600 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003601 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003602 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003603 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003604 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003605 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003606 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003607 N1.getOperand(0).getOpcode() == ISD::AND &&
3608 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003609 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003610 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003611 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003612 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003613 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003614 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003615 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3616 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003617 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003618 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003619 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003620 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003621 }
3622 }
3623
Dan Gohman475871a2008-07-27 21:46:04 +00003624 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3625 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003626
3627 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003628 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003629 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003630 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3631 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003632 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003633 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003634 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003635 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003636 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003637
3638 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3639 // For this to be valid, the second form must not preserve any of the bits
3640 // that are shifted out by the inner shift in the first form. This means
3641 // the outer shift size must be >= the number of bits added by the ext.
3642 // As a corollary, we don't care what kind of ext it is.
3643 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3644 N0.getOpcode() == ISD::ANY_EXTEND ||
3645 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3646 N0.getOperand(0).getOpcode() == ISD::SHL &&
3647 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003648 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003649 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3650 uint64_t c2 = N1C->getZExtValue();
3651 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3652 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3653 if (c2 >= OpSizeInBits - InnerShiftSize) {
3654 if (c1 + c2 >= OpSizeInBits)
3655 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003656 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3657 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003658 N0.getOperand(0)->getOperand(0)),
3659 DAG.getConstant(c1 + c2, N1.getValueType()));
3660 }
3661 }
3662
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003663 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3664 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003665 // Only fold this if the inner shift has no other uses -- if it does, folding
3666 // this will increase the total number of instructions.
3667 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003668 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003669 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003670 if (c1 < VT.getSizeInBits()) {
3671 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003672 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3673 VT.getSizeInBits() - c1);
3674 SDValue Shift;
3675 if (c2 > c1) {
3676 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003677 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003678 DAG.getConstant(c2-c1, N1.getValueType()));
3679 } else {
3680 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003681 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003682 DAG.getConstant(c1-c2, N1.getValueType()));
3683 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003684 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003685 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003686 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003687 }
Bill Wendling88103372009-01-30 21:37:17 +00003688 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003689 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3690 SDValue HiBitsMask =
3691 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3692 VT.getSizeInBits() -
3693 N1C->getZExtValue()),
3694 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003695 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003696 HiBitsMask);
3697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003698
Evan Chenge5b51ac2010-04-17 06:13:15 +00003699 if (N1C) {
3700 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3701 if (NewSHL.getNode())
3702 return NewSHL;
3703 }
3704
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003705 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003706}
3707
Dan Gohman475871a2008-07-27 21:46:04 +00003708SDValue DAGCombiner::visitSRA(SDNode *N) {
3709 SDValue N0 = N->getOperand(0);
3710 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003711 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3712 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003713 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003714 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003715
Bill Wendling88103372009-01-30 21:37:17 +00003716 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003717 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003718 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003719 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003720 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003721 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003722 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003723 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003724 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003725 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003726 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003727 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003728 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003729 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003730 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003731 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3732 // sext_inreg.
3733 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003734 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003735 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3736 if (VT.isVector())
3737 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3738 ExtVT, VT.getVectorNumElements());
3739 if ((!LegalOperations ||
3740 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003741 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003742 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003743 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003744
Bill Wendling88103372009-01-30 21:37:17 +00003745 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003746 if (N1C && N0.getOpcode() == ISD::SRA) {
3747 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003748 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003749 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003750 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003751 DAG.getConstant(Sum, N1C->getValueType(0)));
3752 }
3753 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003754
Bill Wendling88103372009-01-30 21:37:17 +00003755 // fold (sra (shl X, m), (sub result_size, n))
3756 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003757 // result_size - n != m.
3758 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003759 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003760 if (N0.getOpcode() == ISD::SHL) {
3761 // Get the two constanst of the shifts, CN0 = m, CN = n.
3762 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3763 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003764 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003765 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003766 EVT::getIntegerVT(*DAG.getContext(),
3767 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003768 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003769 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003770
Scott Michelfdc40a02009-02-17 22:15:04 +00003771 // If the shift is not a no-op (in which case this should be just a sign
3772 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003773 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003774 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003775 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003776 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3777 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003778 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003779
Owen Anderson95771af2011-02-25 21:41:48 +00003780 SDValue Amt = DAG.getConstant(ShiftAmt,
3781 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003782 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003783 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003784 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003785 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003786 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003787 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003788 }
3789 }
3790 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003791
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003792 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003793 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003794 N1.getOperand(0).getOpcode() == ISD::AND &&
3795 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003796 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003797 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003798 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003799 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003800 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003801 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003802 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3803 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003804 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003805 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003806 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003807 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003808 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003809 }
3810 }
3811
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003812 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3813 // if c1 is equal to the number of bits the trunc removes
3814 if (N0.getOpcode() == ISD::TRUNCATE &&
3815 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3816 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3817 N0.getOperand(0).hasOneUse() &&
3818 N0.getOperand(0).getOperand(1).hasOneUse() &&
3819 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3820 EVT LargeVT = N0.getOperand(0).getValueType();
3821 ConstantSDNode *LargeShiftAmt =
3822 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3823
3824 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3825 LargeShiftAmt->getZExtValue()) {
3826 SDValue Amt =
3827 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003828 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003829 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003830 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003831 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003832 }
3833 }
3834
Scott Michelfdc40a02009-02-17 22:15:04 +00003835 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003836 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3837 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003838
3839
Nate Begeman1d4d4142005-09-01 00:19:25 +00003840 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003841 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003842 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003843
Evan Chenge5b51ac2010-04-17 06:13:15 +00003844 if (N1C) {
3845 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3846 if (NewSRA.getNode())
3847 return NewSRA;
3848 }
3849
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003850 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003851}
3852
Dan Gohman475871a2008-07-27 21:46:04 +00003853SDValue DAGCombiner::visitSRL(SDNode *N) {
3854 SDValue N0 = N->getOperand(0);
3855 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003856 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3857 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003858 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003859 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003860
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003862 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003863 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003864 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003865 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003866 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003867 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003868 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003869 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003870 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003871 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003872 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003873 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003874 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003875 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003876 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003877
Bill Wendling88103372009-01-30 21:37:17 +00003878 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003879 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003880 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003881 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3882 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003883 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003884 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003885 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003886 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003887 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003888
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003889 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003890 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3891 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003892 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003893 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003894 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3895 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003896 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3897 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003898 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003899 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003900 if (c1 + OpSizeInBits == InnerShiftSize) {
3901 if (c1 + c2 >= InnerShiftSize)
3902 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003903 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
3904 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003905 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003906 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003907 }
3908 }
3909
Chris Lattnerefcddc32010-04-15 05:28:43 +00003910 // fold (srl (shl x, c), c) -> (and x, cst2)
3911 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3912 N0.getValueSizeInBits() <= 64) {
3913 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003914 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00003915 DAG.getConstant(~0ULL >> ShAmt, VT));
3916 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003917
Scott Michelfdc40a02009-02-17 22:15:04 +00003918
Chris Lattner06afe072006-05-05 22:53:17 +00003919 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3920 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3921 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003922 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003923 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003924 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003925
Evan Chenge5b51ac2010-04-17 06:13:15 +00003926 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003927 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003928 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003929 N0.getOperand(0),
3930 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003931 AddToWorkList(SmallShift.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003932 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift);
Evan Chenge5b51ac2010-04-17 06:13:15 +00003933 }
Chris Lattner06afe072006-05-05 22:53:17 +00003934 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003935
Chris Lattner3657ffe2006-10-12 20:23:19 +00003936 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3937 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003938 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003939 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003940 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003941 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003942
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003943 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003944 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003945 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003946 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003947 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003948
Chris Lattner350bec02006-04-02 06:11:11 +00003949 // If any of the input bits are KnownOne, then the input couldn't be all
3950 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003951 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003952
Chris Lattner350bec02006-04-02 06:11:11 +00003953 // If all of the bits input the to ctlz node are known to be zero, then
3954 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003955 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003956 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003957
Chris Lattner350bec02006-04-02 06:11:11 +00003958 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003959 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003960 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003961 // could be set on input to the CTLZ node. If this bit is set, the SRL
3962 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3963 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003964 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003965 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003966
Chris Lattner350bec02006-04-02 06:11:11 +00003967 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003968 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003969 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003970 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003971 }
Bill Wendling88103372009-01-30 21:37:17 +00003972
Andrew Trickac6d9be2013-05-25 02:42:55 +00003973 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003974 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003975 }
3976 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003977
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003978 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003979 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003980 N1.getOperand(0).getOpcode() == ISD::AND &&
3981 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003982 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003983 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003984 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003985 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003986 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003987 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003988 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
3989 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003990 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003991 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003992 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003993 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003994 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003995 }
3996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003997
Chris Lattner61a4c072007-04-18 03:06:49 +00003998 // fold operands of srl based on knowledge that the low bits are not
3999 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004000 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4001 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004002
Evan Cheng9ab2b982009-12-18 21:31:31 +00004003 if (N1C) {
4004 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4005 if (NewSRL.getNode())
4006 return NewSRL;
4007 }
4008
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004009 // Attempt to convert a srl of a load into a narrower zero-extending load.
4010 SDValue NarrowLoad = ReduceLoadWidth(N);
4011 if (NarrowLoad.getNode())
4012 return NarrowLoad;
4013
Evan Cheng9ab2b982009-12-18 21:31:31 +00004014 // Here is a common situation. We want to optimize:
4015 //
4016 // %a = ...
4017 // %b = and i32 %a, 2
4018 // %c = srl i32 %b, 1
4019 // brcond i32 %c ...
4020 //
4021 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004022 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004023 // %a = ...
4024 // %b = and %a, 2
4025 // %c = setcc eq %b, 0
4026 // brcond %c ...
4027 //
4028 // However when after the source operand of SRL is optimized into AND, the SRL
4029 // itself may not be optimized further. Look for it and add the BRCOND into
4030 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004031 if (N->hasOneUse()) {
4032 SDNode *Use = *N->use_begin();
4033 if (Use->getOpcode() == ISD::BRCOND)
4034 AddToWorkList(Use);
4035 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4036 // Also look pass the truncate.
4037 Use = *Use->use_begin();
4038 if (Use->getOpcode() == ISD::BRCOND)
4039 AddToWorkList(Use);
4040 }
4041 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004042
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004043 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004044}
4045
Dan Gohman475871a2008-07-27 21:46:04 +00004046SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4047 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004048 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004049
4050 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004051 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004052 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004053 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004054}
4055
Chandler Carruth63974b22011-12-13 01:56:10 +00004056SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4057 SDValue N0 = N->getOperand(0);
4058 EVT VT = N->getValueType(0);
4059
4060 // fold (ctlz_zero_undef c1) -> c2
4061 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004062 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004063 return SDValue();
4064}
4065
Dan Gohman475871a2008-07-27 21:46:04 +00004066SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4067 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004068 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004069
Nate Begeman1d4d4142005-09-01 00:19:25 +00004070 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004071 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004072 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004073 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004074}
4075
Chandler Carruth63974b22011-12-13 01:56:10 +00004076SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4077 SDValue N0 = N->getOperand(0);
4078 EVT VT = N->getValueType(0);
4079
4080 // fold (cttz_zero_undef c1) -> c2
4081 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004082 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004083 return SDValue();
4084}
4085
Dan Gohman475871a2008-07-27 21:46:04 +00004086SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4087 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004088 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004089
Nate Begeman1d4d4142005-09-01 00:19:25 +00004090 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004091 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004092 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004093 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004094}
4095
Dan Gohman475871a2008-07-27 21:46:04 +00004096SDValue DAGCombiner::visitSELECT(SDNode *N) {
4097 SDValue N0 = N->getOperand(0);
4098 SDValue N1 = N->getOperand(1);
4099 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004100 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4102 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004103 EVT VT = N->getValueType(0);
4104 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004105
Bill Wendling34584e62009-01-30 22:02:18 +00004106 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004107 if (N1 == N2)
4108 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004109 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004110 if (N0C && !N0C->isNullValue())
4111 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004112 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004113 if (N0C && N0C->isNullValue())
4114 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004115 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004117 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004118 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004119 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004121 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004122 TLI.getBooleanContents(false) ==
4123 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004124 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004125 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004126 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004127 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004128 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004129 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004130 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004131 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004132 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004133 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4134 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004135 }
Bill Wendling34584e62009-01-30 22:02:18 +00004136 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004138 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004139 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004140 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004141 }
Bill Wendling34584e62009-01-30 22:02:18 +00004142 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004144 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004145 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004146 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004147 }
Bill Wendling34584e62009-01-30 22:02:18 +00004148 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004150 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004151 // fold (select X, X, Y) -> (or X, Y)
4152 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004154 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004155 // fold (select X, Y, X) -> (and X, Y)
4156 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004158 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004159
Chris Lattner40c62d52005-10-18 06:04:22 +00004160 // If we can fold this based on the true/false value, do so.
4161 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004162 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004163
Nate Begeman44728a72005-09-19 22:34:01 +00004164 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004165 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004166 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004168 // having to say they don't support SELECT_CC on every type the DAG knows
4169 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004171 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004172 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004173 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004174 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004175 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004176 }
Bill Wendling34584e62009-01-30 22:02:18 +00004177
Dan Gohman475871a2008-07-27 21:46:04 +00004178 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004179}
4180
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004181SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4182 SDValue N0 = N->getOperand(0);
4183 SDValue N1 = N->getOperand(1);
4184 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004185 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004186
4187 // Canonicalize integer abs.
4188 // vselect (setg[te] X, 0), X, -X ->
4189 // vselect (setgt X, -1), X, -X ->
4190 // vselect (setl[te] X, 0), -X, X ->
4191 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4192 if (N0.getOpcode() == ISD::SETCC) {
4193 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4194 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4195 bool isAbs = false;
4196 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4197
4198 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4199 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4200 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4201 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4202 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4203 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4204 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4205
4206 if (isAbs) {
4207 EVT VT = LHS.getValueType();
4208 SDValue Shift = DAG.getNode(
4209 ISD::SRA, DL, VT, LHS,
4210 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4211 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4212 AddToWorkList(Shift.getNode());
4213 AddToWorkList(Add.getNode());
4214 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4215 }
4216 }
4217
4218 return SDValue();
4219}
4220
Dan Gohman475871a2008-07-27 21:46:04 +00004221SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4222 SDValue N0 = N->getOperand(0);
4223 SDValue N1 = N->getOperand(1);
4224 SDValue N2 = N->getOperand(2);
4225 SDValue N3 = N->getOperand(3);
4226 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004227 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004228
Nate Begeman44728a72005-09-19 22:34:01 +00004229 // fold select_cc lhs, rhs, x, x, cc -> x
4230 if (N2 == N3)
4231 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004232
Chris Lattner5f42a242006-09-20 06:19:26 +00004233 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004234 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004235 N0, N1, CC, SDLoc(N), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004236 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004237
Gabor Greifba36cb52008-08-28 21:40:38 +00004238 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004239 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004240 return N2; // cond always true -> true val
4241 else
4242 return N3; // cond always false -> false val
4243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Chris Lattner5f42a242006-09-20 06:19:26 +00004245 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004246 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004247 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004248 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004249 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004250
Chris Lattner40c62d52005-10-18 06:04:22 +00004251 // If we can fold this based on the true/false value, do so.
4252 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004253 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004254
Nate Begeman44728a72005-09-19 22:34:01 +00004255 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004256 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004257}
4258
Dan Gohman475871a2008-07-27 21:46:04 +00004259SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004260 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004261 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004262 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004263}
4264
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004265// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004266// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004267// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004268// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004269static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004270 unsigned ExtOpc,
4271 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004272 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004273 bool HasCopyToRegUses = false;
4274 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004275 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4276 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004277 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004278 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004279 if (User == N)
4280 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004281 if (UI.getUse().getResNo() != N0.getResNo())
4282 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004283 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004284 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004285 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4286 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4287 // Sign bits will be lost after a zext.
4288 return false;
4289 bool Add = false;
4290 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004292 if (UseOp == N0)
4293 continue;
4294 if (!isa<ConstantSDNode>(UseOp))
4295 return false;
4296 Add = true;
4297 }
4298 if (Add)
4299 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004300 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004301 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004302 // If truncates aren't free and there are users we can't
4303 // extend, it isn't worthwhile.
4304 if (!isTruncFree)
4305 return false;
4306 // Remember if this value is live-out.
4307 if (User->getOpcode() == ISD::CopyToReg)
4308 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004309 }
4310
4311 if (HasCopyToRegUses) {
4312 bool BothLiveOut = false;
4313 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4314 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004315 SDUse &Use = UI.getUse();
4316 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4317 BothLiveOut = true;
4318 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004319 }
4320 }
4321 if (BothLiveOut)
4322 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004323 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004324 return ExtendNodes.size();
4325 }
4326 return true;
4327}
4328
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004329void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004330 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004331 ISD::NodeType ExtType) {
4332 // Extend SetCC uses if necessary.
4333 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4334 SDNode *SetCC = SetCCs[i];
4335 SmallVector<SDValue, 4> Ops;
4336
4337 for (unsigned j = 0; j != 2; ++j) {
4338 SDValue SOp = SetCC->getOperand(j);
4339 if (SOp == Trunc)
4340 Ops.push_back(ExtLoad);
4341 else
4342 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4343 }
4344
4345 Ops.push_back(SetCC->getOperand(2));
4346 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4347 &Ops[0], Ops.size()));
4348 }
4349}
4350
Dan Gohman475871a2008-07-27 21:46:04 +00004351SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4352 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004353 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004354
Nate Begeman1d4d4142005-09-01 00:19:25 +00004355 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004356 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004357 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004358
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004359 // fold (sext (sext x)) -> (sext x)
4360 // fold (sext (aext x)) -> (sext x)
4361 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004362 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004363 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004364
Chris Lattner22558872007-02-26 03:13:59 +00004365 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004366 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4367 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004368 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4369 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004370 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4371 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004372 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004373 // CombineTo deleted the truncate, if needed, but not what's under it.
4374 AddToWorkList(oye);
4375 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004376 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004377 }
Evan Chengc88138f2007-03-22 01:54:19 +00004378
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004379 // See if the value being truncated is already sign extended. If so, just
4380 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004381 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004382 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4383 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4384 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004385 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004386
Chris Lattner22558872007-02-26 03:13:59 +00004387 if (OpBits == DestBits) {
4388 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4389 // bits, it is already ready.
4390 if (NumSignBits > DestBits-MidBits)
4391 return Op;
4392 } else if (OpBits < DestBits) {
4393 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4394 // bits, just sext from i32.
4395 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004396 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004397 } else {
4398 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4399 // bits, just truncate to i32.
4400 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004401 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004403
Chris Lattner22558872007-02-26 03:13:59 +00004404 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004405 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4406 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004407 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004408 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004409 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004410 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4411 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004412 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004413 }
Chris Lattner6007b842006-09-21 06:00:20 +00004414 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004415
Evan Cheng110dec22005-12-14 02:19:23 +00004416 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004417 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004418 // on vectors in one instruction. We only perform this transformation on
4419 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004420 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004421 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004422 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004423 bool DoXform = true;
4424 SmallVector<SDNode*, 4> SetCCs;
4425 if (!N0.hasOneUse())
4426 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4427 if (DoXform) {
4428 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004429 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004430 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004431 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004432 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004433 LN0->isVolatile(), LN0->isNonTemporal(),
4434 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004435 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004436 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004437 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004438 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004439 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004440 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004441 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004442 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004443 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004444
4445 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4446 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004447 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4448 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004449 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004450 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004451 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004452 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004453 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004454 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004455 LN0->getBasePtr(), LN0->getPointerInfo(),
4456 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004457 LN0->isVolatile(), LN0->isNonTemporal(),
4458 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004459 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004460 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004461 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004462 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004463 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004464 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004465 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004467
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004468 // fold (sext (and/or/xor (load x), cst)) ->
4469 // (and/or/xor (sextload x), (sext cst))
4470 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4471 N0.getOpcode() == ISD::XOR) &&
4472 isa<LoadSDNode>(N0.getOperand(0)) &&
4473 N0.getOperand(1).getOpcode() == ISD::Constant &&
4474 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4475 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4476 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4477 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4478 bool DoXform = true;
4479 SmallVector<SDNode*, 4> SetCCs;
4480 if (!N0.hasOneUse())
4481 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4482 SetCCs, TLI);
4483 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004484 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004485 LN0->getChain(), LN0->getBasePtr(),
4486 LN0->getPointerInfo(),
4487 LN0->getMemoryVT(),
4488 LN0->isVolatile(),
4489 LN0->isNonTemporal(),
4490 LN0->getAlignment());
4491 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4492 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004493 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004494 ExtLoad, DAG.getConstant(Mask, VT));
4495 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004496 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004497 N0.getOperand(0).getValueType(), ExtLoad);
4498 CombineTo(N, And);
4499 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004500 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004501 ISD::SIGN_EXTEND);
4502 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4503 }
4504 }
4505 }
4506
Chris Lattner20a35c32007-04-11 05:32:27 +00004507 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004508 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004509 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004510 if (VT.isVector() && !LegalOperations &&
4511 TLI.getBooleanContents(true) ==
4512 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004513 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004514 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4515 // of the same size as the compared operands. Only optimize sext(setcc())
4516 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004517 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004518
4519 // We know that the # elements of the results is the same as the
4520 // # elements of the compare (and the # elements of the compare result
4521 // for that matter). Check to see that they are the same size. If so,
4522 // we know that the element size of the sext'd result matches the
4523 // element size of the compare operands.
4524 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004525 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004526 N0.getOperand(1),
4527 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004528
Dan Gohman3ce89f42010-04-30 17:19:19 +00004529 // If the desired elements are smaller or larger than the source
4530 // elements we can use a matching integer vector type and then
4531 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004532 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004533 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004534 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004535 N0.getOperand(0), N0.getOperand(1),
4536 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004537 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004538 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004539 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004540
Chris Lattner2b7a2712009-07-08 00:31:33 +00004541 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004542 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004543 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004544 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004545 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004546 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004547 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004548 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004549 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004550 if (!VT.isVector() &&
4551 (!LegalOperations ||
4552 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4553 return DAG.getSelect(SDLoc(N), VT,
4554 DAG.getSetCC(SDLoc(N),
4555 getSetCCResultType(VT),
4556 N0.getOperand(0), N0.getOperand(1),
4557 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4558 NegOne, DAG.getConstant(0, VT));
4559 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004560 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004561
Dan Gohman8f0ad582008-04-28 16:58:24 +00004562 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004563 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004564 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004565 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004566
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004567 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004568}
4569
Rafael Espindoladecbc432012-04-09 16:06:03 +00004570// isTruncateOf - If N is a truncate of some other value, return true, record
4571// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4572// This function computes KnownZero to avoid a duplicated call to
4573// ComputeMaskedBits in the caller.
4574static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4575 APInt &KnownZero) {
4576 APInt KnownOne;
4577 if (N->getOpcode() == ISD::TRUNCATE) {
4578 Op = N->getOperand(0);
4579 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4580 return true;
4581 }
4582
4583 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4584 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4585 return false;
4586
4587 SDValue Op0 = N->getOperand(0);
4588 SDValue Op1 = N->getOperand(1);
4589 assert(Op0.getValueType() == Op1.getValueType());
4590
4591 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4592 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004593 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004594 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004595 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004596 Op = Op0;
4597 else
4598 return false;
4599
4600 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4601
4602 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4603 return false;
4604
4605 return true;
4606}
4607
Dan Gohman475871a2008-07-27 21:46:04 +00004608SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4609 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004610 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004611
Nate Begeman1d4d4142005-09-01 00:19:25 +00004612 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004613 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004614 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004615 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004616 // fold (zext (aext x)) -> (zext x)
4617 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004618 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004619 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004620
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004621 // fold (zext (truncate x)) -> (zext x) or
4622 // (zext (truncate x)) -> (truncate x)
4623 // This is valid when the truncated bits of x are already zero.
4624 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004625 SDValue Op;
4626 APInt KnownZero;
4627 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4628 APInt TruncatedBits =
4629 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4630 APInt(Op.getValueSizeInBits(), 0) :
4631 APInt::getBitsSet(Op.getValueSizeInBits(),
4632 N0.getValueSizeInBits(),
4633 std::min(Op.getValueSizeInBits(),
4634 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004635 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004636 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004637 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004638 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004639 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004640
4641 return Op;
4642 }
4643 }
4644
Evan Chengc88138f2007-03-22 01:54:19 +00004645 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4646 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004647 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004648 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4649 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004650 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4651 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004652 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004653 // CombineTo deleted the truncate, if needed, but not what's under it.
4654 AddToWorkList(oye);
4655 }
Eli Friedmane545d382011-04-16 23:25:34 +00004656 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004657 }
Evan Chengc88138f2007-03-22 01:54:19 +00004658 }
4659
Chris Lattner6007b842006-09-21 06:00:20 +00004660 // fold (zext (truncate x)) -> (and x, mask)
4661 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004662 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004663
4664 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4665 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4666 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4667 if (NarrowLoad.getNode()) {
4668 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4669 if (NarrowLoad.getNode() != N0.getNode()) {
4670 CombineTo(N0.getNode(), NarrowLoad);
4671 // CombineTo deleted the truncate, if needed, but not what's under it.
4672 AddToWorkList(oye);
4673 }
4674 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4675 }
4676
Dan Gohman475871a2008-07-27 21:46:04 +00004677 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004678 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004679 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004680 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004681 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004682 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004683 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004684 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004685 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004686 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004687 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004688
Dan Gohman97121ba2009-04-08 00:15:30 +00004689 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4690 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004691 if (N0.getOpcode() == ISD::AND &&
4692 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004693 N0.getOperand(1).getOpcode() == ISD::Constant &&
4694 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4695 N0.getValueType()) ||
4696 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004697 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004698 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004699 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004700 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004701 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004702 }
Dan Gohman220a8232008-03-03 23:51:38 +00004703 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004704 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004705 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004706 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004707 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004708
Evan Cheng110dec22005-12-14 02:19:23 +00004709 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004710 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004711 // on vectors in one instruction. We only perform this transformation on
4712 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004713 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004714 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004715 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004716 bool DoXform = true;
4717 SmallVector<SDNode*, 4> SetCCs;
4718 if (!N0.hasOneUse())
4719 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4720 if (DoXform) {
4721 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004722 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004723 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004724 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004725 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004726 LN0->isVolatile(), LN0->isNonTemporal(),
4727 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004728 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004729 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004730 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004731 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004732
Andrew Trickac6d9be2013-05-25 02:42:55 +00004733 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004734 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004735 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004736 }
Evan Cheng110dec22005-12-14 02:19:23 +00004737 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004738
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004739 // fold (zext (and/or/xor (load x), cst)) ->
4740 // (and/or/xor (zextload x), (zext cst))
4741 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4742 N0.getOpcode() == ISD::XOR) &&
4743 isa<LoadSDNode>(N0.getOperand(0)) &&
4744 N0.getOperand(1).getOpcode() == ISD::Constant &&
4745 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4746 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4747 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4748 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4749 bool DoXform = true;
4750 SmallVector<SDNode*, 4> SetCCs;
4751 if (!N0.hasOneUse())
4752 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4753 SetCCs, TLI);
4754 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004755 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004756 LN0->getChain(), LN0->getBasePtr(),
4757 LN0->getPointerInfo(),
4758 LN0->getMemoryVT(),
4759 LN0->isVolatile(),
4760 LN0->isNonTemporal(),
4761 LN0->getAlignment());
4762 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4763 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004764 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004765 ExtLoad, DAG.getConstant(Mask, VT));
4766 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004767 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004768 N0.getOperand(0).getValueType(), ExtLoad);
4769 CombineTo(N, And);
4770 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004771 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004772 ISD::ZERO_EXTEND);
4773 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4774 }
4775 }
4776 }
4777
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004778 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4779 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004780 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4781 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004782 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004783 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004784 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004785 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004786 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004787 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004788 LN0->getBasePtr(), LN0->getPointerInfo(),
4789 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004790 LN0->isVolatile(), LN0->isNonTemporal(),
4791 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004792 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004793 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004794 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004795 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004796 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004797 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004798 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004799 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004800
Chris Lattner20a35c32007-04-11 05:32:27 +00004801 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004802 if (!LegalOperations && VT.isVector()) {
4803 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4804 // Only do this before legalize for now.
4805 EVT N0VT = N0.getOperand(0).getValueType();
4806 EVT EltVT = VT.getVectorElementType();
4807 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4808 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004809 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004810 // We know that the # elements of the results is the same as the
4811 // # elements of the compare (and the # elements of the compare result
4812 // for that matter). Check to see that they are the same size. If so,
4813 // we know that the element size of the sext'd result matches the
4814 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004815 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4816 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004817 N0.getOperand(1),
4818 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004819 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004820 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004821
4822 // If the desired elements are smaller or larger than the source
4823 // elements we can use a matching integer vector type and then
4824 // truncate/sign extend
4825 EVT MatchingElementType =
4826 EVT::getIntegerVT(*DAG.getContext(),
4827 N0VT.getScalarType().getSizeInBits());
4828 EVT MatchingVectorType =
4829 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4830 N0VT.getVectorNumElements());
4831 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004832 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004833 N0.getOperand(1),
4834 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004835 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4836 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4837 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004838 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004839 }
4840
4841 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004842 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004843 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004844 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004845 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004846 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004848
Evan Cheng9818c042009-12-15 03:00:32 +00004849 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004850 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004851 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004852 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4853 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004854 SDValue ShAmt = N0.getOperand(1);
4855 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004856 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004857 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004858 // If the original shl may be shifting out bits, do not perform this
4859 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004860 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4861 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4862 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004863 return SDValue();
4864 }
Chris Lattnere0751182011-02-13 19:09:16 +00004865
Andrew Trickac6d9be2013-05-25 02:42:55 +00004866 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004867
4868 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004869 if (VT.getSizeInBits() >= 256)
4870 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004871
Chris Lattnere0751182011-02-13 19:09:16 +00004872 return DAG.getNode(N0.getOpcode(), DL, VT,
4873 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4874 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004875 }
4876
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004877 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004878}
4879
Dan Gohman475871a2008-07-27 21:46:04 +00004880SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4881 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004882 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004883
Chris Lattner5ffc0662006-05-05 05:58:59 +00004884 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004885 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004886 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004887 // fold (aext (aext x)) -> (aext x)
4888 // fold (aext (zext x)) -> (zext x)
4889 // fold (aext (sext x)) -> (sext x)
4890 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4891 N0.getOpcode() == ISD::ZERO_EXTEND ||
4892 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004893 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004894
Evan Chengc88138f2007-03-22 01:54:19 +00004895 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4896 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4897 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004898 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4899 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004900 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4901 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004902 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004903 // CombineTo deleted the truncate, if needed, but not what's under it.
4904 AddToWorkList(oye);
4905 }
Eli Friedmane545d382011-04-16 23:25:34 +00004906 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004907 }
Evan Chengc88138f2007-03-22 01:54:19 +00004908 }
4909
Chris Lattner84750582006-09-20 06:29:17 +00004910 // fold (aext (truncate x))
4911 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004912 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004913 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004914 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004915 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004916 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
4917 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004918 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004919
Dan Gohman97121ba2009-04-08 00:15:30 +00004920 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4921 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004922 if (N0.getOpcode() == ISD::AND &&
4923 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004924 N0.getOperand(1).getOpcode() == ISD::Constant &&
4925 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4926 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004928 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004929 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004930 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004931 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004932 }
Dan Gohman220a8232008-03-03 23:51:38 +00004933 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004934 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004935 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00004936 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004938
Chris Lattner5ffc0662006-05-05 05:58:59 +00004939 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004940 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004941 // on vectors in one instruction. We only perform this transformation on
4942 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004943 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004944 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004945 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004946 bool DoXform = true;
4947 SmallVector<SDNode*, 4> SetCCs;
4948 if (!N0.hasOneUse())
4949 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4950 if (DoXform) {
4951 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004952 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004953 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004954 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004955 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004956 LN0->isVolatile(), LN0->isNonTemporal(),
4957 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004958 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004959 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004960 N0.getValueType(), ExtLoad);
4961 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004962 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004963 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004964 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4965 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004966 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004967
Chris Lattner5ffc0662006-05-05 05:58:59 +00004968 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4969 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4970 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004971 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004972 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004973 N0.hasOneUse()) {
4974 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004975 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004976 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00004977 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004978 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004979 LN0->isVolatile(), LN0->isNonTemporal(),
4980 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004981 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004982 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004983 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00004984 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004985 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004986 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004987 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004988
Chris Lattner20a35c32007-04-11 05:32:27 +00004989 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004990 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4991 // Only do this before legalize for now.
4992 if (VT.isVector() && !LegalOperations) {
4993 EVT N0VT = N0.getOperand(0).getValueType();
4994 // We know that the # elements of the results is the same as the
4995 // # elements of the compare (and the # elements of the compare result
4996 // for that matter). Check to see that they are the same size. If so,
4997 // we know that the element size of the sext'd result matches the
4998 // element size of the compare operands.
4999 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005000 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005001 N0.getOperand(1),
5002 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005003 // If the desired elements are smaller or larger than the source
5004 // elements we can use a matching integer vector type and then
5005 // truncate/sign extend
5006 else {
Duncan Sands34727662010-07-12 08:16:59 +00005007 EVT MatchingElementType =
5008 EVT::getIntegerVT(*DAG.getContext(),
5009 N0VT.getScalarType().getSizeInBits());
5010 EVT MatchingVectorType =
5011 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5012 N0VT.getVectorNumElements());
5013 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005014 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005015 N0.getOperand(1),
5016 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005017 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005018 }
5019 }
5020
5021 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005022 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005023 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005024 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005025 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005026 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005027 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005029
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005030 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005031}
5032
Chris Lattner2b4c2792007-10-13 06:35:54 +00005033/// GetDemandedBits - See if the specified operand can be simplified with the
5034/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005035/// simpler operand, otherwise return a null SDValue.
5036SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005037 switch (V.getOpcode()) {
5038 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005039 case ISD::Constant: {
5040 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5041 assert(CV != 0 && "Const value should be ConstSDNode.");
5042 const APInt &CVal = CV->getAPIntValue();
5043 APInt NewVal = CVal & Mask;
5044 if (NewVal != CVal) {
5045 return DAG.getConstant(NewVal, V.getValueType());
5046 }
5047 break;
5048 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005049 case ISD::OR:
5050 case ISD::XOR:
5051 // If the LHS or RHS don't contribute bits to the or, drop them.
5052 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5053 return V.getOperand(1);
5054 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5055 return V.getOperand(0);
5056 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005057 case ISD::SRL:
5058 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005059 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005060 break;
5061 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5062 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005063 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005064
Dan Gohmancc91d632009-01-03 19:22:06 +00005065 // Watch out for shift count overflow though.
5066 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005067 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005068 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005069 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005070 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005071 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005072 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005073 }
Dan Gohman475871a2008-07-27 21:46:04 +00005074 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005075}
5076
Evan Chengc88138f2007-03-22 01:54:19 +00005077/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5078/// bits and then truncated to a narrower type and where N is a multiple
5079/// of number of bits of the narrower type, transform it to a narrower load
5080/// from address + N / num of bits of new type. If the result is to be
5081/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005082SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005083 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005084
Evan Chengc88138f2007-03-22 01:54:19 +00005085 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005086 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005087 EVT VT = N->getValueType(0);
5088 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005089
Dan Gohman7f8613e2008-08-14 20:04:46 +00005090 // This transformation isn't valid for vector loads.
5091 if (VT.isVector())
5092 return SDValue();
5093
Dan Gohmand1996362010-01-09 02:13:55 +00005094 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005095 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005096 if (Opc == ISD::SIGN_EXTEND_INREG) {
5097 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005098 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005099 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005100 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005101 ExtType = ISD::ZEXTLOAD;
5102 N0 = SDValue(N, 0);
5103 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5104 if (!N01) return SDValue();
5105 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5106 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005107 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005108 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5109 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005110
Owen Andersone50ed302009-08-10 22:56:29 +00005111 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005112
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005113 // Do not generate loads of non-round integer types since these can
5114 // be expensive (and would be wrong if the type is not byte sized).
5115 if (!ExtVT.isRound())
5116 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005117
Evan Chengc88138f2007-03-22 01:54:19 +00005118 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005119 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005120 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005121 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005122 // Is the shift amount a multiple of size of VT?
5123 if ((ShAmt & (EVTBits-1)) == 0) {
5124 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005125 // Is the load width a multiple of size of VT?
5126 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005127 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005128 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005129
Chris Lattnercbf68df2010-12-22 08:02:57 +00005130 // At this point, we must have a load or else we can't do the transform.
5131 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005132
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005133 // Because a SRL must be assumed to *need* to zero-extend the high bits
5134 // (as opposed to anyext the high bits), we can't combine the zextload
5135 // lowering of SRL and an sextload.
5136 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5137 return SDValue();
5138
Chris Lattner2831a192010-10-01 05:36:09 +00005139 // If the shift amount is larger than the input type then we're not
5140 // accessing any of the loaded bytes. If the load was a zextload/extload
5141 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005142 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005143 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005144 }
5145 }
5146
Dan Gohman394d6292010-11-03 01:47:46 +00005147 // If the load is shifted left (and the result isn't shifted back right),
5148 // we can fold the truncate through the shift.
5149 unsigned ShLeftAmt = 0;
5150 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005151 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005152 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5153 ShLeftAmt = N01->getZExtValue();
5154 N0 = N0.getOperand(0);
5155 }
5156 }
Owen Anderson95771af2011-02-25 21:41:48 +00005157
Chris Lattner4c32bc22010-12-22 07:36:50 +00005158 // If we haven't found a load, we can't narrow it. Don't transform one with
5159 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005160 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5161 return SDValue();
5162
5163 // Don't change the width of a volatile load.
5164 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5165 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005166 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005167
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005168 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005169 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005170 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005171
Bill Schmidt89e88e32013-01-14 22:04:38 +00005172 // For the transform to be legal, the load must produce only two values
5173 // (the value loaded and the chain). Don't transform a pre-increment
5174 // load, for example, which produces an extra value. Otherwise the
5175 // transformation is not equivalent, and the downstream logic to replace
5176 // uses gets things wrong.
5177 if (LN0->getNumValues() > 2)
5178 return SDValue();
5179
Chris Lattner4c32bc22010-12-22 07:36:50 +00005180 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005181
Evan Cheng16436df2012-06-26 01:19:33 +00005182 if (PtrType == MVT::Untyped || PtrType.isExtended())
5183 // It's not possible to generate a constant of extended or untyped type.
5184 return SDValue();
5185
Chris Lattner4c32bc22010-12-22 07:36:50 +00005186 // For big endian targets, we need to adjust the offset to the pointer to
5187 // load the correct bytes.
5188 if (TLI.isBigEndian()) {
5189 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5190 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5191 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005192 }
5193
Chris Lattner4c32bc22010-12-22 07:36:50 +00005194 uint64_t PtrOff = ShAmt / 8;
5195 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005196 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005197 PtrType, LN0->getBasePtr(),
5198 DAG.getConstant(PtrOff, PtrType));
5199 AddToWorkList(NewPtr.getNode());
5200
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005201 SDValue Load;
5202 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005203 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005204 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005205 LN0->isVolatile(), LN0->isNonTemporal(),
5206 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005207 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005208 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005209 LN0->getPointerInfo().getWithOffset(PtrOff),
5210 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5211 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005212
5213 // Replace the old load's chain with the new load's chain.
5214 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005215 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005216
5217 // Shift the result left, if we've swallowed a left shift.
5218 SDValue Result = Load;
5219 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005220 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005221 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5222 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005223 // If the shift amount is as large as the result size (but, presumably,
5224 // no larger than the source) then the useful bits of the result are
5225 // zero; we can't simply return the shortened shift, because the result
5226 // of that operation is undefined.
5227 if (ShLeftAmt >= VT.getSizeInBits())
5228 Result = DAG.getConstant(0, VT);
5229 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005230 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005231 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005232 }
5233
5234 // Return the new loaded value.
5235 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005236}
5237
Dan Gohman475871a2008-07-27 21:46:04 +00005238SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5239 SDValue N0 = N->getOperand(0);
5240 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005241 EVT VT = N->getValueType(0);
5242 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005243 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005244 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005245
Nate Begeman1d4d4142005-09-01 00:19:25 +00005246 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005247 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005248 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005249
Chris Lattner541a24f2006-05-06 22:43:44 +00005250 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005251 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005252 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005253
Nate Begeman646d7e22005-09-02 21:18:40 +00005254 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5255 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005256 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005257 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005258 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005259 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005260
Dan Gohman75dcf082008-07-31 00:50:31 +00005261 // fold (sext_in_reg (sext x)) -> (sext x)
5262 // fold (sext_in_reg (aext x)) -> (sext x)
5263 // if x is small enough.
5264 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5265 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005266 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5267 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005268 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005269 }
5270
Chris Lattner95a5e052007-04-17 19:03:21 +00005271 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005272 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005273 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005274
Chris Lattner95a5e052007-04-17 19:03:21 +00005275 // fold operands of sext_in_reg based on knowledge that the top bits are not
5276 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005277 if (SimplifyDemandedBits(SDValue(N, 0)))
5278 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005279
Evan Chengc88138f2007-03-22 01:54:19 +00005280 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5281 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005282 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005283 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005284 return NarrowLoad;
5285
Bill Wendling8509c902009-01-30 22:33:24 +00005286 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005287 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005288 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5289 if (N0.getOpcode() == ISD::SRL) {
5290 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005291 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005292 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005293 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005294 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005295 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005296 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005297 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005298 }
5299 }
Evan Chengc88138f2007-03-22 01:54:19 +00005300
Nate Begemanded49632005-10-13 03:11:28 +00005301 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005302 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005303 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005304 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005305 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005306 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005307 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005308 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005309 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005310 LN0->getBasePtr(), LN0->getPointerInfo(),
5311 EVT,
David Greene1e559442010-02-15 17:00:31 +00005312 LN0->isVolatile(), LN0->isNonTemporal(),
5313 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005314 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005315 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005316 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005317 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005318 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005319 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005320 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005321 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005322 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005323 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005324 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005325 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005326 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005327 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005328 LN0->getBasePtr(), LN0->getPointerInfo(),
5329 EVT,
David Greene1e559442010-02-15 17:00:31 +00005330 LN0->isVolatile(), LN0->isNonTemporal(),
5331 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005332 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005333 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005334 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005335 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005336
5337 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5338 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5339 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5340 N0.getOperand(1), false);
5341 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005342 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005343 BSwap, N1);
5344 }
5345
Dan Gohman475871a2008-07-27 21:46:04 +00005346 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005347}
5348
Dan Gohman475871a2008-07-27 21:46:04 +00005349SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5350 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005351 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005352 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005353
5354 // noop truncate
5355 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005356 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005357 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005358 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005359 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005360 // fold (truncate (truncate x)) -> (truncate x)
5361 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005362 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005363 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005364 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5365 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005366 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005367 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005368 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005369 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005370 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005371 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005372 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005373 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005374 // if the source and dest are the same type, we can drop both the extend
5375 // and the truncate.
5376 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005377 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005378
Nadav Rotemcc870a82012-02-05 11:39:23 +00005379 // Fold extract-and-trunc into a narrow extract. For example:
5380 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5381 // i32 y = TRUNCATE(i64 x)
5382 // -- becomes --
5383 // v16i8 b = BITCAST (v2i64 val)
5384 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5385 //
5386 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005387 // creates this pattern) and before operation legalization after which
5388 // we need to be more careful about the vector instructions that we generate.
5389 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5390 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5391
5392 EVT VecTy = N0.getOperand(0).getValueType();
5393 EVT ExTy = N0.getValueType();
5394 EVT TrTy = N->getValueType(0);
5395
5396 unsigned NumElem = VecTy.getVectorNumElements();
5397 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5398
5399 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5400 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5401
5402 SDValue EltNo = N0->getOperand(1);
5403 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5404 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005405 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005406 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5407
Andrew Trickac6d9be2013-05-25 02:42:55 +00005408 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005409 NVT, N0.getOperand(0));
5410
5411 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005412 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005413 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005414 }
5415 }
5416
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005417 // Fold a series of buildvector, bitcast, and truncate if possible.
5418 // For example fold
5419 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5420 // (2xi32 (buildvector x, y)).
5421 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5422 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5423 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5424 N0.getOperand(0).hasOneUse()) {
5425
5426 SDValue BuildVect = N0.getOperand(0);
5427 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5428 EVT TruncVecEltTy = VT.getVectorElementType();
5429
5430 // Check that the element types match.
5431 if (BuildVectEltTy == TruncVecEltTy) {
5432 // Now we only need to compute the offset of the truncated elements.
5433 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5434 unsigned TruncVecNumElts = VT.getVectorNumElements();
5435 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5436
5437 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5438 "Invalid number of elements");
5439
5440 SmallVector<SDValue, 8> Opnds;
5441 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5442 Opnds.push_back(BuildVect.getOperand(i));
5443
Andrew Trickac6d9be2013-05-25 02:42:55 +00005444 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005445 Opnds.size());
5446 }
5447 }
5448
Chris Lattner2b4c2792007-10-13 06:35:54 +00005449 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005450 // only the low bits are being used.
5451 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005452 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005453 // may have different active low bits.
5454 if (!VT.isVector()) {
5455 SDValue Shorter =
5456 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5457 VT.getSizeInBits()));
5458 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005459 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005460 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005461 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005462 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005463 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5464 SDValue Reduced = ReduceLoadWidth(N);
5465 if (Reduced.getNode())
5466 return Reduced;
5467 }
Michael Liao07edaf32012-10-17 23:45:54 +00005468 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5469 // where ... are all 'undef'.
5470 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5471 SmallVector<EVT, 8> VTs;
5472 SDValue V;
5473 unsigned Idx = 0;
5474 unsigned NumDefs = 0;
5475
5476 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5477 SDValue X = N0.getOperand(i);
5478 if (X.getOpcode() != ISD::UNDEF) {
5479 V = X;
5480 Idx = i;
5481 NumDefs++;
5482 }
5483 // Stop if more than one members are non-undef.
5484 if (NumDefs > 1)
5485 break;
5486 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5487 VT.getVectorElementType(),
5488 X.getValueType().getVectorNumElements()));
5489 }
5490
5491 if (NumDefs == 0)
5492 return DAG.getUNDEF(VT);
5493
5494 if (NumDefs == 1) {
5495 assert(V.getNode() && "The single defined operand is empty!");
5496 SmallVector<SDValue, 8> Opnds;
5497 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5498 if (i != Idx) {
5499 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5500 continue;
5501 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005502 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005503 AddToWorkList(NV.getNode());
5504 Opnds.push_back(NV);
5505 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005506 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005507 &Opnds[0], Opnds.size());
5508 }
5509 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005510
5511 // Simplify the operands using demanded-bits information.
5512 if (!VT.isVector() &&
5513 SimplifyDemandedBits(SDValue(N, 0)))
5514 return SDValue(N, 0);
5515
Evan Chenge5b51ac2010-04-17 06:13:15 +00005516 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005517}
5518
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005519static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005520 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005521 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005522 return Elt.getNode();
5523 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005524}
5525
5526/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005527/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005528SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005529 assert(N->getOpcode() == ISD::BUILD_PAIR);
5530
Nate Begemanabc01992009-06-05 21:37:30 +00005531 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5532 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005533 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5534 LD1->getPointerInfo().getAddrSpace() !=
5535 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005536 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005537 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005538
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005539 if (ISD::isNON_EXTLoad(LD2) &&
5540 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005541 // If both are volatile this would reduce the number of volatile loads.
5542 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005543 !LD1->isVolatile() &&
5544 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005545 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005546 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005547 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005548 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005549
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005550 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005551 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005552 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005553 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005554 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005555 }
Bill Wendling67a67682009-01-30 22:44:24 +00005556
Dan Gohman475871a2008-07-27 21:46:04 +00005557 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005558}
5559
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005560SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005562 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005563
Dan Gohman7f321562007-06-25 16:23:39 +00005564 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5565 // Only do this before legalize, since afterward the target may be depending
5566 // on the bitconvert.
5567 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005568 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005569 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005570 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005571 bool isSimple = true;
5572 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5573 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5574 N0.getOperand(i).getOpcode() != ISD::Constant &&
5575 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005576 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005577 break;
5578 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005579
Owen Andersone50ed302009-08-10 22:56:29 +00005580 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005581 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005582 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005583 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005584 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005586
Dan Gohman3dd168d2008-09-05 01:58:21 +00005587 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005588 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005589 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005590 if (Res.getNode() != N) {
5591 if (!LegalOperations ||
5592 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5593 return Res;
5594
5595 // Folding it resulted in an illegal node, and it's too late to
5596 // do that. Clean up the old node and forego the transformation.
5597 // Ideally this won't happen very often, because instcombine
5598 // and the earlier dagcombine runs (where illegal nodes are
5599 // permitted) should have folded most of them already.
5600 DAG.DeleteNode(Res.getNode());
5601 }
Chris Lattner94683772005-12-23 05:30:37 +00005602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005603
Bill Wendling67a67682009-01-30 22:44:24 +00005604 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005605 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005606 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005607 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005608
Chris Lattner57104102005-12-23 05:44:41 +00005609 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005610 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005611 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005612 // Do not change the width of a volatile load.
5613 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005614 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005615 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005616 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005617 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005618 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005619
Evan Cheng59d5b682007-05-07 21:27:48 +00005620 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005621 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005622 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005623 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005624 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005625 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005626 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005627 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005628 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005629 Load.getValue(1));
5630 return Load;
5631 }
Chris Lattner57104102005-12-23 05:44:41 +00005632 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005633
Bill Wendling67a67682009-01-30 22:44:24 +00005634 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5635 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005636 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005637 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5638 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005639 N0.getNode()->hasOneUse() && VT.isInteger() &&
5640 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005641 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005642 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005643 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005644
Duncan Sands83ec4b62008-06-06 12:08:01 +00005645 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005646 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005647 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005648 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005649 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005650 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005651 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005653
Bill Wendling67a67682009-01-30 22:44:24 +00005654 // fold (bitconvert (fcopysign cst, x)) ->
5655 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5656 // Note that we don't handle (copysign x, cst) because this can always be
5657 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005658 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005659 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 VT.isInteger() && !VT.isVector()) {
5661 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005662 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005663 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005664 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005665 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005666 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005667
Duncan Sands25cf2272008-11-24 14:53:14 +00005668 // If X has a different width than the result/lhs, sext it or truncate it.
5669 unsigned VTWidth = VT.getSizeInBits();
5670 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005671 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005672 AddToWorkList(X.getNode());
5673 } else if (OrigXWidth > VTWidth) {
5674 // To get the sign bit in the right place, we have to shift it right
5675 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005676 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005677 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005678 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5679 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005680 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005681 AddToWorkList(X.getNode());
5682 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005683
Duncan Sands25cf2272008-11-24 14:53:14 +00005684 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005685 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005686 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005687 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005688
Andrew Trickac6d9be2013-05-25 02:42:55 +00005689 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005690 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005691 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005692 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005693 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005694
Andrew Trickac6d9be2013-05-25 02:42:55 +00005695 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005696 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005697 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005698
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005699 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005700 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005701 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5702 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005703 return CombineLD;
5704 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005705
Dan Gohman475871a2008-07-27 21:46:04 +00005706 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005707}
5708
Dan Gohman475871a2008-07-27 21:46:04 +00005709SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005710 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005711 return CombineConsecutiveLoads(N, VT);
5712}
5713
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005714/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005715/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005716/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005717SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005718ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005719 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005720
Chris Lattner6258fb22006-04-02 02:53:43 +00005721 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005722 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005723
Duncan Sands83ec4b62008-06-06 12:08:01 +00005724 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5725 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005726
Chris Lattner6258fb22006-04-02 02:53:43 +00005727 // If this is a conversion of N elements of one type to N elements of another
5728 // type, convert each element. This handles FP<->INT cases.
5729 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005730 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5731 BV->getValueType(0).getVectorNumElements());
5732
5733 // Due to the FP element handling below calling this routine recursively,
5734 // we can end up with a scalar-to-vector node here.
5735 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005736 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5737 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005738 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005739
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005741 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005742 SDValue Op = BV->getOperand(i);
5743 // If the vector element type is not legal, the BUILD_VECTOR operands
5744 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005745 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005746 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5747 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005748 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005749 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005750 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005751 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005752 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005754
Chris Lattner6258fb22006-04-02 02:53:43 +00005755 // Otherwise, we're growing or shrinking the elements. To avoid having to
5756 // handle annoying details of growing/shrinking FP values, we convert them to
5757 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005758 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005759 // Convert the input float vector to a int vector where the elements are the
5760 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005762 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005763 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005764 SrcEltVT = IntVT;
5765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005766
Chris Lattner6258fb22006-04-02 02:53:43 +00005767 // Now we know the input is an integer vector. If the output is a FP type,
5768 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005769 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005771 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005772 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005773
Chris Lattner6258fb22006-04-02 02:53:43 +00005774 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005775 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005777
Chris Lattner6258fb22006-04-02 02:53:43 +00005778 // Okay, we know the src/dst types are both integers of differing types.
5779 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005780 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005781 if (SrcBitSize < DstBitSize) {
5782 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005783
Dan Gohman475871a2008-07-27 21:46:04 +00005784 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005785 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005786 i += NumInputsPerOutput) {
5787 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005788 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005789 bool EltIsUndef = true;
5790 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5791 // Shift the previously computed bits over.
5792 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005793 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005794 if (Op.getOpcode() == ISD::UNDEF) continue;
5795 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005796
Jay Foad40f8f622010-12-07 08:25:19 +00005797 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005798 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005799 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005800
Chris Lattner6258fb22006-04-02 02:53:43 +00005801 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005802 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005803 else
5804 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5805 }
5806
Owen Anderson23b9b192009-08-12 00:36:31 +00005807 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005808 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005809 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005811
Chris Lattner6258fb22006-04-02 02:53:43 +00005812 // Finally, this must be the case where we are shrinking elements: each input
5813 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005814 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005815 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005816 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5817 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005819
Dan Gohman7f321562007-06-25 16:23:39 +00005820 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005821 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5822 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005823 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005824 continue;
5825 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005826
Jay Foad40f8f622010-12-07 08:25:19 +00005827 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5828 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005829
Chris Lattner6258fb22006-04-02 02:53:43 +00005830 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005831 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005832 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005833 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005834 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005835 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005836 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005837 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005838 }
5839
5840 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005841 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005842 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5843 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005844
Andrew Trickac6d9be2013-05-25 02:42:55 +00005845 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005846 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005847}
5848
Dan Gohman475871a2008-07-27 21:46:04 +00005849SDValue DAGCombiner::visitFADD(SDNode *N) {
5850 SDValue N0 = N->getOperand(0);
5851 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005852 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5853 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005854 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005855
Dan Gohman7f321562007-06-25 16:23:39 +00005856 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005857 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005858 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005859 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005861
Lang Hames01806942012-06-14 20:37:15 +00005862 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005863 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005864 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005865 // canonicalize constant to RHS
5866 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005867 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005868 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005869 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5870 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005871 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005872 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005873 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005874 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005875 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005876 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005877 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005878 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005879 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005880 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005881 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005882
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005883 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005884 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5885 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5886 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005887 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
5888 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005889 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005890
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005891 // No FP constant should be created after legalization as Instruction
5892 // Selection pass has hard time in dealing with FP constant.
5893 //
5894 // We don't need test this condition for transformation like following, as
5895 // the DAG being transformed implies it is legal to take FP constant as
5896 // operand.
5897 //
5898 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5899 //
5900 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5901
Owen Anderson607ebde2012-11-01 02:00:53 +00005902 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005903 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005904 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) {
5905 return DAG.getConstantFP(0.0, VT);
5906 }
5907
5908 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005909 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005910 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) {
5911 return DAG.getConstantFP(0.0, VT);
5912 }
5913
Owen Anderson43da6c72012-08-30 23:35:16 +00005914 // In unsafe math mode, we can fold chains of FADD's of the same value
5915 // into multiplications. This transform is not safe in general because
5916 // we are reducing the number of rounding steps.
5917 if (DAG.getTarget().Options.UnsafeFPMath &&
5918 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5919 !N0CFP && !N1CFP) {
5920 if (N0.getOpcode() == ISD::FMUL) {
5921 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5922 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5923
Stephen Lin38103d12013-06-14 18:17:35 +00005924 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005925 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005926 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005927 SDValue(CFP00, 0),
5928 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005929 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005930 N1, NewCFP);
5931 }
5932
Stephen Lin38103d12013-06-14 18:17:35 +00005933 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005934 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005935 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005936 SDValue(CFP01, 0),
5937 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005938 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005939 N1, NewCFP);
5940 }
5941
Stephen Lin38103d12013-06-14 18:17:35 +00005942 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00005943 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5944 N1.getOperand(0) == N1.getOperand(1) &&
5945 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005946 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005947 SDValue(CFP00, 0),
5948 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005949 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005950 N0.getOperand(1), NewCFP);
5951 }
5952
Stephen Lin38103d12013-06-14 18:17:35 +00005953 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00005954 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5955 N1.getOperand(0) == N1.getOperand(1) &&
5956 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005957 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005958 SDValue(CFP01, 0),
5959 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005960 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005961 N0.getOperand(0), NewCFP);
5962 }
5963 }
5964
5965 if (N1.getOpcode() == ISD::FMUL) {
5966 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5967 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5968
Stephen Lin38103d12013-06-14 18:17:35 +00005969 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005970 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005971 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005972 SDValue(CFP10, 0),
5973 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005974 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005975 N0, NewCFP);
5976 }
5977
Stephen Lin38103d12013-06-14 18:17:35 +00005978 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005979 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005980 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005981 SDValue(CFP11, 0),
5982 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005983 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005984 N0, NewCFP);
5985 }
5986
Owen Anderson43da6c72012-08-30 23:35:16 +00005987
Stephen Lin38103d12013-06-14 18:17:35 +00005988 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
5989 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
5990 N0.getOperand(0) == N0.getOperand(1) &&
5991 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005992 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005993 SDValue(CFP10, 0),
5994 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005995 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00005996 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00005997 }
5998
Stephen Lin38103d12013-06-14 18:17:35 +00005999 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6000 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6001 N0.getOperand(0) == N0.getOperand(1) &&
6002 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006003 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006004 SDValue(CFP11, 0),
6005 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006006 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006007 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006008 }
6009 }
6010
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006011 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006012 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006013 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006014 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
6015 (N0.getOperand(0) == N1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006016 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006017 N1, DAG.getConstantFP(3.0, VT));
6018 }
6019 }
6020
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006021 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006022 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006023 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006024 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
6025 N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006026 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006027 N0, DAG.getConstantFP(3.0, VT));
6028 }
6029 }
6030
Stephen Lina553bed2013-06-14 21:33:58 +00006031 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006032 if (AllowNewFpConst &&
6033 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006034 N0.getOperand(0) == N0.getOperand(1) &&
6035 N1.getOperand(0) == N1.getOperand(1) &&
6036 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006037 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006038 N0.getOperand(0),
6039 DAG.getConstantFP(4.0, VT));
6040 }
6041 }
6042
Lang Hamesd693caf2012-06-19 22:51:23 +00006043 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006044 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006045 DAG.getTarget().Options.UnsafeFPMath) &&
6046 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006047 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006048
6049 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
6050 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006051 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006052 N0.getOperand(0), N0.getOperand(1), N1);
6053 }
Owen Anderson43da6c72012-08-30 23:35:16 +00006054
Michael Liaob79bff52012-09-01 04:09:16 +00006055 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006056 // Note: Commutes FADD operands.
6057 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006058 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006059 N1.getOperand(0), N1.getOperand(1), N0);
6060 }
6061 }
6062
Dan Gohman475871a2008-07-27 21:46:04 +00006063 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006064}
6065
Dan Gohman475871a2008-07-27 21:46:04 +00006066SDValue DAGCombiner::visitFSUB(SDNode *N) {
6067 SDValue N0 = N->getOperand(0);
6068 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006069 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6070 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006071 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006072 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006073
Dan Gohman7f321562007-06-25 16:23:39 +00006074 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006075 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006076 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006077 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006079
Nate Begemana0e221d2005-10-18 00:28:13 +00006080 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006081 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006082 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006083 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006084 if (DAG.getTarget().Options.UnsafeFPMath &&
6085 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006086 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006087 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006088 if (DAG.getTarget().Options.UnsafeFPMath &&
6089 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006090 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006091 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006092 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006093 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006094 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006095 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006096 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006097 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006098 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006099
Bill Wendling5a894342012-03-15 05:12:00 +00006100 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006101 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006102 // (fsub x, (fadd x, y)) -> (fneg y) &
6103 // (fsub x, (fadd y, x)) -> (fneg y)
6104 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006105 if (N0 == N1)
6106 return DAG.getConstantFP(0.0f, VT);
6107
Bill Wendling5a894342012-03-15 05:12:00 +00006108 if (N1.getOpcode() == ISD::FADD) {
6109 SDValue N10 = N1->getOperand(0);
6110 SDValue N11 = N1->getOperand(1);
6111
6112 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6113 &DAG.getTarget().Options))
6114 return GetNegatedExpression(N11, DAG, LegalOperations);
6115 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6116 &DAG.getTarget().Options))
6117 return GetNegatedExpression(N10, DAG, LegalOperations);
6118 }
6119 }
6120
Lang Hamesd693caf2012-06-19 22:51:23 +00006121 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006122 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006123 DAG.getTarget().Options.UnsafeFPMath) &&
6124 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006125 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006126
6127 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
6128 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006129 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006130 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006131 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006132 }
6133
6134 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6135 // Note: Commutes FSUB operands.
6136 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006137 return DAG.getNode(ISD::FMA, dl, VT,
6138 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006139 N1.getOperand(0)),
6140 N1.getOperand(1), N0);
6141 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006142
6143 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
6144 if (N0.getOpcode() == ISD::FNEG &&
6145 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6146 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6147 SDValue N00 = N0.getOperand(0).getOperand(0);
6148 SDValue N01 = N0.getOperand(0).getOperand(1);
6149 return DAG.getNode(ISD::FMA, dl, VT,
6150 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6151 DAG.getNode(ISD::FNEG, dl, VT, N1));
6152 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006153 }
6154
Dan Gohman475871a2008-07-27 21:46:04 +00006155 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006156}
6157
Dan Gohman475871a2008-07-27 21:46:04 +00006158SDValue DAGCombiner::visitFMUL(SDNode *N) {
6159 SDValue N0 = N->getOperand(0);
6160 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006161 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6162 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006163 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006164 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006165
Dan Gohman7f321562007-06-25 16:23:39 +00006166 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006167 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006168 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006169 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006171
Nate Begeman11af4ea2005-10-17 20:40:11 +00006172 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006173 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006174 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006175 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006176 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006177 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006178 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006179 if (DAG.getTarget().Options.UnsafeFPMath &&
6180 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006181 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006182 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006183 if (DAG.getTarget().Options.UnsafeFPMath &&
6184 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006185 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006186 // fold (fmul A, 1.0) -> A
6187 if (N1CFP && N1CFP->isExactlyValue(1.0))
6188 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006189 // fold (fmul X, 2.0) -> (fadd X, X)
6190 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006191 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006192 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006193 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006194 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006195 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006196
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006197 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006198 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006199 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006200 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006201 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006202 // Both can be negated for free, check to see if at least one is cheaper
6203 // negated.
6204 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006205 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006206 GetNegatedExpression(N0, DAG, LegalOperations),
6207 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006208 }
6209 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006210
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006211 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006212 if (DAG.getTarget().Options.UnsafeFPMath &&
6213 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006214 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006215 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6216 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006217 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006218
Dan Gohman475871a2008-07-27 21:46:04 +00006219 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006220}
6221
Owen Anderson062c0a52012-05-02 22:17:40 +00006222SDValue DAGCombiner::visitFMA(SDNode *N) {
6223 SDValue N0 = N->getOperand(0);
6224 SDValue N1 = N->getOperand(1);
6225 SDValue N2 = N->getOperand(2);
6226 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6227 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6228 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006229 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006230
Owen Anderson607ebde2012-11-01 02:00:53 +00006231 if (DAG.getTarget().Options.UnsafeFPMath) {
6232 if (N0CFP && N0CFP->isZero())
6233 return N2;
6234 if (N1CFP && N1CFP->isZero())
6235 return N2;
6236 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006237 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006238 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006239 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006240 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006241
Owen Anderson85ef6f42012-05-30 18:50:39 +00006242 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006243 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006244 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006245
Owen Anderson58d57292012-09-01 06:04:27 +00006246 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6247 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6248 N2.getOpcode() == ISD::FMUL &&
6249 N0 == N2.getOperand(0) &&
6250 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6251 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6252 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6253 }
6254
6255
6256 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6257 if (DAG.getTarget().Options.UnsafeFPMath &&
6258 N0.getOpcode() == ISD::FMUL && N1CFP &&
6259 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6260 return DAG.getNode(ISD::FMA, dl, VT,
6261 N0.getOperand(0),
6262 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6263 N2);
6264 }
6265
6266 // (fma x, 1, y) -> (fadd x, y)
6267 // (fma x, -1, y) -> (fadd (fneg x), y)
6268 if (N1CFP) {
6269 if (N1CFP->isExactlyValue(1.0))
6270 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6271
6272 if (N1CFP->isExactlyValue(-1.0) &&
6273 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6274 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6275 AddToWorkList(RHSNeg.getNode());
6276 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6277 }
6278 }
6279
6280 // (fma x, c, x) -> (fmul x, (c+1))
6281 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6282 return DAG.getNode(ISD::FMUL, dl, VT,
6283 N0,
6284 DAG.getNode(ISD::FADD, dl, VT,
6285 N1, DAG.getConstantFP(1.0, VT)));
6286 }
6287
6288 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6289 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6290 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6291 return DAG.getNode(ISD::FMUL, dl, VT,
6292 N0,
6293 DAG.getNode(ISD::FADD, dl, VT,
6294 N1, DAG.getConstantFP(-1.0, VT)));
6295 }
6296
6297
Owen Anderson062c0a52012-05-02 22:17:40 +00006298 return SDValue();
6299}
6300
Dan Gohman475871a2008-07-27 21:46:04 +00006301SDValue DAGCombiner::visitFDIV(SDNode *N) {
6302 SDValue N0 = N->getOperand(0);
6303 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006304 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6305 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006306 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006307 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006308
Dan Gohman7f321562007-06-25 16:23:39 +00006309 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006310 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006311 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006312 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006314
Nate Begemana148d982006-01-18 22:35:16 +00006315 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006316 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006317 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006318
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006319 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006320 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006321 // Compute the reciprocal 1.0 / c2.
6322 APFloat N1APF = N1CFP->getValueAPF();
6323 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6324 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006325 // Only do the transform if the reciprocal is a legal fp immediate that
6326 // isn't too nasty (eg NaN, denormal, ...).
6327 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006328 (!LegalOperations ||
6329 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6330 // backend)... we should handle this gracefully after Legalize.
6331 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6332 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6333 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006334 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006335 DAG.getConstantFP(Recip, VT));
6336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006337
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006338 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006339 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006340 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006341 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006342 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006343 // Both can be negated for free, check to see if at least one is cheaper
6344 // negated.
6345 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006346 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006347 GetNegatedExpression(N0, DAG, LegalOperations),
6348 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006349 }
6350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006351
Dan Gohman475871a2008-07-27 21:46:04 +00006352 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006353}
6354
Dan Gohman475871a2008-07-27 21:46:04 +00006355SDValue DAGCombiner::visitFREM(SDNode *N) {
6356 SDValue N0 = N->getOperand(0);
6357 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006358 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6359 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006360 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006361
Nate Begemana148d982006-01-18 22:35:16 +00006362 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006363 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006364 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006365
Dan Gohman475871a2008-07-27 21:46:04 +00006366 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006367}
6368
Dan Gohman475871a2008-07-27 21:46:04 +00006369SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6370 SDValue N0 = N->getOperand(0);
6371 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006372 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6373 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006374 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006375
Ulrich Weigande669c932012-10-29 18:35:49 +00006376 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006377 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006378
Chris Lattner12d83032006-03-05 05:30:57 +00006379 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006380 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006381 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6382 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006383 if (!V.isNegative()) {
6384 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006385 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006386 } else {
6387 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006388 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6389 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006390 }
Chris Lattner12d83032006-03-05 05:30:57 +00006391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006392
Chris Lattner12d83032006-03-05 05:30:57 +00006393 // copysign(fabs(x), y) -> copysign(x, y)
6394 // copysign(fneg(x), y) -> copysign(x, y)
6395 // copysign(copysign(x,z), y) -> copysign(x, y)
6396 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6397 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006398 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006399 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006400
6401 // copysign(x, abs(y)) -> abs(x)
6402 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006403 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006404
Chris Lattner12d83032006-03-05 05:30:57 +00006405 // copysign(x, copysign(y,z)) -> copysign(x, z)
6406 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006407 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006408 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006409
Chris Lattner12d83032006-03-05 05:30:57 +00006410 // copysign(x, fp_extend(y)) -> copysign(x, y)
6411 // copysign(x, fp_round(y)) -> copysign(x, y)
6412 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006413 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006414 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006415
Dan Gohman475871a2008-07-27 21:46:04 +00006416 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006417}
6418
Dan Gohman475871a2008-07-27 21:46:04 +00006419SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6420 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006421 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006422 EVT VT = N->getValueType(0);
6423 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006424
Nate Begeman1d4d4142005-09-01 00:19:25 +00006425 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006426 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006427 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006428 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006429 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006430 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006431
Chris Lattnercda88752008-06-26 00:16:49 +00006432 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6433 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006434 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6435 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006436 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006437 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006438 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006439 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006440
Nadav Rotemed1a3352012-07-23 07:59:50 +00006441 // The next optimizations are desireable only if SELECT_CC can be lowered.
6442 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6443 // having to say they don't support SELECT_CC on every type the DAG knows
6444 // about, since there is no way to mark an opcode illegal at all value types
6445 // (See also visitSELECT)
6446 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6447 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6448 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6449 !VT.isVector() &&
6450 (!LegalOperations ||
6451 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6452 SDValue Ops[] =
6453 { N0.getOperand(0), N0.getOperand(1),
6454 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6455 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006456 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006457 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006458
Nadav Rotemed1a3352012-07-23 07:59:50 +00006459 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6460 // (select_cc x, y, 1.0, 0.0,, cc)
6461 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6462 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6463 (!LegalOperations ||
6464 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6465 SDValue Ops[] =
6466 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6467 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6468 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006469 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006470 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006471 }
6472
Dan Gohman475871a2008-07-27 21:46:04 +00006473 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006474}
6475
Dan Gohman475871a2008-07-27 21:46:04 +00006476SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6477 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006478 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006479 EVT VT = N->getValueType(0);
6480 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006481
Nate Begeman1d4d4142005-09-01 00:19:25 +00006482 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006483 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006484 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006485 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006486 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006487 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006488
Chris Lattnercda88752008-06-26 00:16:49 +00006489 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6490 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006491 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6492 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006493 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006494 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006495 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006496 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006497
Nadav Rotemed1a3352012-07-23 07:59:50 +00006498 // The next optimizations are desireable only if SELECT_CC can be lowered.
6499 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6500 // having to say they don't support SELECT_CC on every type the DAG knows
6501 // about, since there is no way to mark an opcode illegal at all value types
6502 // (See also visitSELECT)
6503 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6504 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006505
Nadav Rotemed1a3352012-07-23 07:59:50 +00006506 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6507 (!LegalOperations ||
6508 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6509 SDValue Ops[] =
6510 { N0.getOperand(0), N0.getOperand(1),
6511 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6512 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006513 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006514 }
6515 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006516
Dan Gohman475871a2008-07-27 21:46:04 +00006517 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006518}
6519
Dan Gohman475871a2008-07-27 21:46:04 +00006520SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6521 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006522 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006523 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006524
Nate Begeman1d4d4142005-09-01 00:19:25 +00006525 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006526 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006527 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006528
Dan Gohman475871a2008-07-27 21:46:04 +00006529 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006530}
6531
Dan Gohman475871a2008-07-27 21:46:04 +00006532SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6533 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006534 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006535 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006536
Nate Begeman1d4d4142005-09-01 00:19:25 +00006537 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006538 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006539 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006540
Dan Gohman475871a2008-07-27 21:46:04 +00006541 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006542}
6543
Dan Gohman475871a2008-07-27 21:46:04 +00006544SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6545 SDValue N0 = N->getOperand(0);
6546 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006547 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006548 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006549
Nate Begeman1d4d4142005-09-01 00:19:25 +00006550 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006551 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006552 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006553
Chris Lattner79dbea52006-03-13 06:26:26 +00006554 // fold (fp_round (fp_extend x)) -> x
6555 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6556 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006557
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006558 // fold (fp_round (fp_round x)) -> (fp_round x)
6559 if (N0.getOpcode() == ISD::FP_ROUND) {
6560 // This is a value preserving truncation if both round's are.
6561 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006562 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006563 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006564 DAG.getIntPtrConstant(IsTrunc));
6565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006566
Chris Lattner79dbea52006-03-13 06:26:26 +00006567 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006568 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006569 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006570 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006571 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006572 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006573 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006575
Dan Gohman475871a2008-07-27 21:46:04 +00006576 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006577}
6578
Dan Gohman475871a2008-07-27 21:46:04 +00006579SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6580 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006581 EVT VT = N->getValueType(0);
6582 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006583 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006584
Nate Begeman1d4d4142005-09-01 00:19:25 +00006585 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006586 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006587 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006588 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006589 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006590
Dan Gohman475871a2008-07-27 21:46:04 +00006591 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006592}
6593
Dan Gohman475871a2008-07-27 21:46:04 +00006594SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6595 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006596 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006597 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006598
Chris Lattner5938bef2007-12-29 06:55:23 +00006599 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006600 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006601 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006602 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006603
Nate Begeman1d4d4142005-09-01 00:19:25 +00006604 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006605 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006606 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006607
6608 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6609 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006610 if (N0.getOpcode() == ISD::FP_ROUND
6611 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006612 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006613 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006614 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006615 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006616 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006617 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006618 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006619
Chris Lattner0bd48932008-01-17 07:00:52 +00006620 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006621 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006622 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006623 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006624 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006625 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006626 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006627 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006628 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006629 LN0->isVolatile(), LN0->isNonTemporal(),
6630 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006631 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006632 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006633 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006634 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006635 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006636 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006637 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006638
Dan Gohman475871a2008-07-27 21:46:04 +00006639 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006640}
6641
Dan Gohman475871a2008-07-27 21:46:04 +00006642SDValue DAGCombiner::visitFNEG(SDNode *N) {
6643 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006644 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006645
Craig Topperdd201ff2012-09-11 01:45:21 +00006646 if (VT.isVector()) {
6647 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6648 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006649 }
6650
Owen Andersonafd3d562012-03-06 00:29:31 +00006651 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6652 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006653 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006654
Chris Lattner3bd39d42008-01-27 17:42:27 +00006655 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6656 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006657 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006658 !VT.isVector() &&
6659 N0.getNode()->hasOneUse() &&
6660 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006662 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006663 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006664 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006665 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006666 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006667 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006668 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006669 }
6670 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006671
Owen Anderson58d57292012-09-01 06:04:27 +00006672 // (fneg (fmul c, x)) -> (fmul -c, x)
6673 if (N0.getOpcode() == ISD::FMUL) {
6674 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6675 if (CFP1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006676 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006677 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006678 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006679 N0.getOperand(1)));
6680 }
6681 }
6682
Dan Gohman475871a2008-07-27 21:46:04 +00006683 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006684}
6685
Owen Anderson7c626d32012-08-13 23:32:49 +00006686SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6687 SDValue N0 = N->getOperand(0);
6688 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6689 EVT VT = N->getValueType(0);
6690
6691 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006692 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006693 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006694
6695 return SDValue();
6696}
6697
6698SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6699 SDValue N0 = N->getOperand(0);
6700 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6701 EVT VT = N->getValueType(0);
6702
6703 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006704 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006705 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006706
6707 return SDValue();
6708}
6709
6710SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6711 SDValue N0 = N->getOperand(0);
6712 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6713 EVT VT = N->getValueType(0);
6714
6715 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006716 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006717 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006718
6719 return SDValue();
6720}
6721
Dan Gohman475871a2008-07-27 21:46:04 +00006722SDValue DAGCombiner::visitFABS(SDNode *N) {
6723 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006724 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006725 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006726
Craig Topperdd201ff2012-09-11 01:45:21 +00006727 if (VT.isVector()) {
6728 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6729 if (FoldedVOp.getNode()) return FoldedVOp;
6730 }
6731
Nate Begeman1d4d4142005-09-01 00:19:25 +00006732 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006733 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006734 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006735 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006736 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006737 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006738 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006739 // fold (fabs (fcopysign x, y)) -> (fabs x)
6740 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006741 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006742
Chris Lattner3bd39d42008-01-27 17:42:27 +00006743 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6744 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006745 if (!TLI.isFAbsFree(VT) &&
6746 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006747 N0.getOperand(0).getValueType().isInteger() &&
6748 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006749 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006750 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006751 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006752 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006753 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006754 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006755 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006756 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006757 }
6758 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006759
Dan Gohman475871a2008-07-27 21:46:04 +00006760 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006761}
6762
Dan Gohman475871a2008-07-27 21:46:04 +00006763SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6764 SDValue Chain = N->getOperand(0);
6765 SDValue N1 = N->getOperand(1);
6766 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006767
Dan Gohmane0f06c72009-11-17 00:47:23 +00006768 // If N is a constant we could fold this into a fallthrough or unconditional
6769 // branch. However that doesn't happen very often in normal code, because
6770 // Instcombine/SimplifyCFG should have handled the available opportunities.
6771 // If we did this folding here, it would be necessary to update the
6772 // MachineBasicBlock CFG, which is awkward.
6773
Nate Begeman750ac1b2006-02-01 07:19:44 +00006774 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6775 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006776 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006777 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6778 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006779 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006780 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006781 N1.getOperand(0), N1.getOperand(1), N2);
6782 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006783
Evan Cheng2a135ae2010-10-04 22:41:01 +00006784 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6785 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6786 (N1.getOperand(0).hasOneUse() &&
6787 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6788 SDNode *Trunc = 0;
6789 if (N1.getOpcode() == ISD::TRUNCATE) {
6790 // Look pass the truncate.
6791 Trunc = N1.getNode();
6792 N1 = N1.getOperand(0);
6793 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006794
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006795 // Match this pattern so that we can generate simpler code:
6796 //
6797 // %a = ...
6798 // %b = and i32 %a, 2
6799 // %c = srl i32 %b, 1
6800 // brcond i32 %c ...
6801 //
6802 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006803 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006804 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006805 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006806 // %c = setcc eq %b, 0
6807 // brcond %c ...
6808 //
6809 // This applies only when the AND constant value has one bit set and the
6810 // SRL constant is equal to the log2 of the AND constant. The back-end is
6811 // smart enough to convert the result into a TEST/JMP sequence.
6812 SDValue Op0 = N1.getOperand(0);
6813 SDValue Op1 = N1.getOperand(1);
6814
6815 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006816 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006817 SDValue AndOp1 = Op0.getOperand(1);
6818
6819 if (AndOp1.getOpcode() == ISD::Constant) {
6820 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6821
6822 if (AndConst.isPowerOf2() &&
6823 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6824 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006825 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006826 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006827 Op0, DAG.getConstant(0, Op0.getValueType()),
6828 ISD::SETNE);
6829
Andrew Trickac6d9be2013-05-25 02:42:55 +00006830 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006831 MVT::Other, Chain, SetCC, N2);
6832 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6833 // will convert it back to (X & C1) >> C2.
6834 CombineTo(N, NewBRCond, false);
6835 // Truncate is dead.
6836 if (Trunc) {
6837 removeFromWorkList(Trunc);
6838 DAG.DeleteNode(Trunc);
6839 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006840 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006841 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006842 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006843 removeFromWorkList(N1.getNode());
6844 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006845 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006846 }
6847 }
6848 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006849
6850 if (Trunc)
6851 // Restore N1 if the above transformation doesn't match.
6852 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006854
Evan Cheng2c755ba2010-02-27 07:36:59 +00006855 // Transform br(xor(x, y)) -> br(x != y)
6856 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6857 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6858 SDNode *TheXor = N1.getNode();
6859 SDValue Op0 = TheXor->getOperand(0);
6860 SDValue Op1 = TheXor->getOperand(1);
6861 if (Op0.getOpcode() == Op1.getOpcode()) {
6862 // Avoid missing important xor optimizations.
6863 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006864 if (Tmp.getNode()) {
6865 if (Tmp.getNode() != TheXor) {
6866 DEBUG(dbgs() << "\nReplacing.8 ";
6867 TheXor->dump(&DAG);
6868 dbgs() << "\nWith: ";
6869 Tmp.getNode()->dump(&DAG);
6870 dbgs() << '\n');
6871 WorkListRemover DeadNodes(*this);
6872 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6873 removeFromWorkList(TheXor);
6874 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006875 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006876 MVT::Other, Chain, Tmp, N2);
6877 }
6878
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006879 // visitXOR has changed XOR's operands or replaced the XOR completely,
6880 // bail out.
6881 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006882 }
6883 }
6884
6885 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6886 bool Equal = false;
6887 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6888 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6889 Op0.getOpcode() == ISD::XOR) {
6890 TheXor = Op0.getNode();
6891 Equal = true;
6892 }
6893
Evan Cheng2a135ae2010-10-04 22:41:01 +00006894 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006895 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00006896 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006897 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006898 SetCCVT,
6899 Op0, Op1,
6900 Equal ? ISD::SETEQ : ISD::SETNE);
6901 // Replace the uses of XOR with SETCC
6902 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006903 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006904 removeFromWorkList(N1.getNode());
6905 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006906 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006907 MVT::Other, Chain, SetCC, N2);
6908 }
6909 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006910
Dan Gohman475871a2008-07-27 21:46:04 +00006911 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006912}
6913
Chris Lattner3ea0b472005-10-05 06:47:48 +00006914// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6915//
Dan Gohman475871a2008-07-27 21:46:04 +00006916SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006917 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006918 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006919
Dan Gohmane0f06c72009-11-17 00:47:23 +00006920 // If N is a constant we could fold this into a fallthrough or unconditional
6921 // branch. However that doesn't happen very often in normal code, because
6922 // Instcombine/SimplifyCFG should have handled the available opportunities.
6923 // If we did this folding here, it would be necessary to update the
6924 // MachineBasicBlock CFG, which is awkward.
6925
Duncan Sands8eab8a22008-06-09 11:32:28 +00006926 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00006927 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006928 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006929 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006930 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006931
Nate Begemane17daeb2005-10-05 21:43:42 +00006932 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006933 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006934 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006935 N->getOperand(0), Simp.getOperand(2),
6936 Simp.getOperand(0), Simp.getOperand(1),
6937 N->getOperand(4));
6938
Dan Gohman475871a2008-07-27 21:46:04 +00006939 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006940}
6941
Evan Chengc4b527a2012-01-13 01:37:24 +00006942/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6943/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006944/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006945static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6946 SelectionDAG &DAG,
6947 const TargetLowering &TLI) {
6948 EVT VT;
6949 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6950 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6951 return false;
6952 VT = Use->getValueType(0);
6953 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6954 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6955 return false;
6956 VT = ST->getValue().getValueType();
6957 } else
6958 return false;
6959
Chandler Carruth56d433d2013-01-07 15:14:13 +00006960 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00006961 if (N->getOpcode() == ISD::ADD) {
6962 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6963 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006964 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006965 AM.BaseOffs = Offset->getSExtValue();
6966 else
Evan Cheng03be3622012-03-06 23:33:32 +00006967 // [reg +/- reg]
6968 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006969 } else if (N->getOpcode() == ISD::SUB) {
6970 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6971 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006972 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006973 AM.BaseOffs = -Offset->getSExtValue();
6974 else
Evan Cheng03be3622012-03-06 23:33:32 +00006975 // [reg +/- reg]
6976 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006977 } else
6978 return false;
6979
6980 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6981}
6982
Duncan Sandsec87aa82008-06-15 20:12:31 +00006983/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6984/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006985/// and it has other uses besides the load / store. After the
6986/// transformation, the new indexed load / store has effectively folded
6987/// the add / subtract in and all of its other uses are redirected to the
6988/// new load / store.
6989bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006990 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006991 return false;
6992
6993 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006994 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006995 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006996 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006997 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006998 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006999 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007000 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007001 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7002 return false;
7003 Ptr = LD->getBasePtr();
7004 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007005 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007006 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007007 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007008 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7009 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7010 return false;
7011 Ptr = ST->getBasePtr();
7012 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007013 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007014 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007015 }
Chris Lattner448f2192006-11-11 00:39:41 +00007016
Chris Lattner9f1794e2006-11-11 00:56:29 +00007017 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7018 // out. There is no reason to make this a preinc/predec.
7019 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007020 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007021 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007022
Chris Lattner9f1794e2006-11-11 00:56:29 +00007023 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007024 SDValue BasePtr;
7025 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007026 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7027 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7028 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007029
7030 // Backends without true r+i pre-indexed forms may need to pass a
7031 // constant base with a variable offset so that constant coercion
7032 // will work with the patterns in canonical form.
7033 bool Swapped = false;
7034 if (isa<ConstantSDNode>(BasePtr)) {
7035 std::swap(BasePtr, Offset);
7036 Swapped = true;
7037 }
7038
Evan Chenga7d4a042007-05-03 23:52:19 +00007039 // Don't create a indexed load / store with zero offset.
7040 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007041 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007042 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007043
Chris Lattner41e53fd2006-11-11 01:00:15 +00007044 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007045 // 1) The new base ptr is a frame index.
7046 // 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 +00007047 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007048 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007049 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007050 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007051
Chris Lattner41e53fd2006-11-11 01:00:15 +00007052 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7053 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007054 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007055 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007056
Chris Lattner41e53fd2006-11-11 01:00:15 +00007057 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007058 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007059 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007060 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007061 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007062 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007063
Hal Finkel089a5f82013-02-08 21:35:47 +00007064 // If the offset is a constant, there may be other adds of constants that
7065 // can be folded with this one. We should do this to avoid having to keep
7066 // a copy of the original base pointer.
7067 SmallVector<SDNode *, 16> OtherUses;
7068 if (isa<ConstantSDNode>(Offset))
7069 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7070 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7071 SDNode *Use = *I;
7072 if (Use == Ptr.getNode())
7073 continue;
7074
7075 if (Use->isPredecessorOf(N))
7076 continue;
7077
7078 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7079 OtherUses.clear();
7080 break;
7081 }
7082
7083 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7084 if (Op1.getNode() == BasePtr.getNode())
7085 std::swap(Op0, Op1);
7086 assert(Op0.getNode() == BasePtr.getNode() &&
7087 "Use of ADD/SUB but not an operand");
7088
7089 if (!isa<ConstantSDNode>(Op1)) {
7090 OtherUses.clear();
7091 break;
7092 }
7093
7094 // FIXME: In some cases, we can be smarter about this.
7095 if (Op1.getValueType() != Offset.getValueType()) {
7096 OtherUses.clear();
7097 break;
7098 }
7099
7100 OtherUses.push_back(Use);
7101 }
7102
7103 if (Swapped)
7104 std::swap(BasePtr, Offset);
7105
Evan Chengc843abe2007-05-24 02:35:39 +00007106 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007107 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007108
7109 // Caches for hasPredecessorHelper
7110 SmallPtrSet<const SDNode *, 32> Visited;
7111 SmallVector<const SDNode *, 16> Worklist;
7112
Gabor Greifba36cb52008-08-28 21:40:38 +00007113 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7114 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007115 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007116 if (Use == N)
7117 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007118 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007119 return false;
7120
Evan Chengc4b527a2012-01-13 01:37:24 +00007121 // If Ptr may be folded in addressing mode of other use, then it's
7122 // not profitable to do this transformation.
7123 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007124 RealUse = true;
7125 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007126
Chris Lattner9f1794e2006-11-11 00:56:29 +00007127 if (!RealUse)
7128 return false;
7129
Dan Gohman475871a2008-07-27 21:46:04 +00007130 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007131 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007132 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007133 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007134 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007135 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007136 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007137 ++PreIndexedNodes;
7138 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007139 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007140 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007141 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007142 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007143 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007144 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007145 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007146 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7147 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007148 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007149 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007150 }
7151
Chris Lattner9f1794e2006-11-11 00:56:29 +00007152 // Finally, since the node is now dead, remove it from the graph.
7153 DAG.DeleteNode(N);
7154
Hal Finkel089a5f82013-02-08 21:35:47 +00007155 if (Swapped)
7156 std::swap(BasePtr, Offset);
7157
7158 // Replace other uses of BasePtr that can be updated to use Ptr
7159 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7160 unsigned OffsetIdx = 1;
7161 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7162 OffsetIdx = 0;
7163 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7164 BasePtr.getNode() && "Expected BasePtr operand");
7165
Silviu Baranga730a5702013-04-26 15:52:24 +00007166 // We need to replace ptr0 in the following expression:
7167 // x0 * offset0 + y0 * ptr0 = t0
7168 // knowing that
7169 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
7170 //
7171 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7172 // indexed load/store and the expresion that needs to be re-written.
7173 //
7174 // Therefore, we have:
7175 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007176
7177 ConstantSDNode *CN =
7178 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007179 int X0, X1, Y0, Y1;
7180 APInt Offset0 = CN->getAPIntValue();
7181 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007182
Silviu Baranga730a5702013-04-26 15:52:24 +00007183 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7184 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7185 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7186 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007187
Silviu Baranga730a5702013-04-26 15:52:24 +00007188 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7189
7190 APInt CNV = Offset0;
7191 if (X0 < 0) CNV = -CNV;
7192 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7193 else CNV = CNV - Offset1;
7194
7195 // We can now generate the new expression.
7196 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7197 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7198
7199 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007200 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007201 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7202 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7203 removeFromWorkList(OtherUses[i]);
7204 DAG.DeleteNode(OtherUses[i]);
7205 }
7206
Chris Lattner9f1794e2006-11-11 00:56:29 +00007207 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007208 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007209 removeFromWorkList(Ptr.getNode());
7210 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007211
7212 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007213}
7214
Duncan Sandsec87aa82008-06-15 20:12:31 +00007215/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007216/// add / sub of the base pointer node into a post-indexed load / store.
7217/// The transformation folded the add / subtract into the new indexed
7218/// load / store effectively and all of its uses are redirected to the
7219/// new load / store.
7220bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007221 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007222 return false;
7223
7224 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007225 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007226 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007227 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007228 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007229 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007230 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007231 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7232 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7233 return false;
7234 Ptr = LD->getBasePtr();
7235 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007236 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007237 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007238 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007239 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7240 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7241 return false;
7242 Ptr = ST->getBasePtr();
7243 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007244 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007245 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007246 }
Chris Lattner448f2192006-11-11 00:39:41 +00007247
Gabor Greifba36cb52008-08-28 21:40:38 +00007248 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007249 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007250
Gabor Greifba36cb52008-08-28 21:40:38 +00007251 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7252 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007253 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007254 if (Op == N ||
7255 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7256 continue;
7257
Dan Gohman475871a2008-07-27 21:46:04 +00007258 SDValue BasePtr;
7259 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007260 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7261 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007262 // Don't create a indexed load / store with zero offset.
7263 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007264 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007265 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007266
Chris Lattner9f1794e2006-11-11 00:56:29 +00007267 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007268 // 1) All uses are load / store ops that use it as base ptr (and
7269 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007270 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7271 // nor a successor of N. Otherwise, if Op is folded that would
7272 // create a cycle.
7273
Evan Chengcaab1292009-05-06 18:25:01 +00007274 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7275 continue;
7276
Chris Lattner9f1794e2006-11-11 00:56:29 +00007277 // Check for #1.
7278 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007279 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7280 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007281 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007282 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007283 continue;
7284
Chris Lattner9f1794e2006-11-11 00:56:29 +00007285 // If all the uses are load / store addresses, then don't do the
7286 // transformation.
7287 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7288 bool RealUse = false;
7289 for (SDNode::use_iterator III = Use->use_begin(),
7290 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007291 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00007292 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007293 RealUse = true;
7294 }
Chris Lattner448f2192006-11-11 00:39:41 +00007295
Chris Lattner9f1794e2006-11-11 00:56:29 +00007296 if (!RealUse) {
7297 TryNext = true;
7298 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007299 }
7300 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007301 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007302
Chris Lattner9f1794e2006-11-11 00:56:29 +00007303 if (TryNext)
7304 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007305
Chris Lattner9f1794e2006-11-11 00:56:29 +00007306 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007307 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007309 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007310 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007311 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007312 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007313 ++PostIndexedNodes;
7314 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007315 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007316 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007317 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007318 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007319 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007320 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007321 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007322 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7323 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007324 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007325 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007326 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007327
Chris Lattner9f1794e2006-11-11 00:56:29 +00007328 // Finally, since the node is now dead, remove it from the graph.
7329 DAG.DeleteNode(N);
7330
7331 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007332 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007333 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007334 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007335 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007336 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007337 }
7338 }
7339 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007340
Chris Lattner448f2192006-11-11 00:39:41 +00007341 return false;
7342}
7343
Dan Gohman475871a2008-07-27 21:46:04 +00007344SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007345 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007346 SDValue Chain = LD->getChain();
7347 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007348
Evan Cheng45a7ca92007-05-01 00:38:21 +00007349 // If load is not volatile and there are no uses of the loaded value (and
7350 // the updated indexed value in case of indexed loads), change uses of the
7351 // chain value into uses of the chain input (i.e. delete the dead load).
7352 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007353 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007354 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007355 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007356 // It's not safe to use the two value CombineTo variant here. e.g.
7357 // v1, chain2 = load chain1, loc
7358 // v2, chain3 = load chain2, loc
7359 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007360 // Now we replace use of chain2 with chain1. This makes the second load
7361 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007362 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007363 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007364 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007365 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007366 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007367 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007368 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007369
Chris Lattner125991a2008-01-24 07:57:06 +00007370 if (N->use_empty()) {
7371 removeFromWorkList(N);
7372 DAG.DeleteNode(N);
7373 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007374
Dan Gohman475871a2008-07-27 21:46:04 +00007375 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007376 }
Evan Cheng498f5592007-05-01 08:53:39 +00007377 } else {
7378 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007379 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007380 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007381 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007382 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007383 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007384 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007385 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007386 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007387 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007388 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007389 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007390 DAG.getUNDEF(N->getValueType(1)));
7391 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007392 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007393 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007394 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007395 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007396 }
7397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007398
Chris Lattner01a22022005-10-10 22:04:48 +00007399 // If this load is directly stored, replace the load value with the stored
7400 // value.
7401 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007402 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007403 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007404 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007405 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7406 if (PrevST->getBasePtr() == Ptr &&
7407 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007408 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007409 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007411
Evan Cheng255f20f2010-04-01 06:04:33 +00007412 // Try to infer better alignment information than the load already has.
7413 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007414 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007415 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7416 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007417 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007418 LD->getValueType(0),
7419 Chain, Ptr, LD->getPointerInfo(),
7420 LD->getMemoryVT(),
7421 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007422 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7423 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007424 }
7425 }
7426
Jim Laskey7ca56af2006-10-11 13:47:09 +00007427 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007428 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007429 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007430
Jim Laskey6ff23e52006-10-04 16:53:27 +00007431 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007432 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007433 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007434
Jim Laskey279f0532006-09-25 16:29:54 +00007435 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007436 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007437 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007438 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007439 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007440 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007441 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007442 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007443 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007444 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007445 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007446 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007447 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007448 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007449 }
Jim Laskey279f0532006-09-25 16:29:54 +00007450
Jim Laskey6ff23e52006-10-04 16:53:27 +00007451 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007452 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007453 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007454
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007455 // Make sure the new and old chains are cleaned up.
7456 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007457
Jim Laskey274062c2006-10-13 23:32:28 +00007458 // Replace uses with load result and token factor. Don't add users
7459 // to work list.
7460 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007461 }
7462 }
7463
Evan Cheng7fc033a2006-11-03 03:06:21 +00007464 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007465 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007466 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007467
Dan Gohman475871a2008-07-27 21:46:04 +00007468 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007469}
7470
Chris Lattner2392ae72010-04-15 04:48:01 +00007471/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7472/// load is having specific bytes cleared out. If so, return the byte size
7473/// being masked out and the shift amount.
7474static std::pair<unsigned, unsigned>
7475CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7476 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007477
Chris Lattner2392ae72010-04-15 04:48:01 +00007478 // Check for the structure we're looking for.
7479 if (V->getOpcode() != ISD::AND ||
7480 !isa<ConstantSDNode>(V->getOperand(1)) ||
7481 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7482 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007483
Chris Lattnere6987582010-04-15 06:10:49 +00007484 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007485 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007486 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007487
Chris Lattnere6987582010-04-15 06:10:49 +00007488 // The store should be chained directly to the load or be an operand of a
7489 // tokenfactor.
7490 if (LD == Chain.getNode())
7491 ; // ok.
7492 else if (Chain->getOpcode() != ISD::TokenFactor)
7493 return Result; // Fail.
7494 else {
7495 bool isOk = false;
7496 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7497 if (Chain->getOperand(i).getNode() == LD) {
7498 isOk = true;
7499 break;
7500 }
7501 if (!isOk) return Result;
7502 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007503
Chris Lattner2392ae72010-04-15 04:48:01 +00007504 // This only handles simple types.
7505 if (V.getValueType() != MVT::i16 &&
7506 V.getValueType() != MVT::i32 &&
7507 V.getValueType() != MVT::i64)
7508 return Result;
7509
7510 // Check the constant mask. Invert it so that the bits being masked out are
7511 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7512 // follow the sign bit for uniformity.
7513 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007514 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007515 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007516 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007517 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7518 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007519
Chris Lattner2392ae72010-04-15 04:48:01 +00007520 // See if we have a continuous run of bits. If so, we have 0*1+0*
7521 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7522 return Result;
7523
7524 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7525 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7526 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007527
Chris Lattner2392ae72010-04-15 04:48:01 +00007528 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7529 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007530 case 1:
7531 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007532 case 4: break;
7533 default: return Result; // All one mask, or 5-byte mask.
7534 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007535
Chris Lattner2392ae72010-04-15 04:48:01 +00007536 // Verify that the first bit starts at a multiple of mask so that the access
7537 // is aligned the same as the access width.
7538 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007539
Chris Lattner2392ae72010-04-15 04:48:01 +00007540 Result.first = MaskedBytes;
7541 Result.second = NotMaskTZ/8;
7542 return Result;
7543}
7544
7545
7546/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7547/// provides a value as specified by MaskInfo. If so, replace the specified
7548/// store with a narrower store of truncated IVal.
7549static SDNode *
7550ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7551 SDValue IVal, StoreSDNode *St,
7552 DAGCombiner *DC) {
7553 unsigned NumBytes = MaskInfo.first;
7554 unsigned ByteShift = MaskInfo.second;
7555 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007556
Chris Lattner2392ae72010-04-15 04:48:01 +00007557 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7558 // that uses this. If not, this is not a replacement.
7559 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7560 ByteShift*8, (ByteShift+NumBytes)*8);
7561 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007562
Chris Lattner2392ae72010-04-15 04:48:01 +00007563 // Check that it is legal on the target to do this. It is legal if the new
7564 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7565 // legalization.
7566 MVT VT = MVT::getIntegerVT(NumBytes*8);
7567 if (!DC->isTypeLegal(VT))
7568 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007569
Chris Lattner2392ae72010-04-15 04:48:01 +00007570 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7571 // shifted by ByteShift and truncated down to NumBytes.
7572 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007573 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007574 DAG.getConstant(ByteShift*8,
7575 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007576
7577 // Figure out the offset for the store and the alignment of the access.
7578 unsigned StOffset;
7579 unsigned NewAlign = St->getAlignment();
7580
7581 if (DAG.getTargetLoweringInfo().isLittleEndian())
7582 StOffset = ByteShift;
7583 else
7584 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007585
Chris Lattner2392ae72010-04-15 04:48:01 +00007586 SDValue Ptr = St->getBasePtr();
7587 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007588 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007589 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7590 NewAlign = MinAlign(NewAlign, StOffset);
7591 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007592
Chris Lattner2392ae72010-04-15 04:48:01 +00007593 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007594 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007595
Chris Lattner2392ae72010-04-15 04:48:01 +00007596 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007597 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007598 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007599 false, false, NewAlign).getNode();
7600}
7601
Evan Cheng8b944d32009-05-28 00:35:15 +00007602
7603/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7604/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7605/// of the loaded bits, try narrowing the load and store if it would end up
7606/// being a win for performance or code size.
7607SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7608 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007609 if (ST->isVolatile())
7610 return SDValue();
7611
Evan Cheng8b944d32009-05-28 00:35:15 +00007612 SDValue Chain = ST->getChain();
7613 SDValue Value = ST->getValue();
7614 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007615 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007616
7617 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007618 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007619
7620 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007621
Chris Lattner2392ae72010-04-15 04:48:01 +00007622 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7623 // is a byte mask indicating a consecutive number of bytes, check to see if
7624 // Y is known to provide just those bytes. If so, we try to replace the
7625 // load + replace + store sequence with a single (narrower) store, which makes
7626 // the load dead.
7627 if (Opc == ISD::OR) {
7628 std::pair<unsigned, unsigned> MaskedLoad;
7629 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7630 if (MaskedLoad.first)
7631 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7632 Value.getOperand(1), ST,this))
7633 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007634
Chris Lattner2392ae72010-04-15 04:48:01 +00007635 // Or is commutative, so try swapping X and Y.
7636 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7637 if (MaskedLoad.first)
7638 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7639 Value.getOperand(0), ST,this))
7640 return SDValue(NewST, 0);
7641 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007642
Evan Cheng8b944d32009-05-28 00:35:15 +00007643 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7644 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007645 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007646
7647 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007648 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7649 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007650 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007651 if (LD->getBasePtr() != Ptr ||
7652 LD->getPointerInfo().getAddrSpace() !=
7653 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007654 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007655
7656 // Find the type to narrow it the load / op / store to.
7657 SDValue N1 = Value.getOperand(1);
7658 unsigned BitWidth = N1.getValueSizeInBits();
7659 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7660 if (Opc == ISD::AND)
7661 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007662 if (Imm == 0 || Imm.isAllOnesValue())
7663 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007664 unsigned ShAmt = Imm.countTrailingZeros();
7665 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7666 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007667 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007668 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007669 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007670 TLI.isNarrowingProfitable(VT, NewVT))) {
7671 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007672 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007673 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007674 if (NewBW >= BitWidth)
7675 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007676
7677 // If the lsb changed does not start at the type bitwidth boundary,
7678 // start at the previous one.
7679 if (ShAmt % NewBW)
7680 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007681 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7682 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007683 if ((Imm & Mask) == Imm) {
7684 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7685 if (Opc == ISD::AND)
7686 NewImm ^= APInt::getAllOnesValue(NewBW);
7687 uint64_t PtrOff = ShAmt / 8;
7688 // For big endian targets, we need to adjust the offset to the pointer to
7689 // load the correct bytes.
7690 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007691 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007692
7693 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007694 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007695 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007696 return SDValue();
7697
Andrew Trickac6d9be2013-05-25 02:42:55 +00007698 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007699 Ptr.getValueType(), Ptr,
7700 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007701 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007702 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007703 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007704 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007705 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007706 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007707 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007708 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007709 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007710 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007711 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007712
7713 AddToWorkList(NewPtr.getNode());
7714 AddToWorkList(NewLD.getNode());
7715 AddToWorkList(NewVal.getNode());
7716 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007717 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007718 ++OpsNarrowed;
7719 return NewST;
7720 }
7721 }
7722
Evan Chengcdcecc02009-05-28 18:41:02 +00007723 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007724}
7725
Evan Cheng31959b12011-02-02 01:06:55 +00007726/// TransformFPLoadStorePair - For a given floating point load / store pair,
7727/// if the load value isn't used by any other operations, then consider
7728/// transforming the pair to integer load / store operations if the target
7729/// deems the transformation profitable.
7730SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7731 StoreSDNode *ST = cast<StoreSDNode>(N);
7732 SDValue Chain = ST->getChain();
7733 SDValue Value = ST->getValue();
7734 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7735 Value.hasOneUse() &&
7736 Chain == SDValue(Value.getNode(), 1)) {
7737 LoadSDNode *LD = cast<LoadSDNode>(Value);
7738 EVT VT = LD->getMemoryVT();
7739 if (!VT.isFloatingPoint() ||
7740 VT != ST->getMemoryVT() ||
7741 LD->isNonTemporal() ||
7742 ST->isNonTemporal() ||
7743 LD->getPointerInfo().getAddrSpace() != 0 ||
7744 ST->getPointerInfo().getAddrSpace() != 0)
7745 return SDValue();
7746
7747 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7748 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7749 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7750 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7751 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7752 return SDValue();
7753
7754 unsigned LDAlign = LD->getAlignment();
7755 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007756 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007757 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007758 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7759 return SDValue();
7760
Andrew Trickac6d9be2013-05-25 02:42:55 +00007761 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007762 LD->getChain(), LD->getBasePtr(),
7763 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007764 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007765
Andrew Trickac6d9be2013-05-25 02:42:55 +00007766 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007767 NewLD, ST->getBasePtr(),
7768 ST->getPointerInfo(),
7769 false, false, STAlign);
7770
7771 AddToWorkList(NewLD.getNode());
7772 AddToWorkList(NewST.getNode());
7773 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007774 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007775 ++LdStFP2Int;
7776 return NewST;
7777 }
7778
7779 return SDValue();
7780}
7781
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007782/// Helper struct to parse and store a memory address as base + index + offset.
7783/// We ignore sign extensions when it is safe to do so.
7784/// The following two expressions are not equivalent. To differentiate we need
7785/// to store whether there was a sign extension involved in the index
7786/// computation.
7787/// (load (i64 add (i64 copyfromreg %c)
7788/// (i64 signextend (add (i8 load %index)
7789/// (i8 1))))
7790/// vs
7791///
7792/// (load (i64 add (i64 copyfromreg %c)
7793/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7794/// (i32 1)))))
7795struct BaseIndexOffset {
7796 SDValue Base;
7797 SDValue Index;
7798 int64_t Offset;
7799 bool IsIndexSignExt;
7800
7801 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7802
7803 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7804 bool IsIndexSignExt) :
7805 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7806
7807 bool equalBaseIndex(const BaseIndexOffset &Other) {
7808 return Other.Base == Base && Other.Index == Index &&
7809 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007810 }
7811
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007812 /// Parses tree in Ptr for base, index, offset addresses.
7813 static BaseIndexOffset match(SDValue Ptr) {
7814 bool IsIndexSignExt = false;
7815
7816 // Just Base or possibly anything else.
7817 if (Ptr->getOpcode() != ISD::ADD)
7818 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7819
7820 // Base + offset.
7821 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7822 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7823 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7824 IsIndexSignExt);
7825 }
7826
7827 // Look at Base + Index + Offset cases.
7828 SDValue Base = Ptr->getOperand(0);
7829 SDValue IndexOffset = Ptr->getOperand(1);
7830
7831 // Skip signextends.
7832 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7833 IndexOffset = IndexOffset->getOperand(0);
7834 IsIndexSignExt = true;
7835 }
7836
7837 // Either the case of Base + Index (no offset) or something else.
7838 if (IndexOffset->getOpcode() != ISD::ADD)
7839 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7840
7841 // Now we have the case of Base + Index + offset.
7842 SDValue Index = IndexOffset->getOperand(0);
7843 SDValue Offset = IndexOffset->getOperand(1);
7844
7845 if (!isa<ConstantSDNode>(Offset))
7846 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7847
7848 // Ignore signextends.
7849 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7850 Index = Index->getOperand(0);
7851 IsIndexSignExt = true;
7852 } else IsIndexSignExt = false;
7853
7854 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7855 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7856 }
7857};
Nadav Rotemc653de62012-10-03 16:11:15 +00007858
7859/// Holds a pointer to an LSBaseSDNode as well as information on where it
7860/// is located in a sequence of memory operations connected by a chain.
7861struct MemOpLink {
7862 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7863 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7864 // Ptr to the mem node.
7865 LSBaseSDNode *MemNode;
7866 // Offset from the base ptr.
7867 int64_t OffsetFromBase;
7868 // What is the sequence number of this mem node.
7869 // Lowest mem operand in the DAG starts at zero.
7870 unsigned SequenceNum;
7871};
7872
7873/// Sorts store nodes in a link according to their offset from a shared
7874// base ptr.
7875struct ConsecutiveMemoryChainSorter {
7876 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7877 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7878 }
7879};
7880
7881bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7882 EVT MemVT = St->getMemoryVT();
7883 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007884 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7885 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007886
7887 // Don't merge vectors into wider inputs.
7888 if (MemVT.isVector() || !MemVT.isSimple())
7889 return false;
7890
7891 // Perform an early exit check. Do not bother looking at stored values that
7892 // are not constants or loads.
7893 SDValue StoredVal = St->getValue();
7894 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7895 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7896 !IsLoadSrc)
7897 return false;
7898
7899 // Only look at ends of store sequences.
7900 SDValue Chain = SDValue(St, 1);
7901 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7902 return false;
7903
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007904 // This holds the base pointer, index, and the offset in bytes from the base
7905 // pointer.
7906 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007907
7908 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007909 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007910 return false;
7911
7912 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007913 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007914 return false;
7915
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007916 // Save the LoadSDNodes that we find in the chain.
7917 // We need to make sure that these nodes do not interfere with
7918 // any of the store nodes.
7919 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7920
7921 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00007922 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007923
Nadav Rotemc653de62012-10-03 16:11:15 +00007924 // Walk up the chain and look for nodes with offsets from the same
7925 // base pointer. Stop when reaching an instruction with a different kind
7926 // or instruction which has a different base pointer.
7927 unsigned Seq = 0;
7928 StoreSDNode *Index = St;
7929 while (Index) {
7930 // If the chain has more than one use, then we can't reorder the mem ops.
7931 if (Index != St && !SDValue(Index, 1)->hasOneUse())
7932 break;
7933
7934 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007935 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007936
7937 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007938 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00007939 break;
7940
7941 // Check that the alignment is the same.
7942 if (Index->getAlignment() != St->getAlignment())
7943 break;
7944
7945 // The memory operands must not be volatile.
7946 if (Index->isVolatile() || Index->isIndexed())
7947 break;
7948
7949 // No truncation.
7950 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
7951 if (St->isTruncatingStore())
7952 break;
7953
7954 // The stored memory type must be the same.
7955 if (Index->getMemoryVT() != MemVT)
7956 break;
7957
7958 // We do not allow unaligned stores because we want to prevent overriding
7959 // stores.
7960 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
7961 break;
7962
7963 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007964 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00007965
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007966 // Find the next memory operand in the chain. If the next operand in the
7967 // chain is a store then move up and continue the scan with the next
7968 // memory operand. If the next operand is a load save it and use alias
7969 // information to check if it interferes with anything.
7970 SDNode *NextInChain = Index->getChain().getNode();
7971 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00007972 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007973 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00007974 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007975 break;
7976 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
7977 // Save the load node for later. Continue the scan.
7978 AliasLoadNodes.push_back(Ldn);
7979 NextInChain = Ldn->getChain().getNode();
7980 continue;
7981 } else {
7982 Index = NULL;
7983 break;
7984 }
7985 }
Nadav Rotemc653de62012-10-03 16:11:15 +00007986 }
7987
7988 // Check if there is anything to merge.
7989 if (StoreNodes.size() < 2)
7990 return false;
7991
7992 // Sort the memory operands according to their distance from the base pointer.
7993 std::sort(StoreNodes.begin(), StoreNodes.end(),
7994 ConsecutiveMemoryChainSorter());
7995
7996 // Scan the memory operations on the chain and find the first non-consecutive
7997 // store memory address.
7998 unsigned LastConsecutiveStore = 0;
7999 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008000 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8001
8002 // Check that the addresses are consecutive starting from the second
8003 // element in the list of stores.
8004 if (i > 0) {
8005 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8006 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8007 break;
8008 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008009
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008010 bool Alias = false;
8011 // Check if this store interferes with any of the loads that we found.
8012 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8013 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8014 Alias = true;
8015 break;
8016 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008017 // We found a load that alias with this store. Stop the sequence.
8018 if (Alias)
8019 break;
8020
Nadav Rotemc653de62012-10-03 16:11:15 +00008021 // Mark this node as useful.
8022 LastConsecutiveStore = i;
8023 }
8024
8025 // The node with the lowest store address.
8026 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8027
8028 // Store the constants into memory as one consecutive store.
8029 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008030 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008031 unsigned LastLegalVectorType = 0;
8032 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008033 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8034 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8035 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008036
8037 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008038 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008039 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008040 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008041 } else {
8042 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008043 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008044 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008045
Nadav Rotemc653de62012-10-03 16:11:15 +00008046 // Find a legal type for the constant store.
8047 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8048 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8049 if (TLI.isTypeLegal(StoreTy))
8050 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008051 // Or check whether a truncstore is legal.
8052 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8053 TargetLowering::TypePromoteInteger) {
8054 EVT LegalizedStoredValueTy =
8055 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8056 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8057 LastLegalType = i+1;
8058 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008059
8060 // Find a legal type for the vector store.
8061 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8062 if (TLI.isTypeLegal(Ty))
8063 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008064 }
8065
Bob Wilson99d8e762012-12-20 01:36:20 +00008066 // We only use vectors if the constant is known to be zero and the
8067 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008068 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008069 LastLegalVectorType = 0;
8070
Nadav Rotemc653de62012-10-03 16:11:15 +00008071 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008072 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008073 return false;
8074
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008075 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008076 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8077
8078 // Make sure we have something to merge.
8079 if (NumElem < 2)
8080 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008081
8082 unsigned EarliestNodeUsed = 0;
8083 for (unsigned i=0; i < NumElem; ++i) {
8084 // Find a chain for the new wide-store operand. Notice that some
8085 // of the store nodes that we found may not be selected for inclusion
8086 // in the wide store. The chain we use needs to be the chain of the
8087 // earliest store node which is *used* and replaced by the wide store.
8088 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8089 EarliestNodeUsed = i;
8090 }
8091
8092 // The earliest Node in the DAG.
8093 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008094 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008095
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008096 SDValue StoredVal;
8097 if (UseVector) {
8098 // Find a legal type for the vector store.
8099 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8100 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8101 StoredVal = DAG.getConstant(0, Ty);
8102 } else {
8103 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8104 APInt StoreInt(StoreBW, 0);
8105
8106 // Construct a single integer constant which is made of the smaller
8107 // constant inputs.
8108 bool IsLE = TLI.isLittleEndian();
8109 for (unsigned i = 0; i < NumElem ; ++i) {
8110 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8111 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8112 SDValue Val = St->getValue();
8113 StoreInt<<=ElementSizeBytes*8;
8114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8115 StoreInt|=C->getAPIntValue().zext(StoreBW);
8116 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8117 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8118 } else {
8119 assert(false && "Invalid constant element type");
8120 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008121 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008122
8123 // Create the new Load and Store operations.
8124 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8125 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008126 }
8127
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008128 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008129 FirstInChain->getBasePtr(),
8130 FirstInChain->getPointerInfo(),
8131 false, false,
8132 FirstInChain->getAlignment());
8133
8134 // Replace the first store with the new store
8135 CombineTo(EarliestOp, NewStore);
8136 // Erase all other stores.
8137 for (unsigned i = 0; i < NumElem ; ++i) {
8138 if (StoreNodes[i].MemNode == EarliestOp)
8139 continue;
8140 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008141 // ReplaceAllUsesWith will replace all uses that existed when it was
8142 // called, but graph optimizations may cause new ones to appear. For
8143 // example, the case in pr14333 looks like
8144 //
8145 // St's chain -> St -> another store -> X
8146 //
8147 // And the only difference from St to the other store is the chain.
8148 // When we change it's chain to be St's chain they become identical,
8149 // get CSEed and the net result is that X is now a use of St.
8150 // Since we know that St is redundant, just iterate.
8151 while (!St->use_empty())
8152 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008153 removeFromWorkList(St);
8154 DAG.DeleteNode(St);
8155 }
8156
8157 return true;
8158 }
8159
8160 // Below we handle the case of multiple consecutive stores that
8161 // come from multiple consecutive loads. We merge them into a single
8162 // wide load and a single wide store.
8163
8164 // Look for load nodes which are used by the stored values.
8165 SmallVector<MemOpLink, 8> LoadNodes;
8166
8167 // Find acceptable loads. Loads need to have the same chain (token factor),
8168 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008169 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008170 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8171 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8172 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8173 if (!Ld) break;
8174
8175 // Loads must only have one use.
8176 if (!Ld->hasNUsesOfValue(1, 0))
8177 break;
8178
8179 // Check that the alignment is the same as the stores.
8180 if (Ld->getAlignment() != St->getAlignment())
8181 break;
8182
8183 // The memory operands must not be volatile.
8184 if (Ld->isVolatile() || Ld->isIndexed())
8185 break;
8186
8187 // We do not accept ext loads.
8188 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8189 break;
8190
8191 // The stored memory type must be the same.
8192 if (Ld->getMemoryVT() != MemVT)
8193 break;
8194
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008195 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008196 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008197 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008198 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008199 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008200 break;
8201 } else {
8202 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008203 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008204 }
8205
8206 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008207 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008208 }
8209
8210 if (LoadNodes.size() < 2)
8211 return false;
8212
8213 // Scan the memory operations on the chain and find the first non-consecutive
8214 // load memory address. These variables hold the index in the store node
8215 // array.
8216 unsigned LastConsecutiveLoad = 0;
8217 // This variable refers to the size and not index in the array.
8218 unsigned LastLegalVectorType = 0;
8219 unsigned LastLegalIntegerType = 0;
8220 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008221 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8222 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8223 // All loads much share the same chain.
8224 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8225 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008226
Nadav Rotemc653de62012-10-03 16:11:15 +00008227 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8228 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8229 break;
8230 LastConsecutiveLoad = i;
8231
8232 // Find a legal type for the vector store.
8233 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8234 if (TLI.isTypeLegal(StoreTy))
8235 LastLegalVectorType = i + 1;
8236
8237 // Find a legal type for the integer store.
8238 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8239 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8240 if (TLI.isTypeLegal(StoreTy))
8241 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008242 // Or check whether a truncstore and extload is legal.
8243 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8244 TargetLowering::TypePromoteInteger) {
8245 EVT LegalizedStoredValueTy =
8246 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8247 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8248 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8249 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8250 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8251 LastLegalIntegerType = i+1;
8252 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008253 }
8254
8255 // Only use vector types if the vector type is larger than the integer type.
8256 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008257 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008258 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8259
8260 // We add +1 here because the LastXXX variables refer to location while
8261 // the NumElem refers to array/index size.
8262 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8263 NumElem = std::min(LastLegalType, NumElem);
8264
8265 if (NumElem < 2)
8266 return false;
8267
8268 // The earliest Node in the DAG.
8269 unsigned EarliestNodeUsed = 0;
8270 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8271 for (unsigned i=1; i<NumElem; ++i) {
8272 // Find a chain for the new wide-store operand. Notice that some
8273 // of the store nodes that we found may not be selected for inclusion
8274 // in the wide store. The chain we use needs to be the chain of the
8275 // earliest store node which is *used* and replaced by the wide store.
8276 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8277 EarliestNodeUsed = i;
8278 }
8279
8280 // Find if it is better to use vectors or integers to load and store
8281 // to memory.
8282 EVT JointMemOpVT;
8283 if (UseVectorTy) {
8284 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8285 } else {
8286 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8287 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8288 }
8289
Andrew Trickac6d9be2013-05-25 02:42:55 +00008290 SDLoc LoadDL(LoadNodes[0].MemNode);
8291 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008292
8293 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8294 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8295 FirstLoad->getChain(),
8296 FirstLoad->getBasePtr(),
8297 FirstLoad->getPointerInfo(),
8298 false, false, false,
8299 FirstLoad->getAlignment());
8300
8301 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8302 FirstInChain->getBasePtr(),
8303 FirstInChain->getPointerInfo(), false, false,
8304 FirstInChain->getAlignment());
8305
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008306 // Replace one of the loads with the new load.
8307 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8308 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8309 SDValue(NewLoad.getNode(), 1));
8310
8311 // Remove the rest of the load chains.
8312 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008313 // Replace all chain users of the old load nodes with the chain of the new
8314 // load node.
8315 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008316 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8317 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008318
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008319 // Replace the first store with the new store.
8320 CombineTo(EarliestOp, NewStore);
8321 // Erase all other stores.
8322 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008323 // Remove all Store nodes.
8324 if (StoreNodes[i].MemNode == EarliestOp)
8325 continue;
8326 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8327 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8328 removeFromWorkList(St);
8329 DAG.DeleteNode(St);
8330 }
8331
8332 return true;
8333}
8334
Dan Gohman475871a2008-07-27 21:46:04 +00008335SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008336 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008337 SDValue Chain = ST->getChain();
8338 SDValue Value = ST->getValue();
8339 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008340
Evan Cheng59d5b682007-05-07 21:27:48 +00008341 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008342 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008343 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008344 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008345 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008346 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008347 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008348 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008349 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008350 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008351 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008352 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008353 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008354 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008355 }
Owen Andersona34d9362011-04-14 17:30:49 +00008356
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008357 // Turn 'store undef, Ptr' -> nothing.
8358 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8359 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008360
Nate Begeman2cbba892006-12-11 02:23:46 +00008361 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008362 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008363 // NOTE: If the original store is volatile, this transform must not increase
8364 // the number of stores. For example, on x86-32 an f64 can be stored in one
8365 // processor operation but an i64 (which is not legal) requires two. So the
8366 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008367 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008368 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008369 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008370 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008371 case MVT::f16: // We don't do this for these yet.
8372 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 case MVT::f128:
8374 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008375 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008376 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008377 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008378 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008379 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008380 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008381 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008382 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008383 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008384 }
8385 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008386 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008387 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008388 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008389 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008390 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008392 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008393 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008394 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008395 }
Owen Andersona34d9362011-04-14 17:30:49 +00008396
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008397 if (!ST->isVolatile() &&
8398 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008399 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008400 // argument passing. Since this is so common, custom legalize the
8401 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008402 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008403 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8404 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008405 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008406
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008407 unsigned Alignment = ST->getAlignment();
8408 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008409 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008410
Andrew Trickac6d9be2013-05-25 02:42:55 +00008411 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008412 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008413 isVolatile, isNonTemporal,
8414 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008415 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008416 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008417 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008418 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008419 Ptr, ST->getPointerInfo().getWithOffset(4),
8420 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008421 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008422 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008423 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008424 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008425
Chris Lattner62be1a72006-12-12 04:16:14 +00008426 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008427 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008428 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008429 }
8430
Evan Cheng255f20f2010-04-01 06:04:33 +00008431 // Try to infer better alignment information than the store already has.
8432 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008433 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8434 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008435 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008436 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8437 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008438 }
8439 }
8440
Evan Cheng31959b12011-02-02 01:06:55 +00008441 // Try transforming a pair floating point load / store ops to integer
8442 // load / store ops.
8443 SDValue NewST = TransformFPLoadStorePair(N);
8444 if (NewST.getNode())
8445 return NewST;
8446
Scott Michelfdc40a02009-02-17 22:15:04 +00008447 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008448 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008449 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008450
Jim Laskey6ff23e52006-10-04 16:53:27 +00008451 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008452 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008453 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008454
8455 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008456 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008457 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008458 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008459 ST->getMemoryVT(), ST->isVolatile(),
8460 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008461 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008462 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008463 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008464 ST->isVolatile(), ST->isNonTemporal(),
8465 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008467
Jim Laskey279f0532006-09-25 16:29:54 +00008468 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008469 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008471
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008472 // Make sure the new and old chains are cleaned up.
8473 AddToWorkList(Token.getNode());
8474
Jim Laskey274062c2006-10-13 23:32:28 +00008475 // Don't add users to work list.
8476 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008477 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008479
Evan Cheng33dbedc2006-11-05 09:31:14 +00008480 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008481 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008482 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008483
Chris Lattner3c872852007-12-29 06:26:16 +00008484 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008485 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008486 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008487 // See if we can simplify the input to this truncstore with knowledge that
8488 // only the low bits are being used. For example:
8489 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008490 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008491 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008492 APInt::getLowBitsSet(
8493 Value.getValueType().getScalarType().getSizeInBits(),
8494 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008495 AddToWorkList(Value.getNode());
8496 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008497 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008498 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008499 ST->isVolatile(), ST->isNonTemporal(),
8500 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008501
Chris Lattnere33544c2007-10-13 06:58:48 +00008502 // Otherwise, see if we can simplify the operation with
8503 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008504 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008505 APInt::getLowBitsSet(
8506 Value.getValueType().getScalarType().getSizeInBits(),
8507 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008508 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008509 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008510
Chris Lattner3c872852007-12-29 06:26:16 +00008511 // If this is a load followed by a store to the same location, then the store
8512 // is dead/noop.
8513 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008514 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008515 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008516 // There can't be any side effects between the load and store, such as
8517 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008518 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008519 // The store is dead, remove it.
8520 return Chain;
8521 }
8522 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008523
Chris Lattnerddf89562008-01-17 19:59:44 +00008524 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8525 // truncating store. We can do this even if this is already a truncstore.
8526 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008527 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008528 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008529 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008530 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008531 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008532 ST->isVolatile(), ST->isNonTemporal(),
8533 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008534 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008535
Nadav Rotemc653de62012-10-03 16:11:15 +00008536 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008537 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008538 if (!LegalTypes) {
8539 bool EverChanged = false;
8540
8541 do {
8542 // There can be multiple store sequences on the same chain.
8543 // Keep trying to merge store sequences until we are unable to do so
8544 // or until we merge the last store on the chain.
8545 bool Changed = MergeConsecutiveStores(ST);
8546 EverChanged |= Changed;
8547 if (!Changed) break;
8548 } while (ST->getOpcode() != ISD::DELETED_NODE);
8549
8550 if (EverChanged)
8551 return SDValue(N, 0);
8552 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008553
Evan Cheng8b944d32009-05-28 00:35:15 +00008554 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008555}
8556
Dan Gohman475871a2008-07-27 21:46:04 +00008557SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8558 SDValue InVec = N->getOperand(0);
8559 SDValue InVal = N->getOperand(1);
8560 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008561 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008562
Bob Wilson492fd452010-05-19 23:42:58 +00008563 // If the inserted element is an UNDEF, just use the input vector.
8564 if (InVal.getOpcode() == ISD::UNDEF)
8565 return InVec;
8566
Nadav Rotem609d54e2011-02-12 14:40:33 +00008567 EVT VT = InVec.getValueType();
8568
Owen Anderson95771af2011-02-25 21:41:48 +00008569 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008570 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8571 return SDValue();
8572
Eli Friedman9db817f2011-09-09 21:04:06 +00008573 // Check that we know which element is being inserted
8574 if (!isa<ConstantSDNode>(EltNo))
8575 return SDValue();
8576 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008577
Eli Friedman9db817f2011-09-09 21:04:06 +00008578 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8579 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8580 // vector elements.
8581 SmallVector<SDValue, 8> Ops;
8582 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
8583 Ops.append(InVec.getNode()->op_begin(),
8584 InVec.getNode()->op_end());
8585 } else if (InVec.getOpcode() == ISD::UNDEF) {
8586 unsigned NElts = VT.getVectorNumElements();
8587 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8588 } else {
8589 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008590 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008591
8592 // Insert the element
8593 if (Elt < Ops.size()) {
8594 // All the operands of BUILD_VECTOR must have the same type;
8595 // we enforce that here.
8596 EVT OpVT = Ops[0].getValueType();
8597 if (InVal.getValueType() != OpVT)
8598 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8599 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8600 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8601 Ops[Elt] = InVal;
8602 }
8603
8604 // Return the new vector
8605 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8606 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008607}
8608
Dan Gohman475871a2008-07-27 21:46:04 +00008609SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008610 // (vextract (scalar_to_vector val, 0) -> val
8611 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008612 EVT VT = InVec.getValueType();
8613 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008614
Duncan Sandsc356f332011-05-09 08:03:33 +00008615 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8616 // Check if the result type doesn't match the inserted element type. A
8617 // SCALAR_TO_VECTOR may truncate the inserted element and the
8618 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8619 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008620 if (InOp.getValueType() != NVT) {
8621 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008622 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008623 }
8624 return InOp;
8625 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008626
Nadav Rotemba05c912012-01-17 21:44:01 +00008627 SDValue EltNo = N->getOperand(1);
8628 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8629
8630 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8631 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008632 // we may introduce new vector instructions which are not backed by TD
8633 // patterns. For example on AVX, extracting elements from a wide vector
8634 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008635 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8636 && ConstEltNo && !LegalOperations) {
8637 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8638 int NumElem = VT.getVectorNumElements();
8639 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8640 // Find the new index to extract from.
8641 int OrigElt = SVOp->getMaskElt(Elt);
8642
8643 // Extracting an undef index is undef.
8644 if (OrigElt == -1)
8645 return DAG.getUNDEF(NVT);
8646
8647 // Select the right vector half to extract from.
8648 if (OrigElt < NumElem) {
8649 InVec = InVec->getOperand(0);
8650 } else {
8651 InVec = InVec->getOperand(1);
8652 OrigElt -= NumElem;
8653 }
8654
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008655 EVT IndexTy = N->getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008656 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008657 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008658 }
8659
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008660 // Perform only after legalization to ensure build_vector / vector_shuffle
8661 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008662 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008663
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008664 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8665 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8666 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008667
Nadav Rotemba05c912012-01-17 21:44:01 +00008668 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008669 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008670 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008671 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008672 EVT ExtVT = VT.getVectorElementType();
8673 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008674
Evan Cheng84387ea2012-03-13 22:00:52 +00008675 // If the result of load has to be truncated, then it's not necessarily
8676 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008677 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008678 return SDValue();
8679
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008680 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008681 // Don't duplicate a load with other uses.
8682 if (!InVec.hasOneUse())
8683 return SDValue();
8684
Owen Andersone50ed302009-08-10 22:56:29 +00008685 EVT BCVT = InVec.getOperand(0).getValueType();
8686 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008687 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008688 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8689 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008690 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008691 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008692 NewLoad = true;
8693 }
Evan Cheng513da432007-10-06 08:19:55 +00008694
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008695 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008696 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008697 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008698 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008699 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008700 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008701 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008702 // Don't duplicate a load with other uses.
8703 if (!InVec.hasOneUse())
8704 return SDValue();
8705
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008706 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008707 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008708 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8709 // =>
8710 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008711
Eli Friedmand6e25602011-12-26 22:49:32 +00008712 // Don't duplicate a load with other uses.
8713 if (!InVec.hasOneUse())
8714 return SDValue();
8715
Mon P Wanga60b5232008-12-11 00:26:16 +00008716 // If the bit convert changed the number of elements, it is unsafe
8717 // to examine the mask.
8718 if (BCNumEltsChanged)
8719 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008720
8721 // Select the input vector, guarding against out of range extract vector.
8722 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008723 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008724 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8725
Eli Friedmand6e25602011-12-26 22:49:32 +00008726 if (InVec.getOpcode() == ISD::BITCAST) {
8727 // Don't duplicate a load with other uses.
8728 if (!InVec.hasOneUse())
8729 return SDValue();
8730
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008731 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008732 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008733 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008734 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008735 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008736 }
8737 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008738
Eli Friedmand6e25602011-12-26 22:49:32 +00008739 // Make sure we found a non-volatile load and the extractelement is
8740 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008741 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008742 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008743
Eric Christopherd81f17a2010-11-03 20:44:42 +00008744 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8745 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008746 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008747
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008748 unsigned Align = LN0->getAlignment();
8749 if (NewLoad) {
8750 // Check the resultant load doesn't need a higher alignment than the
8751 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008752 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008753 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008754 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008755
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008756 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008757 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008758
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008759 Align = NewAlign;
8760 }
8761
Dan Gohman475871a2008-07-27 21:46:04 +00008762 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008763 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008764
Eric Christopherd81f17a2010-11-03 20:44:42 +00008765 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008766 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008767 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008768 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008769 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008770 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008771 DAG.getConstant(PtrOff, PtrType));
8772 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008773
Eli Friedman4db4add2011-11-16 23:50:22 +00008774 // The replacement we need to do here is a little tricky: we need to
8775 // replace an extractelement of a load with a load.
8776 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008777 // Note that this replacement assumes that the extractvalue is the only
8778 // use of the load; that's okay because we don't want to perform this
8779 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008780 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008781 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008782 if (NVT.bitsGT(LVT)) {
8783 // If the result type of vextract is wider than the load, then issue an
8784 // extending load instead.
8785 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8786 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008787 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008788 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8789 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008790 Chain = Load.getValue(1);
8791 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008792 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008793 LN0->getPointerInfo().getWithOffset(PtrOff),
8794 LN0->isVolatile(), LN0->isNonTemporal(),
8795 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008796 Chain = Load.getValue(1);
8797 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008798 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008799 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008800 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008801 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008802 WorkListRemover DeadNodes(*this);
8803 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008804 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008805 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008806 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8807 // worklist explicitly as well.
8808 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008809 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008810 // Make sure to revisit this node to clean it up; it will usually be dead.
8811 AddToWorkList(N);
8812 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008813 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008814
Dan Gohman475871a2008-07-27 21:46:04 +00008815 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008816}
Evan Cheng513da432007-10-06 08:19:55 +00008817
Michael Liaofac14ab2012-10-23 23:06:52 +00008818// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8819SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8820 // We perform this optimization post type-legalization because
8821 // the type-legalizer often scalarizes integer-promoted vectors.
8822 // Performing this optimization before may create bit-casts which
8823 // will be type-legalized to complex code sequences.
8824 // We perform this optimization only before the operation legalizer because we
8825 // may introduce illegal operations.
8826 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8827 return SDValue();
8828
Dan Gohman7f321562007-06-25 16:23:39 +00008829 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008830 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008831 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008832
Nadav Rotemb00418a2011-10-29 21:23:04 +00008833 // Check to see if this is a BUILD_VECTOR of a bunch of values
8834 // which come from any_extend or zero_extend nodes. If so, we can create
8835 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008836 // optimizations. We do not handle sign-extend because we can't fill the sign
8837 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008838 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008839 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008840
Craig Topperd3b58892012-01-17 09:09:48 +00008841 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008842 SDValue In = N->getOperand(i);
8843 // Ignore undef inputs.
8844 if (In.getOpcode() == ISD::UNDEF) continue;
8845
8846 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8847 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8848
Nadav Rotemf47368b2011-10-31 20:08:25 +00008849 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008850 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008851 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008852 break;
8853 }
8854
8855 // The input is a ZeroExt or AnyExt. Check the original type.
8856 EVT InTy = In.getOperand(0).getValueType();
8857
8858 // Check that all of the widened source types are the same.
8859 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008860 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008861 SourceType = InTy;
8862 else if (InTy != SourceType) {
8863 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008864 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008865 break;
8866 }
8867
8868 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008869 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008870 }
8871
Nadav Rotemf47368b2011-10-31 20:08:25 +00008872 // In order to have valid types, all of the inputs must be extended from the
8873 // same source type and all of the inputs must be any or zero extend.
8874 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008875 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008876 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008877 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8878 isPowerOf2_32(SourceType.getSizeInBits());
8879
Nadav Rotem6431ff92012-03-15 08:49:06 +00008880 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8881 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008882 if (!ValidTypes)
8883 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008884
Michael Liaofac14ab2012-10-23 23:06:52 +00008885 bool isLE = TLI.isLittleEndian();
8886 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8887 assert(ElemRatio > 1 && "Invalid element size ratio");
8888 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8889 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008890
Michael Liaofac14ab2012-10-23 23:06:52 +00008891 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8892 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008893
Michael Liaofac14ab2012-10-23 23:06:52 +00008894 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008895 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008896 SDValue Cast = N->getOperand(i);
8897 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8898 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8899 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8900 SDValue In;
8901 if (Cast.getOpcode() == ISD::UNDEF)
8902 In = DAG.getUNDEF(SourceType);
8903 else
8904 In = Cast->getOperand(0);
8905 unsigned Index = isLE ? (i * ElemRatio) :
8906 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008907
Michael Liaofac14ab2012-10-23 23:06:52 +00008908 assert(Index < Ops.size() && "Invalid index");
8909 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008910 }
Chris Lattnerca242442006-03-19 01:27:56 +00008911
Michael Liaofac14ab2012-10-23 23:06:52 +00008912 // The type of the new BUILD_VECTOR node.
8913 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8914 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8915 "Invalid vector size");
8916 // Check if the new vector type is legal.
8917 if (!isTypeLegal(VecVT)) return SDValue();
8918
8919 // Make the new BUILD_VECTOR.
8920 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
8921
8922 // The new BUILD_VECTOR node has the potential to be further optimized.
8923 AddToWorkList(BV.getNode());
8924 // Bitcast to the desired type.
8925 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8926}
8927
Michael Liao1a5cc712012-10-24 04:14:18 +00008928SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
8929 EVT VT = N->getValueType(0);
8930
8931 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008932 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00008933
8934 EVT SrcVT = MVT::Other;
8935 unsigned Opcode = ISD::DELETED_NODE;
8936 unsigned NumDefs = 0;
8937
8938 for (unsigned i = 0; i != NumInScalars; ++i) {
8939 SDValue In = N->getOperand(i);
8940 unsigned Opc = In.getOpcode();
8941
8942 if (Opc == ISD::UNDEF)
8943 continue;
8944
8945 // If all scalar values are floats and converted from integers.
8946 if (Opcode == ISD::DELETED_NODE &&
8947 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
8948 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00008949 }
Tom Stellardd40758b2013-01-02 22:13:01 +00008950
Michael Liao1a5cc712012-10-24 04:14:18 +00008951 if (Opc != Opcode)
8952 return SDValue();
8953
8954 EVT InVT = In.getOperand(0).getValueType();
8955
8956 // If all scalar values are typed differently, bail out. It's chosen to
8957 // simplify BUILD_VECTOR of integer types.
8958 if (SrcVT == MVT::Other)
8959 SrcVT = InVT;
8960 if (SrcVT != InVT)
8961 return SDValue();
8962 NumDefs++;
8963 }
8964
8965 // If the vector has just one element defined, it's not worth to fold it into
8966 // a vectorized one.
8967 if (NumDefs < 2)
8968 return SDValue();
8969
8970 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
8971 && "Should only handle conversion from integer to float.");
8972 assert(SrcVT != MVT::Other && "Cannot determine source type!");
8973
8974 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00008975
8976 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
8977 return SDValue();
8978
Michael Liao1a5cc712012-10-24 04:14:18 +00008979 SmallVector<SDValue, 8> Opnds;
8980 for (unsigned i = 0; i != NumInScalars; ++i) {
8981 SDValue In = N->getOperand(i);
8982
8983 if (In.getOpcode() == ISD::UNDEF)
8984 Opnds.push_back(DAG.getUNDEF(SrcVT));
8985 else
8986 Opnds.push_back(In.getOperand(0));
8987 }
8988 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
8989 &Opnds[0], Opnds.size());
8990 AddToWorkList(BV.getNode());
8991
8992 return DAG.getNode(Opcode, dl, VT, BV);
8993}
8994
Michael Liaofac14ab2012-10-23 23:06:52 +00008995SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
8996 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008997 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00008998 EVT VT = N->getValueType(0);
8999
9000 // A vector built entirely of undefs is undef.
9001 if (ISD::allOperandsUndef(N))
9002 return DAG.getUNDEF(VT);
9003
9004 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9005 if (V.getNode())
9006 return V;
9007
Michael Liao1a5cc712012-10-24 04:14:18 +00009008 V = reduceBuildVecConvertToConvertBuildVec(N);
9009 if (V.getNode())
9010 return V;
9011
Dan Gohman7f321562007-06-25 16:23:39 +00009012 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9013 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9014 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009015
9016 // May only combine to shuffle after legalize if shuffle is legal.
9017 if (LegalOperations &&
9018 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9019 return SDValue();
9020
Dan Gohman475871a2008-07-27 21:46:04 +00009021 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009022 for (unsigned i = 0; i != NumInScalars; ++i) {
9023 // Ignore undef inputs.
9024 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009025
Dan Gohman7f321562007-06-25 16:23:39 +00009026 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009027 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009028 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009029 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009030 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009031 break;
9032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009033
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009034 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009035 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009036 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9037 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009038
Gabor Greifba36cb52008-08-28 21:40:38 +00009039 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009040 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009041 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009042 VecIn2 = ExtractedFromVec;
9043 } else {
9044 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009045 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009046 break;
9047 }
9048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009049
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009050 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009051 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009052 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009053 for (unsigned i = 0; i != NumInScalars; ++i) {
9054 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009055 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009056 continue;
9057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009058
Rafael Espindola15684b22009-04-24 12:40:33 +00009059 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009060 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009061 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009062 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009063 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9064 if (ExtIndex > VT.getVectorNumElements())
9065 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009066
Nate Begeman5a5ca152009-04-29 05:20:52 +00009067 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009068 continue;
9069 }
9070
9071 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009072 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009073 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009074 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009075
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009076 // We can't generate a shuffle node with mismatched input and output types.
9077 // Attempt to transform a single input vector to the correct type.
9078 if ((VT != VecIn1.getValueType())) {
9079 // We don't support shuffeling between TWO values of different types.
9080 if (VecIn2.getNode() != 0)
9081 return SDValue();
9082
9083 // We only support widening of vectors which are half the size of the
9084 // output registers. For example XMM->YMM widening on X86 with AVX.
9085 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9086 return SDValue();
9087
James Molloy8cd08bf2012-09-10 14:01:21 +00009088 // If the input vector type has a different base type to the output
9089 // vector type, bail out.
9090 if (VecIn1.getValueType().getVectorElementType() !=
9091 VT.getVectorElementType())
9092 return SDValue();
9093
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009094 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009095 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009096 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009097 }
9098
9099 // If VecIn2 is unused then change it to undef.
9100 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9101
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009102 // Check that we were able to transform all incoming values to the same
9103 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009104 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9105 VecIn1.getValueType() != VT)
9106 return SDValue();
9107
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009108 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009109 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009110 return SDValue();
9111
Dan Gohman7f321562007-06-25 16:23:39 +00009112 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009113 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009114 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009115 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009116 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009118
Dan Gohman475871a2008-07-27 21:46:04 +00009119 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009120}
9121
Dan Gohman475871a2008-07-27 21:46:04 +00009122SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009123 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9124 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9125 // inputs come from at most two distinct vectors, turn this into a shuffle
9126 // node.
9127
9128 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009129 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009130 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009131
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009132 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009133 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009134 return DAG.getUNDEF(N->getValueType(0));
9135
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009136 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9137 // nodes often generate nop CONCAT_VECTOR nodes.
9138 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9139 // place the incoming vectors at the exact same location.
9140 SDValue SingleSource = SDValue();
9141 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9142
9143 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9144 SDValue Op = N->getOperand(i);
9145
9146 if (Op.getOpcode() == ISD::UNDEF)
9147 continue;
9148
9149 // Check if this is the identity extract:
9150 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9151 return SDValue();
9152
9153 // Find the single incoming vector for the extract_subvector.
9154 if (SingleSource.getNode()) {
9155 if (Op.getOperand(0) != SingleSource)
9156 return SDValue();
9157 } else {
9158 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009159
9160 // Check the source type is the same as the type of the result.
9161 // If not, this concat may extend the vector, so we can not
9162 // optimize it away.
9163 if (SingleSource.getValueType() != N->getValueType(0))
9164 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009165 }
9166
9167 unsigned IdentityIndex = i * PartNumElem;
9168 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9169 // The extract index must be constant.
9170 if (!CS)
9171 return SDValue();
9172
9173 // Check that we are reading from the identity index.
9174 if (CS->getZExtValue() != IdentityIndex)
9175 return SDValue();
9176 }
9177
9178 if (SingleSource.getNode())
9179 return SingleSource;
9180
Dan Gohman475871a2008-07-27 21:46:04 +00009181 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009182}
9183
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009184SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9185 EVT NVT = N->getValueType(0);
9186 SDValue V = N->getOperand(0);
9187
Michael Liao13429e22012-10-17 20:48:33 +00009188 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9189 // Combine:
9190 // (extract_subvec (concat V1, V2, ...), i)
9191 // Into:
9192 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009193 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9194 if (V->getOperand(0).getValueType() != NVT)
9195 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009196 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9197 unsigned NumElems = NVT.getVectorNumElements();
9198 assert((Idx % NumElems) == 0 &&
9199 "IDX in concat is not a multiple of the result vector length.");
9200 return V->getOperand(Idx / NumElems);
9201 }
9202
Michael Liaob4f98ea2013-03-25 23:47:35 +00009203 // Skip bitcasting
9204 if (V->getOpcode() == ISD::BITCAST)
9205 V = V.getOperand(0);
9206
9207 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009208 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009209 // Handle only simple case where vector being inserted and vector
9210 // being extracted are of same type, and are half size of larger vectors.
9211 EVT BigVT = V->getOperand(0).getValueType();
9212 EVT SmallVT = V->getOperand(1).getValueType();
9213 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9214 return SDValue();
9215
9216 // Only handle cases where both indexes are constants with the same type.
9217 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9218 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9219
9220 if (InsIdx && ExtIdx &&
9221 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9222 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9223 // Combine:
9224 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9225 // Into:
9226 // indices are equal or bit offsets are equal => V1
9227 // otherwise => (extract_subvec V1, ExtIdx)
9228 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9229 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9230 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9231 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9232 DAG.getNode(ISD::BITCAST, dl,
9233 N->getOperand(0).getValueType(),
9234 V->getOperand(0)), N->getOperand(1));
9235 }
9236 }
9237
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009238 return SDValue();
9239}
9240
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009241// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9242static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9243 EVT VT = N->getValueType(0);
9244 unsigned NumElts = VT.getVectorNumElements();
9245
9246 SDValue N0 = N->getOperand(0);
9247 SDValue N1 = N->getOperand(1);
9248 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9249
9250 SmallVector<SDValue, 4> Ops;
9251 EVT ConcatVT = N0.getOperand(0).getValueType();
9252 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9253 unsigned NumConcats = NumElts / NumElemsPerConcat;
9254
9255 // Look at every vector that's inserted. We're looking for exact
9256 // subvector-sized copies from a concatenated vector
9257 for (unsigned I = 0; I != NumConcats; ++I) {
9258 // Make sure we're dealing with a copy.
9259 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009260 bool AllUndef = true, NoUndef = true;
9261 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9262 if (SVN->getMaskElt(J) >= 0)
9263 AllUndef = false;
9264 else
9265 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009266 }
9267
Hao Liu3778c042013-05-13 02:07:05 +00009268 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009269 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9270 return SDValue();
9271
9272 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9273 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9274 return SDValue();
9275
9276 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9277 if (FirstElt < N0.getNumOperands())
9278 Ops.push_back(N0.getOperand(FirstElt));
9279 else
9280 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9281
9282 } else if (AllUndef) {
9283 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9284 } else { // Mixed with general masks and undefs, can't do optimization.
9285 return SDValue();
9286 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009287 }
9288
Andrew Trickac6d9be2013-05-25 02:42:55 +00009289 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009290 Ops.size());
9291}
9292
Dan Gohman475871a2008-07-27 21:46:04 +00009293SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009294 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009295 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009296
Mon P Wangaeb06d22008-11-10 04:46:22 +00009297 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009298 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009299
Craig Topperae1bec52012-04-09 05:16:56 +00009300 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009301
Craig Topper481b79c2012-01-04 08:07:43 +00009302 // Canonicalize shuffle undef, undef -> undef
9303 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9304 return DAG.getUNDEF(VT);
9305
9306 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9307
9308 // Canonicalize shuffle v, v -> v, undef
9309 if (N0 == N1) {
9310 SmallVector<int, 8> NewMask;
9311 for (unsigned i = 0; i != NumElts; ++i) {
9312 int Idx = SVN->getMaskElt(i);
9313 if (Idx >= (int)NumElts) Idx -= NumElts;
9314 NewMask.push_back(Idx);
9315 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009316 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009317 &NewMask[0]);
9318 }
9319
9320 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9321 if (N0.getOpcode() == ISD::UNDEF) {
9322 SmallVector<int, 8> NewMask;
9323 for (unsigned i = 0; i != NumElts; ++i) {
9324 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009325 if (Idx >= 0) {
9326 if (Idx < (int)NumElts)
9327 Idx += NumElts;
9328 else
9329 Idx -= NumElts;
9330 }
9331 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009332 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009333 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009334 &NewMask[0]);
9335 }
9336
9337 // Remove references to rhs if it is undef
9338 if (N1.getOpcode() == ISD::UNDEF) {
9339 bool Changed = false;
9340 SmallVector<int, 8> NewMask;
9341 for (unsigned i = 0; i != NumElts; ++i) {
9342 int Idx = SVN->getMaskElt(i);
9343 if (Idx >= (int)NumElts) {
9344 Idx = -1;
9345 Changed = true;
9346 }
9347 NewMask.push_back(Idx);
9348 }
9349 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009350 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009351 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009352
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009353 // If it is a splat, check if the argument vector is another splat or a
9354 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009355 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009356 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009357
Dan Gohman7f321562007-06-25 16:23:39 +00009358 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009359 // not the number of vector elements, look through it. Be careful not to
9360 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009361 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009362 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009363 if (ConvInput.getValueType().isVector() &&
9364 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009365 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009366 }
9367
Dan Gohman7f321562007-06-25 16:23:39 +00009368 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009369 assert(V->getNumOperands() == NumElts &&
9370 "BUILD_VECTOR has wrong number of operands");
9371 SDValue Base;
9372 bool AllSame = true;
9373 for (unsigned i = 0; i != NumElts; ++i) {
9374 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9375 Base = V->getOperand(i);
9376 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009377 }
Evan Cheng917ec982006-07-21 08:25:53 +00009378 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009379 // Splat of <u, u, u, u>, return <u, u, u, u>
9380 if (!Base.getNode())
9381 return N0;
9382 for (unsigned i = 0; i != NumElts; ++i) {
9383 if (V->getOperand(i) != Base) {
9384 AllSame = false;
9385 break;
9386 }
9387 }
9388 // Splat of <x, x, x, x>, return <x, x, x, x>
9389 if (AllSame)
9390 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009391 }
9392 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009393
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009394 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9395 Level < AfterLegalizeVectorOps &&
9396 (N1.getOpcode() == ISD::UNDEF ||
9397 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9398 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9399 SDValue V = partitionShuffleOfConcats(N, DAG);
9400
9401 if (V.getNode())
9402 return V;
9403 }
9404
Nadav Rotem4ac90812012-04-01 19:31:22 +00009405 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009406 // and it reverses the swizzle of the previous shuffle then we can
9407 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009408 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9409 N1.getOpcode() == ISD::UNDEF) {
9410
Nadav Rotem4ac90812012-04-01 19:31:22 +00009411 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9412
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009413 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9414 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9415 return SDValue();
9416
Craig Topperae1bec52012-04-09 05:16:56 +00009417 // The incoming shuffle must be of the same type as the result of the
9418 // current shuffle.
9419 assert(OtherSV->getOperand(0).getValueType() == VT &&
9420 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009421
9422 for (unsigned i = 0; i != NumElts; ++i) {
9423 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009424 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009425 // Next, this index comes from the first value, which is the incoming
9426 // shuffle. Adopt the incoming index.
9427 if (Idx >= 0)
9428 Idx = OtherSV->getMaskElt(Idx);
9429
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009430 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009431 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009432 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009433 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009434
9435 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009436 }
9437
Dan Gohman475871a2008-07-27 21:46:04 +00009438 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009439}
9440
Evan Cheng44f1f092006-04-20 08:56:16 +00009441/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009442/// an AND to a vector_shuffle with the destination vector and a zero vector.
9443/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009444/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009445SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009446 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009447 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009448 SDValue LHS = N->getOperand(0);
9449 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009450 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009451 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009452 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009453 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009454 SmallVector<int, 8> Indices;
9455 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009456 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009457 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009458 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009459 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009460
9461 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009462 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009463 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009464 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009465 else
Dan Gohman475871a2008-07-27 21:46:04 +00009466 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009467 }
9468
9469 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009470 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009471 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009472 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009473
Dan Gohman7f321562007-06-25 16:23:39 +00009474 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009475 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009476 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009477 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009478 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009479 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009480 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009481 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009482 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009483 }
9484 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009485
Dan Gohman475871a2008-07-27 21:46:04 +00009486 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009487}
9488
Dan Gohman7f321562007-06-25 16:23:39 +00009489/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009490SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009491 assert(N->getValueType(0).isVector() &&
9492 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009493
Dan Gohman475871a2008-07-27 21:46:04 +00009494 SDValue LHS = N->getOperand(0);
9495 SDValue RHS = N->getOperand(1);
9496 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009497 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009498
Dan Gohman7f321562007-06-25 16:23:39 +00009499 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009500 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009501 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009502 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009503 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009504 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009505 SDValue LHSOp = LHS.getOperand(i);
9506 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009507 // If these two elements can't be folded, bail out.
9508 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9509 LHSOp.getOpcode() != ISD::Constant &&
9510 LHSOp.getOpcode() != ISD::ConstantFP) ||
9511 (RHSOp.getOpcode() != ISD::UNDEF &&
9512 RHSOp.getOpcode() != ISD::Constant &&
9513 RHSOp.getOpcode() != ISD::ConstantFP))
9514 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009515
Evan Cheng7b336a82006-05-31 06:08:35 +00009516 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009517 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9518 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009519 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009520 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009521 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009522 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009523 break;
9524 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009525
Bob Wilsond7273432010-12-17 23:06:49 +00009526 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009527 EVT RVT = RHSOp.getValueType();
9528 if (RVT != VT) {
9529 // Integer BUILD_VECTOR operands may have types larger than the element
9530 // size (e.g., when the element type is not legal). Prior to type
9531 // legalization, the types may not match between the two BUILD_VECTORS.
9532 // Truncate one of the operands to make them match.
9533 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009534 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009535 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009536 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009537 VT = RVT;
9538 }
9539 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009540 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009541 LHSOp, RHSOp);
9542 if (FoldOp.getOpcode() != ISD::UNDEF &&
9543 FoldOp.getOpcode() != ISD::Constant &&
9544 FoldOp.getOpcode() != ISD::ConstantFP)
9545 break;
9546 Ops.push_back(FoldOp);
9547 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009548 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009549
Bob Wilsond7273432010-12-17 23:06:49 +00009550 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009551 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009552 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009554
Dan Gohman475871a2008-07-27 21:46:04 +00009555 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009556}
9557
Craig Topperdd201ff2012-09-11 01:45:21 +00009558/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9559SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009560 assert(N->getValueType(0).isVector() &&
9561 "SimplifyVUnaryOp only works on vectors!");
9562
9563 SDValue N0 = N->getOperand(0);
9564
9565 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9566 return SDValue();
9567
9568 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9569 SmallVector<SDValue, 8> Ops;
9570 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9571 SDValue Op = N0.getOperand(i);
9572 if (Op.getOpcode() != ISD::UNDEF &&
9573 Op.getOpcode() != ISD::ConstantFP)
9574 break;
9575 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009576 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009577 if (FoldOp.getOpcode() != ISD::UNDEF &&
9578 FoldOp.getOpcode() != ISD::ConstantFP)
9579 break;
9580 Ops.push_back(FoldOp);
9581 AddToWorkList(FoldOp.getNode());
9582 }
9583
9584 if (Ops.size() != N0.getNumOperands())
9585 return SDValue();
9586
Andrew Trickac6d9be2013-05-25 02:42:55 +00009587 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009588 N0.getValueType(), &Ops[0], Ops.size());
9589}
9590
Andrew Trickac6d9be2013-05-25 02:42:55 +00009591SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009592 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009593 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009594
Bill Wendling836ca7d2009-01-30 23:59:18 +00009595 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009596 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009597
Nate Begemanf845b452005-10-08 00:29:44 +00009598 // If we got a simplified select_cc node back from SimplifySelectCC, then
9599 // break it down into a new SETCC node, and a new SELECT node, and then return
9600 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009601 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009602 // Check to see if we got a select_cc back (to turn into setcc/select).
9603 // Otherwise, just return whatever node we got back, like fabs.
9604 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009605 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009606 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009607 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009608 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009609 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009610 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9611 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009612 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009613
Nate Begemanf845b452005-10-08 00:29:44 +00009614 return SCC;
9615 }
Dan Gohman475871a2008-07-27 21:46:04 +00009616 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009617}
9618
Chris Lattner40c62d52005-10-18 06:04:22 +00009619/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9620/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009621/// select. Callers of this should assume that TheSelect is deleted if this
9622/// returns true. As such, they should return the appropriate thing (e.g. the
9623/// node) back to the top-level of the DAG combiner loop to avoid it being
9624/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009625bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009626 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009627
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009628 // Cannot simplify select with vector condition
9629 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9630
Chris Lattner40c62d52005-10-18 06:04:22 +00009631 // If this is a select from two identical things, try to pull the operation
9632 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009633 if (LHS.getOpcode() != RHS.getOpcode() ||
9634 !LHS.hasOneUse() || !RHS.hasOneUse())
9635 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009636
Chris Lattner18061612010-09-21 15:46:59 +00009637 // If this is a load and the token chain is identical, replace the select
9638 // of two loads with a load through a select of the address to load from.
9639 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9640 // constants have been dropped into the constant pool.
9641 if (LHS.getOpcode() == ISD::LOAD) {
9642 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9643 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009644
Chris Lattner18061612010-09-21 15:46:59 +00009645 // Token chains must be identical.
9646 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009647 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009648 LLD->isVolatile() || RLD->isVolatile() ||
9649 // If this is an EXTLOAD, the VT's must match.
9650 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009651 // If this is an EXTLOAD, the kind of extension must match.
9652 (LLD->getExtensionType() != RLD->getExtensionType() &&
9653 // The only exception is if one of the extensions is anyext.
9654 LLD->getExtensionType() != ISD::EXTLOAD &&
9655 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009656 // FIXME: this discards src value information. This is
9657 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009658 // both potential memory locations. Since we are discarding
9659 // src value info, don't do the transformation if the memory
9660 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009661 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009662 RLD->getPointerInfo().getAddrSpace() != 0 ||
9663 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9664 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009665 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009666
Chris Lattnerf1658062010-09-21 15:58:55 +00009667 // Check that the select condition doesn't reach either load. If so,
9668 // folding this will induce a cycle into the DAG. If not, this is safe to
9669 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009670 SDValue Addr;
9671 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009672 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9673 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9674 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9675 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009676 // The loads must not depend on one another.
9677 if (LLD->isPredecessorOf(RLD) ||
9678 RLD->isPredecessorOf(LLD))
9679 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009680 Addr = DAG.getSelect(SDLoc(TheSelect),
9681 LLD->getBasePtr().getValueType(),
9682 TheSelect->getOperand(0), LLD->getBasePtr(),
9683 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009684 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009685 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9686 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9687
9688 if ((LLD->hasAnyUseOfValue(1) &&
9689 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009690 (RLD->hasAnyUseOfValue(1) &&
9691 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009692 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009693
Andrew Trickac6d9be2013-05-25 02:42:55 +00009694 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009695 LLD->getBasePtr().getValueType(),
9696 TheSelect->getOperand(0),
9697 TheSelect->getOperand(1),
9698 LLD->getBasePtr(), RLD->getBasePtr(),
9699 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009700 }
9701
Chris Lattnerf1658062010-09-21 15:58:55 +00009702 SDValue Load;
9703 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9704 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009705 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009706 // FIXME: Discards pointer info.
9707 LLD->getChain(), Addr, MachinePointerInfo(),
9708 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009709 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009710 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009711 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9712 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009713 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009714 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009715 // FIXME: Discards pointer info.
9716 LLD->getChain(), Addr, MachinePointerInfo(),
9717 LLD->getMemoryVT(), LLD->isVolatile(),
9718 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009719 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009720
9721 // Users of the select now use the result of the load.
9722 CombineTo(TheSelect, Load);
9723
9724 // Users of the old loads now use the new load's chain. We know the
9725 // old-load value is dead now.
9726 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9727 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9728 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009729 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009730
Chris Lattner40c62d52005-10-18 06:04:22 +00009731 return false;
9732}
9733
Chris Lattner600fec32009-03-11 05:08:08 +00009734/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9735/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009736SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009737 SDValue N2, SDValue N3,
9738 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009739 // (x ? y : y) -> y.
9740 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009741
Owen Andersone50ed302009-08-10 22:56:29 +00009742 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9744 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9745 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009746
9747 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009748 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009749 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009750 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9751 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009752
9753 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009754 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009755 return N2;
9756 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009757 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009758 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009759
Nate Begemanf845b452005-10-08 00:29:44 +00009760 // Check to see if we can simplify the select into an fabs node
9761 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9762 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009763 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009764 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9765 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9766 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9767 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009768 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009769
Nate Begemanf845b452005-10-08 00:29:44 +00009770 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9771 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9772 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9773 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009774 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009775 }
9776 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009777
Chris Lattner600fec32009-03-11 05:08:08 +00009778 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9779 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9780 // in it. This is a win when the constant is not otherwise available because
9781 // it replaces two constant pool loads with one. We only do this if the FP
9782 // type is known to be legal, because if it isn't, then we are before legalize
9783 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009784 // messing with soft float) and if the ConstantFP is not legal, because if
9785 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009786 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9787 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9788 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009789 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9790 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009791 // If both constants have multiple uses, then we won't need to do an
9792 // extra load, they are likely around in registers for other users.
9793 (TV->hasOneUse() || FV->hasOneUse())) {
9794 Constant *Elts[] = {
9795 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9796 const_cast<ConstantFP*>(TV->getConstantFPValue())
9797 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009798 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009799 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009800
Chris Lattner600fec32009-03-11 05:08:08 +00009801 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009802 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009803 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9804 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009805 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009806
9807 // Get the offsets to the 0 and 1 element of the array so that we can
9808 // select between them.
9809 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009810 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009811 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009812
Chris Lattner600fec32009-03-11 05:08:08 +00009813 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009814 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009815 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009816 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009817 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9818 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009819 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009820 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
9821 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009822 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009823 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009824 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009825 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009826
9827 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009828 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009829
Nate Begemanf845b452005-10-08 00:29:44 +00009830 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009831 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009832 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009833 (N1C->isNullValue() || // (a < 0) ? b : 0
9834 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009835 EVT XType = N0.getValueType();
9836 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009837 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009838 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009839 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009840 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9841 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009842 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009843 SDValue ShCt = DAG.getConstant(ShCtV,
9844 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009845 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009846 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009847 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009848
Duncan Sands8e4eb092008-06-08 20:54:56 +00009849 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009850 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009851 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009852 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009853
9854 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009855 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009856
Andrew Trickac6d9be2013-05-25 02:42:55 +00009857 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009858 XType, N0,
9859 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009860 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009861 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009862
Duncan Sands8e4eb092008-06-08 20:54:56 +00009863 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009864 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009865 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009866 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009867
9868 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009869 }
9870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009871
Owen Andersoned1088a2010-09-22 22:58:22 +00009872 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9873 // where y is has a single bit set.
9874 // A plaintext description would be, we can turn the SELECT_CC into an AND
9875 // when the condition can be materialized as an all-ones register. Any
9876 // single bit-test can be materialized as an all-ones register with
9877 // shift-left and shift-right-arith.
9878 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9879 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009880 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009881 N2C && N2C->isNullValue()) {
9882 SDValue AndLHS = N0->getOperand(0);
9883 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9884 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9885 // Shift the tested bit over the sign bit.
9886 APInt AndMask = ConstAndRHS->getAPIntValue();
9887 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009888 DAG.getConstant(AndMask.countLeadingZeros(),
9889 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009890 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009891
Owen Andersoned1088a2010-09-22 22:58:22 +00009892 // Now arithmetic right shift it all the way over, so the result is either
9893 // all-ones, or zero.
9894 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009895 DAG.getConstant(AndMask.getBitWidth()-1,
9896 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009897 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009898
Owen Andersoned1088a2010-09-22 22:58:22 +00009899 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9900 }
9901 }
9902
Nate Begeman07ed4172005-10-10 21:26:48 +00009903 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009904 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009905 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9906 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009907
Chris Lattner1eba01e2007-04-11 06:50:51 +00009908 // If the caller doesn't want us to simplify this into a zext of a compare,
9909 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009910 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009911 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009912
Nate Begeman07ed4172005-10-10 21:26:48 +00009913 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009914 // NOTE: Don't create a SETCC if it's not legal on this target.
9915 if (!LegalOperations ||
9916 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00009917 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009918 SDValue Temp, SCC;
9919 // cast from setcc result type to select result type
9920 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +00009921 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009922 N0, N1, CC);
9923 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009924 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009925 N2.getValueType());
9926 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009927 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009928 N2.getValueType(), SCC);
9929 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009930 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
9931 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009932 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009933 }
9934
9935 AddToWorkList(SCC.getNode());
9936 AddToWorkList(Temp.getNode());
9937
9938 if (N2C->getAPIntValue() == 1)
9939 return Temp;
9940
9941 // shl setcc result by log2 n2c
9942 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
9943 DAG.getConstant(N2C->getAPIntValue().logBase2(),
9944 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +00009945 }
Nate Begeman07ed4172005-10-10 21:26:48 +00009946 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009947
Nate Begemanf845b452005-10-08 00:29:44 +00009948 // Check to see if this is the equivalent of setcc
9949 // FIXME: Turn all of these into setcc if setcc if setcc is legal
9950 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00009951 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00009952 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00009953 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +00009954 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
9955 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00009956 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009957 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00009958 return Res;
9959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009960
Bill Wendling836ca7d2009-01-30 23:59:18 +00009961 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00009962 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009963 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00009964 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009965 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009966 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009967 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00009968 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00009969 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009970 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00009971 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009972 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009973 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009974 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00009975 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00009976 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00009977 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009978 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00009979 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009980 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00009981 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009982 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009983 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009984 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00009985 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00009986 }
9987 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009988
Benjamin Kramercde51102010-07-08 12:09:56 +00009989 // Check to see if this is an integer abs.
9990 // select_cc setg[te] X, 0, X, -X ->
9991 // select_cc setgt X, -1, X, -X ->
9992 // select_cc setl[te] X, 0, -X, X ->
9993 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00009994 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00009995 if (N1C) {
9996 ConstantSDNode *SubC = NULL;
9997 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
9998 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
9999 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10000 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10001 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10002 (N1C->isOne() && CC == ISD::SETLT)) &&
10003 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10004 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10005
Owen Andersone50ed302009-08-10 22:56:29 +000010006 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010007 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010008 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010009 N0,
10010 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010011 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010012 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010013 XType, N0, Shift);
10014 AddToWorkList(Shift.getNode());
10015 AddToWorkList(Add.getNode());
10016 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010017 }
10018 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010019
Dan Gohman475871a2008-07-27 21:46:04 +000010020 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010021}
10022
Evan Chengfa1eb272007-02-08 22:13:59 +000010023/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010024SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010025 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010026 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010027 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010028 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010029 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010030}
10031
Nate Begeman69575232005-10-20 02:15:44 +000010032/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10033/// return a DAG expression to select that will generate the same value by
10034/// multiplying by a magic number. See:
10035/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010036SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010037 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010038 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010039
Andrew Lenharth232c9102006-06-12 16:07:18 +000010040 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010041 ii != ee; ++ii)
10042 AddToWorkList(*ii);
10043 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010044}
10045
10046/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10047/// return a DAG expression to select that will generate the same value by
10048/// multiplying by a magic number. See:
10049/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010050SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010051 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010052 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010053
Andrew Lenharth232c9102006-06-12 16:07:18 +000010054 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010055 ii != ee; ++ii)
10056 AddToWorkList(*ii);
10057 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010058}
10059
Nate Begemancc66cdd2009-09-25 06:05:26 +000010060/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010061// to alias with anything but itself. Provides base object and offset as
10062// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010063static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010064 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010065 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010066 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010067
Jim Laskey71382342006-10-07 23:37:56 +000010068 // If it's an adding a simple constant then integrate the offset.
10069 if (Base.getOpcode() == ISD::ADD) {
10070 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10071 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010072 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010073 }
10074 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010075
Nate Begemancc66cdd2009-09-25 06:05:26 +000010076 // Return the underlying GlobalValue, and update the Offset. Return false
10077 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10078 // by multiple nodes with different offsets.
10079 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10080 GV = G->getGlobal();
10081 Offset += G->getOffset();
10082 return false;
10083 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010084
Nate Begemancc66cdd2009-09-25 06:05:26 +000010085 // Return the underlying Constant value, and update the Offset. Return false
10086 // for ConstantSDNodes since the same constant pool entry may be represented
10087 // by multiple nodes with different offsets.
10088 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010089 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10090 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010091 Offset += C->getOffset();
10092 return false;
10093 }
Jim Laskey71382342006-10-07 23:37:56 +000010094 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010095 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010096}
10097
10098/// isAlias - Return true if there is any possibility that the two addresses
10099/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010100bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010101 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010102 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010103 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010104 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010105 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010106 unsigned SrcValueAlign2,
10107 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010108 // If they are the same then they must be aliases.
10109 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010110
Jim Laskey71382342006-10-07 23:37:56 +000010111 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010112 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010113 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010114 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010115 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010116 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10117 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010118
Nate Begemancc66cdd2009-09-25 06:05:26 +000010119 // If they have a same base address then check to see if they overlap.
10120 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010121 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010122
Owen Anderson4a9f1502010-09-20 20:39:59 +000010123 // It is possible for different frame indices to alias each other, mostly
10124 // when tail call optimization reuses return address slots for arguments.
10125 // To catch this case, look up the actual index of frame indices to compute
10126 // the real alias relationship.
10127 if (isFrameIndex1 && isFrameIndex2) {
10128 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10129 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10130 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10131 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10132 }
10133
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010134 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010135 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010136 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10137 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010138
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010139 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10140 // compared to the size and offset of the access, we may be able to prove they
10141 // do not alias. This check is conservative for now to catch cases created by
10142 // splitting vector types.
10143 if ((SrcValueAlign1 == SrcValueAlign2) &&
10144 (SrcValueOffset1 != SrcValueOffset2) &&
10145 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10146 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10147 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010148
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010149 // There is no overlap between these relatively aligned accesses of similar
10150 // size, return no alias.
10151 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10152 return false;
10153 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010154
Jim Laskey07a27092006-10-18 19:08:31 +000010155 if (CombinerGlobalAA) {
10156 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010157 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10158 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10159 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010160 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010161 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10162 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010163 if (AAResult == AliasAnalysis::NoAlias)
10164 return false;
10165 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010166
10167 // Otherwise we have to assume they alias.
10168 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010169}
10170
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010171bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10172 SDValue Ptr0, Ptr1;
10173 int64_t Size0, Size1;
10174 const Value *SrcValue0, *SrcValue1;
10175 int SrcValueOffset0, SrcValueOffset1;
10176 unsigned SrcValueAlign0, SrcValueAlign1;
10177 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10178 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10179 SrcValueAlign0, SrcTBAAInfo0);
10180 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10181 SrcValueAlign1, SrcTBAAInfo1);
10182 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010183 SrcValueAlign0, SrcTBAAInfo0,
10184 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10185 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010186}
10187
Jim Laskey71382342006-10-07 23:37:56 +000010188/// FindAliasInfo - Extracts the relevant alias information from the memory
10189/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010190bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010191 SDValue &Ptr, int64_t &Size,
10192 const Value *&SrcValue,
10193 int &SrcValueOffset,
10194 unsigned &SrcValueAlign,
10195 const MDNode *&TBAAInfo) const {
10196 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10197
10198 Ptr = LS->getBasePtr();
10199 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10200 SrcValue = LS->getSrcValue();
10201 SrcValueOffset = LS->getSrcValueOffset();
10202 SrcValueAlign = LS->getOriginalAlignment();
10203 TBAAInfo = LS->getTBAAInfo();
10204 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010205}
10206
Jim Laskey6ff23e52006-10-04 16:53:27 +000010207/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10208/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010209void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
10210 SmallVector<SDValue, 8> &Aliases) {
10211 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010212 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010213
Jim Laskey279f0532006-09-25 16:29:54 +000010214 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010215 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010216 int64_t Size;
10217 const Value *SrcValue;
10218 int SrcValueOffset;
10219 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010220 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010221 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010222 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010223
Jim Laskey6ff23e52006-10-04 16:53:27 +000010224 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010225 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010226 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010227
Jim Laskeybc588b82006-10-05 15:07:25 +000010228 // Look at each chain and determine if it is an alias. If so, add it to the
10229 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010230 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010231 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010232 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010233 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010234
10235 // For TokenFactor nodes, look at each operand and only continue up the
10236 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010237 // find more and revert to original chain since the xform is unlikely to be
10238 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010239 //
10240 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010241 // chain we found before we hit a tokenfactor rather than the original
10242 // chain.
10243 if (Depth > 6 || Aliases.size() == 2) {
10244 Aliases.clear();
10245 Aliases.push_back(OriginalChain);
10246 break;
10247 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010248
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010249 // Don't bother if we've been before.
10250 if (!Visited.insert(Chain.getNode()))
10251 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010252
Jim Laskeybc588b82006-10-05 15:07:25 +000010253 switch (Chain.getOpcode()) {
10254 case ISD::EntryToken:
10255 // Entry token is ideal chain operand, but handled in FindBetterChain.
10256 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010257
Jim Laskeybc588b82006-10-05 15:07:25 +000010258 case ISD::LOAD:
10259 case ISD::STORE: {
10260 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010261 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010262 int64_t OpSize;
10263 const Value *OpSrcValue;
10264 int OpSrcValueOffset;
10265 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010266 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010267 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010268 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010269 OpSrcValueAlign,
10270 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010271
Jim Laskeybc588b82006-10-05 15:07:25 +000010272 // If chain is alias then stop here.
10273 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010274 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010275 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010276 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010277 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010278 Aliases.push_back(Chain);
10279 } else {
10280 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010281 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010282 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010283 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010284 break;
10285 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010286
Jim Laskeybc588b82006-10-05 15:07:25 +000010287 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010288 // We have to check each of the operands of the token factor for "small"
10289 // token factors, so we queue them up. Adding the operands to the queue
10290 // (stack) in reverse order maintains the original order and increases the
10291 // likelihood that getNode will find a matching token factor (CSE.)
10292 if (Chain.getNumOperands() > 16) {
10293 Aliases.push_back(Chain);
10294 break;
10295 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010296 for (unsigned n = Chain.getNumOperands(); n;)
10297 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010298 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010299 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010300
Jim Laskeybc588b82006-10-05 15:07:25 +000010301 default:
10302 // For all other instructions we will just have to take what we can get.
10303 Aliases.push_back(Chain);
10304 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010305 }
10306 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010307}
10308
10309/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10310/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010311SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10312 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010313
Jim Laskey6ff23e52006-10-04 16:53:27 +000010314 // Accumulate all the aliases to this node.
10315 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010316
Dan Gohman71dc7c92011-05-17 22:20:36 +000010317 // If no operands then chain to entry token.
10318 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010319 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010320
10321 // If a single operand then chain to it. We don't need to revisit it.
10322 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010323 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010324
Jim Laskey6ff23e52006-10-04 16:53:27 +000010325 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010326 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010327 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010328}
10329
Nate Begeman1d4d4142005-09-01 00:19:25 +000010330// SelectionDAG::Combine - This is the entry point for the file.
10331//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010332void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010333 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010334 /// run - This is the main entry point to this class.
10335 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010336 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010337}