blob: 47c332a5be49df5149185a71069a627cc6081acc [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
Craig Topper6c64fba2013-07-13 07:43:40 +0000157 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &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,
Craig Toppera0ec3f92013-07-14 04:42:23 +0000282 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000283
Jim Laskey096c22e2006-10-18 12:29:57 +0000284 /// isAlias - Return true if there is any possibility that the two addresses
285 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000286 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000287 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000288 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000289 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000291 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000292 unsigned SrcValueAlign2,
293 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000294
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000295 /// isAlias - Return true if there is any possibility that the two addresses
296 /// overlap.
297 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
298
Jim Laskey7ca56af2006-10-11 13:47:09 +0000299 /// FindAliasInfo - Extracts the relevant alias information from the memory
300 /// node. Returns true if the operand was a load.
301 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000303 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000304 unsigned &SrcValueAlignment,
305 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000309 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000310
Nadav Rotemc653de62012-10-03 16:11:15 +0000311 /// Merge consecutive store operations into a wide store.
312 /// This optimization uses wide integers or vectors when possible.
313 /// \return True if some memory operations were changed.
314 bool MergeConsecutiveStores(StoreSDNode *N);
315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000317 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000318 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000320
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000322 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323
Chris Lattner2392ae72010-04-15 04:48:01 +0000324 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000325
Chris Lattner2392ae72010-04-15 04:48:01 +0000326 /// getShiftAmountTy - Returns a type large enough to hold any valid
327 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000328 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000329 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
330 if (LHSTy.isVector())
331 return LHSTy;
332 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334
Chris Lattner2392ae72010-04-15 04:48:01 +0000335 /// isTypeLegal - This method returns true if we are running before type
336 /// legalization or if the specified VT is legal.
337 bool isTypeLegal(const EVT &VT) {
338 if (!LegalTypes) return true;
339 return TLI.isTypeLegal(VT);
340 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000341
342 /// getSetCCResultType - Convenience wrapper around
343 /// TargetLowering::getSetCCResultType
344 EVT getSetCCResultType(EVT VT) const {
345 return TLI.getSetCCResultType(*DAG.getContext(), VT);
346 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000347 };
348}
349
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000350
351namespace {
352/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
353/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000354class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000355 DAGCombiner &DC;
356public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000357 explicit WorkListRemover(DAGCombiner &dc)
358 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Duncan Sandsedfcf592008-06-11 11:42:12 +0000360 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000361 DC.removeFromWorkList(N);
362 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000363};
364}
365
Chris Lattner24664722006-03-01 04:53:38 +0000366//===----------------------------------------------------------------------===//
367// TargetLowering::DAGCombinerInfo implementation
368//===----------------------------------------------------------------------===//
369
370void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
371 ((DAGCombiner*)DC)->AddToWorkList(N);
372}
373
Cameron Zwariched3caf92011-04-02 02:40:26 +0000374void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
375 ((DAGCombiner*)DC)->removeFromWorkList(N);
376}
377
Dan Gohman475871a2008-07-27 21:46:04 +0000378SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000379CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
380 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000381}
382
Dan Gohman475871a2008-07-27 21:46:04 +0000383SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000384CombineTo(SDNode *N, SDValue Res, bool AddTo) {
385 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000386}
387
388
Dan Gohman475871a2008-07-27 21:46:04 +0000389SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000390CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
391 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000392}
393
Dan Gohmane5af2d32009-01-29 01:59:02 +0000394void TargetLowering::DAGCombinerInfo::
395CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
396 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
397}
Chris Lattner24664722006-03-01 04:53:38 +0000398
Chris Lattner24664722006-03-01 04:53:38 +0000399//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000400// Helper Functions
401//===----------------------------------------------------------------------===//
402
403/// isNegatibleForFree - Return 1 if we can compute the negated form of the
404/// specified expression for the same cost as the expression itself, or 2 if we
405/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000407 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000408 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000409 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000410 // fneg is removable even if it has multiple uses.
411 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Chris Lattner29446522007-05-14 22:04:50 +0000413 // Don't allow anything with multiple uses.
414 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Chris Lattner3adf9512007-05-25 02:19:06 +0000416 // Don't recurse exponentially.
417 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000418
Chris Lattner29446522007-05-14 22:04:50 +0000419 switch (Op.getOpcode()) {
420 default: return false;
421 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000422 // Don't invert constant FP values after legalize. The negated constant
423 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000424 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000425 case ISD::FADD:
426 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000427 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000428
Owen Andersonafd3d562012-03-06 00:29:31 +0000429 // After operation legalization, it might not be legal to create new FSUBs.
430 if (LegalOperations &&
431 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
432 return 0;
433
Craig Topper956342b2012-09-09 22:58:45 +0000434 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000435 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
436 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000438 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000439 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000441 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000442 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000443 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Bill Wendlingd34470c2009-01-30 23:10:18 +0000445 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000446 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Chris Lattner29446522007-05-14 22:04:50 +0000448 case ISD::FMUL:
449 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000450 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000451
Bill Wendlingd34470c2009-01-30 23:10:18 +0000452 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000453 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
454 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000455 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Owen Andersonafd3d562012-03-06 00:29:31 +0000457 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000458 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner29446522007-05-14 22:04:50 +0000460 case ISD::FP_EXTEND:
461 case ISD::FP_ROUND:
462 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000463 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000465 }
466}
467
468/// GetNegatedExpression - If isNegatibleForFree returns true, this function
469/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000470static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000471 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000472 // fneg is removable even if it has multiple uses.
473 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000474
Chris Lattner29446522007-05-14 22:04:50 +0000475 // Don't allow anything with multiple uses.
476 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Chris Lattner3adf9512007-05-25 02:19:06 +0000478 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000479 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000480 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000481 case ISD::ConstantFP: {
482 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
483 V.changeSign();
484 return DAG.getConstantFP(V, Op.getValueType());
485 }
Chris Lattner29446522007-05-14 22:04:50 +0000486 case ISD::FADD:
487 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000488 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000489
Bill Wendlingd34470c2009-01-30 23:10:18 +0000490 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000491 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000492 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000493 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000494 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000495 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000496 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000497 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000498 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000499 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000500 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000501 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000502 Op.getOperand(0));
503 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000504 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000506
Bill Wendlingd34470c2009-01-30 23:10:18 +0000507 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000508 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000509 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000510 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000511
Bill Wendlingd34470c2009-01-30 23:10:18 +0000512 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000513 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000514 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Chris Lattner29446522007-05-14 22:04:50 +0000516 case ISD::FMUL:
517 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000518 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000519
Bill Wendlingd34470c2009-01-30 23:10:18 +0000520 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000521 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000522 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000523 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000524 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000525 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000526 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000527 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000528
Bill Wendlingd34470c2009-01-30 23:10:18 +0000529 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000530 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000531 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000532 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000533 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000534
Chris Lattner29446522007-05-14 22:04:50 +0000535 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000537 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000538 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000539 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000540 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000541 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000542 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000543 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000544 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000545 }
546}
Chris Lattner24664722006-03-01 04:53:38 +0000547
548
Nate Begeman4ebd8052005-09-01 23:24:04 +0000549// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
550// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000551// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000552// nodes based on the type of node we are checking. This simplifies life a
553// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000554static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
555 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 if (N.getOpcode() == ISD::SETCC) {
557 LHS = N.getOperand(0);
558 RHS = N.getOperand(1);
559 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000560 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000562 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563 N.getOperand(2).getOpcode() == ISD::Constant &&
564 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000565 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
567 LHS = N.getOperand(0);
568 RHS = N.getOperand(1);
569 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000571 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 return false;
573}
574
Nate Begeman99801192005-09-07 23:25:52 +0000575// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
576// one use. If this is true, it allows the users to invert the operation for
577// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000578static bool isOneUseSetCC(SDValue N) {
579 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000580 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000581 return true;
582 return false;
583}
584
Andrew Trickac6d9be2013-05-25 02:42:55 +0000585SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000586 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000587 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
589 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N0.getOperand(1)),
594 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000598 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000599 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000602 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Nate Begemancd4d58c2006-02-03 06:46:56 +0000606 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
607 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000608 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000609 SDValue OpNode =
610 DAG.FoldConstantArithmetic(Opc, VT,
611 cast<ConstantSDNode>(N1.getOperand(1)),
612 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000613 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000614 }
615 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000616 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000617 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000618 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000619 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000620 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000621 }
622 }
Bill Wendling35247c32009-01-30 00:45:56 +0000623
Dan Gohman475871a2008-07-27 21:46:04 +0000624 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000625}
626
Dan Gohman475871a2008-07-27 21:46:04 +0000627SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
628 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000629 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
630 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000631 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000632 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000633 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000634 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000635 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000636 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000637 assert((!To[i].getNode() ||
638 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000639 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000641 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000642 if (AddTo) {
643 // Push the new nodes and any users onto the worklist
644 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000645 if (To[i].getNode()) {
646 AddToWorkList(To[i].getNode());
647 AddUsersToWorkList(To[i].getNode());
648 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000649 }
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Dan Gohmandbe664a2009-01-19 21:44:21 +0000652 // Finally, if the node is now dead, remove it from the graph. The node
653 // may not be dead if the replacement process recursively simplified to
654 // something else needing this node.
655 if (N->use_empty()) {
656 // Nodes can be reintroduced into the worklist. Make sure we do not
657 // process a node that has been replaced.
658 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000659
Dan Gohmandbe664a2009-01-19 21:44:21 +0000660 // Finally, since the node is now dead, remove it from the graph.
661 DAG.DeleteNode(N);
662 }
Dan Gohman475871a2008-07-27 21:46:04 +0000663 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000664}
665
Evan Chenge5b51ac2010-04-17 06:13:15 +0000666void DAGCombiner::
667CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000668 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000669 // are deleted, make sure to remove them from our worklist.
670 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000671 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000672
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000673 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000674 AddToWorkList(TLO.New.getNode());
675 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000677 // Finally, if the node is now dead, remove it from the graph. The node
678 // may not be dead if the replacement process recursively simplified to
679 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000680 if (TLO.Old.getNode()->use_empty()) {
681 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000683 // If the operands of this node are only used by the node, they will now
684 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000685 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
686 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
687 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Gabor Greifba36cb52008-08-28 21:40:38 +0000689 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000690 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000691}
692
693/// SimplifyDemandedBits - Check the specified integer node value to see if
694/// it can be simplified or if things it uses can be simplified by bit
695/// propagation. If so, return true.
696bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000697 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000698 APInt KnownZero, KnownOne;
699 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
700 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Dan Gohmane5af2d32009-01-29 01:59:02 +0000702 // Revisit the node.
703 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000704
Dan Gohmane5af2d32009-01-29 01:59:02 +0000705 // Replace the old value with the new one.
706 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000708 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000709 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000710 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000711 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000712
Dan Gohmane5af2d32009-01-29 01:59:02 +0000713 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000714 return true;
715}
716
Evan Cheng95c57ea2010-04-24 04:43:44 +0000717void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000718 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000719 EVT VT = Load->getValueType(0);
720 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000721
Evan Cheng95c57ea2010-04-24 04:43:44 +0000722 DEBUG(dbgs() << "\nReplacing.9 ";
723 Load->dump(&DAG);
724 dbgs() << "\nWith: ";
725 Trunc.getNode()->dump(&DAG);
726 dbgs() << '\n');
727 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000728 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
729 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000730 removeFromWorkList(Load);
731 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000732 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000733}
734
735SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
736 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000737 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000738 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000739 EVT MemVT = LD->getMemoryVT();
740 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000741 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000742 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000743 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000744 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000745 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000746 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000747 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000748 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000749 LD->isNonTemporal(), LD->getAlignment());
750 }
751
Evan Cheng4c26e932010-04-19 19:29:22 +0000752 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000753 switch (Opc) {
754 default: break;
755 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000756 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000758 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000759 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000760 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000761 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000762 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000763 case ISD::Constant: {
764 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000765 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000766 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767 }
Evan Chengcaf77402010-04-23 19:10:30 +0000768 }
769
770 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000772 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000773}
774
Evan Cheng95c57ea2010-04-24 04:43:44 +0000775SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000776 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
777 return SDValue();
778 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000779 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000780 bool Replace = false;
781 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
782 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000783 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000784 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000785
786 if (Replace)
787 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
788 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000789 DAG.getValueType(OldVT));
790}
791
Evan Cheng95c57ea2010-04-24 04:43:44 +0000792SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000793 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000794 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000795 bool Replace = false;
796 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
797 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000798 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000799 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000800
801 if (Replace)
802 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
803 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804}
805
Evan Cheng64b7bf72010-04-16 06:14:10 +0000806/// PromoteIntBinOp - Promote the specified integer binary operation if the
807/// target indicates it is beneficial. e.g. On x86, it's usually better to
808/// promote i16 operations to i32 since i16 instructions are longer.
809SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
810 if (!LegalOperations)
811 return SDValue();
812
813 EVT VT = Op.getValueType();
814 if (VT.isVector() || !VT.isInteger())
815 return SDValue();
816
Evan Chenge5b51ac2010-04-17 06:13:15 +0000817 // If operation type is 'undesirable', e.g. i16 on x86, consider
818 // promoting it.
819 unsigned Opc = Op.getOpcode();
820 if (TLI.isTypeDesirableForOp(Opc, VT))
821 return SDValue();
822
Evan Cheng64b7bf72010-04-16 06:14:10 +0000823 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000824 // Consult target whether it is a good idea to promote this operation and
825 // what's the right type to promote it to.
826 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000827 assert(PVT != VT && "Don't know what type to promote to!");
828
Evan Cheng95c57ea2010-04-24 04:43:44 +0000829 bool Replace0 = false;
830 SDValue N0 = Op.getOperand(0);
831 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
832 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000833 return SDValue();
834
Evan Cheng95c57ea2010-04-24 04:43:44 +0000835 bool Replace1 = false;
836 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000837 SDValue NN1;
838 if (N0 == N1)
839 NN1 = NN0;
840 else {
841 NN1 = PromoteOperand(N1, PVT, Replace1);
842 if (NN1.getNode() == 0)
843 return SDValue();
844 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000845
Evan Cheng95c57ea2010-04-24 04:43:44 +0000846 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000847 if (NN1.getNode())
848 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000849
850 if (Replace0)
851 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
852 if (Replace1)
853 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000854
Evan Chengac7eae52010-04-27 19:48:13 +0000855 DEBUG(dbgs() << "\nPromoting ";
856 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000857 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000858 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000859 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000860 }
861 return SDValue();
862}
863
864/// PromoteIntShiftOp - Promote the specified integer shift operation if the
865/// target indicates it is beneficial. e.g. On x86, it's usually better to
866/// promote i16 operations to i32 since i16 instructions are longer.
867SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
868 if (!LegalOperations)
869 return SDValue();
870
871 EVT VT = Op.getValueType();
872 if (VT.isVector() || !VT.isInteger())
873 return SDValue();
874
875 // If operation type is 'undesirable', e.g. i16 on x86, consider
876 // promoting it.
877 unsigned Opc = Op.getOpcode();
878 if (TLI.isTypeDesirableForOp(Opc, VT))
879 return SDValue();
880
881 EVT PVT = VT;
882 // Consult target whether it is a good idea to promote this operation and
883 // what's the right type to promote it to.
884 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
885 assert(PVT != VT && "Don't know what type to promote to!");
886
Evan Cheng95c57ea2010-04-24 04:43:44 +0000887 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000888 SDValue N0 = Op.getOperand(0);
889 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000890 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000891 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000892 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000893 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000894 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000895 if (N0.getNode() == 0)
896 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000897
Evan Chenge5b51ac2010-04-17 06:13:15 +0000898 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000899 if (Replace)
900 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000901
Evan Chengac7eae52010-04-27 19:48:13 +0000902 DEBUG(dbgs() << "\nPromoting ";
903 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000904 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000905 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000906 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000907 }
908 return SDValue();
909}
910
Evan Cheng4c26e932010-04-19 19:29:22 +0000911SDValue DAGCombiner::PromoteExtend(SDValue Op) {
912 if (!LegalOperations)
913 return SDValue();
914
915 EVT VT = Op.getValueType();
916 if (VT.isVector() || !VT.isInteger())
917 return SDValue();
918
919 // If operation type is 'undesirable', e.g. i16 on x86, consider
920 // promoting it.
921 unsigned Opc = Op.getOpcode();
922 if (TLI.isTypeDesirableForOp(Opc, VT))
923 return SDValue();
924
925 EVT PVT = VT;
926 // Consult target whether it is a good idea to promote this operation and
927 // what's the right type to promote it to.
928 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
929 assert(PVT != VT && "Don't know what type to promote to!");
930 // fold (aext (aext x)) -> (aext x)
931 // fold (aext (zext x)) -> (zext x)
932 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000933 DEBUG(dbgs() << "\nPromoting ";
934 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000935 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000936 }
937 return SDValue();
938}
939
940bool DAGCombiner::PromoteLoad(SDValue Op) {
941 if (!LegalOperations)
942 return false;
943
944 EVT VT = Op.getValueType();
945 if (VT.isVector() || !VT.isInteger())
946 return false;
947
948 // If operation type is 'undesirable', e.g. i16 on x86, consider
949 // promoting it.
950 unsigned Opc = Op.getOpcode();
951 if (TLI.isTypeDesirableForOp(Opc, VT))
952 return false;
953
954 EVT PVT = VT;
955 // Consult target whether it is a good idea to promote this operation and
956 // what's the right type to promote it to.
957 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
958 assert(PVT != VT && "Don't know what type to promote to!");
959
Andrew Trickac6d9be2013-05-25 02:42:55 +0000960 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000961 SDNode *N = Op.getNode();
962 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000963 EVT MemVT = LD->getMemoryVT();
964 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000965 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000966 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000967 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000968 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000969 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000970 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000971 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000972 LD->isNonTemporal(), LD->getAlignment());
973 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
974
Evan Cheng95c57ea2010-04-24 04:43:44 +0000975 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000976 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000977 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000978 Result.getNode()->dump(&DAG);
979 dbgs() << '\n');
980 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000981 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
982 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000983 removeFromWorkList(N);
984 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000985 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000986 return true;
987 }
988 return false;
989}
990
Evan Chenge5b51ac2010-04-17 06:13:15 +0000991
Chris Lattner29446522007-05-14 22:04:50 +0000992//===----------------------------------------------------------------------===//
993// Main DAG Combiner implementation
994//===----------------------------------------------------------------------===//
995
Duncan Sands25cf2272008-11-24 14:53:14 +0000996void DAGCombiner::Run(CombineLevel AtLevel) {
997 // set the instance variables, so that the various visit routines may use it.
998 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000999 LegalOperations = Level >= AfterLegalizeVectorOps;
1000 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001001
Evan Cheng17a568b2008-08-29 22:21:44 +00001002 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001003 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1004 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001005 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001006
Evan Cheng17a568b2008-08-29 22:21:44 +00001007 // Create a dummy node (which is not added to allnodes), that adds a reference
1008 // to the root node, preventing it from being deleted, and tracking any
1009 // changes of the root.
1010 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001011
Jim Laskey26f7fa72006-10-17 19:33:52 +00001012 // The root of the dag may dangle to deleted nodes until the dag combiner is
1013 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001014 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001015
James Molloy6660c052012-02-16 09:17:04 +00001016 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001018 while (!WorkListContents.empty()) {
1019 SDNode *N;
1020 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1021 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1022 // worklist *should* contain, and check the node we want to visit is should
1023 // actually be visited.
1024 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001025 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001026 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1029 // N is deleted from the DAG, since they too may now be dead or may have a
1030 // reduced number of uses, allowing other xforms.
1031 if (N->use_empty() && N != &Dummy) {
1032 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1033 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 DAG.DeleteNode(N);
1036 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001038
Evan Cheng17a568b2008-08-29 22:21:44 +00001039 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001040
Evan Cheng17a568b2008-08-29 22:21:44 +00001041 if (RV.getNode() == 0)
1042 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001043
Evan Cheng17a568b2008-08-29 22:21:44 +00001044 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Evan Cheng17a568b2008-08-29 22:21:44 +00001046 // If we get back the same node we passed in, rather than a new node or
1047 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001049 // mechanics for us, we have no work to do in this case.
1050 if (RV.getNode() == N)
1051 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Evan Cheng17a568b2008-08-29 22:21:44 +00001053 assert(N->getOpcode() != ISD::DELETED_NODE &&
1054 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1055 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001056
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001058 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001059 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001060 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001061 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001062
Devang Patel9728ea22011-05-23 22:04:42 +00001063 // Transfer debug value.
1064 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001065 WorkListRemover DeadNodes(*this);
1066 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001067 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001068 else {
1069 assert(N->getValueType(0) == RV.getValueType() &&
1070 N->getNumValues() == 1 && "Type mismatch");
1071 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001072 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001073 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Evan Cheng17a568b2008-08-29 22:21:44 +00001075 // Push the new node and any users onto the worklist
1076 AddToWorkList(RV.getNode());
1077 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Evan Cheng17a568b2008-08-29 22:21:44 +00001079 // Add any uses of the old node to the worklist in case this node is the
1080 // last one that uses them. They may become dead after this node is
1081 // deleted.
1082 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1083 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Dan Gohmandbe664a2009-01-19 21:44:21 +00001085 // Finally, if the node is now dead, remove it from the graph. The node
1086 // may not be dead if the replacement process recursively simplified to
1087 // something else needing this node.
1088 if (N->use_empty()) {
1089 // Nodes can be reintroduced into the worklist. Make sure we do not
1090 // process a node that has been replaced.
1091 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001092
Dan Gohmandbe664a2009-01-19 21:44:21 +00001093 // Finally, since the node is now dead, remove it from the graph.
1094 DAG.DeleteNode(N);
1095 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner95038592005-10-05 06:35:28 +00001098 // If the root changed (e.g. it was a dead load, update the root).
1099 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001100 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101}
1102
Dan Gohman475871a2008-07-27 21:46:04 +00001103SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001104 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001106 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001107 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 case ISD::ADD: return visitADD(N);
1109 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001110 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001111 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001112 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001113 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 case ISD::MUL: return visitMUL(N);
1115 case ISD::SDIV: return visitSDIV(N);
1116 case ISD::UDIV: return visitUDIV(N);
1117 case ISD::SREM: return visitSREM(N);
1118 case ISD::UREM: return visitUREM(N);
1119 case ISD::MULHU: return visitMULHU(N);
1120 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001121 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1122 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001123 case ISD::SMULO: return visitSMULO(N);
1124 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001125 case ISD::SDIVREM: return visitSDIVREM(N);
1126 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 case ISD::AND: return visitAND(N);
1128 case ISD::OR: return visitOR(N);
1129 case ISD::XOR: return visitXOR(N);
1130 case ISD::SHL: return visitSHL(N);
1131 case ISD::SRA: return visitSRA(N);
1132 case ISD::SRL: return visitSRL(N);
1133 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001134 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001136 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001138 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001139 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001140 case ISD::SELECT_CC: return visitSELECT_CC(N);
1141 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1143 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001144 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001145 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1146 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001147 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001148 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001149 case ISD::FADD: return visitFADD(N);
1150 case ISD::FSUB: return visitFSUB(N);
1151 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001152 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001153 case ISD::FDIV: return visitFDIV(N);
1154 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001155 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1157 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1158 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1159 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1160 case ISD::FP_ROUND: return visitFP_ROUND(N);
1161 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1162 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1163 case ISD::FNEG: return visitFNEG(N);
1164 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001165 case ISD::FFLOOR: return visitFFLOOR(N);
1166 case ISD::FCEIL: return visitFCEIL(N);
1167 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001168 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001169 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001170 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001171 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001172 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001173 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001174 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1175 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001176 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001177 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 }
Dan Gohman475871a2008-07-27 21:46:04 +00001179 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180}
1181
Dan Gohman475871a2008-07-27 21:46:04 +00001182SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001184
1185 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001187 assert(N->getOpcode() != ISD::DELETED_NODE &&
1188 "Node was deleted but visit returned NULL!");
1189
1190 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1191 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1192
1193 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001194 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001195 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001196
1197 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1198 }
1199 }
1200
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001201 // If nothing happened still, try promoting the operation.
1202 if (RV.getNode() == 0) {
1203 switch (N->getOpcode()) {
1204 default: break;
1205 case ISD::ADD:
1206 case ISD::SUB:
1207 case ISD::MUL:
1208 case ISD::AND:
1209 case ISD::OR:
1210 case ISD::XOR:
1211 RV = PromoteIntBinOp(SDValue(N, 0));
1212 break;
1213 case ISD::SHL:
1214 case ISD::SRA:
1215 case ISD::SRL:
1216 RV = PromoteIntShiftOp(SDValue(N, 0));
1217 break;
1218 case ISD::SIGN_EXTEND:
1219 case ISD::ZERO_EXTEND:
1220 case ISD::ANY_EXTEND:
1221 RV = PromoteExtend(SDValue(N, 0));
1222 break;
1223 case ISD::LOAD:
1224 if (PromoteLoad(SDValue(N, 0)))
1225 RV = SDValue(N, 0);
1226 break;
1227 }
1228 }
1229
Scott Michelfdc40a02009-02-17 22:15:04 +00001230 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001231 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001232 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001233 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1234 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue N0 = N->getOperand(0);
1236 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001237
Evan Cheng08b11732008-03-22 01:55:50 +00001238 // Constant operands are canonicalized to RHS.
1239 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001240 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001241 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1242 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001243 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001244 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001245 }
1246 }
1247
Dan Gohman389079b2007-10-08 17:57:15 +00001248 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001249}
Dan Gohman389079b2007-10-08 17:57:15 +00001250
Chris Lattner6270f682006-10-08 22:57:01 +00001251/// getInputChainForNode - Given a node, return its input chain if it has one,
1252/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001253static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001254 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001256 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001257 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001258 return N->getOperand(NumOps-1);
1259 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001261 return N->getOperand(i);
1262 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001263 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001264}
1265
Dan Gohman475871a2008-07-27 21:46:04 +00001266SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001267 // If N has two operands, where one has an input chain equal to the other,
1268 // the 'other' chain is redundant.
1269 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001271 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001272 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001273 return N->getOperand(1);
1274 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001275
Chris Lattnerc76d4412007-05-16 06:37:59 +00001276 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001278 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001279 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Jim Laskey6ff23e52006-10-04 16:53:27 +00001281 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001282 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Jim Laskey71382342006-10-07 23:37:56 +00001284 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001285 // encountered.
1286 for (unsigned i = 0; i < TFs.size(); ++i) {
1287 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001288
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 // Check each of the operands.
1290 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001291 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Jim Laskey6ff23e52006-10-04 16:53:27 +00001293 switch (Op.getOpcode()) {
1294 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001295 // Entry tokens don't need to be added to the list. They are
1296 // rededundant.
1297 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001298 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001299
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001301 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001302 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001305 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001306 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001307 Changed = true;
1308 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001309 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Jim Laskey6ff23e52006-10-04 16:53:27 +00001312 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001313 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001314 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001315 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001316 else
1317 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001319 }
1320 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001321 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001324
1325 // If we've change things around then replace token factor.
1326 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001327 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001328 // The entry token is the only possible outcome.
1329 Result = DAG.getEntryNode();
1330 } else {
1331 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001332 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001334 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001335
Jim Laskey274062c2006-10-13 23:32:28 +00001336 // Don't add users to work list.
1337 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001339
Jim Laskey6ff23e52006-10-04 16:53:27 +00001340 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341}
1342
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001343/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001344SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001345 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001346 // Replacing results may cause a different MERGE_VALUES to suddenly
1347 // be CSE'd with N, and carry its uses with it. Iterate until no
1348 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001349 // First add the users of this node to the work list so that they
1350 // can be tried again once they have new operands.
1351 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001352 do {
1353 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001354 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001355 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001356 removeFromWorkList(N);
1357 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001358 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001359}
1360
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001361static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001362SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001363 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001364 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue N00 = N0.getOperand(0);
1366 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001367 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001368
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001370 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001371 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001372 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1373 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001374 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001375 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001376 N00.getOperand(1), N01));
1377 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001378 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001379
Dan Gohman475871a2008-07-27 21:46:04 +00001380 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001381}
1382
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue DAGCombiner::visitADD(SDNode *N) {
1384 SDValue N0 = N->getOperand(0);
1385 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1387 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001388 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001389
1390 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001391 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001394
1395 // fold (add x, 0) -> x, vector edition
1396 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1397 return N0;
1398 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1399 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001400 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001401
Dan Gohman613e0d82007-07-03 14:03:57 +00001402 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001403 if (N0.getOpcode() == ISD::UNDEF)
1404 return N0;
1405 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001406 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001409 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001410 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001411 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001412 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001416 // fold (add Sym, c) -> Sym+c
1417 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001418 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001419 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001420 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001421 GA->getOffset() +
1422 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001423 // fold ((c1-A)+c2) -> (c1+c2)-A
1424 if (N1C && N0.getOpcode() == ISD::SUB)
1425 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001426 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001427 DAG.getConstant(N1C->getAPIntValue()+
1428 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001429 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001430 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001431 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001433 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 // fold ((0-A) + B) -> B-A
1435 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1436 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (A + (0-B)) -> A-B
1439 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1440 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001441 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001442 // fold (A+(B-A)) -> B
1443 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001444 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001445 // fold ((B-A)+A) -> B
1446 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1447 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001448 // fold (A+(B-(A+C))) to (B-C)
1449 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001450 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001451 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001453 // fold (A+(B-(C+A))) to (B-C)
1454 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001455 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001456 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001457 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001458 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001459 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1460 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001461 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001462 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001463 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001464
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001465 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1466 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1467 SDValue N00 = N0.getOperand(0);
1468 SDValue N01 = N0.getOperand(1);
1469 SDValue N10 = N1.getOperand(0);
1470 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001471
1472 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001473 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1474 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1475 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001476 }
Chris Lattner947c2892006-03-13 06:51:27 +00001477
Dan Gohman475871a2008-07-27 21:46:04 +00001478 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1479 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001481 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001483 APInt LHSZero, LHSOne;
1484 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001485 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001486
Dan Gohman948d8ea2008-02-20 16:33:30 +00001487 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001488 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Chris Lattner947c2892006-03-13 06:51:27 +00001490 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1491 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001492 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001493 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001494 }
1495 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001496
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001497 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001498 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001499 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001500 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001501 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001502 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001503 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001504 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001505 }
1506
Dan Gohmancd9e1552010-01-19 23:30:49 +00001507 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1508 if (N1.getOpcode() == ISD::SHL &&
1509 N1.getOperand(0).getOpcode() == ISD::SUB)
1510 if (ConstantSDNode *C =
1511 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1512 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001513 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1514 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001515 N1.getOperand(0).getOperand(1),
1516 N1.getOperand(1)));
1517 if (N0.getOpcode() == ISD::SHL &&
1518 N0.getOperand(0).getOpcode() == ISD::SUB)
1519 if (ConstantSDNode *C =
1520 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1521 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001522 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1523 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001524 N0.getOperand(0).getOperand(1),
1525 N0.getOperand(1)));
1526
Owen Andersonbc146b02010-09-21 20:42:50 +00001527 if (N1.getOpcode() == ISD::AND) {
1528 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001530 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1531 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001532
Owen Andersonbc146b02010-09-21 20:42:50 +00001533 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1534 // and similar xforms where the inner op is either ~0 or 0.
1535 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001536 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001537 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1538 }
1539 }
1540
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001541 // add (sext i1), X -> sub X, (zext i1)
1542 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1543 N0.getOperand(0).getValueType() == MVT::i1 &&
1544 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001545 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001546 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1547 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1548 }
1549
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001550 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551}
1552
Dan Gohman475871a2008-07-27 21:46:04 +00001553SDValue DAGCombiner::visitADDC(SDNode *N) {
1554 SDValue N0 = N->getOperand(0);
1555 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001556 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001558 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattner91153682007-03-04 20:03:15 +00001560 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001561 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001562 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001564 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner91153682007-03-04 20:03:15 +00001566 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001567 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001568 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattnerb6541762007-03-04 20:40:38 +00001570 // fold (addc x, 0) -> x + no carry out
1571 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001572 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001573 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001575 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001576 APInt LHSZero, LHSOne;
1577 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001578 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001579
Dan Gohman948d8ea2008-02-20 16:33:30 +00001580 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001581 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001582
Chris Lattnerb6541762007-03-04 20:40:38 +00001583 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1584 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001585 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001586 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001587 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001588 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::visitADDE(SDNode *N) {
1595 SDValue N0 = N->getOperand(0);
1596 SDValue N1 = N->getOperand(1);
1597 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner91153682007-03-04 20:03:15 +00001601 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001602 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001603 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001604 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Chris Lattnerb6541762007-03-04 20:40:38 +00001606 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001607 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001608 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Dan Gohman475871a2008-07-27 21:46:04 +00001610 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001611}
1612
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001613// Since it may not be valid to emit a fold to zero for vector initializers
1614// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001615static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001616 SelectionDAG &DAG,
1617 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001618 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001619 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001620 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001621 // Produce a vector of zeros.
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001622 EVT ElemTy = VT.getVectorElementType();
1623 if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) ==
1624 TargetLowering::TypePromoteInteger)
1625 ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy);
1626 assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) &&
1627 "Type for zero vector elements is not legal");
1628 SDValue El = DAG.getConstant(0, ElemTy);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001629 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1630 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1631 &Ops[0], Ops.size());
1632 }
1633 return SDValue();
1634}
1635
Dan Gohman475871a2008-07-27 21:46:04 +00001636SDValue DAGCombiner::visitSUB(SDNode *N) {
1637 SDValue N0 = N->getOperand(0);
1638 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1640 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001641 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1642 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001643 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001644
Dan Gohman7f321562007-06-25 16:23:39 +00001645 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001646 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001647 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001648 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001649
1650 // fold (sub x, 0) -> x, vector edition
1651 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1652 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001653 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001654
Chris Lattner854077d2005-10-17 01:07:11 +00001655 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001656 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001657 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001658 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001659 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001660 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001661 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001662 // fold (sub x, c) -> (add x, -c)
1663 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001664 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001665 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001666 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1667 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001668 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001669 // fold A-(A-B) -> B
1670 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1671 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001672 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001673 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001674 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001676 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001677 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001678 // fold C2-(A+C1) -> (C2-C1)-A
1679 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001680 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1681 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001682 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001683 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001684 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001685 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001686 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001687 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1688 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001689 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001690 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001691 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001692 // fold ((A+(C+B))-B) -> A+C
1693 if (N0.getOpcode() == ISD::ADD &&
1694 N0.getOperand(1).getOpcode() == ISD::ADD &&
1695 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001696 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001697 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001698 // fold ((A-(B-C))-C) -> A-B
1699 if (N0.getOpcode() == ISD::SUB &&
1700 N0.getOperand(1).getOpcode() == ISD::SUB &&
1701 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001702 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001703 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001704
Dan Gohman613e0d82007-07-03 14:03:57 +00001705 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001706 if (N0.getOpcode() == ISD::UNDEF)
1707 return N0;
1708 if (N1.getOpcode() == ISD::UNDEF)
1709 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001710
Dan Gohman6520e202008-10-18 02:06:02 +00001711 // If the relocation model supports it, consider symbol offsets.
1712 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001713 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001714 // fold (sub Sym, c) -> Sym-c
1715 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001716 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001717 GA->getOffset() -
1718 (uint64_t)N1C->getSExtValue());
1719 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1720 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1721 if (GA->getGlobal() == GB->getGlobal())
1722 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1723 VT);
1724 }
1725
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001726 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727}
1728
Craig Toppercc274522012-01-07 09:06:39 +00001729SDValue DAGCombiner::visitSUBC(SDNode *N) {
1730 SDValue N0 = N->getOperand(0);
1731 SDValue N1 = N->getOperand(1);
1732 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1733 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1734 EVT VT = N0.getValueType();
1735
1736 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001737 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001738 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1739 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001740 MVT::Glue));
1741
1742 // fold (subc x, x) -> 0 + no borrow
1743 if (N0 == N1)
1744 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001745 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001746 MVT::Glue));
1747
1748 // fold (subc x, 0) -> x + no borrow
1749 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001750 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001751 MVT::Glue));
1752
1753 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1754 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001755 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1756 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001757 MVT::Glue));
1758
1759 return SDValue();
1760}
1761
1762SDValue DAGCombiner::visitSUBE(SDNode *N) {
1763 SDValue N0 = N->getOperand(0);
1764 SDValue N1 = N->getOperand(1);
1765 SDValue CarryIn = N->getOperand(2);
1766
1767 // fold (sube x, y, false) -> (subc x, y)
1768 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001769 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001770
1771 return SDValue();
1772}
1773
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001774/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1775/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001776static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1777 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1778 if (!C)
1779 return false;
1780
1781 APInt SplatUndef;
1782 unsigned SplatBitSize;
1783 bool HasAnyUndefs;
1784 EVT EltVT = N->getValueType(0).getVectorElementType();
1785 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1786 HasAnyUndefs) &&
1787 EltVT.getSizeInBits() >= SplatBitSize);
1788}
1789
Dan Gohman475871a2008-07-27 21:46:04 +00001790SDValue DAGCombiner::visitMUL(SDNode *N) {
1791 SDValue N0 = N->getOperand(0);
1792 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001793 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001794
Dan Gohman613e0d82007-07-03 14:03:57 +00001795 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001796 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001797 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001798
1799 bool N0IsConst = false;
1800 bool N1IsConst = false;
1801 APInt ConstValue0, ConstValue1;
1802 // fold vector ops
1803 if (VT.isVector()) {
1804 SDValue FoldedVOp = SimplifyVBinOp(N);
1805 if (FoldedVOp.getNode()) return FoldedVOp;
1806
1807 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1808 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1809 } else {
1810 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1811 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1812 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1813 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1814 }
1815
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001817 if (N0IsConst && N1IsConst)
1818 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1819
Nate Begeman99801192005-09-07 23:25:52 +00001820 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001821 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001822 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001824 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 return N1;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001826 // fold (mul x, 1) -> x
1827 if (N1IsConst && ConstValue1 == 1)
1828 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001829 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001830 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001831 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001832 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 // fold (mul x, (1 << c)) -> x << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001834 if (N1IsConst && ConstValue1.isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001835 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001836 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001837 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001838 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001839 if (N1IsConst && (-ConstValue1).isPowerOf2()) {
1840 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001841 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001842 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001843 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001844 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001845 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001846 DAG.getConstant(Log2Val,
1847 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001848 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001849
1850 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001851 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001852 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001853 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1854 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001855 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001856 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001857 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001858 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001859 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001861
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001862 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1863 // use.
1864 {
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001866 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001867 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001868 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1869 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001870 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001871 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001872 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001873 isa<ConstantSDNode>(N1.getOperand(1)) &&
1874 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001875 Sh = N1; Y = N0;
1876 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001877
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001879 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001880 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001881 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001882 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001883 }
1884 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001885
Chris Lattnera1deca32006-03-04 23:33:26 +00001886 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001887 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1888 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1889 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001890 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1891 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001892 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001893 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001894 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Nate Begemancd4d58c2006-02-03 06:46:56 +00001896 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001897 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001899 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001900
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001901 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902}
1903
Dan Gohman475871a2008-07-27 21:46:04 +00001904SDValue DAGCombiner::visitSDIV(SDNode *N) {
1905 SDValue N0 = N->getOperand(0);
1906 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001907 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1908 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001909 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910
Dan Gohman7f321562007-06-25 16:23:39 +00001911 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001912 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001914 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001915 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001916
Nate Begeman1d4d4142005-09-01 00:19:25 +00001917 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001918 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001919 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001920 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001921 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001922 return N0;
1923 // fold (sdiv X, -1) -> 0-X
1924 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001926 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001927 // If we know the sign bits of both operands are zero, strength reduce to a
1928 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001929 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001930 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001931 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001932 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001933 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001934 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001935 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001936 (N1C->getAPIntValue().isPowerOf2() ||
1937 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001938 // If dividing by powers of two is cheap, then don't perform the following
1939 // fold.
1940 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001941 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001942
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001943 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001944
Chris Lattner8f4880b2006-02-16 08:02:36 +00001945 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001946 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001947 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001948 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001949 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001950
Chris Lattner8f4880b2006-02-16 08:02:36 +00001951 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001952 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001953 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001954 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001955 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 AddToWorkList(SRL.getNode());
1957 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001958 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001959 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001960
Nate Begeman405e3ec2005-10-21 00:02:42 +00001961 // If we're dividing by a positive value, we're done. Otherwise, we must
1962 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001963 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001964 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001965
Gabor Greifba36cb52008-08-28 21:40:38 +00001966 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001967 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001968 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001969 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001970
Nate Begeman69575232005-10-20 02:15:44 +00001971 // if integer divide is expensive and we satisfy the requirements, emit an
1972 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001973 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001974 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001975 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001976 }
Dan Gohman7f321562007-06-25 16:23:39 +00001977
Dan Gohman613e0d82007-07-03 14:03:57 +00001978 // undef / X -> 0
1979 if (N0.getOpcode() == ISD::UNDEF)
1980 return DAG.getConstant(0, VT);
1981 // X / undef -> undef
1982 if (N1.getOpcode() == ISD::UNDEF)
1983 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001984
Dan Gohman475871a2008-07-27 21:46:04 +00001985 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001986}
1987
Dan Gohman475871a2008-07-27 21:46:04 +00001988SDValue DAGCombiner::visitUDIV(SDNode *N) {
1989 SDValue N0 = N->getOperand(0);
1990 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1992 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001993 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Dan Gohman7f321562007-06-25 16:23:39 +00001995 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001996 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001998 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001999 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002002 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002003 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002004 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002005 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002006 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002007 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002008 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002009 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002010 if (N1.getOpcode() == ISD::SHL) {
2011 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002012 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002013 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002014 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002015 N1.getOperand(1),
2016 DAG.getConstant(SHC->getAPIntValue()
2017 .logBase2(),
2018 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002019 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002020 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002021 }
2022 }
2023 }
Nate Begeman69575232005-10-20 02:15:44 +00002024 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002025 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002026 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002027 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002028 }
Dan Gohman7f321562007-06-25 16:23:39 +00002029
Dan Gohman613e0d82007-07-03 14:03:57 +00002030 // undef / X -> 0
2031 if (N0.getOpcode() == ISD::UNDEF)
2032 return DAG.getConstant(0, VT);
2033 // X / undef -> undef
2034 if (N1.getOpcode() == ISD::UNDEF)
2035 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038}
2039
Dan Gohman475871a2008-07-27 21:46:04 +00002040SDValue DAGCombiner::visitSREM(SDNode *N) {
2041 SDValue N0 = N->getOperand(0);
2042 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002043 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2044 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002045 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002046
Nate Begeman1d4d4142005-09-01 00:19:25 +00002047 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002048 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002049 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002050 // If we know the sign bits of both operands are zero, strength reduce to a
2051 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002052 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002053 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002054 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002056
Dan Gohman77003042007-11-26 23:46:11 +00002057 // If X/C can be simplified by the division-by-constant logic, lower
2058 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002059 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002060 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002061 AddToWorkList(Div.getNode());
2062 SDValue OptimizedDiv = combine(Div.getNode());
2063 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002064 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002065 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002066 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002067 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002068 return Sub;
2069 }
Chris Lattner26d29902006-10-12 20:58:32 +00002070 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002071
Dan Gohman613e0d82007-07-03 14:03:57 +00002072 // undef % X -> 0
2073 if (N0.getOpcode() == ISD::UNDEF)
2074 return DAG.getConstant(0, VT);
2075 // X % undef -> undef
2076 if (N1.getOpcode() == ISD::UNDEF)
2077 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002078
Dan Gohman475871a2008-07-27 21:46:04 +00002079 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080}
2081
Dan Gohman475871a2008-07-27 21:46:04 +00002082SDValue DAGCombiner::visitUREM(SDNode *N) {
2083 SDValue N0 = N->getOperand(0);
2084 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002085 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2086 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002087 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002090 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002091 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002092 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002093 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002094 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002095 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002096 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2097 if (N1.getOpcode() == ISD::SHL) {
2098 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002099 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002100 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002101 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002103 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002104 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002105 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002106 }
2107 }
2108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Dan Gohman77003042007-11-26 23:46:11 +00002110 // If X/C can be simplified by the division-by-constant logic, lower
2111 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002112 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002113 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002114 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002115 SDValue OptimizedDiv = combine(Div.getNode());
2116 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002117 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002118 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002119 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002120 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002121 return Sub;
2122 }
Chris Lattner26d29902006-10-12 20:58:32 +00002123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Dan Gohman613e0d82007-07-03 14:03:57 +00002125 // undef % X -> 0
2126 if (N0.getOpcode() == ISD::UNDEF)
2127 return DAG.getConstant(0, VT);
2128 // X % undef -> undef
2129 if (N1.getOpcode() == ISD::UNDEF)
2130 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002131
Dan Gohman475871a2008-07-27 21:46:04 +00002132 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133}
2134
Dan Gohman475871a2008-07-27 21:46:04 +00002135SDValue DAGCombiner::visitMULHS(SDNode *N) {
2136 SDValue N0 = N->getOperand(0);
2137 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002140 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002141
Nate Begeman1d4d4142005-09-01 00:19:25 +00002142 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002143 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002144 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002146 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002147 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002148 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002149 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002150 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002151 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002152 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002153
Chris Lattnerde1c3602010-12-13 08:39:01 +00002154 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2155 // plus a shift.
2156 if (VT.isSimple() && !VT.isVector()) {
2157 MVT Simple = VT.getSimpleVT();
2158 unsigned SimpleSize = Simple.getSizeInBits();
2159 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2160 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2161 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2162 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2163 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002164 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002165 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002166 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2167 }
2168 }
Owen Anderson95771af2011-02-25 21:41:48 +00002169
Dan Gohman475871a2008-07-27 21:46:04 +00002170 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171}
2172
Dan Gohman475871a2008-07-27 21:46:04 +00002173SDValue DAGCombiner::visitMULHU(SDNode *N) {
2174 SDValue N0 = N->getOperand(0);
2175 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002176 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002177 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002178 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002179
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002181 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002182 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002184 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002185 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002186 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002187 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002188 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002189
Chris Lattnerde1c3602010-12-13 08:39:01 +00002190 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2191 // plus a shift.
2192 if (VT.isSimple() && !VT.isVector()) {
2193 MVT Simple = VT.getSimpleVT();
2194 unsigned SimpleSize = Simple.getSizeInBits();
2195 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2196 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2197 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2198 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2199 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2200 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002201 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002202 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2203 }
2204 }
Owen Anderson95771af2011-02-25 21:41:48 +00002205
Dan Gohman475871a2008-07-27 21:46:04 +00002206 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207}
2208
Dan Gohman389079b2007-10-08 17:57:15 +00002209/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2210/// compute two values. LoOp and HiOp give the opcodes for the two computations
2211/// that are being performed. Return true if a simplification was made.
2212///
Scott Michelfdc40a02009-02-17 22:15:04 +00002213SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002214 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002215 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002216 bool HiExists = N->hasAnyUseOfValue(1);
2217 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002218 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002219 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002220 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002221 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002222 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002223 }
2224
2225 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002226 bool LoExists = N->hasAnyUseOfValue(0);
2227 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002228 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002229 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002230 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002231 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002232 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002233 }
2234
Evan Cheng44711942007-11-08 09:25:29 +00002235 // If both halves are used, return as it is.
2236 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002237 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002238
2239 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002240 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002241 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002242 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002243 AddToWorkList(Lo.getNode());
2244 SDValue LoOpt = combine(Lo.getNode());
2245 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002246 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002247 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002248 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002249 }
2250
Evan Cheng44711942007-11-08 09:25:29 +00002251 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002252 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002253 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002254 AddToWorkList(Hi.getNode());
2255 SDValue HiOpt = combine(Hi.getNode());
2256 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002257 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002258 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002259 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002260 }
Bill Wendling826d1142009-01-30 03:08:40 +00002261
Dan Gohman475871a2008-07-27 21:46:04 +00002262 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002263}
2264
Dan Gohman475871a2008-07-27 21:46:04 +00002265SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2266 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002268
Chris Lattner33e77d32010-12-15 06:04:19 +00002269 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002270 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002271
2272 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2273 // plus a shift.
2274 if (VT.isSimple() && !VT.isVector()) {
2275 MVT Simple = VT.getSimpleVT();
2276 unsigned SimpleSize = Simple.getSizeInBits();
2277 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2278 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2279 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2280 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2281 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2282 // Compute the high part as N1.
2283 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002284 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002285 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2286 // Compute the low part as N0.
2287 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2288 return CombineTo(N, Lo, Hi);
2289 }
2290 }
Owen Anderson95771af2011-02-25 21:41:48 +00002291
Dan Gohman475871a2008-07-27 21:46:04 +00002292 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002293}
2294
Dan Gohman475871a2008-07-27 21:46:04 +00002295SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2296 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002297 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002298
Chris Lattner33e77d32010-12-15 06:04:19 +00002299 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002300 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002301
Chris Lattner33e77d32010-12-15 06:04:19 +00002302 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2303 // plus a shift.
2304 if (VT.isSimple() && !VT.isVector()) {
2305 MVT Simple = VT.getSimpleVT();
2306 unsigned SimpleSize = Simple.getSizeInBits();
2307 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2308 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2309 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2310 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2311 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2312 // Compute the high part as N1.
2313 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002314 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002315 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2316 // Compute the low part as N0.
2317 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2318 return CombineTo(N, Lo, Hi);
2319 }
2320 }
Owen Anderson95771af2011-02-25 21:41:48 +00002321
Dan Gohman475871a2008-07-27 21:46:04 +00002322 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002323}
2324
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002325SDValue DAGCombiner::visitSMULO(SDNode *N) {
2326 // (smulo x, 2) -> (saddo x, x)
2327 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2328 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002329 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002330 N->getOperand(0), N->getOperand(0));
2331
2332 return SDValue();
2333}
2334
2335SDValue DAGCombiner::visitUMULO(SDNode *N) {
2336 // (umulo x, 2) -> (uaddo x, x)
2337 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2338 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002339 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002340 N->getOperand(0), N->getOperand(0));
2341
2342 return SDValue();
2343}
2344
Dan Gohman475871a2008-07-27 21:46:04 +00002345SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2346 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002347 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002348
Dan Gohman475871a2008-07-27 21:46:04 +00002349 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002350}
2351
Dan Gohman475871a2008-07-27 21:46:04 +00002352SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2353 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002354 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002355
Dan Gohman475871a2008-07-27 21:46:04 +00002356 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002357}
2358
Chris Lattner35e5c142006-05-05 05:51:50 +00002359/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2360/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002361SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2362 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002363 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002364 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Dan Gohmanff00a552010-01-14 03:08:49 +00002366 // Bail early if none of these transforms apply.
2367 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2368
Chris Lattner540121f2006-05-05 06:31:05 +00002369 // For each of OP in AND/OR/XOR:
2370 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2371 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2372 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002373 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002374 //
2375 // do not sink logical op inside of a vector extend, since it may combine
2376 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002377 EVT Op0VT = N0.getOperand(0).getValueType();
2378 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002379 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002380 // Avoid infinite looping with PromoteIntBinOp.
2381 (N0.getOpcode() == ISD::ANY_EXTEND &&
2382 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002383 (N0.getOpcode() == ISD::TRUNCATE &&
2384 (!TLI.isZExtFree(VT, Op0VT) ||
2385 !TLI.isTruncateFree(Op0VT, VT)) &&
2386 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002387 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002388 Op0VT == N1.getOperand(0).getValueType() &&
2389 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002390 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002391 N0.getOperand(0).getValueType(),
2392 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002393 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002394 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002397 // For each of OP in SHL/SRL/SRA/AND...
2398 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2399 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2400 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002401 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002402 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002403 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002404 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002405 N0.getOperand(0).getValueType(),
2406 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002407 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002408 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002409 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002411
Nadav Rotem4ac90812012-04-01 19:31:22 +00002412 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2413 // Only perform this optimization after type legalization and before
2414 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2415 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2416 // we don't want to undo this promotion.
2417 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2418 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002419 if ((N0.getOpcode() == ISD::BITCAST ||
2420 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2421 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002422 SDValue In0 = N0.getOperand(0);
2423 SDValue In1 = N1.getOperand(0);
2424 EVT In0Ty = In0.getValueType();
2425 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002426 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002427 // If both incoming values are integers, and the original types are the
2428 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002429 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002430 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2431 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002432 AddToWorkList(Op.getNode());
2433 return BC;
2434 }
2435 }
2436
2437 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2438 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2439 // If both shuffles use the same mask, and both shuffle within a single
2440 // vector, then it is worthwhile to move the swizzle after the operation.
2441 // The type-legalizer generates this pattern when loading illegal
2442 // vector types from memory. In many cases this allows additional shuffle
2443 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002444 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2445 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2446 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002447 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2448 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002449
2450 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2451 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002452
2453 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002454
2455 // Check that both shuffles use the same mask. The masks are known to be of
2456 // the same length because the result vector type is the same.
2457 bool SameMask = true;
2458 for (unsigned i = 0; i != NumElts; ++i) {
2459 int Idx0 = SVN0->getMaskElt(i);
2460 int Idx1 = SVN1->getMaskElt(i);
2461 if (Idx0 != Idx1) {
2462 SameMask = false;
2463 break;
2464 }
2465 }
2466
Craig Topperf9204232012-04-09 07:19:09 +00002467 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002468 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002469 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002470 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002471 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002472 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002473 }
2474 }
Craig Topperf9204232012-04-09 07:19:09 +00002475
Dan Gohman475871a2008-07-27 21:46:04 +00002476 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002477}
2478
Dan Gohman475871a2008-07-27 21:46:04 +00002479SDValue DAGCombiner::visitAND(SDNode *N) {
2480 SDValue N0 = N->getOperand(0);
2481 SDValue N1 = N->getOperand(1);
2482 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002483 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2484 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002485 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002486 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002487
Dan Gohman7f321562007-06-25 16:23:39 +00002488 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002489 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002490 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002491 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002492
2493 // fold (and x, 0) -> 0, vector edition
2494 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2495 return N0;
2496 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2497 return N1;
2498
2499 // fold (and x, -1) -> x, vector edition
2500 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2501 return N1;
2502 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2503 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002505
Dan Gohman613e0d82007-07-03 14:03:57 +00002506 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002507 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002508 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002510 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002511 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002512 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002513 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002514 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002515 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002516 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002517 return N0;
2518 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002519 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002520 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002521 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002522 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002523 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002524 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002525 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002526 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002527 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002529 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002530 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002531 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2532 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002533 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002534 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002535 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002536 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002537 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002538 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002539
Chris Lattner1ec05d12006-03-01 21:47:21 +00002540 // Replace uses of the AND with uses of the Zero extend node.
2541 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002542
Chris Lattner3603cd62006-02-02 07:17:31 +00002543 // We actually want to replace all uses of the any_extend with the
2544 // zero_extend, to avoid duplicating things. This will later cause this
2545 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002546 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002547 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002548 }
2549 }
Stephen Lin155615d2013-07-08 00:37:03 +00002550 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002551 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2552 // already be zero by virtue of the width of the base type of the load.
2553 //
2554 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2555 // more cases.
2556 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2557 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2558 N0.getOpcode() == ISD::LOAD) {
2559 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2560 N0 : N0.getOperand(0) );
2561
2562 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2563 // This can be a pure constant or a vector splat, in which case we treat the
2564 // vector as a scalar and use the splat value.
2565 APInt Constant = APInt::getNullValue(1);
2566 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2567 Constant = C->getAPIntValue();
2568 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2569 APInt SplatValue, SplatUndef;
2570 unsigned SplatBitSize;
2571 bool HasAnyUndefs;
2572 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2573 SplatBitSize, HasAnyUndefs);
2574 if (IsSplat) {
2575 // Undef bits can contribute to a possible optimisation if set, so
2576 // set them.
2577 SplatValue |= SplatUndef;
2578
2579 // The splat value may be something like "0x00FFFFFF", which means 0 for
2580 // the first vector value and FF for the rest, repeating. We need a mask
2581 // that will apply equally to all members of the vector, so AND all the
2582 // lanes of the constant together.
2583 EVT VT = Vector->getValueType(0);
2584 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002585
2586 // If the splat value has been compressed to a bitlength lower
2587 // than the size of the vector lane, we need to re-expand it to
2588 // the lane size.
2589 if (BitWidth > SplatBitSize)
2590 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2591 SplatBitSize < BitWidth;
2592 SplatBitSize = SplatBitSize * 2)
2593 SplatValue |= SplatValue.shl(SplatBitSize);
2594
James Molloy6259dcd2012-02-20 12:02:38 +00002595 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002596 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002597 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2598 }
2599 }
2600
2601 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2602 // actually legal and isn't going to get expanded, else this is a false
2603 // optimisation.
2604 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2605 Load->getMemoryVT());
2606
2607 // Resize the constant to the same size as the original memory access before
2608 // extension. If it is still the AllOnesValue then this AND is completely
2609 // unneeded.
2610 Constant =
2611 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2612
2613 bool B;
2614 switch (Load->getExtensionType()) {
2615 default: B = false; break;
2616 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2617 case ISD::ZEXTLOAD:
2618 case ISD::NON_EXTLOAD: B = true; break;
2619 }
2620
2621 if (B && Constant.isAllOnesValue()) {
2622 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2623 // preserve semantics once we get rid of the AND.
2624 SDValue NewLoad(Load, 0);
2625 if (Load->getExtensionType() == ISD::EXTLOAD) {
2626 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002627 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002628 Load->getChain(), Load->getBasePtr(),
2629 Load->getOffset(), Load->getMemoryVT(),
2630 Load->getMemOperand());
2631 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002632 if (Load->getNumValues() == 3) {
2633 // PRE/POST_INC loads have 3 values.
2634 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2635 NewLoad.getValue(2) };
2636 CombineTo(Load, To, 3, true);
2637 } else {
2638 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2639 }
James Molloy6259dcd2012-02-20 12:02:38 +00002640 }
2641
2642 // Fold the AND away, taking care not to fold to the old load node if we
2643 // replaced it.
2644 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2645
2646 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2647 }
2648 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002649 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2650 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2651 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2652 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002653
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002654 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002655 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002656 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002657 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002658 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002659 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002660 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002661 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002662 }
Bill Wendling2627a882009-01-30 20:43:18 +00002663 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002664 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002665 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002666 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002667 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002668 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002669 }
Bill Wendling2627a882009-01-30 20:43:18 +00002670 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002671 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002672 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002673 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002674 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002675 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002676 }
2677 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002678 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2679 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2680 Op0 == Op1 && LL.getValueType().isInteger() &&
2681 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2682 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2683 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2684 cast<ConstantSDNode>(RR)->isNullValue()))) {
2685 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2686 LL, DAG.getConstant(1, LL.getValueType()));
2687 AddToWorkList(ADDNode.getNode());
2688 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2689 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2690 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002691 // canonicalize equivalent to ll == rl
2692 if (LL == RR && LR == RL) {
2693 Op1 = ISD::getSetCCSwappedOperands(Op1);
2694 std::swap(RL, RR);
2695 }
2696 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002697 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002698 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002699 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002700 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002701 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2702 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002703 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002704 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002705 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002706 }
2707 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002708
Bill Wendling2627a882009-01-30 20:43:18 +00002709 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002710 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002711 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002712 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002713 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002714
Nate Begemande996292006-02-03 22:24:05 +00002715 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2716 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002717 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002718 SimplifyDemandedBits(SDValue(N, 0)))
2719 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002720
Nate Begemanded49632005-10-13 03:11:28 +00002721 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002722 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002723 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002724 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002725 // If we zero all the possible extended bits, then we can turn this into
2726 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002727 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002728 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002729 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002730 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002731 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002732 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002733 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002734 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002735 LN0->isVolatile(), LN0->isNonTemporal(),
2736 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002737 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002738 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002739 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002740 }
2741 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002742 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002743 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002744 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002745 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002746 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002747 // If we zero all the possible extended bits, then we can turn this into
2748 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002749 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002750 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002751 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002752 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002753 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002754 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002755 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002756 LN0->getBasePtr(), LN0->getPointerInfo(),
2757 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002758 LN0->isVolatile(), LN0->isNonTemporal(),
2759 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002760 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002761 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002762 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002763 }
2764 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002765
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002766 // fold (and (load x), 255) -> (zextload x, i8)
2767 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002768 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2769 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2770 (N0.getOpcode() == ISD::ANY_EXTEND &&
2771 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2772 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2773 LoadSDNode *LN0 = HasAnyExt
2774 ? cast<LoadSDNode>(N0.getOperand(0))
2775 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002776 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002777 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002778 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002779 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2780 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2781 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002782
Evan Chengd40d03e2010-01-06 19:38:29 +00002783 if (ExtVT == LoadedVT &&
2784 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002785 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002786
2787 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002788 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002789 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002790 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002791 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2792 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002793 AddToWorkList(N);
2794 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2795 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2796 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002797
Chris Lattneref7634c2010-01-07 21:53:27 +00002798 // Do not change the width of a volatile load.
2799 // Do not generate loads of non-round integer types since these can
2800 // be expensive (and would be wrong if the type is not byte sized).
2801 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2802 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2803 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002804
Chris Lattneref7634c2010-01-07 21:53:27 +00002805 unsigned Alignment = LN0->getAlignment();
2806 SDValue NewPtr = LN0->getBasePtr();
2807
2808 // For big endian targets, we need to add an offset to the pointer
2809 // to load the correct bytes. For little endian systems, we merely
2810 // need to read fewer bytes from the same pointer.
2811 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002812 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2813 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2814 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002815 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002816 NewPtr, DAG.getConstant(PtrOff, PtrType));
2817 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002818 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002819
2820 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002821
Chris Lattneref7634c2010-01-07 21:53:27 +00002822 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2823 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002824 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002825 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002826 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002827 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2828 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002829 AddToWorkList(N);
2830 CombineTo(LN0, Load, Load.getValue(1));
2831 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002832 }
Evan Cheng466685d2006-10-09 20:57:25 +00002833 }
Chris Lattner15045b62006-02-28 06:35:35 +00002834 }
2835 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002836
Evan Chenga9e13ba2012-07-17 18:54:11 +00002837 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2838 VT.getSizeInBits() <= 64) {
2839 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2840 APInt ADDC = ADDI->getAPIntValue();
2841 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2842 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2843 // immediate for an add, but it is legal if its top c2 bits are set,
2844 // transform the ADD so the immediate doesn't need to be materialized
2845 // in a register.
2846 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2847 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2848 SRLI->getZExtValue());
2849 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2850 ADDC |= Mask;
2851 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2852 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002853 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002854 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2855 CombineTo(N0.getNode(), NewAdd);
2856 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2857 }
2858 }
2859 }
2860 }
2861 }
2862 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002863
Tim Northover5d8c2e42013-08-27 13:46:45 +00002864 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2865 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2866 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2867 N0.getOperand(1), false);
2868 if (BSwap.getNode())
2869 return BSwap;
2870 }
2871
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002872 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002873}
2874
Evan Cheng9568e5c2011-06-21 06:01:08 +00002875/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2876///
2877SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2878 bool DemandHighBits) {
2879 if (!LegalOperations)
2880 return SDValue();
2881
2882 EVT VT = N->getValueType(0);
2883 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2884 return SDValue();
2885 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2886 return SDValue();
2887
2888 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2889 bool LookPassAnd0 = false;
2890 bool LookPassAnd1 = false;
2891 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2892 std::swap(N0, N1);
2893 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2894 std::swap(N0, N1);
2895 if (N0.getOpcode() == ISD::AND) {
2896 if (!N0.getNode()->hasOneUse())
2897 return SDValue();
2898 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2899 if (!N01C || N01C->getZExtValue() != 0xFF00)
2900 return SDValue();
2901 N0 = N0.getOperand(0);
2902 LookPassAnd0 = true;
2903 }
2904
2905 if (N1.getOpcode() == ISD::AND) {
2906 if (!N1.getNode()->hasOneUse())
2907 return SDValue();
2908 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2909 if (!N11C || N11C->getZExtValue() != 0xFF)
2910 return SDValue();
2911 N1 = N1.getOperand(0);
2912 LookPassAnd1 = true;
2913 }
2914
2915 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2916 std::swap(N0, N1);
2917 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2918 return SDValue();
2919 if (!N0.getNode()->hasOneUse() ||
2920 !N1.getNode()->hasOneUse())
2921 return SDValue();
2922
2923 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2924 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2925 if (!N01C || !N11C)
2926 return SDValue();
2927 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2928 return SDValue();
2929
2930 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2931 SDValue N00 = N0->getOperand(0);
2932 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2933 if (!N00.getNode()->hasOneUse())
2934 return SDValue();
2935 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2936 if (!N001C || N001C->getZExtValue() != 0xFF)
2937 return SDValue();
2938 N00 = N00.getOperand(0);
2939 LookPassAnd0 = true;
2940 }
2941
2942 SDValue N10 = N1->getOperand(0);
2943 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2944 if (!N10.getNode()->hasOneUse())
2945 return SDValue();
2946 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2947 if (!N101C || N101C->getZExtValue() != 0xFF00)
2948 return SDValue();
2949 N10 = N10.getOperand(0);
2950 LookPassAnd1 = true;
2951 }
2952
2953 if (N00 != N10)
2954 return SDValue();
2955
Tim Northover5d8c2e42013-08-27 13:46:45 +00002956 // Make sure everything beyond the low halfword gets set to zero since the SRL
2957 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002958 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002959 if (DemandHighBits && OpSizeInBits > 16) {
2960 // If the left-shift isn't masked out then the only way this is a bswap is
2961 // if all bits beyond the low 8 are 0. In that case the entire pattern
2962 // reduces to a left shift anyway: leave it for other parts of the combiner.
2963 if (!LookPassAnd0)
2964 return SDValue();
2965
2966 // However, if the right shift isn't masked out then it might be because
2967 // it's not needed. See if we can spot that too.
2968 if (!LookPassAnd1 &&
2969 !DAG.MaskedValueIsZero(
2970 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2971 return SDValue();
2972 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00002973
Andrew Trickac6d9be2013-05-25 02:42:55 +00002974 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002975 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002976 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002977 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2978 return Res;
2979}
2980
2981/// isBSwapHWordElement - Return true if the specified node is an element
2982/// that makes up a 32-bit packed halfword byteswap. i.e.
2983/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00002984static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002985 if (!N.getNode()->hasOneUse())
2986 return false;
2987
2988 unsigned Opc = N.getOpcode();
2989 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2990 return false;
2991
2992 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2993 if (!N1C)
2994 return false;
2995
2996 unsigned Num;
2997 switch (N1C->getZExtValue()) {
2998 default:
2999 return false;
3000 case 0xFF: Num = 0; break;
3001 case 0xFF00: Num = 1; break;
3002 case 0xFF0000: Num = 2; break;
3003 case 0xFF000000: Num = 3; break;
3004 }
3005
3006 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3007 SDValue N0 = N.getOperand(0);
3008 if (Opc == ISD::AND) {
3009 if (Num == 0 || Num == 2) {
3010 // (x >> 8) & 0xff
3011 // (x >> 8) & 0xff0000
3012 if (N0.getOpcode() != ISD::SRL)
3013 return false;
3014 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3015 if (!C || C->getZExtValue() != 8)
3016 return false;
3017 } else {
3018 // (x << 8) & 0xff00
3019 // (x << 8) & 0xff000000
3020 if (N0.getOpcode() != ISD::SHL)
3021 return false;
3022 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3023 if (!C || C->getZExtValue() != 8)
3024 return false;
3025 }
3026 } else if (Opc == ISD::SHL) {
3027 // (x & 0xff) << 8
3028 // (x & 0xff0000) << 8
3029 if (Num != 0 && Num != 2)
3030 return false;
3031 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3032 if (!C || C->getZExtValue() != 8)
3033 return false;
3034 } else { // Opc == ISD::SRL
3035 // (x & 0xff00) >> 8
3036 // (x & 0xff000000) >> 8
3037 if (Num != 1 && Num != 3)
3038 return false;
3039 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3040 if (!C || C->getZExtValue() != 8)
3041 return false;
3042 }
3043
3044 if (Parts[Num])
3045 return false;
3046
3047 Parts[Num] = N0.getOperand(0).getNode();
3048 return true;
3049}
3050
3051/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3052/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3053/// => (rotl (bswap x), 16)
3054SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3055 if (!LegalOperations)
3056 return SDValue();
3057
3058 EVT VT = N->getValueType(0);
3059 if (VT != MVT::i32)
3060 return SDValue();
3061 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3062 return SDValue();
3063
3064 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3065 // Look for either
3066 // (or (or (and), (and)), (or (and), (and)))
3067 // (or (or (or (and), (and)), (and)), (and))
3068 if (N0.getOpcode() != ISD::OR)
3069 return SDValue();
3070 SDValue N00 = N0.getOperand(0);
3071 SDValue N01 = N0.getOperand(1);
3072
Evan Cheng9a65a012012-12-13 01:34:32 +00003073 if (N1.getOpcode() == ISD::OR &&
3074 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003075 // (or (or (and), (and)), (or (and), (and)))
3076 SDValue N000 = N00.getOperand(0);
3077 if (!isBSwapHWordElement(N000, Parts))
3078 return SDValue();
3079
3080 SDValue N001 = N00.getOperand(1);
3081 if (!isBSwapHWordElement(N001, Parts))
3082 return SDValue();
3083 SDValue N010 = N01.getOperand(0);
3084 if (!isBSwapHWordElement(N010, Parts))
3085 return SDValue();
3086 SDValue N011 = N01.getOperand(1);
3087 if (!isBSwapHWordElement(N011, Parts))
3088 return SDValue();
3089 } else {
3090 // (or (or (or (and), (and)), (and)), (and))
3091 if (!isBSwapHWordElement(N1, Parts))
3092 return SDValue();
3093 if (!isBSwapHWordElement(N01, Parts))
3094 return SDValue();
3095 if (N00.getOpcode() != ISD::OR)
3096 return SDValue();
3097 SDValue N000 = N00.getOperand(0);
3098 if (!isBSwapHWordElement(N000, Parts))
3099 return SDValue();
3100 SDValue N001 = N00.getOperand(1);
3101 if (!isBSwapHWordElement(N001, Parts))
3102 return SDValue();
3103 }
3104
3105 // Make sure the parts are all coming from the same node.
3106 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3107 return SDValue();
3108
Andrew Trickac6d9be2013-05-25 02:42:55 +00003109 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003110 SDValue(Parts[0],0));
3111
3112 // Result of the bswap should be rotated by 16. If it's not legal, than
3113 // do (x << 16) | (x >> 16).
3114 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3115 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003116 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003117 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003118 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3119 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3120 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3121 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003122}
3123
Dan Gohman475871a2008-07-27 21:46:04 +00003124SDValue DAGCombiner::visitOR(SDNode *N) {
3125 SDValue N0 = N->getOperand(0);
3126 SDValue N1 = N->getOperand(1);
3127 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003128 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3129 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003130 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003131
Dan Gohman7f321562007-06-25 16:23:39 +00003132 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003133 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003134 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003135 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003136
3137 // fold (or x, 0) -> x, vector edition
3138 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3139 return N1;
3140 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3141 return N0;
3142
3143 // fold (or x, -1) -> -1, vector edition
3144 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3145 return N0;
3146 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3147 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003149
Dan Gohman613e0d82007-07-03 14:03:57 +00003150 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003151 if (!LegalOperations &&
3152 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003153 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3154 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3155 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003156 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003157 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003158 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003159 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003160 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003161 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003163 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003164 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003165 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003166 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003167 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003168 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003169 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003170 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003171
3172 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3173 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3174 if (BSwap.getNode() != 0)
3175 return BSwap;
3176 BSwap = MatchBSwapHWordLow(N, N0, N1);
3177 if (BSwap.getNode() != 0)
3178 return BSwap;
3179
Nate Begemancd4d58c2006-02-03 06:46:56 +00003180 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003181 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003182 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003183 return ROR;
3184 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003185 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003186 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003187 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003188 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003189 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003190 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3191 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003192 N0.getOperand(0), N1),
3193 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003194 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003195 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3196 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3197 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3198 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003199
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003200 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003201 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003202 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3203 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003204 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003205 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003206 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003207 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003208 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003209 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003210 }
Bill Wendling09025642009-01-30 20:59:34 +00003211 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3212 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003213 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003214 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003215 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003216 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003217 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003218 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003219 }
3220 }
3221 // canonicalize equivalent to ll == rl
3222 if (LL == RR && LR == RL) {
3223 Op1 = ISD::getSetCCSwappedOperands(Op1);
3224 std::swap(RL, RR);
3225 }
3226 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003227 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003228 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003229 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003230 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003231 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3232 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003233 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003234 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003235 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003236 }
3237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003238
Bill Wendling09025642009-01-30 20:59:34 +00003239 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003240 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003241 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003242 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Bill Wendling09025642009-01-30 20:59:34 +00003245 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003246 if (N0.getOpcode() == ISD::AND &&
3247 N1.getOpcode() == ISD::AND &&
3248 N0.getOperand(1).getOpcode() == ISD::Constant &&
3249 N1.getOperand(1).getOpcode() == ISD::Constant &&
3250 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003251 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003252 // We can only do this xform if we know that bits from X that are set in C2
3253 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003254 const APInt &LHSMask =
3255 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3256 const APInt &RHSMask =
3257 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003258
Dan Gohmanea859be2007-06-22 14:59:07 +00003259 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3260 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003261 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003262 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003263 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003264 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003265 }
3266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003267
Chris Lattner516b9622006-09-14 20:50:57 +00003268 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003269 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003270 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003271
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003272 // Simplify the operands using demanded-bits information.
3273 if (!VT.isVector() &&
3274 SimplifyDemandedBits(SDValue(N, 0)))
3275 return SDValue(N, 0);
3276
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003277 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003278}
3279
Chris Lattner516b9622006-09-14 20:50:57 +00003280/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003281static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003282 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003283 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003284 Mask = Op.getOperand(1);
3285 Op = Op.getOperand(0);
3286 } else {
3287 return false;
3288 }
3289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003290
Chris Lattner516b9622006-09-14 20:50:57 +00003291 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3292 Shift = Op;
3293 return true;
3294 }
Bill Wendling09025642009-01-30 20:59:34 +00003295
Scott Michelfdc40a02009-02-17 22:15:04 +00003296 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003297}
3298
Chris Lattner516b9622006-09-14 20:50:57 +00003299// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3300// idioms for rotate, and if the target supports rotation instructions, generate
3301// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003302SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003303 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003304 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003305 if (!TLI.isTypeLegal(VT)) return 0;
3306
3307 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003308 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3309 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003310 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003311
Chris Lattner516b9622006-09-14 20:50:57 +00003312 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003313 SDValue LHSShift; // The shift.
3314 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003315 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3316 return 0; // Not part of a rotate.
3317
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue RHSShift; // The shift.
3319 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003320 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3321 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003322
Chris Lattner516b9622006-09-14 20:50:57 +00003323 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3324 return 0; // Not shifting the same value.
3325
3326 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3327 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003328
Chris Lattner516b9622006-09-14 20:50:57 +00003329 // Canonicalize shl to left side in a shl/srl pair.
3330 if (RHSShift.getOpcode() == ISD::SHL) {
3331 std::swap(LHS, RHS);
3332 std::swap(LHSShift, RHSShift);
3333 std::swap(LHSMask , RHSMask );
3334 }
3335
Duncan Sands83ec4b62008-06-06 12:08:01 +00003336 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003337 SDValue LHSShiftArg = LHSShift.getOperand(0);
3338 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3339 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003340
3341 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3342 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003343 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3344 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003345 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3346 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003347 if ((LShVal + RShVal) != OpSizeInBits)
3348 return 0;
3349
Craig Topper32b73432012-09-29 06:54:22 +00003350 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3351 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003352
Chris Lattner516b9622006-09-14 20:50:57 +00003353 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003354 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003355 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003356
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003358 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3359 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003360 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003361 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003362 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3363 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003364 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003365
Bill Wendling317bd702009-01-30 21:14:50 +00003366 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003368
Gabor Greifba36cb52008-08-28 21:40:38 +00003369 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003371
Chris Lattner516b9622006-09-14 20:50:57 +00003372 // If there is a mask here, and we have a variable shift, we can't be sure
3373 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003374 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003375 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003376
Chris Lattner516b9622006-09-14 20:50:57 +00003377 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3378 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003379 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3380 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003381 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003382 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Stephen Linb4940152013-07-09 00:44:49 +00003383 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003384 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3385 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003386 }
3387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003388
Chris Lattner516b9622006-09-14 20:50:57 +00003389 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3390 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003391 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
Stephen Linb4940152013-07-09 00:44:49 +00003392 RHSShiftAmt == LHSShiftAmt.getOperand(1))
Scott Michelfdc40a02009-02-17 22:15:04 +00003393 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003394 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0)))
3395 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003396 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3397 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003398
Dan Gohman74feef22008-10-17 01:23:35 +00003399 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003400 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3401 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3402 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3403 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3404 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3405 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3406 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3407 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003408 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3409 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003410 if (RExtOp0.getOpcode() == ISD::SUB &&
3411 RExtOp0.getOperand(1) == LExtOp0) {
3412 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003413 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003414 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003415 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003416 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003417 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0)))
3418 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003419 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3420 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003421 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003422 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3423 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003424 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003425 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003426 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003427 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003428 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003429 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0)))
3430 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003431 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3432 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003433 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003434 }
3435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003436
Chris Lattner516b9622006-09-14 20:50:57 +00003437 return 0;
3438}
3439
Dan Gohman475871a2008-07-27 21:46:04 +00003440SDValue DAGCombiner::visitXOR(SDNode *N) {
3441 SDValue N0 = N->getOperand(0);
3442 SDValue N1 = N->getOperand(1);
3443 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003444 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003446 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003447
Dan Gohman7f321562007-06-25 16:23:39 +00003448 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003449 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003450 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003451 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003452
3453 // fold (xor x, 0) -> x, vector edition
3454 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3455 return N1;
3456 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3457 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003459
Evan Cheng26471c42008-03-25 20:08:07 +00003460 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3461 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3462 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003463 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003464 if (N0.getOpcode() == ISD::UNDEF)
3465 return N0;
3466 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003467 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003468 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003469 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003470 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003471 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003472 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003473 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003474 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003475 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003476 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003477 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003478 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003479 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003480 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003481
Nate Begeman1d4d4142005-09-01 00:19:25 +00003482 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003483 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003484 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003485 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3486 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003487
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003488 if (!LegalOperations ||
3489 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003490 switch (N0.getOpcode()) {
3491 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003492 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003493 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003494 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003495 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003496 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003497 N0.getOperand(3), NotCC);
3498 }
3499 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003500 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003501
Chris Lattner61c5ff42007-09-10 21:39:07 +00003502 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003503 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003504 N0.getNode()->hasOneUse() &&
3505 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003506 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003507 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003508 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003509 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003510 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003512
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003513 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003515 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003516 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003517 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3518 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003519 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3520 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003521 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003522 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003523 }
3524 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003525 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003526 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003527 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003528 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003529 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3530 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003531 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3532 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003533 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003534 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003535 }
3536 }
David Majnemer363160a2013-05-08 06:44:42 +00003537 // fold (xor (and x, y), y) -> (and (not x), y)
3538 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3539 N0->getOperand(1) == N1) {
3540 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003541 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003542 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003543 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003544 }
Bill Wendling317bd702009-01-30 21:14:50 +00003545 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003546 if (N1C && N0.getOpcode() == ISD::XOR) {
3547 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3548 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3549 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003550 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003551 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003552 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003553 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003554 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003555 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003556 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003557 }
3558 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003559 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003560 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003561
Chris Lattner35e5c142006-05-05 05:51:50 +00003562 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3563 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003564 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003565 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003567
Chris Lattner3e104b12006-04-08 04:15:24 +00003568 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003569 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SimplifyDemandedBits(SDValue(N, 0)))
3571 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003572
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003573 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003574}
3575
Chris Lattnere70da202007-12-06 07:33:36 +00003576/// visitShiftByConstant - Handle transforms common to the three shifts, when
3577/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003578SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003579 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003580 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003581
Chris Lattnere70da202007-12-06 07:33:36 +00003582 // We want to pull some binops through shifts, so that we have (and (shift))
3583 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3584 // thing happens with address calculations, so it's important to canonicalize
3585 // it.
3586 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003587
Chris Lattnere70da202007-12-06 07:33:36 +00003588 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003589 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003590 case ISD::OR:
3591 case ISD::XOR:
3592 HighBitSet = false; // We can only transform sra if the high bit is clear.
3593 break;
3594 case ISD::AND:
3595 HighBitSet = true; // We can only transform sra if the high bit is set.
3596 break;
3597 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003598 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003599 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003600 HighBitSet = false; // We can only transform sra if the high bit is clear.
3601 break;
3602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003603
Chris Lattnere70da202007-12-06 07:33:36 +00003604 // We require the RHS of the binop to be a constant as well.
3605 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003606 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003607
3608 // FIXME: disable this unless the input to the binop is a shift by a constant.
3609 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003610 //
Bill Wendling88103372009-01-30 21:37:17 +00003611 // void foo(int *X, int i) { X[i & 1235] = 1; }
3612 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003613 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003614 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003615 BinOpLHSVal->getOpcode() != ISD::SRA &&
3616 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3617 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003618 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003619
Owen Andersone50ed302009-08-10 22:56:29 +00003620 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003621
Bill Wendling88103372009-01-30 21:37:17 +00003622 // If this is a signed shift right, and the high bit is modified by the
3623 // logical operation, do not perform the transformation. The highBitSet
3624 // boolean indicates the value of the high bit of the constant which would
3625 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003626 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003627 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3628 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003629 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003631
Chris Lattnere70da202007-12-06 07:33:36 +00003632 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003633 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003634 N->getValueType(0),
3635 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003636
3637 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003638 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003639 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003640 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003641
3642 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003643 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003644}
3645
Dan Gohman475871a2008-07-27 21:46:04 +00003646SDValue DAGCombiner::visitSHL(SDNode *N) {
3647 SDValue N0 = N->getOperand(0);
3648 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003649 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3650 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003651 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003652 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003653
Nate Begeman1d4d4142005-09-01 00:19:25 +00003654 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003655 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003656 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003657 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003658 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003659 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003660 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003661 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003662 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003663 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003664 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003665 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003666 // fold (shl undef, x) -> 0
3667 if (N0.getOpcode() == ISD::UNDEF)
3668 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003669 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003670 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003671 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003672 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003673 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003674 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003675 N1.getOperand(0).getOpcode() == ISD::AND &&
3676 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003677 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003678 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003679 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003680 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003681 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003682 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003683 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3684 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003685 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003686 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003687 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003688 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003689 }
3690 }
3691
Dan Gohman475871a2008-07-27 21:46:04 +00003692 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3693 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003694
3695 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003696 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003697 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003698 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3699 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003700 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003701 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003702 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003703 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003704 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003705
3706 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3707 // For this to be valid, the second form must not preserve any of the bits
3708 // that are shifted out by the inner shift in the first form. This means
3709 // the outer shift size must be >= the number of bits added by the ext.
3710 // As a corollary, we don't care what kind of ext it is.
3711 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3712 N0.getOpcode() == ISD::ANY_EXTEND ||
3713 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3714 N0.getOperand(0).getOpcode() == ISD::SHL &&
3715 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003716 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003717 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3718 uint64_t c2 = N1C->getZExtValue();
3719 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3720 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3721 if (c2 >= OpSizeInBits - InnerShiftSize) {
3722 if (c1 + c2 >= OpSizeInBits)
3723 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003724 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3725 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003726 N0.getOperand(0)->getOperand(0)),
3727 DAG.getConstant(c1 + c2, N1.getValueType()));
3728 }
3729 }
3730
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003731 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3732 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003733 // Only fold this if the inner shift has no other uses -- if it does, folding
3734 // this will increase the total number of instructions.
3735 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003737 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003738 if (c1 < VT.getSizeInBits()) {
3739 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003740 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3741 VT.getSizeInBits() - c1);
3742 SDValue Shift;
3743 if (c2 > c1) {
3744 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003745 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003746 DAG.getConstant(c2-c1, N1.getValueType()));
3747 } else {
3748 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003749 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003750 DAG.getConstant(c1-c2, N1.getValueType()));
3751 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003752 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003753 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003754 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003755 }
Bill Wendling88103372009-01-30 21:37:17 +00003756 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003757 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3758 SDValue HiBitsMask =
3759 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3760 VT.getSizeInBits() -
3761 N1C->getZExtValue()),
3762 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003763 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003764 HiBitsMask);
3765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003766
Evan Chenge5b51ac2010-04-17 06:13:15 +00003767 if (N1C) {
3768 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3769 if (NewSHL.getNode())
3770 return NewSHL;
3771 }
3772
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003773 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003774}
3775
Dan Gohman475871a2008-07-27 21:46:04 +00003776SDValue DAGCombiner::visitSRA(SDNode *N) {
3777 SDValue N0 = N->getOperand(0);
3778 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003779 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3780 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003781 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003782 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003783
Bill Wendling88103372009-01-30 21:37:17 +00003784 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003785 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003786 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003787 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003788 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003789 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003790 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003791 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003792 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003793 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003794 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003795 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003796 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003797 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003798 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003799 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3800 // sext_inreg.
3801 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003802 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003803 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3804 if (VT.isVector())
3805 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3806 ExtVT, VT.getVectorNumElements());
3807 if ((!LegalOperations ||
3808 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003809 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003810 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003811 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003812
Bill Wendling88103372009-01-30 21:37:17 +00003813 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003814 if (N1C && N0.getOpcode() == ISD::SRA) {
3815 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003816 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003817 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003818 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003819 DAG.getConstant(Sum, N1C->getValueType(0)));
3820 }
3821 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003822
Bill Wendling88103372009-01-30 21:37:17 +00003823 // fold (sra (shl X, m), (sub result_size, n))
3824 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003825 // result_size - n != m.
3826 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003827 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003828 if (N0.getOpcode() == ISD::SHL) {
3829 // Get the two constanst of the shifts, CN0 = m, CN = n.
3830 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3831 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003832 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003833 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003834 EVT::getIntegerVT(*DAG.getContext(),
3835 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003836 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003837 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003838
Scott Michelfdc40a02009-02-17 22:15:04 +00003839 // If the shift is not a no-op (in which case this should be just a sign
3840 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003841 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003842 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003843 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003844 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3845 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003846 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003847
Owen Anderson95771af2011-02-25 21:41:48 +00003848 SDValue Amt = DAG.getConstant(ShiftAmt,
3849 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003850 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003851 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003852 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003853 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003854 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003855 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003856 }
3857 }
3858 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003859
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003860 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003861 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003862 N1.getOperand(0).getOpcode() == ISD::AND &&
3863 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003864 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003865 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003866 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003867 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003868 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003869 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003870 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3871 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003872 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003873 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003874 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003875 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003876 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003877 }
3878 }
3879
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003880 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3881 // if c1 is equal to the number of bits the trunc removes
3882 if (N0.getOpcode() == ISD::TRUNCATE &&
3883 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3884 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3885 N0.getOperand(0).hasOneUse() &&
3886 N0.getOperand(0).getOperand(1).hasOneUse() &&
3887 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3888 EVT LargeVT = N0.getOperand(0).getValueType();
3889 ConstantSDNode *LargeShiftAmt =
3890 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3891
3892 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3893 LargeShiftAmt->getZExtValue()) {
3894 SDValue Amt =
3895 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003896 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003897 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003898 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003899 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003900 }
3901 }
3902
Scott Michelfdc40a02009-02-17 22:15:04 +00003903 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003904 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3905 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003906
3907
Nate Begeman1d4d4142005-09-01 00:19:25 +00003908 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003909 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003910 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003911
Evan Chenge5b51ac2010-04-17 06:13:15 +00003912 if (N1C) {
3913 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3914 if (NewSRA.getNode())
3915 return NewSRA;
3916 }
3917
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003918 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003919}
3920
Dan Gohman475871a2008-07-27 21:46:04 +00003921SDValue DAGCombiner::visitSRL(SDNode *N) {
3922 SDValue N0 = N->getOperand(0);
3923 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003926 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003927 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003928
Nate Begeman1d4d4142005-09-01 00:19:25 +00003929 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003930 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003931 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003932 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003933 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003934 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003935 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003936 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003937 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003938 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003939 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003940 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003941 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003942 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003943 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003944 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003945
Bill Wendling88103372009-01-30 21:37:17 +00003946 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003947 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003948 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003949 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3950 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003951 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003952 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003953 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003954 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003955 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003956
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003957 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003958 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3959 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003960 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003961 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003962 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3963 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003964 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3965 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003966 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003967 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003968 if (c1 + OpSizeInBits == InnerShiftSize) {
3969 if (c1 + c2 >= InnerShiftSize)
3970 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003971 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
3972 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003973 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003974 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003975 }
3976 }
3977
Chris Lattnerefcddc32010-04-15 05:28:43 +00003978 // fold (srl (shl x, c), c) -> (and x, cst2)
3979 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3980 N0.getValueSizeInBits() <= 64) {
3981 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003982 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00003983 DAG.getConstant(~0ULL >> ShAmt, VT));
3984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003985
Michael Liao2da86392013-06-21 18:45:27 +00003986 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00003987 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3988 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003989 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003990 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003991 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003992
Evan Chenge5b51ac2010-04-17 06:13:15 +00003993 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003994 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003995 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003996 N0.getOperand(0),
3997 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003998 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00003999 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4000 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4001 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4002 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004003 }
Chris Lattner06afe072006-05-05 22:53:17 +00004004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004005
Chris Lattner3657ffe2006-10-12 20:23:19 +00004006 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4007 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004008 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004009 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004010 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004011 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004012
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004013 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004014 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004015 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004016 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004017 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004018
Chris Lattner350bec02006-04-02 06:11:11 +00004019 // If any of the input bits are KnownOne, then the input couldn't be all
4020 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004021 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004022
Chris Lattner350bec02006-04-02 06:11:11 +00004023 // If all of the bits input the to ctlz node are known to be zero, then
4024 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004025 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004026 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004027
Chris Lattner350bec02006-04-02 06:11:11 +00004028 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004029 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004030 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004031 // could be set on input to the CTLZ node. If this bit is set, the SRL
4032 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4033 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004034 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004035 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004036
Chris Lattner350bec02006-04-02 06:11:11 +00004037 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004038 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004039 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004040 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004041 }
Bill Wendling88103372009-01-30 21:37:17 +00004042
Andrew Trickac6d9be2013-05-25 02:42:55 +00004043 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004044 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004045 }
4046 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004047
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004048 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004049 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004050 N1.getOperand(0).getOpcode() == ISD::AND &&
4051 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004052 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004053 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004054 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004055 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004056 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004057 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004058 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4059 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004060 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004061 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004062 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004063 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004064 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004065 }
4066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004067
Chris Lattner61a4c072007-04-18 03:06:49 +00004068 // fold operands of srl based on knowledge that the low bits are not
4069 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004070 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4071 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004072
Evan Cheng9ab2b982009-12-18 21:31:31 +00004073 if (N1C) {
4074 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4075 if (NewSRL.getNode())
4076 return NewSRL;
4077 }
4078
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004079 // Attempt to convert a srl of a load into a narrower zero-extending load.
4080 SDValue NarrowLoad = ReduceLoadWidth(N);
4081 if (NarrowLoad.getNode())
4082 return NarrowLoad;
4083
Evan Cheng9ab2b982009-12-18 21:31:31 +00004084 // Here is a common situation. We want to optimize:
4085 //
4086 // %a = ...
4087 // %b = and i32 %a, 2
4088 // %c = srl i32 %b, 1
4089 // brcond i32 %c ...
4090 //
4091 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004092 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004093 // %a = ...
4094 // %b = and %a, 2
4095 // %c = setcc eq %b, 0
4096 // brcond %c ...
4097 //
4098 // However when after the source operand of SRL is optimized into AND, the SRL
4099 // itself may not be optimized further. Look for it and add the BRCOND into
4100 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004101 if (N->hasOneUse()) {
4102 SDNode *Use = *N->use_begin();
4103 if (Use->getOpcode() == ISD::BRCOND)
4104 AddToWorkList(Use);
4105 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4106 // Also look pass the truncate.
4107 Use = *Use->use_begin();
4108 if (Use->getOpcode() == ISD::BRCOND)
4109 AddToWorkList(Use);
4110 }
4111 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004112
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004113 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004114}
4115
Dan Gohman475871a2008-07-27 21:46:04 +00004116SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4117 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004118 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004119
4120 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004121 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004122 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004123 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004124}
4125
Chandler Carruth63974b22011-12-13 01:56:10 +00004126SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4127 SDValue N0 = N->getOperand(0);
4128 EVT VT = N->getValueType(0);
4129
4130 // fold (ctlz_zero_undef c1) -> c2
4131 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004132 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004133 return SDValue();
4134}
4135
Dan Gohman475871a2008-07-27 21:46:04 +00004136SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4137 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004138 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004139
Nate Begeman1d4d4142005-09-01 00:19:25 +00004140 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004141 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004142 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004143 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004144}
4145
Chandler Carruth63974b22011-12-13 01:56:10 +00004146SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4147 SDValue N0 = N->getOperand(0);
4148 EVT VT = N->getValueType(0);
4149
4150 // fold (cttz_zero_undef c1) -> c2
4151 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004152 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004153 return SDValue();
4154}
4155
Dan Gohman475871a2008-07-27 21:46:04 +00004156SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4157 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004158 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004159
Nate Begeman1d4d4142005-09-01 00:19:25 +00004160 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004161 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004162 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004163 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004164}
4165
Dan Gohman475871a2008-07-27 21:46:04 +00004166SDValue DAGCombiner::visitSELECT(SDNode *N) {
4167 SDValue N0 = N->getOperand(0);
4168 SDValue N1 = N->getOperand(1);
4169 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004170 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4171 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4172 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004173 EVT VT = N->getValueType(0);
4174 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004175
Bill Wendling34584e62009-01-30 22:02:18 +00004176 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004177 if (N1 == N2)
4178 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004179 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004180 if (N0C && !N0C->isNullValue())
4181 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004182 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004183 if (N0C && N0C->isNullValue())
4184 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004185 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004187 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004188 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004189 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004191 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004192 TLI.getBooleanContents(false) ==
4193 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004194 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004195 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004196 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004197 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004198 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004199 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004200 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004201 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004202 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004203 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4204 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004205 }
Bill Wendling34584e62009-01-30 22:02:18 +00004206 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004207 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004208 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004209 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004210 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004211 }
Bill Wendling34584e62009-01-30 22:02:18 +00004212 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004214 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004215 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004216 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004217 }
Bill Wendling34584e62009-01-30 22:02:18 +00004218 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004220 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004221 // fold (select X, X, Y) -> (or X, Y)
4222 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004224 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004225 // fold (select X, Y, X) -> (and X, Y)
4226 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004228 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004229
Chris Lattner40c62d52005-10-18 06:04:22 +00004230 // If we can fold this based on the true/false value, do so.
4231 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004232 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004233
Nate Begeman44728a72005-09-19 22:34:01 +00004234 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004235 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004236 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004238 // having to say they don't support SELECT_CC on every type the DAG knows
4239 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004241 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004242 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004243 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004244 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004245 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004246 }
Bill Wendling34584e62009-01-30 22:02:18 +00004247
Dan Gohman475871a2008-07-27 21:46:04 +00004248 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004249}
4250
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004251SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4252 SDValue N0 = N->getOperand(0);
4253 SDValue N1 = N->getOperand(1);
4254 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004255 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004256
4257 // Canonicalize integer abs.
4258 // vselect (setg[te] X, 0), X, -X ->
4259 // vselect (setgt X, -1), X, -X ->
4260 // vselect (setl[te] X, 0), -X, X ->
4261 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4262 if (N0.getOpcode() == ISD::SETCC) {
4263 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4264 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4265 bool isAbs = false;
4266 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4267
4268 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4269 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4270 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4271 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4272 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4273 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4274 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4275
4276 if (isAbs) {
4277 EVT VT = LHS.getValueType();
4278 SDValue Shift = DAG.getNode(
4279 ISD::SRA, DL, VT, LHS,
4280 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4281 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4282 AddToWorkList(Shift.getNode());
4283 AddToWorkList(Add.getNode());
4284 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4285 }
4286 }
4287
4288 return SDValue();
4289}
4290
Dan Gohman475871a2008-07-27 21:46:04 +00004291SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4292 SDValue N0 = N->getOperand(0);
4293 SDValue N1 = N->getOperand(1);
4294 SDValue N2 = N->getOperand(2);
4295 SDValue N3 = N->getOperand(3);
4296 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004297 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004298
Nate Begeman44728a72005-09-19 22:34:01 +00004299 // fold select_cc lhs, rhs, x, x, cc -> x
4300 if (N2 == N3)
4301 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004302
Chris Lattner5f42a242006-09-20 06:19:26 +00004303 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004304 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004305 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004306 if (SCC.getNode()) {
4307 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004308
Stephen Lin7e6d6202013-06-15 04:03:33 +00004309 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4310 if (!SCCC->isNullValue())
4311 return N2; // cond always true -> true val
4312 else
4313 return N3; // cond always false -> false val
4314 }
4315
4316 // Fold to a simpler select_cc
4317 if (SCC.getOpcode() == ISD::SETCC)
4318 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4319 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4320 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004322
Chris Lattner40c62d52005-10-18 06:04:22 +00004323 // If we can fold this based on the true/false value, do so.
4324 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004325 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004326
Nate Begeman44728a72005-09-19 22:34:01 +00004327 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004328 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004329}
4330
Dan Gohman475871a2008-07-27 21:46:04 +00004331SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004332 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004333 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004334 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004335}
4336
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004337// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004338// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004339// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004340// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004341static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004342 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004343 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004344 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004345 bool HasCopyToRegUses = false;
4346 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004347 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4348 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004349 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004350 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004351 if (User == N)
4352 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004353 if (UI.getUse().getResNo() != N0.getResNo())
4354 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004355 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004356 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004357 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4358 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4359 // Sign bits will be lost after a zext.
4360 return false;
4361 bool Add = false;
4362 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004363 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004364 if (UseOp == N0)
4365 continue;
4366 if (!isa<ConstantSDNode>(UseOp))
4367 return false;
4368 Add = true;
4369 }
4370 if (Add)
4371 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004372 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004373 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004374 // If truncates aren't free and there are users we can't
4375 // extend, it isn't worthwhile.
4376 if (!isTruncFree)
4377 return false;
4378 // Remember if this value is live-out.
4379 if (User->getOpcode() == ISD::CopyToReg)
4380 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004381 }
4382
4383 if (HasCopyToRegUses) {
4384 bool BothLiveOut = false;
4385 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4386 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004387 SDUse &Use = UI.getUse();
4388 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4389 BothLiveOut = true;
4390 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004391 }
4392 }
4393 if (BothLiveOut)
4394 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004395 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004396 return ExtendNodes.size();
4397 }
4398 return true;
4399}
4400
Craig Topper6c64fba2013-07-13 07:43:40 +00004401void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004402 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004403 ISD::NodeType ExtType) {
4404 // Extend SetCC uses if necessary.
4405 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4406 SDNode *SetCC = SetCCs[i];
4407 SmallVector<SDValue, 4> Ops;
4408
4409 for (unsigned j = 0; j != 2; ++j) {
4410 SDValue SOp = SetCC->getOperand(j);
4411 if (SOp == Trunc)
4412 Ops.push_back(ExtLoad);
4413 else
4414 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4415 }
4416
4417 Ops.push_back(SetCC->getOperand(2));
4418 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4419 &Ops[0], Ops.size()));
4420 }
4421}
4422
Dan Gohman475871a2008-07-27 21:46:04 +00004423SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4424 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004425 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004426
Nate Begeman1d4d4142005-09-01 00:19:25 +00004427 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004428 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004429 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004430
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004431 // fold (sext (sext x)) -> (sext x)
4432 // fold (sext (aext x)) -> (sext x)
4433 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004434 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004435 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004436
Chris Lattner22558872007-02-26 03:13:59 +00004437 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004438 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4439 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4441 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004442 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4443 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004444 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004445 // CombineTo deleted the truncate, if needed, but not what's under it.
4446 AddToWorkList(oye);
4447 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004448 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004449 }
Evan Chengc88138f2007-03-22 01:54:19 +00004450
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004451 // See if the value being truncated is already sign extended. If so, just
4452 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004453 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004454 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4455 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4456 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004457 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004458
Chris Lattner22558872007-02-26 03:13:59 +00004459 if (OpBits == DestBits) {
4460 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4461 // bits, it is already ready.
4462 if (NumSignBits > DestBits-MidBits)
4463 return Op;
4464 } else if (OpBits < DestBits) {
4465 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4466 // bits, just sext from i32.
4467 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004468 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004469 } else {
4470 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4471 // bits, just truncate to i32.
4472 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004473 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004474 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004475
Chris Lattner22558872007-02-26 03:13:59 +00004476 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004477 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4478 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004479 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004480 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004481 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004482 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4483 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004484 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004485 }
Chris Lattner6007b842006-09-21 06:00:20 +00004486 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004487
Evan Cheng110dec22005-12-14 02:19:23 +00004488 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004489 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004490 // on vectors in one instruction. We only perform this transformation on
4491 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004492 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004493 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004494 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004495 bool DoXform = true;
4496 SmallVector<SDNode*, 4> SetCCs;
4497 if (!N0.hasOneUse())
4498 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4499 if (DoXform) {
4500 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004501 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004502 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004503 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004504 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004505 LN0->isVolatile(), LN0->isNonTemporal(),
4506 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004507 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004508 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004509 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004510 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004511 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004512 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004513 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004514 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004515 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004516
4517 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4518 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004519 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4520 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004521 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004522 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004523 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004524 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004525 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004526 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004527 LN0->getBasePtr(), LN0->getPointerInfo(),
4528 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004529 LN0->isVolatile(), LN0->isNonTemporal(),
4530 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004531 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004532 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004533 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004534 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004535 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004536 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004537 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004540 // fold (sext (and/or/xor (load x), cst)) ->
4541 // (and/or/xor (sextload x), (sext cst))
4542 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4543 N0.getOpcode() == ISD::XOR) &&
4544 isa<LoadSDNode>(N0.getOperand(0)) &&
4545 N0.getOperand(1).getOpcode() == ISD::Constant &&
4546 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4547 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4548 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4549 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4550 bool DoXform = true;
4551 SmallVector<SDNode*, 4> SetCCs;
4552 if (!N0.hasOneUse())
4553 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4554 SetCCs, TLI);
4555 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004556 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004557 LN0->getChain(), LN0->getBasePtr(),
4558 LN0->getPointerInfo(),
4559 LN0->getMemoryVT(),
4560 LN0->isVolatile(),
4561 LN0->isNonTemporal(),
4562 LN0->getAlignment());
4563 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4564 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004565 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004566 ExtLoad, DAG.getConstant(Mask, VT));
4567 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004568 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004569 N0.getOperand(0).getValueType(), ExtLoad);
4570 CombineTo(N, And);
4571 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004572 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004573 ISD::SIGN_EXTEND);
4574 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4575 }
4576 }
4577 }
4578
Chris Lattner20a35c32007-04-11 05:32:27 +00004579 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004580 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004581 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004582 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004583 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004584 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004585 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004586 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4587 // of the same size as the compared operands. Only optimize sext(setcc())
4588 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004589 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004590
4591 // We know that the # elements of the results is the same as the
4592 // # elements of the compare (and the # elements of the compare result
4593 // for that matter). Check to see that they are the same size. If so,
4594 // we know that the element size of the sext'd result matches the
4595 // element size of the compare operands.
4596 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004597 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004598 N0.getOperand(1),
4599 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004600
Dan Gohman3ce89f42010-04-30 17:19:19 +00004601 // If the desired elements are smaller or larger than the source
4602 // elements we can use a matching integer vector type and then
4603 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004604 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004605 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004606 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004607 N0.getOperand(0), N0.getOperand(1),
4608 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004609 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004610 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004611 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004612
Chris Lattner2b7a2712009-07-08 00:31:33 +00004613 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004614 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004615 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004616 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004617 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004618 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004619 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004620 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004621 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004622 if (!VT.isVector() &&
4623 (!LegalOperations ||
4624 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4625 return DAG.getSelect(SDLoc(N), VT,
4626 DAG.getSetCC(SDLoc(N),
4627 getSetCCResultType(VT),
4628 N0.getOperand(0), N0.getOperand(1),
4629 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4630 NegOne, DAG.getConstant(0, VT));
4631 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004632 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004633
Dan Gohman8f0ad582008-04-28 16:58:24 +00004634 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004635 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004636 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004637 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004638
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004639 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004640}
4641
Rafael Espindoladecbc432012-04-09 16:06:03 +00004642// isTruncateOf - If N is a truncate of some other value, return true, record
4643// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4644// This function computes KnownZero to avoid a duplicated call to
4645// ComputeMaskedBits in the caller.
4646static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4647 APInt &KnownZero) {
4648 APInt KnownOne;
4649 if (N->getOpcode() == ISD::TRUNCATE) {
4650 Op = N->getOperand(0);
4651 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4652 return true;
4653 }
4654
4655 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4656 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4657 return false;
4658
4659 SDValue Op0 = N->getOperand(0);
4660 SDValue Op1 = N->getOperand(1);
4661 assert(Op0.getValueType() == Op1.getValueType());
4662
4663 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4664 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004665 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004666 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004667 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004668 Op = Op0;
4669 else
4670 return false;
4671
4672 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4673
4674 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4675 return false;
4676
4677 return true;
4678}
4679
Dan Gohman475871a2008-07-27 21:46:04 +00004680SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4681 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004683
Nate Begeman1d4d4142005-09-01 00:19:25 +00004684 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004685 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004686 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004687 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004688 // fold (zext (aext x)) -> (zext x)
4689 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004690 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004691 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004692
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004693 // fold (zext (truncate x)) -> (zext x) or
4694 // (zext (truncate x)) -> (truncate x)
4695 // This is valid when the truncated bits of x are already zero.
4696 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004697 SDValue Op;
4698 APInt KnownZero;
4699 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4700 APInt TruncatedBits =
4701 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4702 APInt(Op.getValueSizeInBits(), 0) :
4703 APInt::getBitsSet(Op.getValueSizeInBits(),
4704 N0.getValueSizeInBits(),
4705 std::min(Op.getValueSizeInBits(),
4706 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004707 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004708 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004709 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004710 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004711 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004712
4713 return Op;
4714 }
4715 }
4716
Evan Chengc88138f2007-03-22 01:54:19 +00004717 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4718 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004719 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004720 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4721 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004722 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4723 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004724 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004725 // CombineTo deleted the truncate, if needed, but not what's under it.
4726 AddToWorkList(oye);
4727 }
Eli Friedmane545d382011-04-16 23:25:34 +00004728 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004729 }
Evan Chengc88138f2007-03-22 01:54:19 +00004730 }
4731
Chris Lattner6007b842006-09-21 06:00:20 +00004732 // fold (zext (truncate x)) -> (and x, mask)
4733 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004734 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004735
4736 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4737 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4738 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4739 if (NarrowLoad.getNode()) {
4740 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4741 if (NarrowLoad.getNode() != N0.getNode()) {
4742 CombineTo(N0.getNode(), NarrowLoad);
4743 // CombineTo deleted the truncate, if needed, but not what's under it.
4744 AddToWorkList(oye);
4745 }
4746 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4747 }
4748
Dan Gohman475871a2008-07-27 21:46:04 +00004749 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004750 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004751 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004752 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004753 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004754 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004755 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004756 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004757 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004758 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004759 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004760
Dan Gohman97121ba2009-04-08 00:15:30 +00004761 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4762 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004763 if (N0.getOpcode() == ISD::AND &&
4764 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004765 N0.getOperand(1).getOpcode() == ISD::Constant &&
4766 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4767 N0.getValueType()) ||
4768 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004769 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004770 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004771 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004772 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004773 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004774 }
Dan Gohman220a8232008-03-03 23:51:38 +00004775 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004776 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004777 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004778 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004780
Evan Cheng110dec22005-12-14 02:19:23 +00004781 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004782 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004783 // on vectors in one instruction. We only perform this transformation on
4784 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004785 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004786 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004787 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004788 bool DoXform = true;
4789 SmallVector<SDNode*, 4> SetCCs;
4790 if (!N0.hasOneUse())
4791 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4792 if (DoXform) {
4793 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004794 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004795 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004796 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004797 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004798 LN0->isVolatile(), LN0->isNonTemporal(),
4799 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004800 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004801 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004802 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004803 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004804
Andrew Trickac6d9be2013-05-25 02:42:55 +00004805 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004806 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004808 }
Evan Cheng110dec22005-12-14 02:19:23 +00004809 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004810
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004811 // fold (zext (and/or/xor (load x), cst)) ->
4812 // (and/or/xor (zextload x), (zext cst))
4813 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4814 N0.getOpcode() == ISD::XOR) &&
4815 isa<LoadSDNode>(N0.getOperand(0)) &&
4816 N0.getOperand(1).getOpcode() == ISD::Constant &&
4817 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4818 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4819 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4820 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4821 bool DoXform = true;
4822 SmallVector<SDNode*, 4> SetCCs;
4823 if (!N0.hasOneUse())
4824 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4825 SetCCs, TLI);
4826 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004827 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004828 LN0->getChain(), LN0->getBasePtr(),
4829 LN0->getPointerInfo(),
4830 LN0->getMemoryVT(),
4831 LN0->isVolatile(),
4832 LN0->isNonTemporal(),
4833 LN0->getAlignment());
4834 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4835 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004836 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004837 ExtLoad, DAG.getConstant(Mask, VT));
4838 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004839 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004840 N0.getOperand(0).getValueType(), ExtLoad);
4841 CombineTo(N, And);
4842 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004843 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004844 ISD::ZERO_EXTEND);
4845 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4846 }
4847 }
4848 }
4849
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004850 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4851 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004852 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4853 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004854 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004855 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004856 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004857 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004858 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004859 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004860 LN0->getBasePtr(), LN0->getPointerInfo(),
4861 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004862 LN0->isVolatile(), LN0->isNonTemporal(),
4863 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004864 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004865 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004866 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004867 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004868 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004870 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004872
Chris Lattner20a35c32007-04-11 05:32:27 +00004873 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004874 if (!LegalOperations && VT.isVector()) {
4875 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4876 // Only do this before legalize for now.
4877 EVT N0VT = N0.getOperand(0).getValueType();
4878 EVT EltVT = VT.getVectorElementType();
4879 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4880 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004881 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004882 // We know that the # elements of the results is the same as the
4883 // # elements of the compare (and the # elements of the compare result
4884 // for that matter). Check to see that they are the same size. If so,
4885 // we know that the element size of the sext'd result matches the
4886 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004887 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4888 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004889 N0.getOperand(1),
4890 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004891 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004892 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004893
4894 // If the desired elements are smaller or larger than the source
4895 // elements we can use a matching integer vector type and then
4896 // truncate/sign extend
4897 EVT MatchingElementType =
4898 EVT::getIntegerVT(*DAG.getContext(),
4899 N0VT.getScalarType().getSizeInBits());
4900 EVT MatchingVectorType =
4901 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4902 N0VT.getVectorNumElements());
4903 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004904 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004905 N0.getOperand(1),
4906 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004907 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4908 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4909 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004910 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004911 }
4912
4913 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004914 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004915 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004916 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004917 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004918 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004920
Evan Cheng9818c042009-12-15 03:00:32 +00004921 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004922 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004923 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004924 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4925 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004926 SDValue ShAmt = N0.getOperand(1);
4927 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004928 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004929 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004930 // If the original shl may be shifting out bits, do not perform this
4931 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004932 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4933 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4934 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004935 return SDValue();
4936 }
Chris Lattnere0751182011-02-13 19:09:16 +00004937
Andrew Trickac6d9be2013-05-25 02:42:55 +00004938 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004939
4940 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004941 if (VT.getSizeInBits() >= 256)
4942 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004943
Chris Lattnere0751182011-02-13 19:09:16 +00004944 return DAG.getNode(N0.getOpcode(), DL, VT,
4945 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4946 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004947 }
4948
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004949 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004950}
4951
Dan Gohman475871a2008-07-27 21:46:04 +00004952SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4953 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004954 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004955
Chris Lattner5ffc0662006-05-05 05:58:59 +00004956 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004957 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004958 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004959 // fold (aext (aext x)) -> (aext x)
4960 // fold (aext (zext x)) -> (zext x)
4961 // fold (aext (sext x)) -> (sext x)
4962 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4963 N0.getOpcode() == ISD::ZERO_EXTEND ||
4964 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004965 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004966
Evan Chengc88138f2007-03-22 01:54:19 +00004967 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4968 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4969 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004970 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4971 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004972 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4973 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004974 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004975 // CombineTo deleted the truncate, if needed, but not what's under it.
4976 AddToWorkList(oye);
4977 }
Eli Friedmane545d382011-04-16 23:25:34 +00004978 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004979 }
Evan Chengc88138f2007-03-22 01:54:19 +00004980 }
4981
Chris Lattner84750582006-09-20 06:29:17 +00004982 // fold (aext (truncate x))
4983 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004984 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004985 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004986 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004987 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004988 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
4989 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004991
Dan Gohman97121ba2009-04-08 00:15:30 +00004992 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4993 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004994 if (N0.getOpcode() == ISD::AND &&
4995 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004996 N0.getOperand(1).getOpcode() == ISD::Constant &&
4997 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4998 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004999 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005000 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005001 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005002 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005003 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005004 }
Dan Gohman220a8232008-03-03 23:51:38 +00005005 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005006 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005007 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005008 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005010
Chris Lattner5ffc0662006-05-05 05:58:59 +00005011 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005012 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005013 // on vectors in one instruction. We only perform this transformation on
5014 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005015 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005016 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005017 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005018 bool DoXform = true;
5019 SmallVector<SDNode*, 4> SetCCs;
5020 if (!N0.hasOneUse())
5021 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5022 if (DoXform) {
5023 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005024 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005025 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005026 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005027 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005028 LN0->isVolatile(), LN0->isNonTemporal(),
5029 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005030 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005031 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005032 N0.getValueType(), ExtLoad);
5033 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005034 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005035 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005036 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5037 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005038 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005039
Chris Lattner5ffc0662006-05-05 05:58:59 +00005040 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5041 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5042 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005043 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005044 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005045 N0.hasOneUse()) {
5046 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005047 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005048 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005049 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005050 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005051 LN0->isVolatile(), LN0->isNonTemporal(),
5052 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005053 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005054 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005055 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005056 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005057 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005058 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005060
Chris Lattner20a35c32007-04-11 05:32:27 +00005061 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005062 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5063 // Only do this before legalize for now.
5064 if (VT.isVector() && !LegalOperations) {
5065 EVT N0VT = N0.getOperand(0).getValueType();
5066 // We know that the # elements of the results is the same as the
5067 // # elements of the compare (and the # elements of the compare result
5068 // for that matter). Check to see that they are the same size. If so,
5069 // we know that the element size of the sext'd result matches the
5070 // element size of the compare operands.
5071 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005072 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005073 N0.getOperand(1),
5074 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005075 // If the desired elements are smaller or larger than the source
5076 // elements we can use a matching integer vector type and then
5077 // truncate/sign extend
5078 else {
Duncan Sands34727662010-07-12 08:16:59 +00005079 EVT MatchingElementType =
5080 EVT::getIntegerVT(*DAG.getContext(),
5081 N0VT.getScalarType().getSizeInBits());
5082 EVT MatchingVectorType =
5083 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5084 N0VT.getVectorNumElements());
5085 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005086 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005087 N0.getOperand(1),
5088 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005089 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005090 }
5091 }
5092
5093 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005094 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005095 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005096 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005097 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005098 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005099 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005100 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005101
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005102 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005103}
5104
Chris Lattner2b4c2792007-10-13 06:35:54 +00005105/// GetDemandedBits - See if the specified operand can be simplified with the
5106/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005107/// simpler operand, otherwise return a null SDValue.
5108SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005109 switch (V.getOpcode()) {
5110 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005111 case ISD::Constant: {
5112 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5113 assert(CV != 0 && "Const value should be ConstSDNode.");
5114 const APInt &CVal = CV->getAPIntValue();
5115 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005116 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005117 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005118 break;
5119 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005120 case ISD::OR:
5121 case ISD::XOR:
5122 // If the LHS or RHS don't contribute bits to the or, drop them.
5123 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5124 return V.getOperand(1);
5125 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5126 return V.getOperand(0);
5127 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005128 case ISD::SRL:
5129 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005130 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005131 break;
5132 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5133 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005134 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005135
Dan Gohmancc91d632009-01-03 19:22:06 +00005136 // Watch out for shift count overflow though.
5137 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005138 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005139 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005140 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005141 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005142 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005143 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005144 }
Dan Gohman475871a2008-07-27 21:46:04 +00005145 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005146}
5147
Evan Chengc88138f2007-03-22 01:54:19 +00005148/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5149/// bits and then truncated to a narrower type and where N is a multiple
5150/// of number of bits of the narrower type, transform it to a narrower load
5151/// from address + N / num of bits of new type. If the result is to be
5152/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005153SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005154 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005155
Evan Chengc88138f2007-03-22 01:54:19 +00005156 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005157 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005158 EVT VT = N->getValueType(0);
5159 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005160
Dan Gohman7f8613e2008-08-14 20:04:46 +00005161 // This transformation isn't valid for vector loads.
5162 if (VT.isVector())
5163 return SDValue();
5164
Dan Gohmand1996362010-01-09 02:13:55 +00005165 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005166 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005167 if (Opc == ISD::SIGN_EXTEND_INREG) {
5168 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005169 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005170 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005171 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005172 ExtType = ISD::ZEXTLOAD;
5173 N0 = SDValue(N, 0);
5174 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5175 if (!N01) return SDValue();
5176 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5177 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005178 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005179 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5180 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005181
Owen Andersone50ed302009-08-10 22:56:29 +00005182 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005183
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005184 // Do not generate loads of non-round integer types since these can
5185 // be expensive (and would be wrong if the type is not byte sized).
5186 if (!ExtVT.isRound())
5187 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005188
Evan Chengc88138f2007-03-22 01:54:19 +00005189 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005190 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005191 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005192 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005193 // Is the shift amount a multiple of size of VT?
5194 if ((ShAmt & (EVTBits-1)) == 0) {
5195 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005196 // Is the load width a multiple of size of VT?
5197 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005198 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005199 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005200
Chris Lattnercbf68df2010-12-22 08:02:57 +00005201 // At this point, we must have a load or else we can't do the transform.
5202 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005203
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005204 // Because a SRL must be assumed to *need* to zero-extend the high bits
5205 // (as opposed to anyext the high bits), we can't combine the zextload
5206 // lowering of SRL and an sextload.
5207 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5208 return SDValue();
5209
Chris Lattner2831a192010-10-01 05:36:09 +00005210 // If the shift amount is larger than the input type then we're not
5211 // accessing any of the loaded bytes. If the load was a zextload/extload
5212 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005213 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005214 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005215 }
5216 }
5217
Dan Gohman394d6292010-11-03 01:47:46 +00005218 // If the load is shifted left (and the result isn't shifted back right),
5219 // we can fold the truncate through the shift.
5220 unsigned ShLeftAmt = 0;
5221 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005222 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005223 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5224 ShLeftAmt = N01->getZExtValue();
5225 N0 = N0.getOperand(0);
5226 }
5227 }
Owen Anderson95771af2011-02-25 21:41:48 +00005228
Chris Lattner4c32bc22010-12-22 07:36:50 +00005229 // If we haven't found a load, we can't narrow it. Don't transform one with
5230 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005231 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5232 return SDValue();
5233
5234 // Don't change the width of a volatile load.
5235 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5236 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005237 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005238
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005239 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005240 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005241 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005242
Bill Schmidt89e88e32013-01-14 22:04:38 +00005243 // For the transform to be legal, the load must produce only two values
5244 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005245 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005246 // transformation is not equivalent, and the downstream logic to replace
5247 // uses gets things wrong.
5248 if (LN0->getNumValues() > 2)
5249 return SDValue();
5250
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005251 // If the load that we're shrinking is an extload and we're not just
5252 // discarding the extension we can't simply shrink the load. Bail.
5253 // TODO: It would be possible to merge the extensions in some cases.
5254 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5255 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5256 return SDValue();
5257
Chris Lattner4c32bc22010-12-22 07:36:50 +00005258 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005259
Evan Cheng16436df2012-06-26 01:19:33 +00005260 if (PtrType == MVT::Untyped || PtrType.isExtended())
5261 // It's not possible to generate a constant of extended or untyped type.
5262 return SDValue();
5263
Chris Lattner4c32bc22010-12-22 07:36:50 +00005264 // For big endian targets, we need to adjust the offset to the pointer to
5265 // load the correct bytes.
5266 if (TLI.isBigEndian()) {
5267 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5268 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5269 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005270 }
5271
Chris Lattner4c32bc22010-12-22 07:36:50 +00005272 uint64_t PtrOff = ShAmt / 8;
5273 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005274 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005275 PtrType, LN0->getBasePtr(),
5276 DAG.getConstant(PtrOff, PtrType));
5277 AddToWorkList(NewPtr.getNode());
5278
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005279 SDValue Load;
5280 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005281 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005282 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005283 LN0->isVolatile(), LN0->isNonTemporal(),
5284 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005285 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005286 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005287 LN0->getPointerInfo().getWithOffset(PtrOff),
5288 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5289 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005290
5291 // Replace the old load's chain with the new load's chain.
5292 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005293 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005294
5295 // Shift the result left, if we've swallowed a left shift.
5296 SDValue Result = Load;
5297 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005298 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005299 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5300 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005301 // If the shift amount is as large as the result size (but, presumably,
5302 // no larger than the source) then the useful bits of the result are
5303 // zero; we can't simply return the shortened shift, because the result
5304 // of that operation is undefined.
5305 if (ShLeftAmt >= VT.getSizeInBits())
5306 Result = DAG.getConstant(0, VT);
5307 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005308 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005309 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005310 }
5311
5312 // Return the new loaded value.
5313 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005314}
5315
Dan Gohman475871a2008-07-27 21:46:04 +00005316SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5317 SDValue N0 = N->getOperand(0);
5318 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005319 EVT VT = N->getValueType(0);
5320 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005321 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005322 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005323
Nate Begeman1d4d4142005-09-01 00:19:25 +00005324 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005325 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005326 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005327
Chris Lattner541a24f2006-05-06 22:43:44 +00005328 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005329 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005330 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005331
Nate Begeman646d7e22005-09-02 21:18:40 +00005332 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5333 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005334 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005335 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005336 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005337
Dan Gohman75dcf082008-07-31 00:50:31 +00005338 // fold (sext_in_reg (sext x)) -> (sext x)
5339 // fold (sext_in_reg (aext x)) -> (sext x)
5340 // if x is small enough.
5341 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5342 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005343 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5344 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005345 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005346 }
5347
Chris Lattner95a5e052007-04-17 19:03:21 +00005348 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005349 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005350 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005351
Chris Lattner95a5e052007-04-17 19:03:21 +00005352 // fold operands of sext_in_reg based on knowledge that the top bits are not
5353 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005354 if (SimplifyDemandedBits(SDValue(N, 0)))
5355 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005356
Evan Chengc88138f2007-03-22 01:54:19 +00005357 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5358 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005360 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005361 return NarrowLoad;
5362
Bill Wendling8509c902009-01-30 22:33:24 +00005363 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005364 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005365 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5366 if (N0.getOpcode() == ISD::SRL) {
5367 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005368 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005369 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005370 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005371 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005372 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005373 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005374 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005375 }
5376 }
Evan Chengc88138f2007-03-22 01:54:19 +00005377
Nate Begemanded49632005-10-13 03:11:28 +00005378 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005379 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005380 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005381 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005382 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005383 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005384 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005385 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005386 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005387 LN0->getBasePtr(), LN0->getPointerInfo(),
5388 EVT,
David Greene1e559442010-02-15 17:00:31 +00005389 LN0->isVolatile(), LN0->isNonTemporal(),
5390 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005391 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005392 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005393 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005394 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005395 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005396 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005397 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005398 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005399 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005400 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005401 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005402 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005403 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005404 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005405 LN0->getBasePtr(), LN0->getPointerInfo(),
5406 EVT,
David Greene1e559442010-02-15 17:00:31 +00005407 LN0->isVolatile(), LN0->isNonTemporal(),
5408 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005409 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005410 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005411 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005412 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005413
5414 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5415 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5416 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5417 N0.getOperand(1), false);
5418 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005419 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005420 BSwap, N1);
5421 }
5422
Dan Gohman475871a2008-07-27 21:46:04 +00005423 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005424}
5425
Dan Gohman475871a2008-07-27 21:46:04 +00005426SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5427 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005428 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005429 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005430
5431 // noop truncate
5432 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005433 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005434 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005435 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005436 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005437 // fold (truncate (truncate x)) -> (truncate x)
5438 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005439 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005440 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005441 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5442 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005443 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005444 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005445 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005446 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005447 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005448 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005449 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005450 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005451 // if the source and dest are the same type, we can drop both the extend
5452 // and the truncate.
5453 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005454 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005455
Nadav Rotemcc870a82012-02-05 11:39:23 +00005456 // Fold extract-and-trunc into a narrow extract. For example:
5457 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5458 // i32 y = TRUNCATE(i64 x)
5459 // -- becomes --
5460 // v16i8 b = BITCAST (v2i64 val)
5461 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5462 //
5463 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005464 // creates this pattern) and before operation legalization after which
5465 // we need to be more careful about the vector instructions that we generate.
5466 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5467 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5468
5469 EVT VecTy = N0.getOperand(0).getValueType();
5470 EVT ExTy = N0.getValueType();
5471 EVT TrTy = N->getValueType(0);
5472
5473 unsigned NumElem = VecTy.getVectorNumElements();
5474 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5475
5476 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5477 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5478
5479 SDValue EltNo = N0->getOperand(1);
5480 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5481 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005482 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005483 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5484
Andrew Trickac6d9be2013-05-25 02:42:55 +00005485 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005486 NVT, N0.getOperand(0));
5487
5488 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005489 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005490 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005491 }
5492 }
5493
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005494 // Fold a series of buildvector, bitcast, and truncate if possible.
5495 // For example fold
5496 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5497 // (2xi32 (buildvector x, y)).
5498 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5499 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5500 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5501 N0.getOperand(0).hasOneUse()) {
5502
5503 SDValue BuildVect = N0.getOperand(0);
5504 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5505 EVT TruncVecEltTy = VT.getVectorElementType();
5506
5507 // Check that the element types match.
5508 if (BuildVectEltTy == TruncVecEltTy) {
5509 // Now we only need to compute the offset of the truncated elements.
5510 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5511 unsigned TruncVecNumElts = VT.getVectorNumElements();
5512 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5513
5514 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5515 "Invalid number of elements");
5516
5517 SmallVector<SDValue, 8> Opnds;
5518 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5519 Opnds.push_back(BuildVect.getOperand(i));
5520
Andrew Trickac6d9be2013-05-25 02:42:55 +00005521 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005522 Opnds.size());
5523 }
5524 }
5525
Chris Lattner2b4c2792007-10-13 06:35:54 +00005526 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005527 // only the low bits are being used.
5528 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005529 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005530 // may have different active low bits.
5531 if (!VT.isVector()) {
5532 SDValue Shorter =
5533 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5534 VT.getSizeInBits()));
5535 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005536 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005537 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005538 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005539 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005540 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5541 SDValue Reduced = ReduceLoadWidth(N);
5542 if (Reduced.getNode())
5543 return Reduced;
5544 }
Michael Liao07edaf32012-10-17 23:45:54 +00005545 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5546 // where ... are all 'undef'.
5547 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5548 SmallVector<EVT, 8> VTs;
5549 SDValue V;
5550 unsigned Idx = 0;
5551 unsigned NumDefs = 0;
5552
5553 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5554 SDValue X = N0.getOperand(i);
5555 if (X.getOpcode() != ISD::UNDEF) {
5556 V = X;
5557 Idx = i;
5558 NumDefs++;
5559 }
5560 // Stop if more than one members are non-undef.
5561 if (NumDefs > 1)
5562 break;
5563 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5564 VT.getVectorElementType(),
5565 X.getValueType().getVectorNumElements()));
5566 }
5567
5568 if (NumDefs == 0)
5569 return DAG.getUNDEF(VT);
5570
5571 if (NumDefs == 1) {
5572 assert(V.getNode() && "The single defined operand is empty!");
5573 SmallVector<SDValue, 8> Opnds;
5574 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5575 if (i != Idx) {
5576 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5577 continue;
5578 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005579 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005580 AddToWorkList(NV.getNode());
5581 Opnds.push_back(NV);
5582 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005583 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005584 &Opnds[0], Opnds.size());
5585 }
5586 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005587
5588 // Simplify the operands using demanded-bits information.
5589 if (!VT.isVector() &&
5590 SimplifyDemandedBits(SDValue(N, 0)))
5591 return SDValue(N, 0);
5592
Evan Chenge5b51ac2010-04-17 06:13:15 +00005593 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005594}
5595
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005596static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005598 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005599 return Elt.getNode();
5600 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005601}
5602
5603/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005604/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005605SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005606 assert(N->getOpcode() == ISD::BUILD_PAIR);
5607
Nate Begemanabc01992009-06-05 21:37:30 +00005608 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5609 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005610 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5611 LD1->getPointerInfo().getAddrSpace() !=
5612 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005613 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005614 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005615
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005616 if (ISD::isNON_EXTLoad(LD2) &&
5617 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005618 // If both are volatile this would reduce the number of volatile loads.
5619 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005620 !LD1->isVolatile() &&
5621 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005622 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005623 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005624 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005625 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005626
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005627 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005628 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005629 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005630 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005631 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005632 }
Bill Wendling67a67682009-01-30 22:44:24 +00005633
Dan Gohman475871a2008-07-27 21:46:04 +00005634 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005635}
5636
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005637SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005638 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005639 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005640
Dan Gohman7f321562007-06-25 16:23:39 +00005641 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5642 // Only do this before legalize, since afterward the target may be depending
5643 // on the bitconvert.
5644 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005645 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005646 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005647 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005648 bool isSimple = true;
5649 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5650 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5651 N0.getOperand(i).getOpcode() != ISD::Constant &&
5652 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005653 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005654 break;
5655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005656
Owen Andersone50ed302009-08-10 22:56:29 +00005657 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005658 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005659 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005660 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005661 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005663
Dan Gohman3dd168d2008-09-05 01:58:21 +00005664 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005665 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005666 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005667 if (Res.getNode() != N) {
5668 if (!LegalOperations ||
5669 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5670 return Res;
5671
5672 // Folding it resulted in an illegal node, and it's too late to
5673 // do that. Clean up the old node and forego the transformation.
5674 // Ideally this won't happen very often, because instcombine
5675 // and the earlier dagcombine runs (where illegal nodes are
5676 // permitted) should have folded most of them already.
5677 DAG.DeleteNode(Res.getNode());
5678 }
Chris Lattner94683772005-12-23 05:30:37 +00005679 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005680
Bill Wendling67a67682009-01-30 22:44:24 +00005681 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005682 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005683 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005684 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005685
Chris Lattner57104102005-12-23 05:44:41 +00005686 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005687 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005688 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005689 // Do not change the width of a volatile load.
5690 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005691 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005692 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005693 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005694 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005695 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005696
Evan Cheng59d5b682007-05-07 21:27:48 +00005697 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005698 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005699 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005700 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005701 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005702 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005703 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005704 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005705 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005706 Load.getValue(1));
5707 return Load;
5708 }
Chris Lattner57104102005-12-23 05:44:41 +00005709 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005710
Bill Wendling67a67682009-01-30 22:44:24 +00005711 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5712 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005713 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005714 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5715 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005716 N0.getNode()->hasOneUse() && VT.isInteger() &&
5717 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005718 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005719 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005720 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005721
Duncan Sands83ec4b62008-06-06 12:08:01 +00005722 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005723 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005724 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005725 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005726 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005727 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005728 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005729 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005730
Bill Wendling67a67682009-01-30 22:44:24 +00005731 // fold (bitconvert (fcopysign cst, x)) ->
5732 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5733 // Note that we don't handle (copysign x, cst) because this can always be
5734 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005735 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005736 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005737 VT.isInteger() && !VT.isVector()) {
5738 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005739 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005740 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005741 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005742 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005743 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005744
Duncan Sands25cf2272008-11-24 14:53:14 +00005745 // If X has a different width than the result/lhs, sext it or truncate it.
5746 unsigned VTWidth = VT.getSizeInBits();
5747 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005748 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005749 AddToWorkList(X.getNode());
5750 } else if (OrigXWidth > VTWidth) {
5751 // To get the sign bit in the right place, we have to shift it right
5752 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005753 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005754 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005755 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5756 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005757 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005758 AddToWorkList(X.getNode());
5759 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005760
Duncan Sands25cf2272008-11-24 14:53:14 +00005761 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005762 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005763 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005764 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005765
Andrew Trickac6d9be2013-05-25 02:42:55 +00005766 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005767 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005768 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005769 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005770 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005771
Andrew Trickac6d9be2013-05-25 02:42:55 +00005772 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005773 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005774 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005775
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005776 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005777 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005778 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5779 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005780 return CombineLD;
5781 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005782
Dan Gohman475871a2008-07-27 21:46:04 +00005783 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005784}
5785
Dan Gohman475871a2008-07-27 21:46:04 +00005786SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005787 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005788 return CombineConsecutiveLoads(N, VT);
5789}
5790
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005792/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005793/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005794SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005795ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005796 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005797
Chris Lattner6258fb22006-04-02 02:53:43 +00005798 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005799 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005800
Duncan Sands83ec4b62008-06-06 12:08:01 +00005801 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5802 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005803
Chris Lattner6258fb22006-04-02 02:53:43 +00005804 // If this is a conversion of N elements of one type to N elements of another
5805 // type, convert each element. This handles FP<->INT cases.
5806 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005807 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5808 BV->getValueType(0).getVectorNumElements());
5809
5810 // Due to the FP element handling below calling this routine recursively,
5811 // we can end up with a scalar-to-vector node here.
5812 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005813 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5814 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005815 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005816
Dan Gohman475871a2008-07-27 21:46:04 +00005817 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005818 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005819 SDValue Op = BV->getOperand(i);
5820 // If the vector element type is not legal, the BUILD_VECTOR operands
5821 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005822 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005823 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5824 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005825 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005826 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005827 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005828 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005829 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005830 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005831
Chris Lattner6258fb22006-04-02 02:53:43 +00005832 // Otherwise, we're growing or shrinking the elements. To avoid having to
5833 // handle annoying details of growing/shrinking FP values, we convert them to
5834 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005835 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005836 // Convert the input float vector to a int vector where the elements are the
5837 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005838 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005839 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005840 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005841 SrcEltVT = IntVT;
5842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005843
Chris Lattner6258fb22006-04-02 02:53:43 +00005844 // Now we know the input is an integer vector. If the output is a FP type,
5845 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005846 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005848 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005849 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005850
Chris Lattner6258fb22006-04-02 02:53:43 +00005851 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005852 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005853 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005854
Chris Lattner6258fb22006-04-02 02:53:43 +00005855 // Okay, we know the src/dst types are both integers of differing types.
5856 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005857 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005858 if (SrcBitSize < DstBitSize) {
5859 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005860
Dan Gohman475871a2008-07-27 21:46:04 +00005861 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005862 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005863 i += NumInputsPerOutput) {
5864 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005865 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005866 bool EltIsUndef = true;
5867 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5868 // Shift the previously computed bits over.
5869 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005870 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005871 if (Op.getOpcode() == ISD::UNDEF) continue;
5872 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005873
Jay Foad40f8f622010-12-07 08:25:19 +00005874 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005875 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005877
Chris Lattner6258fb22006-04-02 02:53:43 +00005878 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005879 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005880 else
5881 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5882 }
5883
Owen Anderson23b9b192009-08-12 00:36:31 +00005884 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005885 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005886 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005887 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005888
Chris Lattner6258fb22006-04-02 02:53:43 +00005889 // Finally, this must be the case where we are shrinking elements: each input
5890 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005891 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005892 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005893 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5894 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005895 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005896
Dan Gohman7f321562007-06-25 16:23:39 +00005897 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005898 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5899 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005900 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005901 continue;
5902 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005903
Jay Foad40f8f622010-12-07 08:25:19 +00005904 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5905 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005906
Chris Lattner6258fb22006-04-02 02:53:43 +00005907 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005908 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005909 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005910 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005911 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005912 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005913 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005914 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005915 }
5916
5917 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005918 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005919 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5920 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005921
Andrew Trickac6d9be2013-05-25 02:42:55 +00005922 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005923 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005924}
5925
Dan Gohman475871a2008-07-27 21:46:04 +00005926SDValue DAGCombiner::visitFADD(SDNode *N) {
5927 SDValue N0 = N->getOperand(0);
5928 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005929 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5930 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005931 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005932
Dan Gohman7f321562007-06-25 16:23:39 +00005933 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005934 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005935 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005936 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005938
Lang Hames01806942012-06-14 20:37:15 +00005939 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005940 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005941 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005942 // canonicalize constant to RHS
5943 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005944 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005945 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005946 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5947 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005948 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005949 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005950 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005951 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005952 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005953 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005954 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005955 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005956 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005957 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005958 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005959
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005960 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005961 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5962 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5963 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005964 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
5965 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005966 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005967
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005968 // No FP constant should be created after legalization as Instruction
5969 // Selection pass has hard time in dealing with FP constant.
5970 //
5971 // We don't need test this condition for transformation like following, as
5972 // the DAG being transformed implies it is legal to take FP constant as
5973 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00005974 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005975 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00005976 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005977 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5978
Owen Anderson607ebde2012-11-01 02:00:53 +00005979 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005980 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005981 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00005982 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005983
5984 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005985 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005986 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00005987 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005988
Owen Anderson43da6c72012-08-30 23:35:16 +00005989 // In unsafe math mode, we can fold chains of FADD's of the same value
5990 // into multiplications. This transform is not safe in general because
5991 // we are reducing the number of rounding steps.
5992 if (DAG.getTarget().Options.UnsafeFPMath &&
5993 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5994 !N0CFP && !N1CFP) {
5995 if (N0.getOpcode() == ISD::FMUL) {
5996 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5997 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5998
Stephen Lin38103d12013-06-14 18:17:35 +00005999 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006000 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006001 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006002 SDValue(CFP00, 0),
6003 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006004 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006005 N1, NewCFP);
6006 }
6007
Stephen Lin38103d12013-06-14 18:17:35 +00006008 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006009 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006010 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006011 SDValue(CFP01, 0),
6012 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006013 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006014 N1, NewCFP);
6015 }
6016
Stephen Lin38103d12013-06-14 18:17:35 +00006017 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006018 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6019 N1.getOperand(0) == N1.getOperand(1) &&
6020 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006021 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006022 SDValue(CFP00, 0),
6023 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006024 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006025 N0.getOperand(1), NewCFP);
6026 }
6027
Stephen Lin38103d12013-06-14 18:17:35 +00006028 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006029 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6030 N1.getOperand(0) == N1.getOperand(1) &&
6031 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006032 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006033 SDValue(CFP01, 0),
6034 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006035 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006036 N0.getOperand(0), NewCFP);
6037 }
6038 }
6039
6040 if (N1.getOpcode() == ISD::FMUL) {
6041 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6042 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6043
Stephen Lin38103d12013-06-14 18:17:35 +00006044 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006045 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006046 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006047 SDValue(CFP10, 0),
6048 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006049 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006050 N0, NewCFP);
6051 }
6052
Stephen Lin38103d12013-06-14 18:17:35 +00006053 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006054 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006055 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006056 SDValue(CFP11, 0),
6057 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006058 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006059 N0, NewCFP);
6060 }
6061
Owen Anderson43da6c72012-08-30 23:35:16 +00006062
Stephen Lin38103d12013-06-14 18:17:35 +00006063 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6064 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6065 N0.getOperand(0) == N0.getOperand(1) &&
6066 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006067 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006068 SDValue(CFP10, 0),
6069 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006070 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006071 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006072 }
6073
Stephen Lin38103d12013-06-14 18:17:35 +00006074 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6075 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6076 N0.getOperand(0) == N0.getOperand(1) &&
6077 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006078 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006079 SDValue(CFP11, 0),
6080 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006081 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006082 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006083 }
6084 }
6085
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006086 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006087 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006088 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006089 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006090 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006091 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006092 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006093 }
6094
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006095 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006096 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006097 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006098 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006099 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006100 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006101 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006102 }
6103
Stephen Lina553bed2013-06-14 21:33:58 +00006104 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006105 if (AllowNewFpConst &&
6106 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006107 N0.getOperand(0) == N0.getOperand(1) &&
6108 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006109 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006110 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006111 N0.getOperand(0),
6112 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006113 }
6114
Lang Hamesd693caf2012-06-19 22:51:23 +00006115 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006116 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006117 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006118 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6119 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006120
6121 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006122 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006123 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006124 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006125
Michael Liaob79bff52012-09-01 04:09:16 +00006126 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006127 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006128 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006129 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006130 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006131 }
6132
Dan Gohman475871a2008-07-27 21:46:04 +00006133 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006134}
6135
Dan Gohman475871a2008-07-27 21:46:04 +00006136SDValue DAGCombiner::visitFSUB(SDNode *N) {
6137 SDValue N0 = N->getOperand(0);
6138 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6140 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006142 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006143
Dan Gohman7f321562007-06-25 16:23:39 +00006144 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006145 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006146 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006147 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006149
Nate Begemana0e221d2005-10-18 00:28:13 +00006150 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006151 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006152 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006153 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006154 if (DAG.getTarget().Options.UnsafeFPMath &&
6155 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006156 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006157 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006158 if (DAG.getTarget().Options.UnsafeFPMath &&
6159 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006160 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006161 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006162 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006163 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006164 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006165 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006166 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006167 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006168 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006169
Bill Wendling5a894342012-03-15 05:12:00 +00006170 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006171 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006172 // (fsub x, (fadd x, y)) -> (fneg y) &
6173 // (fsub x, (fadd y, x)) -> (fneg y)
6174 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006175 if (N0 == N1)
6176 return DAG.getConstantFP(0.0f, VT);
6177
Bill Wendling5a894342012-03-15 05:12:00 +00006178 if (N1.getOpcode() == ISD::FADD) {
6179 SDValue N10 = N1->getOperand(0);
6180 SDValue N11 = N1->getOperand(1);
6181
6182 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6183 &DAG.getTarget().Options))
6184 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006185
Stephen Linb4940152013-07-09 00:44:49 +00006186 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6187 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006188 return GetNegatedExpression(N10, DAG, LegalOperations);
6189 }
6190 }
6191
Lang Hamesd693caf2012-06-19 22:51:23 +00006192 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006193 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006194 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006195 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6196 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006197
6198 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006199 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006200 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006201 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006202 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006203
6204 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6205 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006206 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006207 return DAG.getNode(ISD::FMA, dl, VT,
6208 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006209 N1.getOperand(0)),
6210 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006211
Stephen Linb4940152013-07-09 00:44:49 +00006212 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006213 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006214 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6215 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6216 SDValue N00 = N0.getOperand(0).getOperand(0);
6217 SDValue N01 = N0.getOperand(0).getOperand(1);
6218 return DAG.getNode(ISD::FMA, dl, VT,
6219 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6220 DAG.getNode(ISD::FNEG, dl, VT, N1));
6221 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006222 }
6223
Dan Gohman475871a2008-07-27 21:46:04 +00006224 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006225}
6226
Dan Gohman475871a2008-07-27 21:46:04 +00006227SDValue DAGCombiner::visitFMUL(SDNode *N) {
6228 SDValue N0 = N->getOperand(0);
6229 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006230 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6231 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006232 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006233 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006234
Dan Gohman7f321562007-06-25 16:23:39 +00006235 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006236 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006237 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006238 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006240
Nate Begeman11af4ea2005-10-17 20:40:11 +00006241 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006242 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006243 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006244 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006245 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006246 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006247 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006248 if (DAG.getTarget().Options.UnsafeFPMath &&
6249 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006250 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006251 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006252 if (DAG.getTarget().Options.UnsafeFPMath &&
6253 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006254 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006255 // fold (fmul A, 1.0) -> A
6256 if (N1CFP && N1CFP->isExactlyValue(1.0))
6257 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006258 // fold (fmul X, 2.0) -> (fadd X, X)
6259 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006260 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006261 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006262 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006263 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006264 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006265
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006266 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006267 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006268 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006269 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006270 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006271 // Both can be negated for free, check to see if at least one is cheaper
6272 // negated.
6273 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006274 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006275 GetNegatedExpression(N0, DAG, LegalOperations),
6276 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006277 }
6278 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006279
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006280 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006281 if (DAG.getTarget().Options.UnsafeFPMath &&
6282 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006283 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006284 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6285 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006286 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006287
Dan Gohman475871a2008-07-27 21:46:04 +00006288 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006289}
6290
Owen Anderson062c0a52012-05-02 22:17:40 +00006291SDValue DAGCombiner::visitFMA(SDNode *N) {
6292 SDValue N0 = N->getOperand(0);
6293 SDValue N1 = N->getOperand(1);
6294 SDValue N2 = N->getOperand(2);
6295 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6296 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6297 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006298 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006299
Owen Anderson607ebde2012-11-01 02:00:53 +00006300 if (DAG.getTarget().Options.UnsafeFPMath) {
6301 if (N0CFP && N0CFP->isZero())
6302 return N2;
6303 if (N1CFP && N1CFP->isZero())
6304 return N2;
6305 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006306 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006307 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006308 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006309 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006310
Owen Anderson85ef6f42012-05-30 18:50:39 +00006311 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006312 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006313 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006314
Owen Anderson58d57292012-09-01 06:04:27 +00006315 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6316 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6317 N2.getOpcode() == ISD::FMUL &&
6318 N0 == N2.getOperand(0) &&
6319 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6320 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6321 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6322 }
6323
6324
6325 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6326 if (DAG.getTarget().Options.UnsafeFPMath &&
6327 N0.getOpcode() == ISD::FMUL && N1CFP &&
6328 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6329 return DAG.getNode(ISD::FMA, dl, VT,
6330 N0.getOperand(0),
6331 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6332 N2);
6333 }
6334
6335 // (fma x, 1, y) -> (fadd x, y)
6336 // (fma x, -1, y) -> (fadd (fneg x), y)
6337 if (N1CFP) {
6338 if (N1CFP->isExactlyValue(1.0))
6339 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6340
6341 if (N1CFP->isExactlyValue(-1.0) &&
6342 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6343 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6344 AddToWorkList(RHSNeg.getNode());
6345 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6346 }
6347 }
6348
6349 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006350 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6351 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006352 DAG.getNode(ISD::FADD, dl, VT,
6353 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006354
6355 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6356 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006357 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6358 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006359 DAG.getNode(ISD::FADD, dl, VT,
6360 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006361
6362
Owen Anderson062c0a52012-05-02 22:17:40 +00006363 return SDValue();
6364}
6365
Dan Gohman475871a2008-07-27 21:46:04 +00006366SDValue DAGCombiner::visitFDIV(SDNode *N) {
6367 SDValue N0 = N->getOperand(0);
6368 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006369 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6370 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006371 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006372 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006373
Dan Gohman7f321562007-06-25 16:23:39 +00006374 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006375 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006376 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006377 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006378 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006379
Nate Begemana148d982006-01-18 22:35:16 +00006380 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006381 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006382 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006383
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006384 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006385 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006386 // Compute the reciprocal 1.0 / c2.
6387 APFloat N1APF = N1CFP->getValueAPF();
6388 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6389 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006390 // Only do the transform if the reciprocal is a legal fp immediate that
6391 // isn't too nasty (eg NaN, denormal, ...).
6392 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006393 (!LegalOperations ||
6394 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6395 // backend)... we should handle this gracefully after Legalize.
6396 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6397 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6398 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006399 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006400 DAG.getConstantFP(Recip, VT));
6401 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006402
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006403 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006404 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006405 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006406 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006407 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006408 // Both can be negated for free, check to see if at least one is cheaper
6409 // negated.
6410 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006411 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006412 GetNegatedExpression(N0, DAG, LegalOperations),
6413 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006414 }
6415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006416
Dan Gohman475871a2008-07-27 21:46:04 +00006417 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006418}
6419
Dan Gohman475871a2008-07-27 21:46:04 +00006420SDValue DAGCombiner::visitFREM(SDNode *N) {
6421 SDValue N0 = N->getOperand(0);
6422 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006423 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6424 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006425 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006426
Nate Begemana148d982006-01-18 22:35:16 +00006427 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006428 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006429 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006430
Dan Gohman475871a2008-07-27 21:46:04 +00006431 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006432}
6433
Dan Gohman475871a2008-07-27 21:46:04 +00006434SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6435 SDValue N0 = N->getOperand(0);
6436 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006437 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6438 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006439 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006440
Ulrich Weigande669c932012-10-29 18:35:49 +00006441 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006442 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006443
Chris Lattner12d83032006-03-05 05:30:57 +00006444 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006445 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006446 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6447 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006448 if (!V.isNegative()) {
6449 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006450 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006451 } else {
6452 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006453 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6454 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006455 }
Chris Lattner12d83032006-03-05 05:30:57 +00006456 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006457
Chris Lattner12d83032006-03-05 05:30:57 +00006458 // copysign(fabs(x), y) -> copysign(x, y)
6459 // copysign(fneg(x), y) -> copysign(x, y)
6460 // copysign(copysign(x,z), y) -> copysign(x, y)
6461 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6462 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006463 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006464 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006465
6466 // copysign(x, abs(y)) -> abs(x)
6467 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006468 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006469
Chris Lattner12d83032006-03-05 05:30:57 +00006470 // copysign(x, copysign(y,z)) -> copysign(x, z)
6471 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006472 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006473 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006474
Chris Lattner12d83032006-03-05 05:30:57 +00006475 // copysign(x, fp_extend(y)) -> copysign(x, y)
6476 // copysign(x, fp_round(y)) -> copysign(x, y)
6477 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006478 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006479 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006480
Dan Gohman475871a2008-07-27 21:46:04 +00006481 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006482}
6483
Dan Gohman475871a2008-07-27 21:46:04 +00006484SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6485 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006486 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006487 EVT VT = N->getValueType(0);
6488 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006489
Nate Begeman1d4d4142005-09-01 00:19:25 +00006490 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006491 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006492 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006493 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006494 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006495 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006496
Chris Lattnercda88752008-06-26 00:16:49 +00006497 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6498 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006499 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6500 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006501 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006502 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006503 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006504 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006505
Nadav Rotemed1a3352012-07-23 07:59:50 +00006506 // The next optimizations are desireable only if SELECT_CC can be lowered.
6507 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6508 // having to say they don't support SELECT_CC on every type the DAG knows
6509 // about, since there is no way to mark an opcode illegal at all value types
6510 // (See also visitSELECT)
6511 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6512 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6513 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6514 !VT.isVector() &&
6515 (!LegalOperations ||
6516 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6517 SDValue Ops[] =
6518 { N0.getOperand(0), N0.getOperand(1),
6519 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6520 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006521 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006522 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006523
Nadav Rotemed1a3352012-07-23 07:59:50 +00006524 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6525 // (select_cc x, y, 1.0, 0.0,, cc)
6526 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6527 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6528 (!LegalOperations ||
6529 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6530 SDValue Ops[] =
6531 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6532 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6533 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006534 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006535 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006536 }
6537
Dan Gohman475871a2008-07-27 21:46:04 +00006538 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006539}
6540
Dan Gohman475871a2008-07-27 21:46:04 +00006541SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6542 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006543 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006544 EVT VT = N->getValueType(0);
6545 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006546
Nate Begeman1d4d4142005-09-01 00:19:25 +00006547 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006548 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006549 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006550 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006551 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006552 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006553
Chris Lattnercda88752008-06-26 00:16:49 +00006554 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6555 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006556 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6557 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006558 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006559 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006560 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006561 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006562
Nadav Rotemed1a3352012-07-23 07:59:50 +00006563 // The next optimizations are desireable only if SELECT_CC can be lowered.
6564 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6565 // having to say they don't support SELECT_CC on every type the DAG knows
6566 // about, since there is no way to mark an opcode illegal at all value types
6567 // (See also visitSELECT)
6568 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6569 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006570
Nadav Rotemed1a3352012-07-23 07:59:50 +00006571 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6572 (!LegalOperations ||
6573 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6574 SDValue Ops[] =
6575 { N0.getOperand(0), N0.getOperand(1),
6576 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6577 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006578 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006579 }
6580 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006581
Dan Gohman475871a2008-07-27 21:46:04 +00006582 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006583}
6584
Dan Gohman475871a2008-07-27 21:46:04 +00006585SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6586 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006587 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006589
Nate Begeman1d4d4142005-09-01 00:19:25 +00006590 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006591 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006592 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006593
Dan Gohman475871a2008-07-27 21:46:04 +00006594 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006595}
6596
Dan Gohman475871a2008-07-27 21:46:04 +00006597SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6598 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006599 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006600 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006601
Nate Begeman1d4d4142005-09-01 00:19:25 +00006602 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006603 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006604 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006605
Dan Gohman475871a2008-07-27 21:46:04 +00006606 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006607}
6608
Dan Gohman475871a2008-07-27 21:46:04 +00006609SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6610 SDValue N0 = N->getOperand(0);
6611 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006612 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006613 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006614
Nate Begeman1d4d4142005-09-01 00:19:25 +00006615 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006616 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006617 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006618
Chris Lattner79dbea52006-03-13 06:26:26 +00006619 // fold (fp_round (fp_extend x)) -> x
6620 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6621 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006622
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006623 // fold (fp_round (fp_round x)) -> (fp_round x)
6624 if (N0.getOpcode() == ISD::FP_ROUND) {
6625 // This is a value preserving truncation if both round's are.
6626 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006627 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006628 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006629 DAG.getIntPtrConstant(IsTrunc));
6630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006631
Chris Lattner79dbea52006-03-13 06:26:26 +00006632 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006633 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006634 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006635 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006636 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006637 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006638 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006639 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006640
Dan Gohman475871a2008-07-27 21:46:04 +00006641 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006642}
6643
Dan Gohman475871a2008-07-27 21:46:04 +00006644SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6645 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006646 EVT VT = N->getValueType(0);
6647 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006648 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006649
Nate Begeman1d4d4142005-09-01 00:19:25 +00006650 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006651 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006652 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006653 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006654 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006655
Dan Gohman475871a2008-07-27 21:46:04 +00006656 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006657}
6658
Dan Gohman475871a2008-07-27 21:46:04 +00006659SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6660 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006661 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006662 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006663
Chris Lattner5938bef2007-12-29 06:55:23 +00006664 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006665 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006666 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006667 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006668
Nate Begeman1d4d4142005-09-01 00:19:25 +00006669 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006670 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006671 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006672
6673 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6674 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006675 if (N0.getOpcode() == ISD::FP_ROUND
6676 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006677 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006678 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006679 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006680 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006681 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006682 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006683 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006684
Chris Lattner0bd48932008-01-17 07:00:52 +00006685 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006686 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006687 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006688 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006689 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006690 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006691 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006692 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006693 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006694 LN0->isVolatile(), LN0->isNonTemporal(),
6695 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006696 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006697 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006698 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006699 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006700 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006701 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006702 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006703
Dan Gohman475871a2008-07-27 21:46:04 +00006704 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006705}
6706
Dan Gohman475871a2008-07-27 21:46:04 +00006707SDValue DAGCombiner::visitFNEG(SDNode *N) {
6708 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006709 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006710
Craig Topperdd201ff2012-09-11 01:45:21 +00006711 if (VT.isVector()) {
6712 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6713 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006714 }
6715
Owen Andersonafd3d562012-03-06 00:29:31 +00006716 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6717 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006718 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006719
Chris Lattner3bd39d42008-01-27 17:42:27 +00006720 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6721 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006722 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006723 !VT.isVector() &&
6724 N0.getNode()->hasOneUse() &&
6725 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006726 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006727 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006728 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006729 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006730 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006731 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006732 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006733 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006734 }
6735 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006736
Owen Anderson58d57292012-09-01 06:04:27 +00006737 // (fneg (fmul c, x)) -> (fmul -c, x)
6738 if (N0.getOpcode() == ISD::FMUL) {
6739 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006740 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006741 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006742 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006743 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006744 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006745 }
6746
Dan Gohman475871a2008-07-27 21:46:04 +00006747 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006748}
6749
Owen Anderson7c626d32012-08-13 23:32:49 +00006750SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6751 SDValue N0 = N->getOperand(0);
6752 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6753 EVT VT = N->getValueType(0);
6754
6755 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006756 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006757 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006758
6759 return SDValue();
6760}
6761
6762SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6763 SDValue N0 = N->getOperand(0);
6764 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6765 EVT VT = N->getValueType(0);
6766
6767 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006768 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006769 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006770
6771 return SDValue();
6772}
6773
6774SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6775 SDValue N0 = N->getOperand(0);
6776 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6777 EVT VT = N->getValueType(0);
6778
6779 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006780 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006781 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006782
6783 return SDValue();
6784}
6785
Dan Gohman475871a2008-07-27 21:46:04 +00006786SDValue DAGCombiner::visitFABS(SDNode *N) {
6787 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006788 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006789 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006790
Craig Topperdd201ff2012-09-11 01:45:21 +00006791 if (VT.isVector()) {
6792 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6793 if (FoldedVOp.getNode()) return FoldedVOp;
6794 }
6795
Nate Begeman1d4d4142005-09-01 00:19:25 +00006796 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006797 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006798 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006799 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006800 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006801 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006802 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006803 // fold (fabs (fcopysign x, y)) -> (fabs x)
6804 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006805 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006806
Chris Lattner3bd39d42008-01-27 17:42:27 +00006807 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6808 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006809 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006810 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006811 N0.getOperand(0).getValueType().isInteger() &&
6812 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006813 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006814 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006815 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006816 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006817 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006818 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006819 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006820 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006821 }
6822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006823
Dan Gohman475871a2008-07-27 21:46:04 +00006824 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006825}
6826
Dan Gohman475871a2008-07-27 21:46:04 +00006827SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6828 SDValue Chain = N->getOperand(0);
6829 SDValue N1 = N->getOperand(1);
6830 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006831
Dan Gohmane0f06c72009-11-17 00:47:23 +00006832 // If N is a constant we could fold this into a fallthrough or unconditional
6833 // branch. However that doesn't happen very often in normal code, because
6834 // Instcombine/SimplifyCFG should have handled the available opportunities.
6835 // If we did this folding here, it would be necessary to update the
6836 // MachineBasicBlock CFG, which is awkward.
6837
Nate Begeman750ac1b2006-02-01 07:19:44 +00006838 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6839 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006840 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006841 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6842 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006843 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006844 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006845 N1.getOperand(0), N1.getOperand(1), N2);
6846 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006847
Evan Cheng2a135ae2010-10-04 22:41:01 +00006848 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6849 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6850 (N1.getOperand(0).hasOneUse() &&
6851 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6852 SDNode *Trunc = 0;
6853 if (N1.getOpcode() == ISD::TRUNCATE) {
6854 // Look pass the truncate.
6855 Trunc = N1.getNode();
6856 N1 = N1.getOperand(0);
6857 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006858
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006859 // Match this pattern so that we can generate simpler code:
6860 //
6861 // %a = ...
6862 // %b = and i32 %a, 2
6863 // %c = srl i32 %b, 1
6864 // brcond i32 %c ...
6865 //
6866 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006867 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006868 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006869 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006870 // %c = setcc eq %b, 0
6871 // brcond %c ...
6872 //
6873 // This applies only when the AND constant value has one bit set and the
6874 // SRL constant is equal to the log2 of the AND constant. The back-end is
6875 // smart enough to convert the result into a TEST/JMP sequence.
6876 SDValue Op0 = N1.getOperand(0);
6877 SDValue Op1 = N1.getOperand(1);
6878
6879 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006880 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006881 SDValue AndOp1 = Op0.getOperand(1);
6882
6883 if (AndOp1.getOpcode() == ISD::Constant) {
6884 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6885
6886 if (AndConst.isPowerOf2() &&
6887 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6888 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006889 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006890 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006891 Op0, DAG.getConstant(0, Op0.getValueType()),
6892 ISD::SETNE);
6893
Andrew Trickac6d9be2013-05-25 02:42:55 +00006894 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006895 MVT::Other, Chain, SetCC, N2);
6896 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6897 // will convert it back to (X & C1) >> C2.
6898 CombineTo(N, NewBRCond, false);
6899 // Truncate is dead.
6900 if (Trunc) {
6901 removeFromWorkList(Trunc);
6902 DAG.DeleteNode(Trunc);
6903 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006904 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006905 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006906 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006907 removeFromWorkList(N1.getNode());
6908 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006909 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006910 }
6911 }
6912 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006913
6914 if (Trunc)
6915 // Restore N1 if the above transformation doesn't match.
6916 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006917 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006918
Evan Cheng2c755ba2010-02-27 07:36:59 +00006919 // Transform br(xor(x, y)) -> br(x != y)
6920 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6921 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6922 SDNode *TheXor = N1.getNode();
6923 SDValue Op0 = TheXor->getOperand(0);
6924 SDValue Op1 = TheXor->getOperand(1);
6925 if (Op0.getOpcode() == Op1.getOpcode()) {
6926 // Avoid missing important xor optimizations.
6927 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006928 if (Tmp.getNode()) {
6929 if (Tmp.getNode() != TheXor) {
6930 DEBUG(dbgs() << "\nReplacing.8 ";
6931 TheXor->dump(&DAG);
6932 dbgs() << "\nWith: ";
6933 Tmp.getNode()->dump(&DAG);
6934 dbgs() << '\n');
6935 WorkListRemover DeadNodes(*this);
6936 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6937 removeFromWorkList(TheXor);
6938 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006939 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006940 MVT::Other, Chain, Tmp, N2);
6941 }
6942
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006943 // visitXOR has changed XOR's operands or replaced the XOR completely,
6944 // bail out.
6945 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006946 }
6947 }
6948
6949 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6950 bool Equal = false;
6951 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6952 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6953 Op0.getOpcode() == ISD::XOR) {
6954 TheXor = Op0.getNode();
6955 Equal = true;
6956 }
6957
Evan Cheng2a135ae2010-10-04 22:41:01 +00006958 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006959 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00006960 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006961 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006962 SetCCVT,
6963 Op0, Op1,
6964 Equal ? ISD::SETEQ : ISD::SETNE);
6965 // Replace the uses of XOR with SETCC
6966 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006967 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006968 removeFromWorkList(N1.getNode());
6969 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006970 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006971 MVT::Other, Chain, SetCC, N2);
6972 }
6973 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006974
Dan Gohman475871a2008-07-27 21:46:04 +00006975 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006976}
6977
Chris Lattner3ea0b472005-10-05 06:47:48 +00006978// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6979//
Dan Gohman475871a2008-07-27 21:46:04 +00006980SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006981 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006982 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006983
Dan Gohmane0f06c72009-11-17 00:47:23 +00006984 // If N is a constant we could fold this into a fallthrough or unconditional
6985 // branch. However that doesn't happen very often in normal code, because
6986 // Instcombine/SimplifyCFG should have handled the available opportunities.
6987 // If we did this folding here, it would be necessary to update the
6988 // MachineBasicBlock CFG, which is awkward.
6989
Duncan Sands8eab8a22008-06-09 11:32:28 +00006990 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00006991 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006992 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006993 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006994 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006995
Nate Begemane17daeb2005-10-05 21:43:42 +00006996 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006997 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006998 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006999 N->getOperand(0), Simp.getOperand(2),
7000 Simp.getOperand(0), Simp.getOperand(1),
7001 N->getOperand(4));
7002
Dan Gohman475871a2008-07-27 21:46:04 +00007003 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007004}
7005
Evan Chengc4b527a2012-01-13 01:37:24 +00007006/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7007/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007008/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007009static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7010 SelectionDAG &DAG,
7011 const TargetLowering &TLI) {
7012 EVT VT;
7013 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7014 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7015 return false;
7016 VT = Use->getValueType(0);
7017 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7018 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7019 return false;
7020 VT = ST->getValue().getValueType();
7021 } else
7022 return false;
7023
Chandler Carruth56d433d2013-01-07 15:14:13 +00007024 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007025 if (N->getOpcode() == ISD::ADD) {
7026 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7027 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007028 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007029 AM.BaseOffs = Offset->getSExtValue();
7030 else
Evan Cheng03be3622012-03-06 23:33:32 +00007031 // [reg +/- reg]
7032 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007033 } else if (N->getOpcode() == ISD::SUB) {
7034 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7035 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007036 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007037 AM.BaseOffs = -Offset->getSExtValue();
7038 else
Evan Cheng03be3622012-03-06 23:33:32 +00007039 // [reg +/- reg]
7040 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007041 } else
7042 return false;
7043
7044 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7045}
7046
Duncan Sandsec87aa82008-06-15 20:12:31 +00007047/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7048/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007049/// and it has other uses besides the load / store. After the
7050/// transformation, the new indexed load / store has effectively folded
7051/// the add / subtract in and all of its other uses are redirected to the
7052/// new load / store.
7053bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007054 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007055 return false;
7056
7057 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007058 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007059 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007060 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007061 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007062 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007063 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007064 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007065 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7066 return false;
7067 Ptr = LD->getBasePtr();
7068 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007069 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007070 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007071 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007072 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7073 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7074 return false;
7075 Ptr = ST->getBasePtr();
7076 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007077 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007078 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007079 }
Chris Lattner448f2192006-11-11 00:39:41 +00007080
Chris Lattner9f1794e2006-11-11 00:56:29 +00007081 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7082 // out. There is no reason to make this a preinc/predec.
7083 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007084 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007085 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007086
Chris Lattner9f1794e2006-11-11 00:56:29 +00007087 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007088 SDValue BasePtr;
7089 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007090 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7091 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7092 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007093
7094 // Backends without true r+i pre-indexed forms may need to pass a
7095 // constant base with a variable offset so that constant coercion
7096 // will work with the patterns in canonical form.
7097 bool Swapped = false;
7098 if (isa<ConstantSDNode>(BasePtr)) {
7099 std::swap(BasePtr, Offset);
7100 Swapped = true;
7101 }
7102
Evan Chenga7d4a042007-05-03 23:52:19 +00007103 // Don't create a indexed load / store with zero offset.
7104 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007105 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007106 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Chris Lattner41e53fd2006-11-11 01:00:15 +00007108 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007109 // 1) The new base ptr is a frame index.
7110 // 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 +00007111 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007112 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007113 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007114 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007115
Chris Lattner41e53fd2006-11-11 01:00:15 +00007116 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7117 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007118 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007119 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007120
Chris Lattner41e53fd2006-11-11 01:00:15 +00007121 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007122 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007123 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007124 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007125 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007126 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007127
Hal Finkel089a5f82013-02-08 21:35:47 +00007128 // If the offset is a constant, there may be other adds of constants that
7129 // can be folded with this one. We should do this to avoid having to keep
7130 // a copy of the original base pointer.
7131 SmallVector<SDNode *, 16> OtherUses;
7132 if (isa<ConstantSDNode>(Offset))
7133 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7134 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7135 SDNode *Use = *I;
7136 if (Use == Ptr.getNode())
7137 continue;
7138
7139 if (Use->isPredecessorOf(N))
7140 continue;
7141
7142 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7143 OtherUses.clear();
7144 break;
7145 }
7146
7147 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7148 if (Op1.getNode() == BasePtr.getNode())
7149 std::swap(Op0, Op1);
7150 assert(Op0.getNode() == BasePtr.getNode() &&
7151 "Use of ADD/SUB but not an operand");
7152
7153 if (!isa<ConstantSDNode>(Op1)) {
7154 OtherUses.clear();
7155 break;
7156 }
7157
7158 // FIXME: In some cases, we can be smarter about this.
7159 if (Op1.getValueType() != Offset.getValueType()) {
7160 OtherUses.clear();
7161 break;
7162 }
7163
7164 OtherUses.push_back(Use);
7165 }
7166
7167 if (Swapped)
7168 std::swap(BasePtr, Offset);
7169
Evan Chengc843abe2007-05-24 02:35:39 +00007170 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007171 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007172
7173 // Caches for hasPredecessorHelper
7174 SmallPtrSet<const SDNode *, 32> Visited;
7175 SmallVector<const SDNode *, 16> Worklist;
7176
Gabor Greifba36cb52008-08-28 21:40:38 +00007177 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7178 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007179 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007180 if (Use == N)
7181 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007182 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007183 return false;
7184
Evan Chengc4b527a2012-01-13 01:37:24 +00007185 // If Ptr may be folded in addressing mode of other use, then it's
7186 // not profitable to do this transformation.
7187 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007188 RealUse = true;
7189 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007190
Chris Lattner9f1794e2006-11-11 00:56:29 +00007191 if (!RealUse)
7192 return false;
7193
Dan Gohman475871a2008-07-27 21:46:04 +00007194 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007195 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007196 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007197 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007198 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007199 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007200 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007201 ++PreIndexedNodes;
7202 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007203 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007204 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007205 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007206 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007207 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007208 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007209 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007210 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7211 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007212 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007213 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007214 }
7215
Chris Lattner9f1794e2006-11-11 00:56:29 +00007216 // Finally, since the node is now dead, remove it from the graph.
7217 DAG.DeleteNode(N);
7218
Hal Finkel089a5f82013-02-08 21:35:47 +00007219 if (Swapped)
7220 std::swap(BasePtr, Offset);
7221
7222 // Replace other uses of BasePtr that can be updated to use Ptr
7223 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7224 unsigned OffsetIdx = 1;
7225 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7226 OffsetIdx = 0;
7227 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7228 BasePtr.getNode() && "Expected BasePtr operand");
7229
Silviu Baranga730a5702013-04-26 15:52:24 +00007230 // We need to replace ptr0 in the following expression:
7231 // x0 * offset0 + y0 * ptr0 = t0
7232 // knowing that
7233 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007234 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007235 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7236 // indexed load/store and the expresion that needs to be re-written.
7237 //
7238 // Therefore, we have:
7239 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007240
7241 ConstantSDNode *CN =
7242 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007243 int X0, X1, Y0, Y1;
7244 APInt Offset0 = CN->getAPIntValue();
7245 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007246
Silviu Baranga730a5702013-04-26 15:52:24 +00007247 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7248 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7249 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7250 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007251
Silviu Baranga730a5702013-04-26 15:52:24 +00007252 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7253
7254 APInt CNV = Offset0;
7255 if (X0 < 0) CNV = -CNV;
7256 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7257 else CNV = CNV - Offset1;
7258
7259 // We can now generate the new expression.
7260 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7261 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7262
7263 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007264 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007265 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7266 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7267 removeFromWorkList(OtherUses[i]);
7268 DAG.DeleteNode(OtherUses[i]);
7269 }
7270
Chris Lattner9f1794e2006-11-11 00:56:29 +00007271 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007272 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007273 removeFromWorkList(Ptr.getNode());
7274 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007275
7276 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007277}
7278
Duncan Sandsec87aa82008-06-15 20:12:31 +00007279/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007280/// add / sub of the base pointer node into a post-indexed load / store.
7281/// The transformation folded the add / subtract into the new indexed
7282/// load / store effectively and all of its uses are redirected to the
7283/// new load / store.
7284bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007285 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007286 return false;
7287
7288 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007289 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007290 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007291 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007292 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007293 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007294 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007295 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7296 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7297 return false;
7298 Ptr = LD->getBasePtr();
7299 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007300 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007301 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007302 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007303 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7304 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7305 return false;
7306 Ptr = ST->getBasePtr();
7307 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007308 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007309 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007310 }
Chris Lattner448f2192006-11-11 00:39:41 +00007311
Gabor Greifba36cb52008-08-28 21:40:38 +00007312 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007313 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007314
Gabor Greifba36cb52008-08-28 21:40:38 +00007315 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7316 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007317 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007318 if (Op == N ||
7319 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7320 continue;
7321
Dan Gohman475871a2008-07-27 21:46:04 +00007322 SDValue BasePtr;
7323 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007324 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7325 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007326 // Don't create a indexed load / store with zero offset.
7327 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007328 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007329 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007330
Chris Lattner9f1794e2006-11-11 00:56:29 +00007331 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007332 // 1) All uses are load / store ops that use it as base ptr (and
7333 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007334 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7335 // nor a successor of N. Otherwise, if Op is folded that would
7336 // create a cycle.
7337
Evan Chengcaab1292009-05-06 18:25:01 +00007338 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7339 continue;
7340
Chris Lattner9f1794e2006-11-11 00:56:29 +00007341 // Check for #1.
7342 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007343 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7344 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007345 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007346 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007347 continue;
7348
Chris Lattner9f1794e2006-11-11 00:56:29 +00007349 // If all the uses are load / store addresses, then don't do the
7350 // transformation.
7351 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7352 bool RealUse = false;
7353 for (SDNode::use_iterator III = Use->use_begin(),
7354 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007355 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007356 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007357 RealUse = true;
7358 }
Chris Lattner448f2192006-11-11 00:39:41 +00007359
Chris Lattner9f1794e2006-11-11 00:56:29 +00007360 if (!RealUse) {
7361 TryNext = true;
7362 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007363 }
7364 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007365 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007366
Chris Lattner9f1794e2006-11-11 00:56:29 +00007367 if (TryNext)
7368 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007369
Chris Lattner9f1794e2006-11-11 00:56:29 +00007370 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007371 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007372 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007373 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007374 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007375 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007376 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007377 ++PostIndexedNodes;
7378 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007379 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007380 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007381 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007382 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007383 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007384 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007385 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007386 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7387 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007388 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007389 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007390 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007391
Chris Lattner9f1794e2006-11-11 00:56:29 +00007392 // Finally, since the node is now dead, remove it from the graph.
7393 DAG.DeleteNode(N);
7394
7395 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007396 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007397 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007398 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007399 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007400 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007401 }
7402 }
7403 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007404
Chris Lattner448f2192006-11-11 00:39:41 +00007405 return false;
7406}
7407
Dan Gohman475871a2008-07-27 21:46:04 +00007408SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007409 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007410 SDValue Chain = LD->getChain();
7411 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007412
Evan Cheng45a7ca92007-05-01 00:38:21 +00007413 // If load is not volatile and there are no uses of the loaded value (and
7414 // the updated indexed value in case of indexed loads), change uses of the
7415 // chain value into uses of the chain input (i.e. delete the dead load).
7416 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007417 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007418 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007419 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007420 // It's not safe to use the two value CombineTo variant here. e.g.
7421 // v1, chain2 = load chain1, loc
7422 // v2, chain3 = load chain2, loc
7423 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007424 // Now we replace use of chain2 with chain1. This makes the second load
7425 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007426 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007427 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007428 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007429 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007430 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007431 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007432 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007433
Chris Lattner125991a2008-01-24 07:57:06 +00007434 if (N->use_empty()) {
7435 removeFromWorkList(N);
7436 DAG.DeleteNode(N);
7437 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007438
Dan Gohman475871a2008-07-27 21:46:04 +00007439 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007440 }
Evan Cheng498f5592007-05-01 08:53:39 +00007441 } else {
7442 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007443 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007444 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007445 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007446 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007447 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007448 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007449 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007450 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007451 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007454 DAG.getUNDEF(N->getValueType(1)));
7455 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007456 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007457 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007458 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007459 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007460 }
7461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007462
Chris Lattner01a22022005-10-10 22:04:48 +00007463 // If this load is directly stored, replace the load value with the stored
7464 // value.
7465 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007466 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007467 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007468 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007469 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7470 if (PrevST->getBasePtr() == Ptr &&
7471 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007472 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007473 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007474 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007475
Evan Cheng255f20f2010-04-01 06:04:33 +00007476 // Try to infer better alignment information than the load already has.
7477 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007478 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007479 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7480 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007481 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007482 LD->getValueType(0),
7483 Chain, Ptr, LD->getPointerInfo(),
7484 LD->getMemoryVT(),
7485 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007486 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7487 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007488 }
7489 }
7490
Jim Laskey7ca56af2006-10-11 13:47:09 +00007491 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007492 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007493 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007494
Jim Laskey6ff23e52006-10-04 16:53:27 +00007495 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007496 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007497 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007498
Jim Laskey279f0532006-09-25 16:29:54 +00007499 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007500 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007501 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007502 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007503 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007504 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007505 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007506 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007507 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007508 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007509 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007510 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007511 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007512 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007513 }
Jim Laskey279f0532006-09-25 16:29:54 +00007514
Jim Laskey6ff23e52006-10-04 16:53:27 +00007515 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007516 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007517 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007518
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007519 // Make sure the new and old chains are cleaned up.
7520 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007521
Jim Laskey274062c2006-10-13 23:32:28 +00007522 // Replace uses with load result and token factor. Don't add users
7523 // to work list.
7524 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007525 }
7526 }
7527
Evan Cheng7fc033a2006-11-03 03:06:21 +00007528 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007529 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007530 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007531
Dan Gohman475871a2008-07-27 21:46:04 +00007532 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007533}
7534
Chris Lattner2392ae72010-04-15 04:48:01 +00007535/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7536/// load is having specific bytes cleared out. If so, return the byte size
7537/// being masked out and the shift amount.
7538static std::pair<unsigned, unsigned>
7539CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7540 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007541
Chris Lattner2392ae72010-04-15 04:48:01 +00007542 // Check for the structure we're looking for.
7543 if (V->getOpcode() != ISD::AND ||
7544 !isa<ConstantSDNode>(V->getOperand(1)) ||
7545 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7546 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007547
Chris Lattnere6987582010-04-15 06:10:49 +00007548 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007549 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007550 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007551
Chris Lattnere6987582010-04-15 06:10:49 +00007552 // The store should be chained directly to the load or be an operand of a
7553 // tokenfactor.
7554 if (LD == Chain.getNode())
7555 ; // ok.
7556 else if (Chain->getOpcode() != ISD::TokenFactor)
7557 return Result; // Fail.
7558 else {
7559 bool isOk = false;
7560 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7561 if (Chain->getOperand(i).getNode() == LD) {
7562 isOk = true;
7563 break;
7564 }
7565 if (!isOk) return Result;
7566 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007567
Chris Lattner2392ae72010-04-15 04:48:01 +00007568 // This only handles simple types.
7569 if (V.getValueType() != MVT::i16 &&
7570 V.getValueType() != MVT::i32 &&
7571 V.getValueType() != MVT::i64)
7572 return Result;
7573
7574 // Check the constant mask. Invert it so that the bits being masked out are
7575 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7576 // follow the sign bit for uniformity.
7577 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007578 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007579 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007580 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007581 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7582 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007583
Chris Lattner2392ae72010-04-15 04:48:01 +00007584 // See if we have a continuous run of bits. If so, we have 0*1+0*
7585 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7586 return Result;
7587
7588 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7589 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7590 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007591
Chris Lattner2392ae72010-04-15 04:48:01 +00007592 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7593 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007594 case 1:
7595 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007596 case 4: break;
7597 default: return Result; // All one mask, or 5-byte mask.
7598 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007599
Chris Lattner2392ae72010-04-15 04:48:01 +00007600 // Verify that the first bit starts at a multiple of mask so that the access
7601 // is aligned the same as the access width.
7602 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007603
Chris Lattner2392ae72010-04-15 04:48:01 +00007604 Result.first = MaskedBytes;
7605 Result.second = NotMaskTZ/8;
7606 return Result;
7607}
7608
7609
7610/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7611/// provides a value as specified by MaskInfo. If so, replace the specified
7612/// store with a narrower store of truncated IVal.
7613static SDNode *
7614ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7615 SDValue IVal, StoreSDNode *St,
7616 DAGCombiner *DC) {
7617 unsigned NumBytes = MaskInfo.first;
7618 unsigned ByteShift = MaskInfo.second;
7619 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007620
Chris Lattner2392ae72010-04-15 04:48:01 +00007621 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7622 // that uses this. If not, this is not a replacement.
7623 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7624 ByteShift*8, (ByteShift+NumBytes)*8);
7625 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007626
Chris Lattner2392ae72010-04-15 04:48:01 +00007627 // Check that it is legal on the target to do this. It is legal if the new
7628 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7629 // legalization.
7630 MVT VT = MVT::getIntegerVT(NumBytes*8);
7631 if (!DC->isTypeLegal(VT))
7632 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007633
Chris Lattner2392ae72010-04-15 04:48:01 +00007634 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7635 // shifted by ByteShift and truncated down to NumBytes.
7636 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007637 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007638 DAG.getConstant(ByteShift*8,
7639 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007640
7641 // Figure out the offset for the store and the alignment of the access.
7642 unsigned StOffset;
7643 unsigned NewAlign = St->getAlignment();
7644
7645 if (DAG.getTargetLoweringInfo().isLittleEndian())
7646 StOffset = ByteShift;
7647 else
7648 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007649
Chris Lattner2392ae72010-04-15 04:48:01 +00007650 SDValue Ptr = St->getBasePtr();
7651 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007652 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007653 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7654 NewAlign = MinAlign(NewAlign, StOffset);
7655 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007656
Chris Lattner2392ae72010-04-15 04:48:01 +00007657 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007658 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007659
Chris Lattner2392ae72010-04-15 04:48:01 +00007660 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007661 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007662 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007663 false, false, NewAlign).getNode();
7664}
7665
Evan Cheng8b944d32009-05-28 00:35:15 +00007666
7667/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7668/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7669/// of the loaded bits, try narrowing the load and store if it would end up
7670/// being a win for performance or code size.
7671SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7672 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007673 if (ST->isVolatile())
7674 return SDValue();
7675
Evan Cheng8b944d32009-05-28 00:35:15 +00007676 SDValue Chain = ST->getChain();
7677 SDValue Value = ST->getValue();
7678 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007679 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007680
7681 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007682 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007683
7684 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007685
Chris Lattner2392ae72010-04-15 04:48:01 +00007686 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7687 // is a byte mask indicating a consecutive number of bytes, check to see if
7688 // Y is known to provide just those bytes. If so, we try to replace the
7689 // load + replace + store sequence with a single (narrower) store, which makes
7690 // the load dead.
7691 if (Opc == ISD::OR) {
7692 std::pair<unsigned, unsigned> MaskedLoad;
7693 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7694 if (MaskedLoad.first)
7695 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7696 Value.getOperand(1), ST,this))
7697 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007698
Chris Lattner2392ae72010-04-15 04:48:01 +00007699 // Or is commutative, so try swapping X and Y.
7700 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7701 if (MaskedLoad.first)
7702 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7703 Value.getOperand(0), ST,this))
7704 return SDValue(NewST, 0);
7705 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007706
Evan Cheng8b944d32009-05-28 00:35:15 +00007707 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7708 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007709 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007710
7711 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007712 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7713 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007714 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007715 if (LD->getBasePtr() != Ptr ||
7716 LD->getPointerInfo().getAddrSpace() !=
7717 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007718 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007719
7720 // Find the type to narrow it the load / op / store to.
7721 SDValue N1 = Value.getOperand(1);
7722 unsigned BitWidth = N1.getValueSizeInBits();
7723 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7724 if (Opc == ISD::AND)
7725 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007726 if (Imm == 0 || Imm.isAllOnesValue())
7727 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007728 unsigned ShAmt = Imm.countTrailingZeros();
7729 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7730 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007731 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007732 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007733 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007734 TLI.isNarrowingProfitable(VT, NewVT))) {
7735 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007736 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007737 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007738 if (NewBW >= BitWidth)
7739 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007740
7741 // If the lsb changed does not start at the type bitwidth boundary,
7742 // start at the previous one.
7743 if (ShAmt % NewBW)
7744 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007745 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7746 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007747 if ((Imm & Mask) == Imm) {
7748 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7749 if (Opc == ISD::AND)
7750 NewImm ^= APInt::getAllOnesValue(NewBW);
7751 uint64_t PtrOff = ShAmt / 8;
7752 // For big endian targets, we need to adjust the offset to the pointer to
7753 // load the correct bytes.
7754 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007755 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007756
7757 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007758 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007759 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007760 return SDValue();
7761
Andrew Trickac6d9be2013-05-25 02:42:55 +00007762 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007763 Ptr.getValueType(), Ptr,
7764 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007765 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007766 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007767 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007768 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007769 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007770 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007771 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007772 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007773 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007774 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007775 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007776
7777 AddToWorkList(NewPtr.getNode());
7778 AddToWorkList(NewLD.getNode());
7779 AddToWorkList(NewVal.getNode());
7780 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007781 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007782 ++OpsNarrowed;
7783 return NewST;
7784 }
7785 }
7786
Evan Chengcdcecc02009-05-28 18:41:02 +00007787 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007788}
7789
Evan Cheng31959b12011-02-02 01:06:55 +00007790/// TransformFPLoadStorePair - For a given floating point load / store pair,
7791/// if the load value isn't used by any other operations, then consider
7792/// transforming the pair to integer load / store operations if the target
7793/// deems the transformation profitable.
7794SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7795 StoreSDNode *ST = cast<StoreSDNode>(N);
7796 SDValue Chain = ST->getChain();
7797 SDValue Value = ST->getValue();
7798 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7799 Value.hasOneUse() &&
7800 Chain == SDValue(Value.getNode(), 1)) {
7801 LoadSDNode *LD = cast<LoadSDNode>(Value);
7802 EVT VT = LD->getMemoryVT();
7803 if (!VT.isFloatingPoint() ||
7804 VT != ST->getMemoryVT() ||
7805 LD->isNonTemporal() ||
7806 ST->isNonTemporal() ||
7807 LD->getPointerInfo().getAddrSpace() != 0 ||
7808 ST->getPointerInfo().getAddrSpace() != 0)
7809 return SDValue();
7810
7811 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7812 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7813 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7814 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7815 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7816 return SDValue();
7817
7818 unsigned LDAlign = LD->getAlignment();
7819 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007820 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007821 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007822 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7823 return SDValue();
7824
Andrew Trickac6d9be2013-05-25 02:42:55 +00007825 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007826 LD->getChain(), LD->getBasePtr(),
7827 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007828 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007829
Andrew Trickac6d9be2013-05-25 02:42:55 +00007830 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007831 NewLD, ST->getBasePtr(),
7832 ST->getPointerInfo(),
7833 false, false, STAlign);
7834
7835 AddToWorkList(NewLD.getNode());
7836 AddToWorkList(NewST.getNode());
7837 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007838 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007839 ++LdStFP2Int;
7840 return NewST;
7841 }
7842
7843 return SDValue();
7844}
7845
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007846/// Helper struct to parse and store a memory address as base + index + offset.
7847/// We ignore sign extensions when it is safe to do so.
7848/// The following two expressions are not equivalent. To differentiate we need
7849/// to store whether there was a sign extension involved in the index
7850/// computation.
7851/// (load (i64 add (i64 copyfromreg %c)
7852/// (i64 signextend (add (i8 load %index)
7853/// (i8 1))))
7854/// vs
7855///
7856/// (load (i64 add (i64 copyfromreg %c)
7857/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7858/// (i32 1)))))
7859struct BaseIndexOffset {
7860 SDValue Base;
7861 SDValue Index;
7862 int64_t Offset;
7863 bool IsIndexSignExt;
7864
7865 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7866
7867 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7868 bool IsIndexSignExt) :
7869 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7870
7871 bool equalBaseIndex(const BaseIndexOffset &Other) {
7872 return Other.Base == Base && Other.Index == Index &&
7873 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007874 }
7875
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007876 /// Parses tree in Ptr for base, index, offset addresses.
7877 static BaseIndexOffset match(SDValue Ptr) {
7878 bool IsIndexSignExt = false;
7879
Juergen Ributzka915e9362013-08-21 21:53:38 +00007880 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
7881 // instruction, then it could be just the BASE or everything else we don't
7882 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007883 if (Ptr->getOpcode() != ISD::ADD)
7884 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7885
Juergen Ributzka915e9362013-08-21 21:53:38 +00007886 // We know that we have at least an ADD instruction. Try to pattern match
7887 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007888 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7889 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7890 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7891 IsIndexSignExt);
7892 }
7893
Juergen Ributzka915e9362013-08-21 21:53:38 +00007894 // Inside a loop the current BASE pointer is calculated using an ADD and a
7895 // MUL insruction. In this case Ptr is the actual BASE pointer.
7896 // (i64 add (i64 %array_ptr)
7897 // (i64 mul (i64 %induction_var)
7898 // (i64 %element_size)))
7899 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL) {
7900 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7901 }
7902
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007903 // Look at Base + Index + Offset cases.
7904 SDValue Base = Ptr->getOperand(0);
7905 SDValue IndexOffset = Ptr->getOperand(1);
7906
7907 // Skip signextends.
7908 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7909 IndexOffset = IndexOffset->getOperand(0);
7910 IsIndexSignExt = true;
7911 }
7912
7913 // Either the case of Base + Index (no offset) or something else.
7914 if (IndexOffset->getOpcode() != ISD::ADD)
7915 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7916
7917 // Now we have the case of Base + Index + offset.
7918 SDValue Index = IndexOffset->getOperand(0);
7919 SDValue Offset = IndexOffset->getOperand(1);
7920
7921 if (!isa<ConstantSDNode>(Offset))
7922 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7923
7924 // Ignore signextends.
7925 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7926 Index = Index->getOperand(0);
7927 IsIndexSignExt = true;
7928 } else IsIndexSignExt = false;
7929
7930 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7931 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7932 }
7933};
Nadav Rotemc653de62012-10-03 16:11:15 +00007934
7935/// Holds a pointer to an LSBaseSDNode as well as information on where it
7936/// is located in a sequence of memory operations connected by a chain.
7937struct MemOpLink {
7938 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7939 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7940 // Ptr to the mem node.
7941 LSBaseSDNode *MemNode;
7942 // Offset from the base ptr.
7943 int64_t OffsetFromBase;
7944 // What is the sequence number of this mem node.
7945 // Lowest mem operand in the DAG starts at zero.
7946 unsigned SequenceNum;
7947};
7948
7949/// Sorts store nodes in a link according to their offset from a shared
7950// base ptr.
7951struct ConsecutiveMemoryChainSorter {
7952 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7953 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7954 }
7955};
7956
7957bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7958 EVT MemVT = St->getMemoryVT();
7959 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007960 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7961 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007962
7963 // Don't merge vectors into wider inputs.
7964 if (MemVT.isVector() || !MemVT.isSimple())
7965 return false;
7966
7967 // Perform an early exit check. Do not bother looking at stored values that
7968 // are not constants or loads.
7969 SDValue StoredVal = St->getValue();
7970 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7971 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7972 !IsLoadSrc)
7973 return false;
7974
7975 // Only look at ends of store sequences.
7976 SDValue Chain = SDValue(St, 1);
7977 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7978 return false;
7979
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007980 // This holds the base pointer, index, and the offset in bytes from the base
7981 // pointer.
7982 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007983
7984 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007985 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007986 return false;
7987
7988 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007989 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007990 return false;
7991
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007992 // Save the LoadSDNodes that we find in the chain.
7993 // We need to make sure that these nodes do not interfere with
7994 // any of the store nodes.
7995 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7996
7997 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00007998 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007999
Nadav Rotemc653de62012-10-03 16:11:15 +00008000 // Walk up the chain and look for nodes with offsets from the same
8001 // base pointer. Stop when reaching an instruction with a different kind
8002 // or instruction which has a different base pointer.
8003 unsigned Seq = 0;
8004 StoreSDNode *Index = St;
8005 while (Index) {
8006 // If the chain has more than one use, then we can't reorder the mem ops.
8007 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8008 break;
8009
8010 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008011 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008012
8013 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008014 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008015 break;
8016
8017 // Check that the alignment is the same.
8018 if (Index->getAlignment() != St->getAlignment())
8019 break;
8020
8021 // The memory operands must not be volatile.
8022 if (Index->isVolatile() || Index->isIndexed())
8023 break;
8024
8025 // No truncation.
8026 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8027 if (St->isTruncatingStore())
8028 break;
8029
8030 // The stored memory type must be the same.
8031 if (Index->getMemoryVT() != MemVT)
8032 break;
8033
8034 // We do not allow unaligned stores because we want to prevent overriding
8035 // stores.
8036 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8037 break;
8038
8039 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008040 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008041
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008042 // Find the next memory operand in the chain. If the next operand in the
8043 // chain is a store then move up and continue the scan with the next
8044 // memory operand. If the next operand is a load save it and use alias
8045 // information to check if it interferes with anything.
8046 SDNode *NextInChain = Index->getChain().getNode();
8047 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008048 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008049 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008050 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008051 break;
8052 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8053 // Save the load node for later. Continue the scan.
8054 AliasLoadNodes.push_back(Ldn);
8055 NextInChain = Ldn->getChain().getNode();
8056 continue;
8057 } else {
8058 Index = NULL;
8059 break;
8060 }
8061 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008062 }
8063
8064 // Check if there is anything to merge.
8065 if (StoreNodes.size() < 2)
8066 return false;
8067
8068 // Sort the memory operands according to their distance from the base pointer.
8069 std::sort(StoreNodes.begin(), StoreNodes.end(),
8070 ConsecutiveMemoryChainSorter());
8071
8072 // Scan the memory operations on the chain and find the first non-consecutive
8073 // store memory address.
8074 unsigned LastConsecutiveStore = 0;
8075 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008076 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8077
8078 // Check that the addresses are consecutive starting from the second
8079 // element in the list of stores.
8080 if (i > 0) {
8081 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8082 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8083 break;
8084 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008085
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008086 bool Alias = false;
8087 // Check if this store interferes with any of the loads that we found.
8088 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8089 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8090 Alias = true;
8091 break;
8092 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008093 // We found a load that alias with this store. Stop the sequence.
8094 if (Alias)
8095 break;
8096
Nadav Rotemc653de62012-10-03 16:11:15 +00008097 // Mark this node as useful.
8098 LastConsecutiveStore = i;
8099 }
8100
8101 // The node with the lowest store address.
8102 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8103
8104 // Store the constants into memory as one consecutive store.
8105 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008106 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008107 unsigned LastLegalVectorType = 0;
8108 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008109 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8110 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8111 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008112
8113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008114 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008115 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008116 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008117 } else {
8118 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008119 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008120 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008121
Nadav Rotemc653de62012-10-03 16:11:15 +00008122 // Find a legal type for the constant store.
8123 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8124 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8125 if (TLI.isTypeLegal(StoreTy))
8126 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008127 // Or check whether a truncstore is legal.
8128 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8129 TargetLowering::TypePromoteInteger) {
8130 EVT LegalizedStoredValueTy =
8131 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8132 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8133 LastLegalType = i+1;
8134 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008135
8136 // Find a legal type for the vector store.
8137 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8138 if (TLI.isTypeLegal(Ty))
8139 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008140 }
8141
Bob Wilson99d8e762012-12-20 01:36:20 +00008142 // We only use vectors if the constant is known to be zero and the
8143 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008144 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008145 LastLegalVectorType = 0;
8146
Nadav Rotemc653de62012-10-03 16:11:15 +00008147 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008148 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008149 return false;
8150
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008151 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008152 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8153
8154 // Make sure we have something to merge.
8155 if (NumElem < 2)
8156 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008157
8158 unsigned EarliestNodeUsed = 0;
8159 for (unsigned i=0; i < NumElem; ++i) {
8160 // Find a chain for the new wide-store operand. Notice that some
8161 // of the store nodes that we found may not be selected for inclusion
8162 // in the wide store. The chain we use needs to be the chain of the
8163 // earliest store node which is *used* and replaced by the wide store.
8164 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8165 EarliestNodeUsed = i;
8166 }
8167
8168 // The earliest Node in the DAG.
8169 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008170 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008171
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008172 SDValue StoredVal;
8173 if (UseVector) {
8174 // Find a legal type for the vector store.
8175 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8176 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8177 StoredVal = DAG.getConstant(0, Ty);
8178 } else {
8179 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8180 APInt StoreInt(StoreBW, 0);
8181
8182 // Construct a single integer constant which is made of the smaller
8183 // constant inputs.
8184 bool IsLE = TLI.isLittleEndian();
8185 for (unsigned i = 0; i < NumElem ; ++i) {
8186 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8187 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8188 SDValue Val = St->getValue();
8189 StoreInt<<=ElementSizeBytes*8;
8190 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8191 StoreInt|=C->getAPIntValue().zext(StoreBW);
8192 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8193 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8194 } else {
8195 assert(false && "Invalid constant element type");
8196 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008197 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008198
8199 // Create the new Load and Store operations.
8200 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8201 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008202 }
8203
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008204 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008205 FirstInChain->getBasePtr(),
8206 FirstInChain->getPointerInfo(),
8207 false, false,
8208 FirstInChain->getAlignment());
8209
8210 // Replace the first store with the new store
8211 CombineTo(EarliestOp, NewStore);
8212 // Erase all other stores.
8213 for (unsigned i = 0; i < NumElem ; ++i) {
8214 if (StoreNodes[i].MemNode == EarliestOp)
8215 continue;
8216 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008217 // ReplaceAllUsesWith will replace all uses that existed when it was
8218 // called, but graph optimizations may cause new ones to appear. For
8219 // example, the case in pr14333 looks like
8220 //
8221 // St's chain -> St -> another store -> X
8222 //
8223 // And the only difference from St to the other store is the chain.
8224 // When we change it's chain to be St's chain they become identical,
8225 // get CSEed and the net result is that X is now a use of St.
8226 // Since we know that St is redundant, just iterate.
8227 while (!St->use_empty())
8228 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008229 removeFromWorkList(St);
8230 DAG.DeleteNode(St);
8231 }
8232
8233 return true;
8234 }
8235
8236 // Below we handle the case of multiple consecutive stores that
8237 // come from multiple consecutive loads. We merge them into a single
8238 // wide load and a single wide store.
8239
8240 // Look for load nodes which are used by the stored values.
8241 SmallVector<MemOpLink, 8> LoadNodes;
8242
8243 // Find acceptable loads. Loads need to have the same chain (token factor),
8244 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008245 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008246 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8247 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8248 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8249 if (!Ld) break;
8250
8251 // Loads must only have one use.
8252 if (!Ld->hasNUsesOfValue(1, 0))
8253 break;
8254
8255 // Check that the alignment is the same as the stores.
8256 if (Ld->getAlignment() != St->getAlignment())
8257 break;
8258
8259 // The memory operands must not be volatile.
8260 if (Ld->isVolatile() || Ld->isIndexed())
8261 break;
8262
8263 // We do not accept ext loads.
8264 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8265 break;
8266
8267 // The stored memory type must be the same.
8268 if (Ld->getMemoryVT() != MemVT)
8269 break;
8270
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008271 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008272 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008273 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008274 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008275 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008276 break;
8277 } else {
8278 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008279 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008280 }
8281
8282 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008283 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008284 }
8285
8286 if (LoadNodes.size() < 2)
8287 return false;
8288
8289 // Scan the memory operations on the chain and find the first non-consecutive
8290 // load memory address. These variables hold the index in the store node
8291 // array.
8292 unsigned LastConsecutiveLoad = 0;
8293 // This variable refers to the size and not index in the array.
8294 unsigned LastLegalVectorType = 0;
8295 unsigned LastLegalIntegerType = 0;
8296 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008297 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8298 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8299 // All loads much share the same chain.
8300 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8301 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008302
Nadav Rotemc653de62012-10-03 16:11:15 +00008303 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8304 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8305 break;
8306 LastConsecutiveLoad = i;
8307
8308 // Find a legal type for the vector store.
8309 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8310 if (TLI.isTypeLegal(StoreTy))
8311 LastLegalVectorType = i + 1;
8312
8313 // Find a legal type for the integer store.
8314 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8315 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8316 if (TLI.isTypeLegal(StoreTy))
8317 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008318 // Or check whether a truncstore and extload is legal.
8319 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8320 TargetLowering::TypePromoteInteger) {
8321 EVT LegalizedStoredValueTy =
8322 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8323 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8324 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8325 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8326 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8327 LastLegalIntegerType = i+1;
8328 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008329 }
8330
8331 // Only use vector types if the vector type is larger than the integer type.
8332 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008333 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008334 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8335
8336 // We add +1 here because the LastXXX variables refer to location while
8337 // the NumElem refers to array/index size.
8338 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8339 NumElem = std::min(LastLegalType, NumElem);
8340
8341 if (NumElem < 2)
8342 return false;
8343
8344 // The earliest Node in the DAG.
8345 unsigned EarliestNodeUsed = 0;
8346 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8347 for (unsigned i=1; i<NumElem; ++i) {
8348 // Find a chain for the new wide-store operand. Notice that some
8349 // of the store nodes that we found may not be selected for inclusion
8350 // in the wide store. The chain we use needs to be the chain of the
8351 // earliest store node which is *used* and replaced by the wide store.
8352 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8353 EarliestNodeUsed = i;
8354 }
8355
8356 // Find if it is better to use vectors or integers to load and store
8357 // to memory.
8358 EVT JointMemOpVT;
8359 if (UseVectorTy) {
8360 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8361 } else {
8362 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8363 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8364 }
8365
Andrew Trickac6d9be2013-05-25 02:42:55 +00008366 SDLoc LoadDL(LoadNodes[0].MemNode);
8367 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008368
8369 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8370 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8371 FirstLoad->getChain(),
8372 FirstLoad->getBasePtr(),
8373 FirstLoad->getPointerInfo(),
8374 false, false, false,
8375 FirstLoad->getAlignment());
8376
8377 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8378 FirstInChain->getBasePtr(),
8379 FirstInChain->getPointerInfo(), false, false,
8380 FirstInChain->getAlignment());
8381
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008382 // Replace one of the loads with the new load.
8383 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8384 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8385 SDValue(NewLoad.getNode(), 1));
8386
8387 // Remove the rest of the load chains.
8388 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008389 // Replace all chain users of the old load nodes with the chain of the new
8390 // load node.
8391 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008392 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8393 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008394
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008395 // Replace the first store with the new store.
8396 CombineTo(EarliestOp, NewStore);
8397 // Erase all other stores.
8398 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008399 // Remove all Store nodes.
8400 if (StoreNodes[i].MemNode == EarliestOp)
8401 continue;
8402 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8403 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8404 removeFromWorkList(St);
8405 DAG.DeleteNode(St);
8406 }
8407
8408 return true;
8409}
8410
Dan Gohman475871a2008-07-27 21:46:04 +00008411SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008412 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008413 SDValue Chain = ST->getChain();
8414 SDValue Value = ST->getValue();
8415 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008416
Evan Cheng59d5b682007-05-07 21:27:48 +00008417 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008418 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008419 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008420 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008421 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008422 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008423 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008424 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008425 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008426 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008427 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008428 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008429 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008430 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008431 }
Owen Andersona34d9362011-04-14 17:30:49 +00008432
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008433 // Turn 'store undef, Ptr' -> nothing.
8434 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8435 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008436
Nate Begeman2cbba892006-12-11 02:23:46 +00008437 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008438 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008439 // NOTE: If the original store is volatile, this transform must not increase
8440 // the number of stores. For example, on x86-32 an f64 can be stored in one
8441 // processor operation but an i64 (which is not legal) requires two. So the
8442 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008443 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008444 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00008445 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008446 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008447 case MVT::f16: // We don't do this for these yet.
8448 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008449 case MVT::f128:
8450 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008451 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008452 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008453 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008454 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008455 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008456 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008457 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008458 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008459 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008460 }
8461 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008462 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008463 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008464 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008465 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008466 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008467 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008468 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008469 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008470 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008471 }
Owen Andersona34d9362011-04-14 17:30:49 +00008472
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008473 if (!ST->isVolatile() &&
8474 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008475 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008476 // argument passing. Since this is so common, custom legalize the
8477 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008478 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8480 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008481 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008482
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008483 unsigned Alignment = ST->getAlignment();
8484 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008485 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008486
Andrew Trickac6d9be2013-05-25 02:42:55 +00008487 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008488 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008489 isVolatile, isNonTemporal,
8490 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008491 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008492 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008493 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008494 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008495 Ptr, ST->getPointerInfo().getWithOffset(4),
8496 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008497 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008498 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008499 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008500 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008501
Chris Lattner62be1a72006-12-12 04:16:14 +00008502 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008503 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008504 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008505 }
8506
Evan Cheng255f20f2010-04-01 06:04:33 +00008507 // Try to infer better alignment information than the store already has.
8508 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008509 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8510 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008511 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008512 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8513 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008514 }
8515 }
8516
Evan Cheng31959b12011-02-02 01:06:55 +00008517 // Try transforming a pair floating point load / store ops to integer
8518 // load / store ops.
8519 SDValue NewST = TransformFPLoadStorePair(N);
8520 if (NewST.getNode())
8521 return NewST;
8522
Scott Michelfdc40a02009-02-17 22:15:04 +00008523 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008524 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008525 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008526
Jim Laskey6ff23e52006-10-04 16:53:27 +00008527 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008528 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008530
8531 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008532 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008533 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008534 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008535 ST->getMemoryVT(), ST->isVolatile(),
8536 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008537 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008538 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008539 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008540 ST->isVolatile(), ST->isNonTemporal(),
8541 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008542 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008543
Jim Laskey279f0532006-09-25 16:29:54 +00008544 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008545 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008546 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008547
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008548 // Make sure the new and old chains are cleaned up.
8549 AddToWorkList(Token.getNode());
8550
Jim Laskey274062c2006-10-13 23:32:28 +00008551 // Don't add users to work list.
8552 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008553 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008555
Evan Cheng33dbedc2006-11-05 09:31:14 +00008556 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008557 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008558 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008559
Chris Lattner3c872852007-12-29 06:26:16 +00008560 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008561 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008562 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008563 // See if we can simplify the input to this truncstore with knowledge that
8564 // only the low bits are being used. For example:
8565 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008566 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008567 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008568 APInt::getLowBitsSet(
8569 Value.getValueType().getScalarType().getSizeInBits(),
8570 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008571 AddToWorkList(Value.getNode());
8572 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008573 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008574 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008575 ST->isVolatile(), ST->isNonTemporal(),
8576 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008577
Chris Lattnere33544c2007-10-13 06:58:48 +00008578 // Otherwise, see if we can simplify the operation with
8579 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008580 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008581 APInt::getLowBitsSet(
8582 Value.getValueType().getScalarType().getSizeInBits(),
8583 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008584 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008586
Chris Lattner3c872852007-12-29 06:26:16 +00008587 // If this is a load followed by a store to the same location, then the store
8588 // is dead/noop.
8589 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008590 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008591 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008592 // There can't be any side effects between the load and store, such as
8593 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008594 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008595 // The store is dead, remove it.
8596 return Chain;
8597 }
8598 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008599
Chris Lattnerddf89562008-01-17 19:59:44 +00008600 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8601 // truncating store. We can do this even if this is already a truncstore.
8602 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008603 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008604 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008605 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008606 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008607 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008608 ST->isVolatile(), ST->isNonTemporal(),
8609 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008610 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008611
Nadav Rotemc653de62012-10-03 16:11:15 +00008612 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008613 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008614 if (!LegalTypes) {
8615 bool EverChanged = false;
8616
8617 do {
8618 // There can be multiple store sequences on the same chain.
8619 // Keep trying to merge store sequences until we are unable to do so
8620 // or until we merge the last store on the chain.
8621 bool Changed = MergeConsecutiveStores(ST);
8622 EverChanged |= Changed;
8623 if (!Changed) break;
8624 } while (ST->getOpcode() != ISD::DELETED_NODE);
8625
8626 if (EverChanged)
8627 return SDValue(N, 0);
8628 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008629
Evan Cheng8b944d32009-05-28 00:35:15 +00008630 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008631}
8632
Dan Gohman475871a2008-07-27 21:46:04 +00008633SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8634 SDValue InVec = N->getOperand(0);
8635 SDValue InVal = N->getOperand(1);
8636 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008637 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008638
Bob Wilson492fd452010-05-19 23:42:58 +00008639 // If the inserted element is an UNDEF, just use the input vector.
8640 if (InVal.getOpcode() == ISD::UNDEF)
8641 return InVec;
8642
Nadav Rotem609d54e2011-02-12 14:40:33 +00008643 EVT VT = InVec.getValueType();
8644
Owen Anderson95771af2011-02-25 21:41:48 +00008645 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008646 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8647 return SDValue();
8648
Eli Friedman9db817f2011-09-09 21:04:06 +00008649 // Check that we know which element is being inserted
8650 if (!isa<ConstantSDNode>(EltNo))
8651 return SDValue();
8652 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008653
Eli Friedman9db817f2011-09-09 21:04:06 +00008654 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8655 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8656 // vector elements.
8657 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00008658 // Do not combine these two vectors if the output vector will not replace
8659 // the input vector.
8660 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00008661 Ops.append(InVec.getNode()->op_begin(),
8662 InVec.getNode()->op_end());
8663 } else if (InVec.getOpcode() == ISD::UNDEF) {
8664 unsigned NElts = VT.getVectorNumElements();
8665 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8666 } else {
8667 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008668 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008669
8670 // Insert the element
8671 if (Elt < Ops.size()) {
8672 // All the operands of BUILD_VECTOR must have the same type;
8673 // we enforce that here.
8674 EVT OpVT = Ops[0].getValueType();
8675 if (InVal.getValueType() != OpVT)
8676 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8677 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8678 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8679 Ops[Elt] = InVal;
8680 }
8681
8682 // Return the new vector
8683 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8684 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008685}
8686
Dan Gohman475871a2008-07-27 21:46:04 +00008687SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008688 // (vextract (scalar_to_vector val, 0) -> val
8689 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008690 EVT VT = InVec.getValueType();
8691 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008692
Duncan Sandsc356f332011-05-09 08:03:33 +00008693 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8694 // Check if the result type doesn't match the inserted element type. A
8695 // SCALAR_TO_VECTOR may truncate the inserted element and the
8696 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8697 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008698 if (InOp.getValueType() != NVT) {
8699 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008700 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008701 }
8702 return InOp;
8703 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008704
Nadav Rotemba05c912012-01-17 21:44:01 +00008705 SDValue EltNo = N->getOperand(1);
8706 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8707
8708 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8709 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008710 // we may introduce new vector instructions which are not backed by TD
8711 // patterns. For example on AVX, extracting elements from a wide vector
8712 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008713 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8714 && ConstEltNo && !LegalOperations) {
8715 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8716 int NumElem = VT.getVectorNumElements();
8717 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8718 // Find the new index to extract from.
8719 int OrigElt = SVOp->getMaskElt(Elt);
8720
8721 // Extracting an undef index is undef.
8722 if (OrigElt == -1)
8723 return DAG.getUNDEF(NVT);
8724
8725 // Select the right vector half to extract from.
8726 if (OrigElt < NumElem) {
8727 InVec = InVec->getOperand(0);
8728 } else {
8729 InVec = InVec->getOperand(1);
8730 OrigElt -= NumElem;
8731 }
8732
Tom Stellard425b76c2013-08-05 22:22:01 +00008733 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008734 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008735 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008736 }
8737
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008738 // Perform only after legalization to ensure build_vector / vector_shuffle
8739 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008740 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008741
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008742 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8743 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8744 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008745
Nadav Rotemba05c912012-01-17 21:44:01 +00008746 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008747 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008748 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008749 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008750 EVT ExtVT = VT.getVectorElementType();
8751 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008752
Evan Cheng84387ea2012-03-13 22:00:52 +00008753 // If the result of load has to be truncated, then it's not necessarily
8754 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008755 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008756 return SDValue();
8757
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008758 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008759 // Don't duplicate a load with other uses.
8760 if (!InVec.hasOneUse())
8761 return SDValue();
8762
Owen Andersone50ed302009-08-10 22:56:29 +00008763 EVT BCVT = InVec.getOperand(0).getValueType();
8764 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008765 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008766 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8767 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008768 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008769 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008770 NewLoad = true;
8771 }
Evan Cheng513da432007-10-06 08:19:55 +00008772
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008773 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008774 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008775 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008776 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008777 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008778 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008779 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008780 // Don't duplicate a load with other uses.
8781 if (!InVec.hasOneUse())
8782 return SDValue();
8783
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008784 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008785 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008786 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8787 // =>
8788 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008789
Eli Friedmand6e25602011-12-26 22:49:32 +00008790 // Don't duplicate a load with other uses.
8791 if (!InVec.hasOneUse())
8792 return SDValue();
8793
Mon P Wanga60b5232008-12-11 00:26:16 +00008794 // If the bit convert changed the number of elements, it is unsafe
8795 // to examine the mask.
8796 if (BCNumEltsChanged)
8797 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008798
8799 // Select the input vector, guarding against out of range extract vector.
8800 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008801 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008802 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8803
Eli Friedmand6e25602011-12-26 22:49:32 +00008804 if (InVec.getOpcode() == ISD::BITCAST) {
8805 // Don't duplicate a load with other uses.
8806 if (!InVec.hasOneUse())
8807 return SDValue();
8808
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008809 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008810 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008811 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008812 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008813 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008814 }
8815 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008816
Eli Friedmand6e25602011-12-26 22:49:32 +00008817 // Make sure we found a non-volatile load and the extractelement is
8818 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008819 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008820 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008821
Eric Christopherd81f17a2010-11-03 20:44:42 +00008822 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8823 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008824 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008825
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008826 unsigned Align = LN0->getAlignment();
8827 if (NewLoad) {
8828 // Check the resultant load doesn't need a higher alignment than the
8829 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008830 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008831 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008832 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008833
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008834 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008835 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008836
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008837 Align = NewAlign;
8838 }
8839
Dan Gohman475871a2008-07-27 21:46:04 +00008840 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008841 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008842
Eric Christopherd81f17a2010-11-03 20:44:42 +00008843 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008844 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008845 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008846 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008847 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008848 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008849 DAG.getConstant(PtrOff, PtrType));
8850 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008851
Eli Friedman4db4add2011-11-16 23:50:22 +00008852 // The replacement we need to do here is a little tricky: we need to
8853 // replace an extractelement of a load with a load.
8854 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008855 // Note that this replacement assumes that the extractvalue is the only
8856 // use of the load; that's okay because we don't want to perform this
8857 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008858 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008859 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008860 if (NVT.bitsGT(LVT)) {
8861 // If the result type of vextract is wider than the load, then issue an
8862 // extending load instead.
8863 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8864 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008865 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008866 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8867 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008868 Chain = Load.getValue(1);
8869 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008870 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008871 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00008872 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008873 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008874 Chain = Load.getValue(1);
8875 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008876 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008877 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008878 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008879 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008880 WorkListRemover DeadNodes(*this);
8881 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008882 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008883 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008884 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8885 // worklist explicitly as well.
8886 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008887 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008888 // Make sure to revisit this node to clean it up; it will usually be dead.
8889 AddToWorkList(N);
8890 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008891 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008892
Dan Gohman475871a2008-07-27 21:46:04 +00008893 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008894}
Evan Cheng513da432007-10-06 08:19:55 +00008895
Michael Liaofac14ab2012-10-23 23:06:52 +00008896// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8897SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8898 // We perform this optimization post type-legalization because
8899 // the type-legalizer often scalarizes integer-promoted vectors.
8900 // Performing this optimization before may create bit-casts which
8901 // will be type-legalized to complex code sequences.
8902 // We perform this optimization only before the operation legalizer because we
8903 // may introduce illegal operations.
8904 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8905 return SDValue();
8906
Dan Gohman7f321562007-06-25 16:23:39 +00008907 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008908 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008909 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008910
Nadav Rotemb00418a2011-10-29 21:23:04 +00008911 // Check to see if this is a BUILD_VECTOR of a bunch of values
8912 // which come from any_extend or zero_extend nodes. If so, we can create
8913 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008914 // optimizations. We do not handle sign-extend because we can't fill the sign
8915 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008916 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008917 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008918
Craig Topperd3b58892012-01-17 09:09:48 +00008919 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008920 SDValue In = N->getOperand(i);
8921 // Ignore undef inputs.
8922 if (In.getOpcode() == ISD::UNDEF) continue;
8923
8924 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8925 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8926
Nadav Rotemf47368b2011-10-31 20:08:25 +00008927 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008928 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008929 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008930 break;
8931 }
8932
8933 // The input is a ZeroExt or AnyExt. Check the original type.
8934 EVT InTy = In.getOperand(0).getValueType();
8935
8936 // Check that all of the widened source types are the same.
8937 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008938 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008939 SourceType = InTy;
8940 else if (InTy != SourceType) {
8941 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008942 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008943 break;
8944 }
8945
8946 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008947 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008948 }
8949
Nadav Rotemf47368b2011-10-31 20:08:25 +00008950 // In order to have valid types, all of the inputs must be extended from the
8951 // same source type and all of the inputs must be any or zero extend.
8952 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008953 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008954 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008955 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8956 isPowerOf2_32(SourceType.getSizeInBits());
8957
Nadav Rotem6431ff92012-03-15 08:49:06 +00008958 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8959 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008960 if (!ValidTypes)
8961 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008962
Michael Liaofac14ab2012-10-23 23:06:52 +00008963 bool isLE = TLI.isLittleEndian();
8964 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8965 assert(ElemRatio > 1 && "Invalid element size ratio");
8966 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8967 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008968
Michael Liaofac14ab2012-10-23 23:06:52 +00008969 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8970 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008971
Michael Liaofac14ab2012-10-23 23:06:52 +00008972 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008973 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008974 SDValue Cast = N->getOperand(i);
8975 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8976 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8977 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8978 SDValue In;
8979 if (Cast.getOpcode() == ISD::UNDEF)
8980 In = DAG.getUNDEF(SourceType);
8981 else
8982 In = Cast->getOperand(0);
8983 unsigned Index = isLE ? (i * ElemRatio) :
8984 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008985
Michael Liaofac14ab2012-10-23 23:06:52 +00008986 assert(Index < Ops.size() && "Invalid index");
8987 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008988 }
Chris Lattnerca242442006-03-19 01:27:56 +00008989
Michael Liaofac14ab2012-10-23 23:06:52 +00008990 // The type of the new BUILD_VECTOR node.
8991 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8992 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8993 "Invalid vector size");
8994 // Check if the new vector type is legal.
8995 if (!isTypeLegal(VecVT)) return SDValue();
8996
8997 // Make the new BUILD_VECTOR.
8998 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
8999
9000 // The new BUILD_VECTOR node has the potential to be further optimized.
9001 AddToWorkList(BV.getNode());
9002 // Bitcast to the desired type.
9003 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9004}
9005
Michael Liao1a5cc712012-10-24 04:14:18 +00009006SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9007 EVT VT = N->getValueType(0);
9008
9009 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009010 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009011
9012 EVT SrcVT = MVT::Other;
9013 unsigned Opcode = ISD::DELETED_NODE;
9014 unsigned NumDefs = 0;
9015
9016 for (unsigned i = 0; i != NumInScalars; ++i) {
9017 SDValue In = N->getOperand(i);
9018 unsigned Opc = In.getOpcode();
9019
9020 if (Opc == ISD::UNDEF)
9021 continue;
9022
9023 // If all scalar values are floats and converted from integers.
9024 if (Opcode == ISD::DELETED_NODE &&
9025 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9026 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009027 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009028
Michael Liao1a5cc712012-10-24 04:14:18 +00009029 if (Opc != Opcode)
9030 return SDValue();
9031
9032 EVT InVT = In.getOperand(0).getValueType();
9033
9034 // If all scalar values are typed differently, bail out. It's chosen to
9035 // simplify BUILD_VECTOR of integer types.
9036 if (SrcVT == MVT::Other)
9037 SrcVT = InVT;
9038 if (SrcVT != InVT)
9039 return SDValue();
9040 NumDefs++;
9041 }
9042
9043 // If the vector has just one element defined, it's not worth to fold it into
9044 // a vectorized one.
9045 if (NumDefs < 2)
9046 return SDValue();
9047
9048 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9049 && "Should only handle conversion from integer to float.");
9050 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9051
9052 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009053
9054 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9055 return SDValue();
9056
Michael Liao1a5cc712012-10-24 04:14:18 +00009057 SmallVector<SDValue, 8> Opnds;
9058 for (unsigned i = 0; i != NumInScalars; ++i) {
9059 SDValue In = N->getOperand(i);
9060
9061 if (In.getOpcode() == ISD::UNDEF)
9062 Opnds.push_back(DAG.getUNDEF(SrcVT));
9063 else
9064 Opnds.push_back(In.getOperand(0));
9065 }
9066 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9067 &Opnds[0], Opnds.size());
9068 AddToWorkList(BV.getNode());
9069
9070 return DAG.getNode(Opcode, dl, VT, BV);
9071}
9072
Michael Liaofac14ab2012-10-23 23:06:52 +00009073SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9074 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009075 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009076 EVT VT = N->getValueType(0);
9077
9078 // A vector built entirely of undefs is undef.
9079 if (ISD::allOperandsUndef(N))
9080 return DAG.getUNDEF(VT);
9081
9082 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9083 if (V.getNode())
9084 return V;
9085
Michael Liao1a5cc712012-10-24 04:14:18 +00009086 V = reduceBuildVecConvertToConvertBuildVec(N);
9087 if (V.getNode())
9088 return V;
9089
Dan Gohman7f321562007-06-25 16:23:39 +00009090 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9091 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9092 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009093
9094 // May only combine to shuffle after legalize if shuffle is legal.
9095 if (LegalOperations &&
9096 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9097 return SDValue();
9098
Dan Gohman475871a2008-07-27 21:46:04 +00009099 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009100 for (unsigned i = 0; i != NumInScalars; ++i) {
9101 // Ignore undef inputs.
9102 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009103
Dan Gohman7f321562007-06-25 16:23:39 +00009104 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009105 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009106 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009107 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009108 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009109 break;
9110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009111
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009112 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009113 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009114 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9115 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009116
Gabor Greifba36cb52008-08-28 21:40:38 +00009117 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009118 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009119 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009120 VecIn2 = ExtractedFromVec;
9121 } else {
9122 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009123 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009124 break;
9125 }
9126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009127
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009128 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009129 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009130 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009131 for (unsigned i = 0; i != NumInScalars; ++i) {
9132 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009133 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009134 continue;
9135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009136
Rafael Espindola15684b22009-04-24 12:40:33 +00009137 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009138 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009139 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009140 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009141 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9142 if (ExtIndex > VT.getVectorNumElements())
9143 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009144
Nate Begeman5a5ca152009-04-29 05:20:52 +00009145 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009146 continue;
9147 }
9148
9149 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009150 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009151 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009152 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009153
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009154 // We can't generate a shuffle node with mismatched input and output types.
9155 // Attempt to transform a single input vector to the correct type.
9156 if ((VT != VecIn1.getValueType())) {
9157 // We don't support shuffeling between TWO values of different types.
9158 if (VecIn2.getNode() != 0)
9159 return SDValue();
9160
9161 // We only support widening of vectors which are half the size of the
9162 // output registers. For example XMM->YMM widening on X86 with AVX.
9163 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9164 return SDValue();
9165
James Molloy8cd08bf2012-09-10 14:01:21 +00009166 // If the input vector type has a different base type to the output
9167 // vector type, bail out.
9168 if (VecIn1.getValueType().getVectorElementType() !=
9169 VT.getVectorElementType())
9170 return SDValue();
9171
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009172 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009173 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009174 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009175 }
9176
9177 // If VecIn2 is unused then change it to undef.
9178 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9179
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009180 // Check that we were able to transform all incoming values to the same
9181 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009182 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9183 VecIn1.getValueType() != VT)
9184 return SDValue();
9185
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009186 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009187 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009188 return SDValue();
9189
Dan Gohman7f321562007-06-25 16:23:39 +00009190 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009191 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009192 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009193 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009194 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009196
Dan Gohman475871a2008-07-27 21:46:04 +00009197 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009198}
9199
Dan Gohman475871a2008-07-27 21:46:04 +00009200SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009201 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9202 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9203 // inputs come from at most two distinct vectors, turn this into a shuffle
9204 // node.
9205
9206 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009207 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009208 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009209
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009210 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009211 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009212 return DAG.getUNDEF(N->getValueType(0));
9213
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009214 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9215 // nodes often generate nop CONCAT_VECTOR nodes.
9216 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9217 // place the incoming vectors at the exact same location.
9218 SDValue SingleSource = SDValue();
9219 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9220
9221 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9222 SDValue Op = N->getOperand(i);
9223
9224 if (Op.getOpcode() == ISD::UNDEF)
9225 continue;
9226
9227 // Check if this is the identity extract:
9228 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9229 return SDValue();
9230
9231 // Find the single incoming vector for the extract_subvector.
9232 if (SingleSource.getNode()) {
9233 if (Op.getOperand(0) != SingleSource)
9234 return SDValue();
9235 } else {
9236 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009237
9238 // Check the source type is the same as the type of the result.
9239 // If not, this concat may extend the vector, so we can not
9240 // optimize it away.
9241 if (SingleSource.getValueType() != N->getValueType(0))
9242 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009243 }
9244
9245 unsigned IdentityIndex = i * PartNumElem;
9246 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9247 // The extract index must be constant.
9248 if (!CS)
9249 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009250
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009251 // Check that we are reading from the identity index.
9252 if (CS->getZExtValue() != IdentityIndex)
9253 return SDValue();
9254 }
9255
9256 if (SingleSource.getNode())
9257 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009258
Dan Gohman475871a2008-07-27 21:46:04 +00009259 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009260}
9261
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009262SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9263 EVT NVT = N->getValueType(0);
9264 SDValue V = N->getOperand(0);
9265
Michael Liao13429e22012-10-17 20:48:33 +00009266 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9267 // Combine:
9268 // (extract_subvec (concat V1, V2, ...), i)
9269 // Into:
9270 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009271 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9272 if (V->getOperand(0).getValueType() != NVT)
9273 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009274 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9275 unsigned NumElems = NVT.getVectorNumElements();
9276 assert((Idx % NumElems) == 0 &&
9277 "IDX in concat is not a multiple of the result vector length.");
9278 return V->getOperand(Idx / NumElems);
9279 }
9280
Michael Liaob4f98ea2013-03-25 23:47:35 +00009281 // Skip bitcasting
9282 if (V->getOpcode() == ISD::BITCAST)
9283 V = V.getOperand(0);
9284
9285 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009286 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009287 // Handle only simple case where vector being inserted and vector
9288 // being extracted are of same type, and are half size of larger vectors.
9289 EVT BigVT = V->getOperand(0).getValueType();
9290 EVT SmallVT = V->getOperand(1).getValueType();
9291 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9292 return SDValue();
9293
9294 // Only handle cases where both indexes are constants with the same type.
9295 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9296 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9297
9298 if (InsIdx && ExtIdx &&
9299 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9300 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9301 // Combine:
9302 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9303 // Into:
9304 // indices are equal or bit offsets are equal => V1
9305 // otherwise => (extract_subvec V1, ExtIdx)
9306 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9307 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9308 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9309 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9310 DAG.getNode(ISD::BITCAST, dl,
9311 N->getOperand(0).getValueType(),
9312 V->getOperand(0)), N->getOperand(1));
9313 }
9314 }
9315
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009316 return SDValue();
9317}
9318
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009319// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9320static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9321 EVT VT = N->getValueType(0);
9322 unsigned NumElts = VT.getVectorNumElements();
9323
9324 SDValue N0 = N->getOperand(0);
9325 SDValue N1 = N->getOperand(1);
9326 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9327
9328 SmallVector<SDValue, 4> Ops;
9329 EVT ConcatVT = N0.getOperand(0).getValueType();
9330 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9331 unsigned NumConcats = NumElts / NumElemsPerConcat;
9332
9333 // Look at every vector that's inserted. We're looking for exact
9334 // subvector-sized copies from a concatenated vector
9335 for (unsigned I = 0; I != NumConcats; ++I) {
9336 // Make sure we're dealing with a copy.
9337 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009338 bool AllUndef = true, NoUndef = true;
9339 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9340 if (SVN->getMaskElt(J) >= 0)
9341 AllUndef = false;
9342 else
9343 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009344 }
9345
Hao Liu3778c042013-05-13 02:07:05 +00009346 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009347 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9348 return SDValue();
9349
9350 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9351 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9352 return SDValue();
9353
9354 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9355 if (FirstElt < N0.getNumOperands())
9356 Ops.push_back(N0.getOperand(FirstElt));
9357 else
9358 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9359
9360 } else if (AllUndef) {
9361 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9362 } else { // Mixed with general masks and undefs, can't do optimization.
9363 return SDValue();
9364 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009365 }
9366
Andrew Trickac6d9be2013-05-25 02:42:55 +00009367 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009368 Ops.size());
9369}
9370
Dan Gohman475871a2008-07-27 21:46:04 +00009371SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009372 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009373 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009374
Mon P Wangaeb06d22008-11-10 04:46:22 +00009375 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009376 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009377
Craig Topperae1bec52012-04-09 05:16:56 +00009378 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009379
Craig Topper481b79c2012-01-04 08:07:43 +00009380 // Canonicalize shuffle undef, undef -> undef
9381 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9382 return DAG.getUNDEF(VT);
9383
9384 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9385
9386 // Canonicalize shuffle v, v -> v, undef
9387 if (N0 == N1) {
9388 SmallVector<int, 8> NewMask;
9389 for (unsigned i = 0; i != NumElts; ++i) {
9390 int Idx = SVN->getMaskElt(i);
9391 if (Idx >= (int)NumElts) Idx -= NumElts;
9392 NewMask.push_back(Idx);
9393 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009394 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009395 &NewMask[0]);
9396 }
9397
9398 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9399 if (N0.getOpcode() == ISD::UNDEF) {
9400 SmallVector<int, 8> NewMask;
9401 for (unsigned i = 0; i != NumElts; ++i) {
9402 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009403 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +00009404 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +00009405 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +00009406 else
9407 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +00009408 }
9409 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009410 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009411 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009412 &NewMask[0]);
9413 }
9414
9415 // Remove references to rhs if it is undef
9416 if (N1.getOpcode() == ISD::UNDEF) {
9417 bool Changed = false;
9418 SmallVector<int, 8> NewMask;
9419 for (unsigned i = 0; i != NumElts; ++i) {
9420 int Idx = SVN->getMaskElt(i);
9421 if (Idx >= (int)NumElts) {
9422 Idx = -1;
9423 Changed = true;
9424 }
9425 NewMask.push_back(Idx);
9426 }
9427 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009428 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009429 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009430
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009431 // If it is a splat, check if the argument vector is another splat or a
9432 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009433 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009434 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009435
Dan Gohman7f321562007-06-25 16:23:39 +00009436 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009437 // not the number of vector elements, look through it. Be careful not to
9438 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009439 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009440 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009441 if (ConvInput.getValueType().isVector() &&
9442 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009443 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009444 }
9445
Dan Gohman7f321562007-06-25 16:23:39 +00009446 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009447 assert(V->getNumOperands() == NumElts &&
9448 "BUILD_VECTOR has wrong number of operands");
9449 SDValue Base;
9450 bool AllSame = true;
9451 for (unsigned i = 0; i != NumElts; ++i) {
9452 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9453 Base = V->getOperand(i);
9454 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009455 }
Evan Cheng917ec982006-07-21 08:25:53 +00009456 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009457 // Splat of <u, u, u, u>, return <u, u, u, u>
9458 if (!Base.getNode())
9459 return N0;
9460 for (unsigned i = 0; i != NumElts; ++i) {
9461 if (V->getOperand(i) != Base) {
9462 AllSame = false;
9463 break;
9464 }
9465 }
9466 // Splat of <x, x, x, x>, return <x, x, x, x>
9467 if (AllSame)
9468 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009469 }
9470 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009471
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009472 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9473 Level < AfterLegalizeVectorOps &&
9474 (N1.getOpcode() == ISD::UNDEF ||
9475 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9476 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9477 SDValue V = partitionShuffleOfConcats(N, DAG);
9478
9479 if (V.getNode())
9480 return V;
9481 }
9482
Nadav Rotem4ac90812012-04-01 19:31:22 +00009483 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009484 // and it reverses the swizzle of the previous shuffle then we can
9485 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009486 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9487 N1.getOpcode() == ISD::UNDEF) {
9488
Nadav Rotem4ac90812012-04-01 19:31:22 +00009489 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9490
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009491 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9492 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9493 return SDValue();
9494
Craig Topperae1bec52012-04-09 05:16:56 +00009495 // The incoming shuffle must be of the same type as the result of the
9496 // current shuffle.
9497 assert(OtherSV->getOperand(0).getValueType() == VT &&
9498 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009499
9500 for (unsigned i = 0; i != NumElts; ++i) {
9501 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009502 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009503 // Next, this index comes from the first value, which is the incoming
9504 // shuffle. Adopt the incoming index.
9505 if (Idx >= 0)
9506 Idx = OtherSV->getMaskElt(Idx);
9507
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009508 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009509 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009510 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009511 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009512
9513 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009514 }
9515
Dan Gohman475871a2008-07-27 21:46:04 +00009516 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009517}
9518
Evan Cheng44f1f092006-04-20 08:56:16 +00009519/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009520/// an AND to a vector_shuffle with the destination vector and a zero vector.
9521/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009522/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009523SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009524 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009525 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009526 SDValue LHS = N->getOperand(0);
9527 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009528 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009529 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009530 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009531 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009532 SmallVector<int, 8> Indices;
9533 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009534 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009535 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009536 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009537 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009538
9539 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009540 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009541 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009542 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009543 else
Dan Gohman475871a2008-07-27 21:46:04 +00009544 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009545 }
9546
9547 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009548 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009549 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009550 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009551
Dan Gohman7f321562007-06-25 16:23:39 +00009552 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009553 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009554 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009555 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009556 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009557 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009558 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009559 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009560 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009561 }
9562 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009563
Dan Gohman475871a2008-07-27 21:46:04 +00009564 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009565}
9566
Dan Gohman7f321562007-06-25 16:23:39 +00009567/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009568SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009569 assert(N->getValueType(0).isVector() &&
9570 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009571
Dan Gohman475871a2008-07-27 21:46:04 +00009572 SDValue LHS = N->getOperand(0);
9573 SDValue RHS = N->getOperand(1);
9574 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009575 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009576
Dan Gohman7f321562007-06-25 16:23:39 +00009577 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009578 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009579 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009580 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009581 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009582 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009583 SDValue LHSOp = LHS.getOperand(i);
9584 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009585 // If these two elements can't be folded, bail out.
9586 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9587 LHSOp.getOpcode() != ISD::Constant &&
9588 LHSOp.getOpcode() != ISD::ConstantFP) ||
9589 (RHSOp.getOpcode() != ISD::UNDEF &&
9590 RHSOp.getOpcode() != ISD::Constant &&
9591 RHSOp.getOpcode() != ISD::ConstantFP))
9592 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009593
Evan Cheng7b336a82006-05-31 06:08:35 +00009594 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009595 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9596 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009597 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009598 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009599 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009600 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009601 break;
9602 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009603
Bob Wilsond7273432010-12-17 23:06:49 +00009604 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009605 EVT RVT = RHSOp.getValueType();
9606 if (RVT != VT) {
9607 // Integer BUILD_VECTOR operands may have types larger than the element
9608 // size (e.g., when the element type is not legal). Prior to type
9609 // legalization, the types may not match between the two BUILD_VECTORS.
9610 // Truncate one of the operands to make them match.
9611 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009612 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009613 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009614 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009615 VT = RVT;
9616 }
9617 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009618 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009619 LHSOp, RHSOp);
9620 if (FoldOp.getOpcode() != ISD::UNDEF &&
9621 FoldOp.getOpcode() != ISD::Constant &&
9622 FoldOp.getOpcode() != ISD::ConstantFP)
9623 break;
9624 Ops.push_back(FoldOp);
9625 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009627
Bob Wilsond7273432010-12-17 23:06:49 +00009628 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009629 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009630 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009632
Dan Gohman475871a2008-07-27 21:46:04 +00009633 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009634}
9635
Craig Topperdd201ff2012-09-11 01:45:21 +00009636/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9637SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009638 assert(N->getValueType(0).isVector() &&
9639 "SimplifyVUnaryOp only works on vectors!");
9640
9641 SDValue N0 = N->getOperand(0);
9642
9643 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9644 return SDValue();
9645
9646 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9647 SmallVector<SDValue, 8> Ops;
9648 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9649 SDValue Op = N0.getOperand(i);
9650 if (Op.getOpcode() != ISD::UNDEF &&
9651 Op.getOpcode() != ISD::ConstantFP)
9652 break;
9653 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009654 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009655 if (FoldOp.getOpcode() != ISD::UNDEF &&
9656 FoldOp.getOpcode() != ISD::ConstantFP)
9657 break;
9658 Ops.push_back(FoldOp);
9659 AddToWorkList(FoldOp.getNode());
9660 }
9661
9662 if (Ops.size() != N0.getNumOperands())
9663 return SDValue();
9664
Andrew Trickac6d9be2013-05-25 02:42:55 +00009665 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009666 N0.getValueType(), &Ops[0], Ops.size());
9667}
9668
Andrew Trickac6d9be2013-05-25 02:42:55 +00009669SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009670 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009671 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009672
Bill Wendling836ca7d2009-01-30 23:59:18 +00009673 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009674 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009675
Nate Begemanf845b452005-10-08 00:29:44 +00009676 // If we got a simplified select_cc node back from SimplifySelectCC, then
9677 // break it down into a new SETCC node, and a new SELECT node, and then return
9678 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009679 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009680 // Check to see if we got a select_cc back (to turn into setcc/select).
9681 // Otherwise, just return whatever node we got back, like fabs.
9682 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009683 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009684 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009685 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009686 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009687 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009688 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9689 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009690 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009691
Nate Begemanf845b452005-10-08 00:29:44 +00009692 return SCC;
9693 }
Dan Gohman475871a2008-07-27 21:46:04 +00009694 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009695}
9696
Chris Lattner40c62d52005-10-18 06:04:22 +00009697/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9698/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009699/// select. Callers of this should assume that TheSelect is deleted if this
9700/// returns true. As such, they should return the appropriate thing (e.g. the
9701/// node) back to the top-level of the DAG combiner loop to avoid it being
9702/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009703bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009704 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009705
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009706 // Cannot simplify select with vector condition
9707 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9708
Chris Lattner40c62d52005-10-18 06:04:22 +00009709 // If this is a select from two identical things, try to pull the operation
9710 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009711 if (LHS.getOpcode() != RHS.getOpcode() ||
9712 !LHS.hasOneUse() || !RHS.hasOneUse())
9713 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009714
Chris Lattner18061612010-09-21 15:46:59 +00009715 // If this is a load and the token chain is identical, replace the select
9716 // of two loads with a load through a select of the address to load from.
9717 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9718 // constants have been dropped into the constant pool.
9719 if (LHS.getOpcode() == ISD::LOAD) {
9720 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9721 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009722
Chris Lattner18061612010-09-21 15:46:59 +00009723 // Token chains must be identical.
9724 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009725 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009726 LLD->isVolatile() || RLD->isVolatile() ||
9727 // If this is an EXTLOAD, the VT's must match.
9728 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009729 // If this is an EXTLOAD, the kind of extension must match.
9730 (LLD->getExtensionType() != RLD->getExtensionType() &&
9731 // The only exception is if one of the extensions is anyext.
9732 LLD->getExtensionType() != ISD::EXTLOAD &&
9733 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009734 // FIXME: this discards src value information. This is
9735 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009736 // both potential memory locations. Since we are discarding
9737 // src value info, don't do the transformation if the memory
9738 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009739 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009740 RLD->getPointerInfo().getAddrSpace() != 0 ||
9741 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9742 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009743 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009744
Chris Lattnerf1658062010-09-21 15:58:55 +00009745 // Check that the select condition doesn't reach either load. If so,
9746 // folding this will induce a cycle into the DAG. If not, this is safe to
9747 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009748 SDValue Addr;
9749 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009750 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9751 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9752 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9753 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009754 // The loads must not depend on one another.
9755 if (LLD->isPredecessorOf(RLD) ||
9756 RLD->isPredecessorOf(LLD))
9757 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009758 Addr = DAG.getSelect(SDLoc(TheSelect),
9759 LLD->getBasePtr().getValueType(),
9760 TheSelect->getOperand(0), LLD->getBasePtr(),
9761 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009762 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009763 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9764 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9765
9766 if ((LLD->hasAnyUseOfValue(1) &&
9767 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009768 (RLD->hasAnyUseOfValue(1) &&
9769 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009770 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009771
Andrew Trickac6d9be2013-05-25 02:42:55 +00009772 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009773 LLD->getBasePtr().getValueType(),
9774 TheSelect->getOperand(0),
9775 TheSelect->getOperand(1),
9776 LLD->getBasePtr(), RLD->getBasePtr(),
9777 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009778 }
9779
Chris Lattnerf1658062010-09-21 15:58:55 +00009780 SDValue Load;
9781 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9782 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009783 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009784 // FIXME: Discards pointer info.
9785 LLD->getChain(), Addr, MachinePointerInfo(),
9786 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009787 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009788 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009789 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9790 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009791 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009792 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009793 // FIXME: Discards pointer info.
9794 LLD->getChain(), Addr, MachinePointerInfo(),
9795 LLD->getMemoryVT(), LLD->isVolatile(),
9796 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009797 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009798
9799 // Users of the select now use the result of the load.
9800 CombineTo(TheSelect, Load);
9801
9802 // Users of the old loads now use the new load's chain. We know the
9803 // old-load value is dead now.
9804 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9805 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9806 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009808
Chris Lattner40c62d52005-10-18 06:04:22 +00009809 return false;
9810}
9811
Chris Lattner600fec32009-03-11 05:08:08 +00009812/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9813/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009814SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009815 SDValue N2, SDValue N3,
9816 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009817 // (x ? y : y) -> y.
9818 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009819
Owen Andersone50ed302009-08-10 22:56:29 +00009820 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009821 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9822 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9823 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009824
9825 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009826 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009827 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009828 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9829 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009830
9831 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009832 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009833 return N2;
9834 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009835 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009836 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009837
Nate Begemanf845b452005-10-08 00:29:44 +00009838 // Check to see if we can simplify the select into an fabs node
9839 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9840 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009841 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009842 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9843 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9844 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9845 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009846 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009847
Nate Begemanf845b452005-10-08 00:29:44 +00009848 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9849 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9850 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9851 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009852 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009853 }
9854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009855
Chris Lattner600fec32009-03-11 05:08:08 +00009856 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9857 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9858 // in it. This is a win when the constant is not otherwise available because
9859 // it replaces two constant pool loads with one. We only do this if the FP
9860 // type is known to be legal, because if it isn't, then we are before legalize
9861 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009862 // messing with soft float) and if the ConstantFP is not legal, because if
9863 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009864 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9865 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9866 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009867 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9868 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009869 // If both constants have multiple uses, then we won't need to do an
9870 // extra load, they are likely around in registers for other users.
9871 (TV->hasOneUse() || FV->hasOneUse())) {
9872 Constant *Elts[] = {
9873 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9874 const_cast<ConstantFP*>(TV->getConstantFPValue())
9875 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009876 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009877 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009878
Chris Lattner600fec32009-03-11 05:08:08 +00009879 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009880 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009881 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9882 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009883 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009884
9885 // Get the offsets to the 0 and 1 element of the array so that we can
9886 // select between them.
9887 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009888 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009889 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009890
Chris Lattner600fec32009-03-11 05:08:08 +00009891 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009892 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009893 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009894 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009895 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9896 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009897 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +00009898 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +00009899 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009900 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009901 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009902 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009903 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009904
9905 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009907
Nate Begemanf845b452005-10-08 00:29:44 +00009908 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009909 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009910 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009911 (N1C->isNullValue() || // (a < 0) ? b : 0
9912 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009913 EVT XType = N0.getValueType();
9914 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009915 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009916 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009917 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009918 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9919 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009920 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009921 SDValue ShCt = DAG.getConstant(ShCtV,
9922 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009923 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009924 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009925 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009926
Duncan Sands8e4eb092008-06-08 20:54:56 +00009927 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009928 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009929 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009930 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009931
9932 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009933 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009934
Andrew Trickac6d9be2013-05-25 02:42:55 +00009935 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009936 XType, N0,
9937 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009938 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009939 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009940
Duncan Sands8e4eb092008-06-08 20:54:56 +00009941 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009942 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009943 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009944 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009945
9946 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009947 }
9948 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009949
Owen Andersoned1088a2010-09-22 22:58:22 +00009950 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9951 // where y is has a single bit set.
9952 // A plaintext description would be, we can turn the SELECT_CC into an AND
9953 // when the condition can be materialized as an all-ones register. Any
9954 // single bit-test can be materialized as an all-ones register with
9955 // shift-left and shift-right-arith.
9956 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9957 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009958 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009959 N2C && N2C->isNullValue()) {
9960 SDValue AndLHS = N0->getOperand(0);
9961 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9962 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9963 // Shift the tested bit over the sign bit.
9964 APInt AndMask = ConstAndRHS->getAPIntValue();
9965 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009966 DAG.getConstant(AndMask.countLeadingZeros(),
9967 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009968 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009969
Owen Andersoned1088a2010-09-22 22:58:22 +00009970 // Now arithmetic right shift it all the way over, so the result is either
9971 // all-ones, or zero.
9972 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009973 DAG.getConstant(AndMask.getBitWidth()-1,
9974 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009975 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009976
Owen Andersoned1088a2010-09-22 22:58:22 +00009977 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9978 }
9979 }
9980
Nate Begeman07ed4172005-10-10 21:26:48 +00009981 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009982 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009983 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9984 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009985
Chris Lattner1eba01e2007-04-11 06:50:51 +00009986 // If the caller doesn't want us to simplify this into a zext of a compare,
9987 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009988 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009989 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009990
Nate Begeman07ed4172005-10-10 21:26:48 +00009991 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009992 // NOTE: Don't create a SETCC if it's not legal on this target.
9993 if (!LegalOperations ||
9994 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00009995 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009996 SDValue Temp, SCC;
9997 // cast from setcc result type to select result type
9998 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +00009999 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010000 N0, N1, CC);
10001 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010002 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010003 N2.getValueType());
10004 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010005 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010006 N2.getValueType(), SCC);
10007 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010008 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10009 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010010 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010011 }
10012
10013 AddToWorkList(SCC.getNode());
10014 AddToWorkList(Temp.getNode());
10015
10016 if (N2C->getAPIntValue() == 1)
10017 return Temp;
10018
10019 // shl setcc result by log2 n2c
10020 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
10021 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10022 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010023 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010024 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010025
Nate Begemanf845b452005-10-08 00:29:44 +000010026 // Check to see if this is the equivalent of setcc
10027 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10028 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010029 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010030 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010031 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010032 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10033 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010034 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010035 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010036 return Res;
10037 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010038
Bill Wendling836ca7d2009-01-30 23:59:18 +000010039 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010040 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010041 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010042 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010043 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010044 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010045 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010046 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010047 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010048 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010049 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010050 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010051 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010052 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010053 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010054 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010055 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010056 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010057 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010058 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010059 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010060 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010061 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010062 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010063 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010064 }
10065 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010066
Benjamin Kramercde51102010-07-08 12:09:56 +000010067 // Check to see if this is an integer abs.
10068 // select_cc setg[te] X, 0, X, -X ->
10069 // select_cc setgt X, -1, X, -X ->
10070 // select_cc setl[te] X, 0, -X, X ->
10071 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010072 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010073 if (N1C) {
10074 ConstantSDNode *SubC = NULL;
10075 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10076 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10077 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10078 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10079 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10080 (N1C->isOne() && CC == ISD::SETLT)) &&
10081 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10082 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10083
Owen Andersone50ed302009-08-10 22:56:29 +000010084 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010085 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010086 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010087 N0,
10088 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010089 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010090 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010091 XType, N0, Shift);
10092 AddToWorkList(Shift.getNode());
10093 AddToWorkList(Add.getNode());
10094 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010095 }
10096 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010097
Dan Gohman475871a2008-07-27 21:46:04 +000010098 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010099}
10100
Evan Chengfa1eb272007-02-08 22:13:59 +000010101/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010102SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010103 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010104 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010105 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010106 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010107 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010108}
10109
Nate Begeman69575232005-10-20 02:15:44 +000010110/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10111/// return a DAG expression to select that will generate the same value by
10112/// multiplying by a magic number. See:
10113/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010114SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010115 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010116 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010117
Andrew Lenharth232c9102006-06-12 16:07:18 +000010118 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010119 ii != ee; ++ii)
10120 AddToWorkList(*ii);
10121 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010122}
10123
10124/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10125/// return a DAG expression to select that will generate the same value by
10126/// multiplying by a magic number. See:
10127/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010128SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010129 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010130 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010131
Andrew Lenharth232c9102006-06-12 16:07:18 +000010132 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010133 ii != ee; ++ii)
10134 AddToWorkList(*ii);
10135 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010136}
10137
Nate Begemancc66cdd2009-09-25 06:05:26 +000010138/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010139// to alias with anything but itself. Provides base object and offset as
10140// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010141static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010142 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010143 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010144 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010145
Jim Laskey71382342006-10-07 23:37:56 +000010146 // If it's an adding a simple constant then integrate the offset.
10147 if (Base.getOpcode() == ISD::ADD) {
10148 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10149 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010150 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010151 }
10152 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010153
Nate Begemancc66cdd2009-09-25 06:05:26 +000010154 // Return the underlying GlobalValue, and update the Offset. Return false
10155 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10156 // by multiple nodes with different offsets.
10157 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10158 GV = G->getGlobal();
10159 Offset += G->getOffset();
10160 return false;
10161 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010162
Nate Begemancc66cdd2009-09-25 06:05:26 +000010163 // Return the underlying Constant value, and update the Offset. Return false
10164 // for ConstantSDNodes since the same constant pool entry may be represented
10165 // by multiple nodes with different offsets.
10166 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010167 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10168 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010169 Offset += C->getOffset();
10170 return false;
10171 }
Jim Laskey71382342006-10-07 23:37:56 +000010172 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010173 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010174}
10175
10176/// isAlias - Return true if there is any possibility that the two addresses
10177/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010178bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010179 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010180 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010181 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010182 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010183 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010184 unsigned SrcValueAlign2,
10185 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010186 // If they are the same then they must be aliases.
10187 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010188
Jim Laskey71382342006-10-07 23:37:56 +000010189 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010190 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010191 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010192 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010193 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010194 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10195 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010196
Nate Begemancc66cdd2009-09-25 06:05:26 +000010197 // If they have a same base address then check to see if they overlap.
10198 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010199 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010200
Owen Anderson4a9f1502010-09-20 20:39:59 +000010201 // It is possible for different frame indices to alias each other, mostly
10202 // when tail call optimization reuses return address slots for arguments.
10203 // To catch this case, look up the actual index of frame indices to compute
10204 // the real alias relationship.
10205 if (isFrameIndex1 && isFrameIndex2) {
10206 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10207 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10208 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10209 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10210 }
10211
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010212 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010213 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010214 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10215 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010216
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010217 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10218 // compared to the size and offset of the access, we may be able to prove they
10219 // do not alias. This check is conservative for now to catch cases created by
10220 // splitting vector types.
10221 if ((SrcValueAlign1 == SrcValueAlign2) &&
10222 (SrcValueOffset1 != SrcValueOffset2) &&
10223 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10224 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10225 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010226
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010227 // There is no overlap between these relatively aligned accesses of similar
10228 // size, return no alias.
10229 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10230 return false;
10231 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010232
Jim Laskey07a27092006-10-18 19:08:31 +000010233 if (CombinerGlobalAA) {
10234 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010235 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10236 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10237 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010238 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010239 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10240 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010241 if (AAResult == AliasAnalysis::NoAlias)
10242 return false;
10243 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010244
10245 // Otherwise we have to assume they alias.
10246 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010247}
10248
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010249bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10250 SDValue Ptr0, Ptr1;
10251 int64_t Size0, Size1;
10252 const Value *SrcValue0, *SrcValue1;
10253 int SrcValueOffset0, SrcValueOffset1;
10254 unsigned SrcValueAlign0, SrcValueAlign1;
10255 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10256 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10257 SrcValueAlign0, SrcTBAAInfo0);
10258 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10259 SrcValueAlign1, SrcTBAAInfo1);
10260 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010261 SrcValueAlign0, SrcTBAAInfo0,
10262 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10263 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010264}
10265
Jim Laskey71382342006-10-07 23:37:56 +000010266/// FindAliasInfo - Extracts the relevant alias information from the memory
10267/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010268bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010269 SDValue &Ptr, int64_t &Size,
10270 const Value *&SrcValue,
10271 int &SrcValueOffset,
10272 unsigned &SrcValueAlign,
10273 const MDNode *&TBAAInfo) const {
10274 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10275
10276 Ptr = LS->getBasePtr();
10277 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10278 SrcValue = LS->getSrcValue();
10279 SrcValueOffset = LS->getSrcValueOffset();
10280 SrcValueAlign = LS->getOriginalAlignment();
10281 TBAAInfo = LS->getTBAAInfo();
10282 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010283}
10284
Jim Laskey6ff23e52006-10-04 16:53:27 +000010285/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10286/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010287void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010288 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010289 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010290 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010291
Jim Laskey279f0532006-09-25 16:29:54 +000010292 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010293 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010294 int64_t Size;
10295 const Value *SrcValue;
10296 int SrcValueOffset;
10297 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010298 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010299 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010300 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010301
Jim Laskey6ff23e52006-10-04 16:53:27 +000010302 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010303 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010304 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010305
Jim Laskeybc588b82006-10-05 15:07:25 +000010306 // Look at each chain and determine if it is an alias. If so, add it to the
10307 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010308 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010309 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010310 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010311 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010312
10313 // For TokenFactor nodes, look at each operand and only continue up the
10314 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010315 // find more and revert to original chain since the xform is unlikely to be
10316 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010317 //
10318 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010319 // chain we found before we hit a tokenfactor rather than the original
10320 // chain.
10321 if (Depth > 6 || Aliases.size() == 2) {
10322 Aliases.clear();
10323 Aliases.push_back(OriginalChain);
10324 break;
10325 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010326
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010327 // Don't bother if we've been before.
10328 if (!Visited.insert(Chain.getNode()))
10329 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010330
Jim Laskeybc588b82006-10-05 15:07:25 +000010331 switch (Chain.getOpcode()) {
10332 case ISD::EntryToken:
10333 // Entry token is ideal chain operand, but handled in FindBetterChain.
10334 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010335
Jim Laskeybc588b82006-10-05 15:07:25 +000010336 case ISD::LOAD:
10337 case ISD::STORE: {
10338 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010339 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010340 int64_t OpSize;
10341 const Value *OpSrcValue;
10342 int OpSrcValueOffset;
10343 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010344 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010345 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010346 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010347 OpSrcValueAlign,
10348 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010349
Jim Laskeybc588b82006-10-05 15:07:25 +000010350 // If chain is alias then stop here.
10351 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010352 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010353 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010354 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010355 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010356 Aliases.push_back(Chain);
10357 } else {
10358 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010359 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010360 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010361 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010362 break;
10363 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010364
Jim Laskeybc588b82006-10-05 15:07:25 +000010365 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010366 // We have to check each of the operands of the token factor for "small"
10367 // token factors, so we queue them up. Adding the operands to the queue
10368 // (stack) in reverse order maintains the original order and increases the
10369 // likelihood that getNode will find a matching token factor (CSE.)
10370 if (Chain.getNumOperands() > 16) {
10371 Aliases.push_back(Chain);
10372 break;
10373 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010374 for (unsigned n = Chain.getNumOperands(); n;)
10375 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010376 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010377 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010378
Jim Laskeybc588b82006-10-05 15:07:25 +000010379 default:
10380 // For all other instructions we will just have to take what we can get.
10381 Aliases.push_back(Chain);
10382 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010383 }
10384 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010385}
10386
10387/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10388/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010389SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10390 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010391
Jim Laskey6ff23e52006-10-04 16:53:27 +000010392 // Accumulate all the aliases to this node.
10393 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010394
Dan Gohman71dc7c92011-05-17 22:20:36 +000010395 // If no operands then chain to entry token.
10396 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010397 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010398
10399 // If a single operand then chain to it. We don't need to revisit it.
10400 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010401 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010402
Jim Laskey6ff23e52006-10-04 16:53:27 +000010403 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010404 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010405 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010406}
10407
Nate Begeman1d4d4142005-09-01 00:19:25 +000010408// SelectionDAG::Combine - This is the entry point for the file.
10409//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010410void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010411 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010412 /// run - This is the main entry point to this class.
10413 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010414 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010415}