blob: ee6c2a39f3ed71c2b351ce6abc4e272afdc009f8 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner600fec32009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner00161a62008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000026#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000030#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/Statistic.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000035#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000037#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000038using namespace llvm;
39
Chris Lattnercd3245a2006-12-19 22:41:21 +000040STATISTIC(NodesCombined , "Number of dag nodes combined");
41STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
42STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000043STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000044STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000045
Nate Begeman1d4d4142005-09-01 00:19:25 +000046namespace {
Jim Laskey71382342006-10-07 23:37:56 +000047 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000048 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000049 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000050
Jim Laskey07a27092006-10-18 19:08:31 +000051 static cl::opt<bool>
52 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
53 cl::desc("Include global information in alias analysis"));
54
Jim Laskeybc588b82006-10-05 15:07:25 +000055//------------------------------ DAGCombiner ---------------------------------//
56
Nick Lewycky6726b6d2009-10-25 06:33:48 +000057 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000058 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000059 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000060 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 bool LegalOperations;
63 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000064
65 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000066 //
67 // This has the semantics that when adding to the worklist,
68 // the item added must be next to be processed. It should
69 // also only appear once. The naive approach to this takes
70 // linear time.
71 //
72 // To reduce the insert/remove time to logarithmic, we use
73 // a set and a vector to maintain our worklist.
74 //
75 // The set contains the items on the worklist, but does not
76 // maintain the order they should be visited.
77 //
78 // The vector maintains the order nodes should be visited, but may
79 // contain duplicate or removed nodes. When choosing a node to
80 // visit, we pop off the order stack until we find an item that is
81 // also in the contents set. All operations are O(log N).
82 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000083 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000084
Jim Laskeyc7c3f112006-10-16 20:52:31 +000085 // AA - Used for DAG load/store alias analysis.
86 AliasAnalysis &AA;
87
Nate Begeman1d4d4142005-09-01 00:19:25 +000088 /// AddUsersToWorkList - When an instruction is simplified, add all users of
89 /// the instruction to the work lists because they might get more simplified
90 /// now.
91 ///
92 void AddUsersToWorkList(SDNode *N) {
93 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000094 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000095 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 }
97
Dan Gohman389079b2007-10-08 17:57:15 +000098 /// visit - call the node-specific routine that knows how to fold each
99 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000101
Chris Lattner24664722006-03-01 04:53:38 +0000102 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000103 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000104 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000105 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000106 WorkListContents.insert(N);
107 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000108 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000109
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000110 /// removeFromWorkList - remove all instances of N from the worklist.
111 ///
112 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000113 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000114 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Dan Gohman475871a2008-07-27 21:46:04 +0000116 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000117 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Dan Gohman475871a2008-07-27 21:46:04 +0000119 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000120 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000124 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000126 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000127 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000128
129 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000130
131 private:
132
Chris Lattner012f2412006-02-17 21:58:01 +0000133 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000134 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000135 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000137 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
138 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000139 return SimplifyDemandedBits(Op, Demanded);
140 }
141
Dan Gohman475871a2008-07-27 21:46:04 +0000142 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000143
Chris Lattner448f2192006-11-11 00:39:41 +0000144 bool CombineToPreIndexedLoadStore(SDNode *N);
145 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000146
Evan Cheng95c57ea2010-04-24 04:43:44 +0000147 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
148 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
149 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
150 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000151 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000152 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000153 SDValue PromoteExtend(SDValue Op);
154 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000156 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
157 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
158 ISD::NodeType ExtType);
159
Dan Gohman389079b2007-10-08 17:57:15 +0000160 /// combine - call the node-specific routine that knows how to fold each
161 /// particular type of node. If that doesn't do anything, try the
162 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164
165 // Visitation implementation - Implement dag node combining for different
166 // node types. The semantics are as follows:
167 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000168 // SDValue.getNode() == 0 - No change was made
169 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
170 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000171 //
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDValue visitTokenFactor(SDNode *N);
173 SDValue visitMERGE_VALUES(SDNode *N);
174 SDValue visitADD(SDNode *N);
175 SDValue visitSUB(SDNode *N);
176 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000177 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000179 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000180 SDValue visitMUL(SDNode *N);
181 SDValue visitSDIV(SDNode *N);
182 SDValue visitUDIV(SDNode *N);
183 SDValue visitSREM(SDNode *N);
184 SDValue visitUREM(SDNode *N);
185 SDValue visitMULHU(SDNode *N);
186 SDValue visitMULHS(SDNode *N);
187 SDValue visitSMUL_LOHI(SDNode *N);
188 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000189 SDValue visitSMULO(SDNode *N);
190 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue visitSDIVREM(SDNode *N);
192 SDValue visitUDIVREM(SDNode *N);
193 SDValue visitAND(SDNode *N);
194 SDValue visitOR(SDNode *N);
195 SDValue visitXOR(SDNode *N);
196 SDValue SimplifyVBinOp(SDNode *N);
197 SDValue visitSHL(SDNode *N);
198 SDValue visitSRA(SDNode *N);
199 SDValue visitSRL(SDNode *N);
200 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000201 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTPOP(SDNode *N);
205 SDValue visitSELECT(SDNode *N);
206 SDValue visitSELECT_CC(SDNode *N);
207 SDValue visitSETCC(SDNode *N);
208 SDValue visitSIGN_EXTEND(SDNode *N);
209 SDValue visitZERO_EXTEND(SDNode *N);
210 SDValue visitANY_EXTEND(SDNode *N);
211 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
212 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000213 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000214 SDValue visitBUILD_PAIR(SDNode *N);
215 SDValue visitFADD(SDNode *N);
216 SDValue visitFSUB(SDNode *N);
217 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000218 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue visitFDIV(SDNode *N);
220 SDValue visitFREM(SDNode *N);
221 SDValue visitFCOPYSIGN(SDNode *N);
222 SDValue visitSINT_TO_FP(SDNode *N);
223 SDValue visitUINT_TO_FP(SDNode *N);
224 SDValue visitFP_TO_SINT(SDNode *N);
225 SDValue visitFP_TO_UINT(SDNode *N);
226 SDValue visitFP_ROUND(SDNode *N);
227 SDValue visitFP_ROUND_INREG(SDNode *N);
228 SDValue visitFP_EXTEND(SDNode *N);
229 SDValue visitFNEG(SDNode *N);
230 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000231 SDValue visitFCEIL(SDNode *N);
232 SDValue visitFTRUNC(SDNode *N);
233 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000234 SDValue visitBRCOND(SDNode *N);
235 SDValue visitBR_CC(SDNode *N);
236 SDValue visitLOAD(SDNode *N);
237 SDValue visitSTORE(SDNode *N);
238 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
239 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
240 SDValue visitBUILD_VECTOR(SDNode *N);
241 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000242 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000243 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Jim Grosbach9a526492010-06-23 16:07:42 +0000244 SDValue visitMEMBARRIER(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000245
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000247 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000248
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
252 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000253 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000254 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
255 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000256 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000257 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000258 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000260 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000261 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue BuildSDIV(SDNode *N);
264 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000265 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
266 bool DemandHighBits = true);
267 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000268 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000269 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000270 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000271 SDValue TransformFPLoadStorePair(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Dan Gohman475871a2008-07-27 21:46:04 +0000273 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Jim Laskey6ff23e52006-10-04 16:53:27 +0000275 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
276 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
278 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279
Jim Laskey096c22e2006-10-18 12:29:57 +0000280 /// isAlias - Return true if there is any possibility that the two addresses
281 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000282 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000283 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000284 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000285 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000286 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000287 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000288 unsigned SrcValueAlign2,
289 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000290
Jim Laskey7ca56af2006-10-11 13:47:09 +0000291 /// FindAliasInfo - Extracts the relevant alias information from the memory
292 /// node. Returns true if the operand was a load.
293 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000294 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000295 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000296 unsigned &SrcValueAlignment,
297 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Jim Laskey279f0532006-09-25 16:29:54 +0000299 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000300 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000301 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000302
Chris Lattner2392ae72010-04-15 04:48:01 +0000303 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000304 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000305 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000306 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000309 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000310
Chris Lattner2392ae72010-04-15 04:48:01 +0000311 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312
Chris Lattner2392ae72010-04-15 04:48:01 +0000313 /// getShiftAmountTy - Returns a type large enough to hold any valid
314 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000315 EVT getShiftAmountTy(EVT LHSTy) {
316 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 /// isTypeLegal - This method returns true if we are running before type
320 /// legalization or if the specified VT is legal.
321 bool isTypeLegal(const EVT &VT) {
322 if (!LegalTypes) return true;
323 return TLI.isTypeLegal(VT);
324 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000325 };
326}
327
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000328
329namespace {
330/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
331/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000332class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000333 DAGCombiner &DC;
334public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000335 explicit WorkListRemover(DAGCombiner &dc)
336 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Duncan Sandsedfcf592008-06-11 11:42:12 +0000338 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000339 DC.removeFromWorkList(N);
340 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000341};
342}
343
Chris Lattner24664722006-03-01 04:53:38 +0000344//===----------------------------------------------------------------------===//
345// TargetLowering::DAGCombinerInfo implementation
346//===----------------------------------------------------------------------===//
347
348void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
349 ((DAGCombiner*)DC)->AddToWorkList(N);
350}
351
Cameron Zwariched3caf92011-04-02 02:40:26 +0000352void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
353 ((DAGCombiner*)DC)->removeFromWorkList(N);
354}
355
Dan Gohman475871a2008-07-27 21:46:04 +0000356SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000357CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
358 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000359}
360
Dan Gohman475871a2008-07-27 21:46:04 +0000361SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000362CombineTo(SDNode *N, SDValue Res, bool AddTo) {
363 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000364}
365
366
Dan Gohman475871a2008-07-27 21:46:04 +0000367SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000368CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
369 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000370}
371
Dan Gohmane5af2d32009-01-29 01:59:02 +0000372void TargetLowering::DAGCombinerInfo::
373CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
374 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
375}
Chris Lattner24664722006-03-01 04:53:38 +0000376
Chris Lattner24664722006-03-01 04:53:38 +0000377//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000378// Helper Functions
379//===----------------------------------------------------------------------===//
380
381/// isNegatibleForFree - Return 1 if we can compute the negated form of the
382/// specified expression for the same cost as the expression itself, or 2 if we
383/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000384static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000385 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000386 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000387 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000388 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000390 return 0;
391
Chris Lattner29446522007-05-14 22:04:50 +0000392 // fneg is removable even if it has multiple uses.
393 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000394
Chris Lattner29446522007-05-14 22:04:50 +0000395 // Don't allow anything with multiple uses.
396 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000397
Chris Lattner3adf9512007-05-25 02:19:06 +0000398 // Don't recurse exponentially.
399 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000400
Chris Lattner29446522007-05-14 22:04:50 +0000401 switch (Op.getOpcode()) {
402 default: return false;
403 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000404 // Don't invert constant FP values after legalize. The negated constant
405 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000407 case ISD::FADD:
408 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000409 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000410
Owen Andersonafd3d562012-03-06 00:29:31 +0000411 // After operation legalization, it might not be legal to create new FSUBs.
412 if (LegalOperations &&
413 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
414 return 0;
415
Bill Wendlingd34470c2009-01-30 23:10:18 +0000416 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000417 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
418 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000419 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000420 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000421 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000422 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000423 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000425 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000426
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000428 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Chris Lattner29446522007-05-14 22:04:50 +0000430 case ISD::FMUL:
431 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000432 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000433
Bill Wendlingd34470c2009-01-30 23:10:18 +0000434 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
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;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
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);
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FP_EXTEND:
443 case ISD::FP_ROUND:
444 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000445 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000446 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000447 }
448}
449
450/// GetNegatedExpression - If isNegatibleForFree returns true, this function
451/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000452static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000453 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000454 // fneg is removable even if it has multiple uses.
455 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Chris Lattner29446522007-05-14 22:04:50 +0000457 // Don't allow anything with multiple uses.
458 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner3adf9512007-05-25 02:19:06 +0000460 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000461 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000462 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000463 case ISD::ConstantFP: {
464 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
465 V.changeSign();
466 return DAG.getConstantFP(V, Op.getValueType());
467 }
Chris Lattner29446522007-05-14 22:04:50 +0000468 case ISD::FADD:
469 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000470 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Bill Wendlingd34470c2009-01-30 23:10:18 +0000472 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000473 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000474 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000475 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000476 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000478 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000479 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000480 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000481 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000483 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000484 Op.getOperand(0));
485 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000486 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000487 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000488
Bill Wendlingd34470c2009-01-30 23:10:18 +0000489 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000490 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000491 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000492 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000493
Bill Wendlingd34470c2009-01-30 23:10:18 +0000494 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000495 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
496 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000497
Chris Lattner29446522007-05-14 22:04:50 +0000498 case ISD::FMUL:
499 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000500 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000501
Bill Wendlingd34470c2009-01-30 23:10:18 +0000502 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000503 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000504 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000506 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000507 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000508 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000509 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000512 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000513 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000514 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000515 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Chris Lattner29446522007-05-14 22:04:50 +0000517 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000518 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000519 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000520 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000521 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000522 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000523 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000524 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000525 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000526 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000527 }
528}
Chris Lattner24664722006-03-01 04:53:38 +0000529
530
Nate Begeman4ebd8052005-09-01 23:24:04 +0000531// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
532// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000533// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000534// nodes based on the type of node we are checking. This simplifies life a
535// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000536static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
537 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 if (N.getOpcode() == ISD::SETCC) {
539 LHS = N.getOperand(0);
540 RHS = N.getOperand(1);
541 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000542 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000544 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 N.getOperand(2).getOpcode() == ISD::Constant &&
546 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000547 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
549 LHS = N.getOperand(0);
550 RHS = N.getOperand(1);
551 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 return false;
555}
556
Nate Begeman99801192005-09-07 23:25:52 +0000557// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
558// one use. If this is true, it allows the users to invert the operation for
559// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000560static bool isOneUseSetCC(SDValue N) {
561 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000562 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000563 return true;
564 return false;
565}
566
Bill Wendling35247c32009-01-30 00:45:56 +0000567SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
568 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000569 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000570 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
571 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000572 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000573 SDValue OpNode =
574 DAG.FoldConstantArithmetic(Opc, VT,
575 cast<ConstantSDNode>(N0.getOperand(1)),
576 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000577 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000578 }
579 if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000580 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
581 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
582 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000583 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000584 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000585 }
586 }
Bill Wendling35247c32009-01-30 00:45:56 +0000587
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
589 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N1.getOperand(1)),
594 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000598 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000599 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N1.getOperand(0), N0);
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, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Dan Gohman475871a2008-07-27 21:46:04 +0000606 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000607}
608
Dan Gohman475871a2008-07-27 21:46:04 +0000609SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
610 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000611 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
612 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000613 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000614 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000615 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000616 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000617 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000618 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000619 assert((!To[i].getNode() ||
620 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000621 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000622 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000623 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000624 if (AddTo) {
625 // Push the new nodes and any users onto the worklist
626 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000627 if (To[i].getNode()) {
628 AddToWorkList(To[i].getNode());
629 AddUsersToWorkList(To[i].getNode());
630 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000631 }
632 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000633
Dan Gohmandbe664a2009-01-19 21:44:21 +0000634 // Finally, if the node is now dead, remove it from the graph. The node
635 // may not be dead if the replacement process recursively simplified to
636 // something else needing this node.
637 if (N->use_empty()) {
638 // Nodes can be reintroduced into the worklist. Make sure we do not
639 // process a node that has been replaced.
640 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000641
Dan Gohmandbe664a2009-01-19 21:44:21 +0000642 // Finally, since the node is now dead, remove it from the graph.
643 DAG.DeleteNode(N);
644 }
Dan Gohman475871a2008-07-27 21:46:04 +0000645 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646}
647
Evan Chenge5b51ac2010-04-17 06:13:15 +0000648void DAGCombiner::
649CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000650 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000651 // are deleted, make sure to remove them from our worklist.
652 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000653 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000654
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000655 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000656 AddToWorkList(TLO.New.getNode());
657 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000658
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000659 // Finally, if the node is now dead, remove it from the graph. The node
660 // may not be dead if the replacement process recursively simplified to
661 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000662 if (TLO.Old.getNode()->use_empty()) {
663 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665 // If the operands of this node are only used by the node, they will now
666 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000667 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
668 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
669 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000670
Gabor Greifba36cb52008-08-28 21:40:38 +0000671 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000672 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673}
674
675/// SimplifyDemandedBits - Check the specified integer node value to see if
676/// it can be simplified or if things it uses can be simplified by bit
677/// propagation. If so, return true.
678bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000679 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000680 APInt KnownZero, KnownOne;
681 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
682 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000683
Dan Gohmane5af2d32009-01-29 01:59:02 +0000684 // Revisit the node.
685 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Dan Gohmane5af2d32009-01-29 01:59:02 +0000687 // Replace the old value with the new one.
688 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000690 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000691 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000692 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000693 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000694
Dan Gohmane5af2d32009-01-29 01:59:02 +0000695 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000696 return true;
697}
698
Evan Cheng95c57ea2010-04-24 04:43:44 +0000699void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
700 DebugLoc dl = Load->getDebugLoc();
701 EVT VT = Load->getValueType(0);
702 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000703
Evan Cheng95c57ea2010-04-24 04:43:44 +0000704 DEBUG(dbgs() << "\nReplacing.9 ";
705 Load->dump(&DAG);
706 dbgs() << "\nWith: ";
707 Trunc.getNode()->dump(&DAG);
708 dbgs() << '\n');
709 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000710 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
711 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000712 removeFromWorkList(Load);
713 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000714 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000715}
716
717SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
718 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000719 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000720 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000721 EVT MemVT = LD->getMemoryVT();
722 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000723 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000724 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000725 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000726 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000727 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000728 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000729 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000730 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000731 LD->isNonTemporal(), LD->getAlignment());
732 }
733
Evan Cheng4c26e932010-04-19 19:29:22 +0000734 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000735 switch (Opc) {
736 default: break;
737 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000738 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000739 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000740 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000741 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000742 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000743 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000744 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000745 case ISD::Constant: {
746 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000747 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000748 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000749 }
Evan Chengcaf77402010-04-23 19:10:30 +0000750 }
751
752 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000753 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000754 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000755}
756
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000758 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
759 return SDValue();
760 EVT OldVT = Op.getValueType();
761 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 bool Replace = false;
763 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
764 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000765 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000766 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000767
768 if (Replace)
769 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
770 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 DAG.getValueType(OldVT));
772}
773
Evan Cheng95c57ea2010-04-24 04:43:44 +0000774SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000775 EVT OldVT = Op.getValueType();
776 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000777 bool Replace = false;
778 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
779 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000781 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782
783 if (Replace)
784 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
785 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000786}
787
Evan Cheng64b7bf72010-04-16 06:14:10 +0000788/// PromoteIntBinOp - Promote the specified integer binary operation if the
789/// target indicates it is beneficial. e.g. On x86, it's usually better to
790/// promote i16 operations to i32 since i16 instructions are longer.
791SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
792 if (!LegalOperations)
793 return SDValue();
794
795 EVT VT = Op.getValueType();
796 if (VT.isVector() || !VT.isInteger())
797 return SDValue();
798
Evan Chenge5b51ac2010-04-17 06:13:15 +0000799 // If operation type is 'undesirable', e.g. i16 on x86, consider
800 // promoting it.
801 unsigned Opc = Op.getOpcode();
802 if (TLI.isTypeDesirableForOp(Opc, VT))
803 return SDValue();
804
Evan Cheng64b7bf72010-04-16 06:14:10 +0000805 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000806 // Consult target whether it is a good idea to promote this operation and
807 // what's the right type to promote it to.
808 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000809 assert(PVT != VT && "Don't know what type to promote to!");
810
Evan Cheng95c57ea2010-04-24 04:43:44 +0000811 bool Replace0 = false;
812 SDValue N0 = Op.getOperand(0);
813 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
814 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000815 return SDValue();
816
Evan Cheng95c57ea2010-04-24 04:43:44 +0000817 bool Replace1 = false;
818 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000819 SDValue NN1;
820 if (N0 == N1)
821 NN1 = NN0;
822 else {
823 NN1 = PromoteOperand(N1, PVT, Replace1);
824 if (NN1.getNode() == 0)
825 return SDValue();
826 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000827
Evan Cheng95c57ea2010-04-24 04:43:44 +0000828 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000829 if (NN1.getNode())
830 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000831
832 if (Replace0)
833 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
834 if (Replace1)
835 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000836
Evan Chengac7eae52010-04-27 19:48:13 +0000837 DEBUG(dbgs() << "\nPromoting ";
838 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000839 DebugLoc dl = Op.getDebugLoc();
840 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000841 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000842 }
843 return SDValue();
844}
845
846/// PromoteIntShiftOp - Promote the specified integer shift operation if the
847/// target indicates it is beneficial. e.g. On x86, it's usually better to
848/// promote i16 operations to i32 since i16 instructions are longer.
849SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
850 if (!LegalOperations)
851 return SDValue();
852
853 EVT VT = Op.getValueType();
854 if (VT.isVector() || !VT.isInteger())
855 return SDValue();
856
857 // If operation type is 'undesirable', e.g. i16 on x86, consider
858 // promoting it.
859 unsigned Opc = Op.getOpcode();
860 if (TLI.isTypeDesirableForOp(Opc, VT))
861 return SDValue();
862
863 EVT PVT = VT;
864 // Consult target whether it is a good idea to promote this operation and
865 // what's the right type to promote it to.
866 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
867 assert(PVT != VT && "Don't know what type to promote to!");
868
Evan Cheng95c57ea2010-04-24 04:43:44 +0000869 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000870 SDValue N0 = Op.getOperand(0);
871 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000872 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000873 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000874 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000875 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000876 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000877 if (N0.getNode() == 0)
878 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000879
Evan Chenge5b51ac2010-04-17 06:13:15 +0000880 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 if (Replace)
882 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000883
Evan Chengac7eae52010-04-27 19:48:13 +0000884 DEBUG(dbgs() << "\nPromoting ";
885 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000886 DebugLoc dl = Op.getDebugLoc();
887 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000888 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000889 }
890 return SDValue();
891}
892
Evan Cheng4c26e932010-04-19 19:29:22 +0000893SDValue DAGCombiner::PromoteExtend(SDValue Op) {
894 if (!LegalOperations)
895 return SDValue();
896
897 EVT VT = Op.getValueType();
898 if (VT.isVector() || !VT.isInteger())
899 return SDValue();
900
901 // If operation type is 'undesirable', e.g. i16 on x86, consider
902 // promoting it.
903 unsigned Opc = Op.getOpcode();
904 if (TLI.isTypeDesirableForOp(Opc, VT))
905 return SDValue();
906
907 EVT PVT = VT;
908 // Consult target whether it is a good idea to promote this operation and
909 // what's the right type to promote it to.
910 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
911 assert(PVT != VT && "Don't know what type to promote to!");
912 // fold (aext (aext x)) -> (aext x)
913 // fold (aext (zext x)) -> (zext x)
914 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000915 DEBUG(dbgs() << "\nPromoting ";
916 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000917 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
918 }
919 return SDValue();
920}
921
922bool DAGCombiner::PromoteLoad(SDValue Op) {
923 if (!LegalOperations)
924 return false;
925
926 EVT VT = Op.getValueType();
927 if (VT.isVector() || !VT.isInteger())
928 return false;
929
930 // If operation type is 'undesirable', e.g. i16 on x86, consider
931 // promoting it.
932 unsigned Opc = Op.getOpcode();
933 if (TLI.isTypeDesirableForOp(Opc, VT))
934 return false;
935
936 EVT PVT = VT;
937 // Consult target whether it is a good idea to promote this operation and
938 // what's the right type to promote it to.
939 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
940 assert(PVT != VT && "Don't know what type to promote to!");
941
942 DebugLoc dl = Op.getDebugLoc();
943 SDNode *N = Op.getNode();
944 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000945 EVT MemVT = LD->getMemoryVT();
946 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000947 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000948 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000949 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000950 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000951 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000952 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000953 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000954 LD->isNonTemporal(), LD->getAlignment());
955 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
956
Evan Cheng95c57ea2010-04-24 04:43:44 +0000957 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000958 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000959 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000960 Result.getNode()->dump(&DAG);
961 dbgs() << '\n');
962 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000963 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
964 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000965 removeFromWorkList(N);
966 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000967 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000968 return true;
969 }
970 return false;
971}
972
Evan Chenge5b51ac2010-04-17 06:13:15 +0000973
Chris Lattner29446522007-05-14 22:04:50 +0000974//===----------------------------------------------------------------------===//
975// Main DAG Combiner implementation
976//===----------------------------------------------------------------------===//
977
Duncan Sands25cf2272008-11-24 14:53:14 +0000978void DAGCombiner::Run(CombineLevel AtLevel) {
979 // set the instance variables, so that the various visit routines may use it.
980 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000981 LegalOperations = Level >= AfterLegalizeVectorOps;
982 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000983
Evan Cheng17a568b2008-08-29 22:21:44 +0000984 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000985 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
986 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000987 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000988
Evan Cheng17a568b2008-08-29 22:21:44 +0000989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted, and tracking any
991 // changes of the root.
992 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000993
Jim Laskey26f7fa72006-10-17 19:33:52 +0000994 // The root of the dag may dangle to deleted nodes until the dag combiner is
995 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000996 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000997
James Molloy6660c052012-02-16 09:17:04 +0000998 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +0000999 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001000 while (!WorkListContents.empty()) {
1001 SDNode *N;
1002 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1003 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1004 // worklist *should* contain, and check the node we want to visit is should
1005 // actually be visited.
1006 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001007 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001008 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001009
Evan Cheng17a568b2008-08-29 22:21:44 +00001010 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1011 // N is deleted from the DAG, since they too may now be dead or may have a
1012 // reduced number of uses, allowing other xforms.
1013 if (N->use_empty() && N != &Dummy) {
1014 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1015 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 DAG.DeleteNode(N);
1018 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
Evan Cheng17a568b2008-08-29 22:21:44 +00001021 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001022
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 if (RV.getNode() == 0)
1024 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If we get back the same node we passed in, rather than a new node or
1029 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001030 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001031 // mechanics for us, we have no work to do in this case.
1032 if (RV.getNode() == N)
1033 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 assert(N->getOpcode() != ISD::DELETED_NODE &&
1036 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1037 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001038
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001039 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001040 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001041 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001042 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001043 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001044
Devang Patel9728ea22011-05-23 22:04:42 +00001045 // Transfer debug value.
1046 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001047 WorkListRemover DeadNodes(*this);
1048 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001049 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 else {
1051 assert(N->getValueType(0) == RV.getValueType() &&
1052 N->getNumValues() == 1 && "Type mismatch");
1053 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001054 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Evan Cheng17a568b2008-08-29 22:21:44 +00001057 // Push the new node and any users onto the worklist
1058 AddToWorkList(RV.getNode());
1059 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Evan Cheng17a568b2008-08-29 22:21:44 +00001061 // Add any uses of the old node to the worklist in case this node is the
1062 // last one that uses them. They may become dead after this node is
1063 // deleted.
1064 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1065 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Dan Gohmandbe664a2009-01-19 21:44:21 +00001067 // Finally, if the node is now dead, remove it from the graph. The node
1068 // may not be dead if the replacement process recursively simplified to
1069 // something else needing this node.
1070 if (N->use_empty()) {
1071 // Nodes can be reintroduced into the worklist. Make sure we do not
1072 // process a node that has been replaced.
1073 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Dan Gohmandbe664a2009-01-19 21:44:21 +00001075 // Finally, since the node is now dead, remove it from the graph.
1076 DAG.DeleteNode(N);
1077 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Chris Lattner95038592005-10-05 06:35:28 +00001080 // If the root changed (e.g. it was a dead load, update the root).
1081 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001082 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083}
1084
Dan Gohman475871a2008-07-27 21:46:04 +00001085SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001086 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001088 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001089 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 case ISD::ADD: return visitADD(N);
1091 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001092 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001093 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001094 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001095 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 case ISD::MUL: return visitMUL(N);
1097 case ISD::SDIV: return visitSDIV(N);
1098 case ISD::UDIV: return visitUDIV(N);
1099 case ISD::SREM: return visitSREM(N);
1100 case ISD::UREM: return visitUREM(N);
1101 case ISD::MULHU: return visitMULHU(N);
1102 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001103 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1104 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001105 case ISD::SMULO: return visitSMULO(N);
1106 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001107 case ISD::SDIVREM: return visitSDIVREM(N);
1108 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 case ISD::AND: return visitAND(N);
1110 case ISD::OR: return visitOR(N);
1111 case ISD::XOR: return visitXOR(N);
1112 case ISD::SHL: return visitSHL(N);
1113 case ISD::SRA: return visitSRA(N);
1114 case ISD::SRL: return visitSRL(N);
1115 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001116 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001118 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001119 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001120 case ISD::SELECT: return visitSELECT(N);
1121 case ISD::SELECT_CC: return visitSELECT_CC(N);
1122 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1124 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001125 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1127 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001128 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001129 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001130 case ISD::FADD: return visitFADD(N);
1131 case ISD::FSUB: return visitFSUB(N);
1132 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001133 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001134 case ISD::FDIV: return visitFDIV(N);
1135 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001136 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1138 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1139 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1140 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1141 case ISD::FP_ROUND: return visitFP_ROUND(N);
1142 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1143 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1144 case ISD::FNEG: return visitFNEG(N);
1145 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001146 case ISD::FFLOOR: return visitFFLOOR(N);
1147 case ISD::FCEIL: return visitFCEIL(N);
1148 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001149 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001150 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001151 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001152 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001153 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001154 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001155 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1156 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001157 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001158 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Jim Grosbach9a526492010-06-23 16:07:42 +00001159 case ISD::MEMBARRIER: return visitMEMBARRIER(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 }
Dan Gohman475871a2008-07-27 21:46:04 +00001161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162}
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001166
1167 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001169 assert(N->getOpcode() != ISD::DELETED_NODE &&
1170 "Node was deleted but visit returned NULL!");
1171
1172 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1173 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1174
1175 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001176 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001177 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001178
1179 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1180 }
1181 }
1182
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001183 // If nothing happened still, try promoting the operation.
1184 if (RV.getNode() == 0) {
1185 switch (N->getOpcode()) {
1186 default: break;
1187 case ISD::ADD:
1188 case ISD::SUB:
1189 case ISD::MUL:
1190 case ISD::AND:
1191 case ISD::OR:
1192 case ISD::XOR:
1193 RV = PromoteIntBinOp(SDValue(N, 0));
1194 break;
1195 case ISD::SHL:
1196 case ISD::SRA:
1197 case ISD::SRL:
1198 RV = PromoteIntShiftOp(SDValue(N, 0));
1199 break;
1200 case ISD::SIGN_EXTEND:
1201 case ISD::ZERO_EXTEND:
1202 case ISD::ANY_EXTEND:
1203 RV = PromoteExtend(SDValue(N, 0));
1204 break;
1205 case ISD::LOAD:
1206 if (PromoteLoad(SDValue(N, 0)))
1207 RV = SDValue(N, 0);
1208 break;
1209 }
1210 }
1211
Scott Michelfdc40a02009-02-17 22:15:04 +00001212 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001213 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001214 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001215 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1216 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue N0 = N->getOperand(0);
1218 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001219
Evan Cheng08b11732008-03-22 01:55:50 +00001220 // Constant operands are canonicalized to RHS.
1221 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001223 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1224 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001225 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001226 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001227 }
1228 }
1229
Dan Gohman389079b2007-10-08 17:57:15 +00001230 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001231}
Dan Gohman389079b2007-10-08 17:57:15 +00001232
Chris Lattner6270f682006-10-08 22:57:01 +00001233/// getInputChainForNode - Given a node, return its input chain if it has one,
1234/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001235static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001236 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001238 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001240 return N->getOperand(NumOps-1);
1241 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001243 return N->getOperand(i);
1244 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001245 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001246}
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001249 // If N has two operands, where one has an input chain equal to the other,
1250 // the 'other' chain is redundant.
1251 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001253 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001255 return N->getOperand(1);
1256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001257
Chris Lattnerc76d4412007-05-16 06:37:59 +00001258 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001260 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001261 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001262
Jim Laskey6ff23e52006-10-04 16:53:27 +00001263 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001264 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Jim Laskey71382342006-10-07 23:37:56 +00001266 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001267 // encountered.
1268 for (unsigned i = 0; i < TFs.size(); ++i) {
1269 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Jim Laskey6ff23e52006-10-04 16:53:27 +00001271 // Check each of the operands.
1272 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001273 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Jim Laskey6ff23e52006-10-04 16:53:27 +00001275 switch (Op.getOpcode()) {
1276 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001277 // Entry tokens don't need to be added to the list. They are
1278 // rededundant.
1279 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001280 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001283 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001285 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001287 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 Changed = true;
1290 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001291 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001292 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001295 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001296 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001297 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001298 else
1299 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001301 }
1302 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001304
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001306
1307 // If we've change things around then replace token factor.
1308 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001309 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // The entry token is the only possible outcome.
1311 Result = DAG.getEntryNode();
1312 } else {
1313 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001314 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001316 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001317
Jim Laskey274062c2006-10-13 23:32:28 +00001318 // Don't add users to work list.
1319 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001321
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323}
1324
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001325/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001327 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001328 // Replacing results may cause a different MERGE_VALUES to suddenly
1329 // be CSE'd with N, and carry its uses with it. Iterate until no
1330 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001331 // First add the users of this node to the work list so that they
1332 // can be tried again once they have new operands.
1333 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001334 do {
1335 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001336 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001337 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001338 removeFromWorkList(N);
1339 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001340 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001341}
1342
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001343static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001344SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1345 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001346 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue N00 = N0.getOperand(0);
1348 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001349 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001350
Gabor Greifba36cb52008-08-28 21:40:38 +00001351 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001352 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001353 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1354 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1355 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1356 N00.getOperand(0), N01),
1357 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1358 N00.getOperand(1), N01));
1359 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001360 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001361
Dan Gohman475871a2008-07-27 21:46:04 +00001362 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001363}
1364
Dan Gohman475871a2008-07-27 21:46:04 +00001365SDValue DAGCombiner::visitADD(SDNode *N) {
1366 SDValue N0 = N->getOperand(0);
1367 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001370 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001371
1372 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001373 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001375 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001376 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001377
Dan Gohman613e0d82007-07-03 14:03:57 +00001378 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001379 if (N0.getOpcode() == ISD::UNDEF)
1380 return N0;
1381 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001382 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001384 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001385 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001386 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001387 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001388 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001392 // fold (add Sym, c) -> Sym+c
1393 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001394 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001395 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001396 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001397 GA->getOffset() +
1398 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001399 // fold ((c1-A)+c2) -> (c1+c2)-A
1400 if (N1C && N0.getOpcode() == ISD::SUB)
1401 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001402 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001403 DAG.getConstant(N1C->getAPIntValue()+
1404 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001405 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001406 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001407 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001409 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold ((0-A) + B) -> B-A
1411 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1412 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001413 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (A + (0-B)) -> A-B
1415 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1416 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001417 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001418 // fold (A+(B-A)) -> B
1419 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001421 // fold ((B-A)+A) -> B
1422 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1423 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001424 // fold (A+(B-(A+C))) to (B-C)
1425 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001426 N0 == N1.getOperand(1).getOperand(0))
1427 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001428 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001429 // fold (A+(B-(C+A))) to (B-C)
1430 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001431 N0 == N1.getOperand(1).getOperand(1))
1432 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001433 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001434 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001435 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1436 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001437 N0 == N1.getOperand(0).getOperand(1))
1438 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1439 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001440
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001441 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1442 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1443 SDValue N00 = N0.getOperand(0);
1444 SDValue N01 = N0.getOperand(1);
1445 SDValue N10 = N1.getOperand(0);
1446 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001447
1448 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1449 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1450 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1451 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 }
Chris Lattner947c2892006-03-13 06:51:27 +00001453
Dan Gohman475871a2008-07-27 21:46:04 +00001454 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1455 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Chris Lattner947c2892006-03-13 06:51:27 +00001457 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001459 APInt LHSZero, LHSOne;
1460 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001461 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462
Dan Gohman948d8ea2008-02-20 16:33:30 +00001463 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001464 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001465
Chris Lattner947c2892006-03-13 06:51:27 +00001466 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1467 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001468 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001469 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001470 }
1471 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001472
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001473 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001474 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001475 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001476 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001477 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001478 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001479 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001480 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001481 }
1482
Dan Gohmancd9e1552010-01-19 23:30:49 +00001483 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1484 if (N1.getOpcode() == ISD::SHL &&
1485 N1.getOperand(0).getOpcode() == ISD::SUB)
1486 if (ConstantSDNode *C =
1487 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1488 if (C->getAPIntValue() == 0)
1489 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1490 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1491 N1.getOperand(0).getOperand(1),
1492 N1.getOperand(1)));
1493 if (N0.getOpcode() == ISD::SHL &&
1494 N0.getOperand(0).getOpcode() == ISD::SUB)
1495 if (ConstantSDNode *C =
1496 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1497 if (C->getAPIntValue() == 0)
1498 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1499 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1500 N0.getOperand(0).getOperand(1),
1501 N0.getOperand(1)));
1502
Owen Andersonbc146b02010-09-21 20:42:50 +00001503 if (N1.getOpcode() == ISD::AND) {
1504 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001505 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001506 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1507 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508
Owen Andersonbc146b02010-09-21 20:42:50 +00001509 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1510 // and similar xforms where the inner op is either ~0 or 0.
1511 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1512 DebugLoc DL = N->getDebugLoc();
1513 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1514 }
1515 }
1516
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001517 // add (sext i1), X -> sub X, (zext i1)
1518 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1519 N0.getOperand(0).getValueType() == MVT::i1 &&
1520 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1521 DebugLoc DL = N->getDebugLoc();
1522 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1523 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1524 }
1525
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001526 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527}
1528
Dan Gohman475871a2008-07-27 21:46:04 +00001529SDValue DAGCombiner::visitADDC(SDNode *N) {
1530 SDValue N0 = N->getOperand(0);
1531 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001532 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1533 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001534 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001535
Chris Lattner91153682007-03-04 20:03:15 +00001536 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001537 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001538 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001539 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001540 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001541
Chris Lattner91153682007-03-04 20:03:15 +00001542 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001543 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001544 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattnerb6541762007-03-04 20:40:38 +00001546 // fold (addc x, 0) -> x + no carry out
1547 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001548 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001549 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Dale Johannesen874ae252009-06-02 03:12:52 +00001551 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001552 APInt LHSZero, LHSOne;
1553 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001554 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001555
Dan Gohman948d8ea2008-02-20 16:33:30 +00001556 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001557 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerb6541762007-03-04 20:40:38 +00001559 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1560 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001561 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendling14036c02009-01-30 02:38:00 +00001562 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001564 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Dan Gohman475871a2008-07-27 21:46:04 +00001567 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001568}
1569
Dan Gohman475871a2008-07-27 21:46:04 +00001570SDValue DAGCombiner::visitADDE(SDNode *N) {
1571 SDValue N0 = N->getOperand(0);
1572 SDValue N1 = N->getOperand(1);
1573 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001574 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Chris Lattner91153682007-03-04 20:03:15 +00001577 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001578 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001579 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1580 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerb6541762007-03-04 20:40:38 +00001582 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001583 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001584 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001587}
1588
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001589// Since it may not be valid to emit a fold to zero for vector initializers
1590// check if we can before folding.
1591static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001592 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001593 if (!VT.isVector()) {
1594 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001595 }
1596 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001597 // Produce a vector of zeros.
1598 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1599 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1600 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1601 &Ops[0], Ops.size());
1602 }
1603 return SDValue();
1604}
1605
Dan Gohman475871a2008-07-27 21:46:04 +00001606SDValue DAGCombiner::visitSUB(SDNode *N) {
1607 SDValue N0 = N->getOperand(0);
1608 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001611 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1612 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001613 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Dan Gohman7f321562007-06-25 16:23:39 +00001615 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001616 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001617 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001619 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001620
Chris Lattner854077d2005-10-17 01:07:11 +00001621 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001622 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001623 if (N0 == N1)
1624 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001627 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001628 // fold (sub x, c) -> (add x, -c)
1629 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001630 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001631 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001632 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1633 if (N0C && N0C->isAllOnesValue())
1634 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001635 // fold A-(A-B) -> B
1636 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1637 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001639 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001642 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001643 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001644 // fold C2-(A+C1) -> (C2-C1)-A
1645 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
1646 SDValue NewC = DAG.getConstant((N0C->getAPIntValue() - N1C1->getAPIntValue()), VT);
1647 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001648 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001649 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001650 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001651 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001652 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1653 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001654 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001655 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1656 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001657 // fold ((A+(C+B))-B) -> A+C
1658 if (N0.getOpcode() == ISD::ADD &&
1659 N0.getOperand(1).getOpcode() == ISD::ADD &&
1660 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001661 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1662 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001663 // fold ((A-(B-C))-C) -> A-B
1664 if (N0.getOpcode() == ISD::SUB &&
1665 N0.getOperand(1).getOpcode() == ISD::SUB &&
1666 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001667 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1668 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001669
Dan Gohman613e0d82007-07-03 14:03:57 +00001670 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001671 if (N0.getOpcode() == ISD::UNDEF)
1672 return N0;
1673 if (N1.getOpcode() == ISD::UNDEF)
1674 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001675
Dan Gohman6520e202008-10-18 02:06:02 +00001676 // If the relocation model supports it, consider symbol offsets.
1677 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001678 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001679 // fold (sub Sym, c) -> Sym-c
1680 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001681 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001682 GA->getOffset() -
1683 (uint64_t)N1C->getSExtValue());
1684 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1685 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1686 if (GA->getGlobal() == GB->getGlobal())
1687 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1688 VT);
1689 }
1690
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001691 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692}
1693
Craig Toppercc274522012-01-07 09:06:39 +00001694SDValue DAGCombiner::visitSUBC(SDNode *N) {
1695 SDValue N0 = N->getOperand(0);
1696 SDValue N1 = N->getOperand(1);
1697 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1698 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1699 EVT VT = N0.getValueType();
1700
1701 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001702 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001703 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1704 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1705 MVT::Glue));
1706
1707 // fold (subc x, x) -> 0 + no borrow
1708 if (N0 == N1)
1709 return CombineTo(N, DAG.getConstant(0, VT),
1710 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1711 MVT::Glue));
1712
1713 // fold (subc x, 0) -> x + no borrow
1714 if (N1C && N1C->isNullValue())
1715 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1716 MVT::Glue));
1717
1718 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1719 if (N0C && N0C->isAllOnesValue())
1720 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1721 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1722 MVT::Glue));
1723
1724 return SDValue();
1725}
1726
1727SDValue DAGCombiner::visitSUBE(SDNode *N) {
1728 SDValue N0 = N->getOperand(0);
1729 SDValue N1 = N->getOperand(1);
1730 SDValue CarryIn = N->getOperand(2);
1731
1732 // fold (sube x, y, false) -> (subc x, y)
1733 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1734 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1735
1736 return SDValue();
1737}
1738
Dan Gohman475871a2008-07-27 21:46:04 +00001739SDValue DAGCombiner::visitMUL(SDNode *N) {
1740 SDValue N0 = N->getOperand(0);
1741 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001742 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001744 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Dan Gohman7f321562007-06-25 16:23:39 +00001746 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001747 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001749 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001751
Dan Gohman613e0d82007-07-03 14:03:57 +00001752 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001753 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001754 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001755 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001756 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001757 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001758 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001759 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001760 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001762 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001765 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001766 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1767 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001769 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001770 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001771 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001772 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001773 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001774 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1775 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001776 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001777 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001778 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1779 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001780 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001781 DAG.getConstant(Log2Val,
1782 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001783 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001784 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001785 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001786 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001787 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1788 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001789 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001790 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1791 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001793
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001794 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1795 // use.
1796 {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001798 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1799 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001800 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001801 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001803 isa<ConstantSDNode>(N1.getOperand(1)) &&
1804 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001805 Sh = N1; Y = N0;
1806 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001807
Gabor Greifba36cb52008-08-28 21:40:38 +00001808 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001809 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1810 Sh.getOperand(0), Y);
1811 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1812 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001813 }
1814 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001815
Chris Lattnera1deca32006-03-04 23:33:26 +00001816 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001817 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001818 isa<ConstantSDNode>(N0.getOperand(1)))
1819 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1820 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1821 N0.getOperand(0), N1),
1822 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1823 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Nate Begemancd4d58c2006-02-03 06:46:56 +00001825 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001826 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001828 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001829
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001830 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831}
1832
Dan Gohman475871a2008-07-27 21:46:04 +00001833SDValue DAGCombiner::visitSDIV(SDNode *N) {
1834 SDValue N0 = N->getOperand(0);
1835 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001836 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1837 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839
Dan Gohman7f321562007-06-25 16:23:39 +00001840 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001841 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001848 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001849 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001850 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001851 return N0;
1852 // fold (sdiv X, -1) -> 0-X
1853 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001854 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1855 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001856 // If we know the sign bits of both operands are zero, strength reduce to a
1857 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001858 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001859 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001860 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1861 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001862 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001863 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001864 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001865 (N1C->getAPIntValue().isPowerOf2() ||
1866 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001867 // If dividing by powers of two is cheap, then don't perform the following
1868 // fold.
1869 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001870 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001871
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001872 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001873
Chris Lattner8f4880b2006-02-16 08:02:36 +00001874 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001875 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1876 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001877 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001879
Chris Lattner8f4880b2006-02-16 08:02:36 +00001880 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001881 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1882 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001883 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001884 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001885 AddToWorkList(SRL.getNode());
1886 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001887 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001888 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001889
Nate Begeman405e3ec2005-10-21 00:02:42 +00001890 // If we're dividing by a positive value, we're done. Otherwise, we must
1891 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001892 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001893 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001894
Gabor Greifba36cb52008-08-28 21:40:38 +00001895 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001896 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1897 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001898 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001899
Nate Begeman69575232005-10-20 02:15:44 +00001900 // if integer divide is expensive and we satisfy the requirements, emit an
1901 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001902 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001905 }
Dan Gohman7f321562007-06-25 16:23:39 +00001906
Dan Gohman613e0d82007-07-03 14:03:57 +00001907 // undef / X -> 0
1908 if (N0.getOpcode() == ISD::UNDEF)
1909 return DAG.getConstant(0, VT);
1910 // X / undef -> undef
1911 if (N1.getOpcode() == ISD::UNDEF)
1912 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue DAGCombiner::visitUDIV(SDNode *N) {
1918 SDValue N0 = N->getOperand(0);
1919 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Dan Gohman7f321562007-06-25 16:23:39 +00001924 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001932 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001934 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001936 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001937 getShiftAmountTy(N0.getValueType())));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001938 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1939 if (N1.getOpcode() == ISD::SHL) {
1940 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001941 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001943 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1944 N1.getOperand(1),
1945 DAG.getConstant(SHC->getAPIntValue()
1946 .logBase2(),
1947 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001948 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001949 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001950 }
1951 }
1952 }
Nate Begeman69575232005-10-20 02:15:44 +00001953 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001954 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001957 }
Dan Gohman7f321562007-06-25 16:23:39 +00001958
Dan Gohman613e0d82007-07-03 14:03:57 +00001959 // undef / X -> 0
1960 if (N0.getOpcode() == ISD::UNDEF)
1961 return DAG.getConstant(0, VT);
1962 // X / undef -> undef
1963 if (N1.getOpcode() == ISD::UNDEF)
1964 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001965
Dan Gohman475871a2008-07-27 21:46:04 +00001966 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967}
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue DAGCombiner::visitSREM(SDNode *N) {
1970 SDValue N0 = N->getOperand(0);
1971 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1973 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001974 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001975
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001977 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001978 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001979 // If we know the sign bits of both operands are zero, strength reduce to a
1980 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001981 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001982 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001983 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohman77003042007-11-26 23:46:11 +00001986 // If X/C can be simplified by the division-by-constant logic, lower
1987 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001988 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001989 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001990 AddToWorkList(Div.getNode());
1991 SDValue OptimizedDiv = combine(Div.getNode());
1992 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001993 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1994 OptimizedDiv, N1);
1995 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001997 return Sub;
1998 }
Chris Lattner26d29902006-10-12 20:58:32 +00001999 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Dan Gohman613e0d82007-07-03 14:03:57 +00002001 // undef % X -> 0
2002 if (N0.getOpcode() == ISD::UNDEF)
2003 return DAG.getConstant(0, VT);
2004 // X % undef -> undef
2005 if (N1.getOpcode() == ISD::UNDEF)
2006 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002007
Dan Gohman475871a2008-07-27 21:46:04 +00002008 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002009}
2010
Dan Gohman475871a2008-07-27 21:46:04 +00002011SDValue DAGCombiner::visitUREM(SDNode *N) {
2012 SDValue N0 = N->getOperand(0);
2013 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002014 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2015 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002016 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002019 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002020 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002021 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002022 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002023 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002024 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002025 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2026 if (N1.getOpcode() == ISD::SHL) {
2027 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002030 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002032 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002034 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002035 }
2036 }
2037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Dan Gohman77003042007-11-26 23:46:11 +00002039 // If X/C can be simplified by the division-by-constant logic, lower
2040 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002041 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002042 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002043 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002044 SDValue OptimizedDiv = combine(Div.getNode());
2045 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002046 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2047 OptimizedDiv, N1);
2048 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002049 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002050 return Sub;
2051 }
Chris Lattner26d29902006-10-12 20:58:32 +00002052 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002053
Dan Gohman613e0d82007-07-03 14:03:57 +00002054 // undef % X -> 0
2055 if (N0.getOpcode() == ISD::UNDEF)
2056 return DAG.getConstant(0, VT);
2057 // X % undef -> undef
2058 if (N1.getOpcode() == ISD::UNDEF)
2059 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002060
Dan Gohman475871a2008-07-27 21:46:04 +00002061 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002062}
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue DAGCombiner::visitMULHS(SDNode *N) {
2065 SDValue N0 = N->getOperand(0);
2066 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002069 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002070
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002073 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002074 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002075 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002076 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2077 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002078 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002079 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002080 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002081 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002082
Chris Lattnerde1c3602010-12-13 08:39:01 +00002083 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2084 // plus a shift.
2085 if (VT.isSimple() && !VT.isVector()) {
2086 MVT Simple = VT.getSimpleVT();
2087 unsigned SimpleSize = Simple.getSizeInBits();
2088 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2089 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2090 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2091 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2092 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002093 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002094 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002095 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2096 }
2097 }
Owen Anderson95771af2011-02-25 21:41:48 +00002098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100}
2101
Dan Gohman475871a2008-07-27 21:46:04 +00002102SDValue DAGCombiner::visitMULHU(SDNode *N) {
2103 SDValue N0 = N->getOperand(0);
2104 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002107 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002108
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002110 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002113 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002115 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002116 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002118
Chris Lattnerde1c3602010-12-13 08:39:01 +00002119 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2120 // plus a shift.
2121 if (VT.isSimple() && !VT.isVector()) {
2122 MVT Simple = VT.getSimpleVT();
2123 unsigned SimpleSize = Simple.getSizeInBits();
2124 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2125 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2126 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2127 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2128 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2129 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002130 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002131 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2132 }
2133 }
Owen Anderson95771af2011-02-25 21:41:48 +00002134
Dan Gohman475871a2008-07-27 21:46:04 +00002135 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136}
2137
Dan Gohman389079b2007-10-08 17:57:15 +00002138/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2139/// compute two values. LoOp and HiOp give the opcodes for the two computations
2140/// that are being performed. Return true if a simplification was made.
2141///
Scott Michelfdc40a02009-02-17 22:15:04 +00002142SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002143 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002144 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002145 bool HiExists = N->hasAnyUseOfValue(1);
2146 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002147 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002148 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002149 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2150 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002151 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002152 }
2153
2154 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002155 bool LoExists = N->hasAnyUseOfValue(0);
2156 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002157 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002158 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002159 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2160 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002161 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002162 }
2163
Evan Cheng44711942007-11-08 09:25:29 +00002164 // If both halves are used, return as it is.
2165 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002166 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002167
2168 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002169 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002170 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2171 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 AddToWorkList(Lo.getNode());
2173 SDValue LoOpt = combine(Lo.getNode());
2174 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002175 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002176 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002177 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002178 }
2179
Evan Cheng44711942007-11-08 09:25:29 +00002180 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002181 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002182 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002183 AddToWorkList(Hi.getNode());
2184 SDValue HiOpt = combine(Hi.getNode());
2185 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002186 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002187 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002188 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002189 }
Bill Wendling826d1142009-01-30 03:08:40 +00002190
Dan Gohman475871a2008-07-27 21:46:04 +00002191 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002192}
2193
Dan Gohman475871a2008-07-27 21:46:04 +00002194SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2195 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002197
Chris Lattner33e77d32010-12-15 06:04:19 +00002198 EVT VT = N->getValueType(0);
2199 DebugLoc DL = N->getDebugLoc();
2200
2201 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2202 // plus a shift.
2203 if (VT.isSimple() && !VT.isVector()) {
2204 MVT Simple = VT.getSimpleVT();
2205 unsigned SimpleSize = Simple.getSizeInBits();
2206 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2207 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2208 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2209 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2210 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2211 // Compute the high part as N1.
2212 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002213 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002214 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2215 // Compute the low part as N0.
2216 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2217 return CombineTo(N, Lo, Hi);
2218 }
2219 }
Owen Anderson95771af2011-02-25 21:41:48 +00002220
Dan Gohman475871a2008-07-27 21:46:04 +00002221 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002222}
2223
Dan Gohman475871a2008-07-27 21:46:04 +00002224SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2225 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002227
Chris Lattner33e77d32010-12-15 06:04:19 +00002228 EVT VT = N->getValueType(0);
2229 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002230
Chris Lattner33e77d32010-12-15 06:04:19 +00002231 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2232 // plus a shift.
2233 if (VT.isSimple() && !VT.isVector()) {
2234 MVT Simple = VT.getSimpleVT();
2235 unsigned SimpleSize = Simple.getSizeInBits();
2236 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2237 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2238 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2239 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2240 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2241 // Compute the high part as N1.
2242 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002243 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002244 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2245 // Compute the low part as N0.
2246 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2247 return CombineTo(N, Lo, Hi);
2248 }
2249 }
Owen Anderson95771af2011-02-25 21:41:48 +00002250
Dan Gohman475871a2008-07-27 21:46:04 +00002251 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002252}
2253
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002254SDValue DAGCombiner::visitSMULO(SDNode *N) {
2255 // (smulo x, 2) -> (saddo x, x)
2256 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2257 if (C2->getAPIntValue() == 2)
2258 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2259 N->getOperand(0), N->getOperand(0));
2260
2261 return SDValue();
2262}
2263
2264SDValue DAGCombiner::visitUMULO(SDNode *N) {
2265 // (umulo x, 2) -> (uaddo x, x)
2266 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2267 if (C2->getAPIntValue() == 2)
2268 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2269 N->getOperand(0), N->getOperand(0));
2270
2271 return SDValue();
2272}
2273
Dan Gohman475871a2008-07-27 21:46:04 +00002274SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2275 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002276 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002277
Dan Gohman475871a2008-07-27 21:46:04 +00002278 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002279}
2280
Dan Gohman475871a2008-07-27 21:46:04 +00002281SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2282 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002283 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002284
Dan Gohman475871a2008-07-27 21:46:04 +00002285 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002286}
2287
Chris Lattner35e5c142006-05-05 05:51:50 +00002288/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2289/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002290SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2291 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002292 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002293 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002294
Dan Gohmanff00a552010-01-14 03:08:49 +00002295 // Bail early if none of these transforms apply.
2296 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2297
Chris Lattner540121f2006-05-05 06:31:05 +00002298 // For each of OP in AND/OR/XOR:
2299 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2300 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2301 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002302 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002303 //
2304 // do not sink logical op inside of a vector extend, since it may combine
2305 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002306 EVT Op0VT = N0.getOperand(0).getValueType();
2307 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002308 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002309 // Avoid infinite looping with PromoteIntBinOp.
2310 (N0.getOpcode() == ISD::ANY_EXTEND &&
2311 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002312 (N0.getOpcode() == ISD::TRUNCATE &&
2313 (!TLI.isZExtFree(VT, Op0VT) ||
2314 !TLI.isTruncateFree(Op0VT, VT)) &&
2315 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002316 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002317 Op0VT == N1.getOperand(0).getValueType() &&
2318 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002319 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2320 N0.getOperand(0).getValueType(),
2321 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002323 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002326 // For each of OP in SHL/SRL/SRA/AND...
2327 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2328 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2329 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002330 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002331 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002332 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002333 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2334 N0.getOperand(0).getValueType(),
2335 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002336 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002337 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2338 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Nadav Rotem4ac90812012-04-01 19:31:22 +00002341 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2342 // Only perform this optimization after type legalization and before
2343 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2344 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2345 // we don't want to undo this promotion.
2346 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2347 // on scalars.
2348 if ((N0.getOpcode() == ISD::BITCAST || N0.getOpcode() == ISD::SCALAR_TO_VECTOR)
Nadav Rotem2dd83eb2012-07-10 13:25:08 +00002349 && Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002350 SDValue In0 = N0.getOperand(0);
2351 SDValue In1 = N1.getOperand(0);
2352 EVT In0Ty = In0.getValueType();
2353 EVT In1Ty = In1.getValueType();
2354 // If both incoming values are integers, and the original types are the same.
2355 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
2356 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), In0Ty, In0, In1);
2357 SDValue BC = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, Op);
2358 AddToWorkList(Op.getNode());
2359 return BC;
2360 }
2361 }
2362
2363 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2364 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2365 // If both shuffles use the same mask, and both shuffle within a single
2366 // vector, then it is worthwhile to move the swizzle after the operation.
2367 // The type-legalizer generates this pattern when loading illegal
2368 // vector types from memory. In many cases this allows additional shuffle
2369 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002370 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2371 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2372 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002373 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2374 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002375
2376 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2377 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002378
2379 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002380
2381 // Check that both shuffles use the same mask. The masks are known to be of
2382 // the same length because the result vector type is the same.
2383 bool SameMask = true;
2384 for (unsigned i = 0; i != NumElts; ++i) {
2385 int Idx0 = SVN0->getMaskElt(i);
2386 int Idx1 = SVN1->getMaskElt(i);
2387 if (Idx0 != Idx1) {
2388 SameMask = false;
2389 break;
2390 }
2391 }
2392
Craig Topperf9204232012-04-09 07:19:09 +00002393 if (SameMask) {
2394 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
2395 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002396 AddToWorkList(Op.getNode());
Craig Topperf9204232012-04-09 07:19:09 +00002397 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op,
2398 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002399 }
2400 }
Craig Topperf9204232012-04-09 07:19:09 +00002401
Dan Gohman475871a2008-07-27 21:46:04 +00002402 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002403}
2404
Dan Gohman475871a2008-07-27 21:46:04 +00002405SDValue DAGCombiner::visitAND(SDNode *N) {
2406 SDValue N0 = N->getOperand(0);
2407 SDValue N1 = N->getOperand(1);
2408 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002409 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002411 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002412 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002413
Dan Gohman7f321562007-06-25 16:23:39 +00002414 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002415 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002417 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Dan Gohman613e0d82007-07-03 14:03:57 +00002420 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002421 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002422 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002424 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002425 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002426 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002427 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002428 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002429 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002430 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return N0;
2432 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002433 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002434 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002435 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002436 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002437 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002438 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002439 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002440 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002441 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002443 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002445 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2446 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002447 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002448 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002449 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002450 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002451 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2452 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002453
Chris Lattner1ec05d12006-03-01 21:47:21 +00002454 // Replace uses of the AND with uses of the Zero extend node.
2455 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002456
Chris Lattner3603cd62006-02-02 07:17:31 +00002457 // We actually want to replace all uses of the any_extend with the
2458 // zero_extend, to avoid duplicating things. This will later cause this
2459 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002460 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002461 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002462 }
2463 }
James Molloy6259dcd2012-02-20 12:02:38 +00002464 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2465 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2466 // already be zero by virtue of the width of the base type of the load.
2467 //
2468 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2469 // more cases.
2470 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2471 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2472 N0.getOpcode() == ISD::LOAD) {
2473 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2474 N0 : N0.getOperand(0) );
2475
2476 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2477 // This can be a pure constant or a vector splat, in which case we treat the
2478 // vector as a scalar and use the splat value.
2479 APInt Constant = APInt::getNullValue(1);
2480 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2481 Constant = C->getAPIntValue();
2482 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2483 APInt SplatValue, SplatUndef;
2484 unsigned SplatBitSize;
2485 bool HasAnyUndefs;
2486 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2487 SplatBitSize, HasAnyUndefs);
2488 if (IsSplat) {
2489 // Undef bits can contribute to a possible optimisation if set, so
2490 // set them.
2491 SplatValue |= SplatUndef;
2492
2493 // The splat value may be something like "0x00FFFFFF", which means 0 for
2494 // the first vector value and FF for the rest, repeating. We need a mask
2495 // that will apply equally to all members of the vector, so AND all the
2496 // lanes of the constant together.
2497 EVT VT = Vector->getValueType(0);
2498 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
2499 Constant = APInt::getAllOnesValue(BitWidth);
2500 for (unsigned i = 0, n = VT.getVectorNumElements(); i < n; ++i)
2501 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2502 }
2503 }
2504
2505 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2506 // actually legal and isn't going to get expanded, else this is a false
2507 // optimisation.
2508 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2509 Load->getMemoryVT());
2510
2511 // Resize the constant to the same size as the original memory access before
2512 // extension. If it is still the AllOnesValue then this AND is completely
2513 // unneeded.
2514 Constant =
2515 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2516
2517 bool B;
2518 switch (Load->getExtensionType()) {
2519 default: B = false; break;
2520 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2521 case ISD::ZEXTLOAD:
2522 case ISD::NON_EXTLOAD: B = true; break;
2523 }
2524
2525 if (B && Constant.isAllOnesValue()) {
2526 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2527 // preserve semantics once we get rid of the AND.
2528 SDValue NewLoad(Load, 0);
2529 if (Load->getExtensionType() == ISD::EXTLOAD) {
2530 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
2531 Load->getValueType(0), Load->getDebugLoc(),
2532 Load->getChain(), Load->getBasePtr(),
2533 Load->getOffset(), Load->getMemoryVT(),
2534 Load->getMemOperand());
2535 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002536 if (Load->getNumValues() == 3) {
2537 // PRE/POST_INC loads have 3 values.
2538 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2539 NewLoad.getValue(2) };
2540 CombineTo(Load, To, 3, true);
2541 } else {
2542 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2543 }
James Molloy6259dcd2012-02-20 12:02:38 +00002544 }
2545
2546 // Fold the AND away, taking care not to fold to the old load node if we
2547 // replaced it.
2548 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2549
2550 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2551 }
2552 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002553 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2554 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2555 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2556 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002558 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002560 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002561 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002562 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2563 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002564 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002565 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002566 }
Bill Wendling2627a882009-01-30 20:43:18 +00002567 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002568 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002569 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2570 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002571 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002572 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002573 }
Bill Wendling2627a882009-01-30 20:43:18 +00002574 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002575 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002576 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2577 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002578 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002579 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002580 }
2581 }
2582 // canonicalize equivalent to ll == rl
2583 if (LL == RR && LR == RL) {
2584 Op1 = ISD::getSetCCSwappedOperands(Op1);
2585 std::swap(RL, RR);
2586 }
2587 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002588 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002589 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002590 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002591 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002592 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2593 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002594 }
2595 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002596
Bill Wendling2627a882009-01-30 20:43:18 +00002597 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002598 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002599 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002600 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002602
Nate Begemande996292006-02-03 22:24:05 +00002603 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2604 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002605 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002606 SimplifyDemandedBits(SDValue(N, 0)))
2607 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002608
Nate Begemanded49632005-10-13 03:11:28 +00002609 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002610 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002611 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002612 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002613 // If we zero all the possible extended bits, then we can turn this into
2614 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002615 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002616 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002617 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002618 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002619 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002620 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002621 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002622 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002623 LN0->isVolatile(), LN0->isNonTemporal(),
2624 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002625 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002626 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002627 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002628 }
2629 }
2630 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002631 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002632 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002633 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002634 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002635 // If we zero all the possible extended bits, then we can turn this into
2636 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002637 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002638 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002639 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002640 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002641 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002642 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002643 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002644 LN0->getBasePtr(), LN0->getPointerInfo(),
2645 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002646 LN0->isVolatile(), LN0->isNonTemporal(),
2647 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002648 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002649 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002650 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002651 }
2652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002653
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002654 // fold (and (load x), 255) -> (zextload x, i8)
2655 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002656 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2657 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2658 (N0.getOpcode() == ISD::ANY_EXTEND &&
2659 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2660 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2661 LoadSDNode *LN0 = HasAnyExt
2662 ? cast<LoadSDNode>(N0.getOperand(0))
2663 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002664 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002665 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002666 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002667 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2668 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2669 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002670
Evan Chengd40d03e2010-01-06 19:38:29 +00002671 if (ExtVT == LoadedVT &&
2672 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002673 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002674
2675 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002676 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002677 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002678 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002679 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2680 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002681 AddToWorkList(N);
2682 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2683 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2684 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002685
Chris Lattneref7634c2010-01-07 21:53:27 +00002686 // Do not change the width of a volatile load.
2687 // Do not generate loads of non-round integer types since these can
2688 // be expensive (and would be wrong if the type is not byte sized).
2689 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2690 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2691 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002692
Chris Lattneref7634c2010-01-07 21:53:27 +00002693 unsigned Alignment = LN0->getAlignment();
2694 SDValue NewPtr = LN0->getBasePtr();
2695
2696 // For big endian targets, we need to add an offset to the pointer
2697 // to load the correct bytes. For little endian systems, we merely
2698 // need to read fewer bytes from the same pointer.
2699 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002700 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2701 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2702 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002703 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2704 NewPtr, DAG.getConstant(PtrOff, PtrType));
2705 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002706 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002707
2708 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002709
Chris Lattneref7634c2010-01-07 21:53:27 +00002710 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2711 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002712 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002713 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002714 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002715 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2716 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002717 AddToWorkList(N);
2718 CombineTo(LN0, Load, Load.getValue(1));
2719 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002720 }
Evan Cheng466685d2006-10-09 20:57:25 +00002721 }
Chris Lattner15045b62006-02-28 06:35:35 +00002722 }
2723 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002724
Evan Chenga9e13ba2012-07-17 18:54:11 +00002725 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2726 VT.getSizeInBits() <= 64) {
2727 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2728 APInt ADDC = ADDI->getAPIntValue();
2729 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2730 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2731 // immediate for an add, but it is legal if its top c2 bits are set,
2732 // transform the ADD so the immediate doesn't need to be materialized
2733 // in a register.
2734 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2735 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2736 SRLI->getZExtValue());
2737 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2738 ADDC |= Mask;
2739 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2740 SDValue NewAdd =
2741 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
2742 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2743 CombineTo(N0.getNode(), NewAdd);
2744 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2745 }
2746 }
2747 }
2748 }
2749 }
2750 }
2751
2752
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002753 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002754}
2755
Evan Cheng9568e5c2011-06-21 06:01:08 +00002756/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2757///
2758SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2759 bool DemandHighBits) {
2760 if (!LegalOperations)
2761 return SDValue();
2762
2763 EVT VT = N->getValueType(0);
2764 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2765 return SDValue();
2766 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2767 return SDValue();
2768
2769 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2770 bool LookPassAnd0 = false;
2771 bool LookPassAnd1 = false;
2772 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2773 std::swap(N0, N1);
2774 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2775 std::swap(N0, N1);
2776 if (N0.getOpcode() == ISD::AND) {
2777 if (!N0.getNode()->hasOneUse())
2778 return SDValue();
2779 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2780 if (!N01C || N01C->getZExtValue() != 0xFF00)
2781 return SDValue();
2782 N0 = N0.getOperand(0);
2783 LookPassAnd0 = true;
2784 }
2785
2786 if (N1.getOpcode() == ISD::AND) {
2787 if (!N1.getNode()->hasOneUse())
2788 return SDValue();
2789 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2790 if (!N11C || N11C->getZExtValue() != 0xFF)
2791 return SDValue();
2792 N1 = N1.getOperand(0);
2793 LookPassAnd1 = true;
2794 }
2795
2796 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2797 std::swap(N0, N1);
2798 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2799 return SDValue();
2800 if (!N0.getNode()->hasOneUse() ||
2801 !N1.getNode()->hasOneUse())
2802 return SDValue();
2803
2804 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2805 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2806 if (!N01C || !N11C)
2807 return SDValue();
2808 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2809 return SDValue();
2810
2811 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2812 SDValue N00 = N0->getOperand(0);
2813 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2814 if (!N00.getNode()->hasOneUse())
2815 return SDValue();
2816 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2817 if (!N001C || N001C->getZExtValue() != 0xFF)
2818 return SDValue();
2819 N00 = N00.getOperand(0);
2820 LookPassAnd0 = true;
2821 }
2822
2823 SDValue N10 = N1->getOperand(0);
2824 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2825 if (!N10.getNode()->hasOneUse())
2826 return SDValue();
2827 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2828 if (!N101C || N101C->getZExtValue() != 0xFF00)
2829 return SDValue();
2830 N10 = N10.getOperand(0);
2831 LookPassAnd1 = true;
2832 }
2833
2834 if (N00 != N10)
2835 return SDValue();
2836
2837 // Make sure everything beyond the low halfword is zero since the SRL 16
2838 // will clear the top bits.
2839 unsigned OpSizeInBits = VT.getSizeInBits();
2840 if (DemandHighBits && OpSizeInBits > 16 &&
2841 (!LookPassAnd0 || !LookPassAnd1) &&
2842 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2843 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002844
Evan Cheng9568e5c2011-06-21 06:01:08 +00002845 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2846 if (OpSizeInBits > 16)
2847 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2848 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2849 return Res;
2850}
2851
2852/// isBSwapHWordElement - Return true if the specified node is an element
2853/// that makes up a 32-bit packed halfword byteswap. i.e.
2854/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2855static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2856 if (!N.getNode()->hasOneUse())
2857 return false;
2858
2859 unsigned Opc = N.getOpcode();
2860 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2861 return false;
2862
2863 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2864 if (!N1C)
2865 return false;
2866
2867 unsigned Num;
2868 switch (N1C->getZExtValue()) {
2869 default:
2870 return false;
2871 case 0xFF: Num = 0; break;
2872 case 0xFF00: Num = 1; break;
2873 case 0xFF0000: Num = 2; break;
2874 case 0xFF000000: Num = 3; break;
2875 }
2876
2877 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2878 SDValue N0 = N.getOperand(0);
2879 if (Opc == ISD::AND) {
2880 if (Num == 0 || Num == 2) {
2881 // (x >> 8) & 0xff
2882 // (x >> 8) & 0xff0000
2883 if (N0.getOpcode() != ISD::SRL)
2884 return false;
2885 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2886 if (!C || C->getZExtValue() != 8)
2887 return false;
2888 } else {
2889 // (x << 8) & 0xff00
2890 // (x << 8) & 0xff000000
2891 if (N0.getOpcode() != ISD::SHL)
2892 return false;
2893 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2894 if (!C || C->getZExtValue() != 8)
2895 return false;
2896 }
2897 } else if (Opc == ISD::SHL) {
2898 // (x & 0xff) << 8
2899 // (x & 0xff0000) << 8
2900 if (Num != 0 && Num != 2)
2901 return false;
2902 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2903 if (!C || C->getZExtValue() != 8)
2904 return false;
2905 } else { // Opc == ISD::SRL
2906 // (x & 0xff00) >> 8
2907 // (x & 0xff000000) >> 8
2908 if (Num != 1 && Num != 3)
2909 return false;
2910 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2911 if (!C || C->getZExtValue() != 8)
2912 return false;
2913 }
2914
2915 if (Parts[Num])
2916 return false;
2917
2918 Parts[Num] = N0.getOperand(0).getNode();
2919 return true;
2920}
2921
2922/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2923/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2924/// => (rotl (bswap x), 16)
2925SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2926 if (!LegalOperations)
2927 return SDValue();
2928
2929 EVT VT = N->getValueType(0);
2930 if (VT != MVT::i32)
2931 return SDValue();
2932 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2933 return SDValue();
2934
2935 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2936 // Look for either
2937 // (or (or (and), (and)), (or (and), (and)))
2938 // (or (or (or (and), (and)), (and)), (and))
2939 if (N0.getOpcode() != ISD::OR)
2940 return SDValue();
2941 SDValue N00 = N0.getOperand(0);
2942 SDValue N01 = N0.getOperand(1);
2943
2944 if (N1.getOpcode() == ISD::OR) {
2945 // (or (or (and), (and)), (or (and), (and)))
2946 SDValue N000 = N00.getOperand(0);
2947 if (!isBSwapHWordElement(N000, Parts))
2948 return SDValue();
2949
2950 SDValue N001 = N00.getOperand(1);
2951 if (!isBSwapHWordElement(N001, Parts))
2952 return SDValue();
2953 SDValue N010 = N01.getOperand(0);
2954 if (!isBSwapHWordElement(N010, Parts))
2955 return SDValue();
2956 SDValue N011 = N01.getOperand(1);
2957 if (!isBSwapHWordElement(N011, Parts))
2958 return SDValue();
2959 } else {
2960 // (or (or (or (and), (and)), (and)), (and))
2961 if (!isBSwapHWordElement(N1, Parts))
2962 return SDValue();
2963 if (!isBSwapHWordElement(N01, Parts))
2964 return SDValue();
2965 if (N00.getOpcode() != ISD::OR)
2966 return SDValue();
2967 SDValue N000 = N00.getOperand(0);
2968 if (!isBSwapHWordElement(N000, Parts))
2969 return SDValue();
2970 SDValue N001 = N00.getOperand(1);
2971 if (!isBSwapHWordElement(N001, Parts))
2972 return SDValue();
2973 }
2974
2975 // Make sure the parts are all coming from the same node.
2976 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
2977 return SDValue();
2978
2979 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
2980 SDValue(Parts[0],0));
2981
2982 // Result of the bswap should be rotated by 16. If it's not legal, than
2983 // do (x << 16) | (x >> 16).
2984 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
2985 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
2986 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
2987 else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
2988 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
2989 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
2990 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
2991 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
2992}
2993
Dan Gohman475871a2008-07-27 21:46:04 +00002994SDValue DAGCombiner::visitOR(SDNode *N) {
2995 SDValue N0 = N->getOperand(0);
2996 SDValue N1 = N->getOperand(1);
2997 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002998 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003000 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003001
Dan Gohman7f321562007-06-25 16:23:39 +00003002 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003003 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003004 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003005 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003006 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003007
Dan Gohman613e0d82007-07-03 14:03:57 +00003008 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003009 if (!LegalOperations &&
3010 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003011 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3012 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3013 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003014 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003015 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003016 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003017 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003018 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00003019 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003020 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003021 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003022 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003023 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003024 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003025 return N1;
3026 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003027 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003028 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003029
3030 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3031 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3032 if (BSwap.getNode() != 0)
3033 return BSwap;
3034 BSwap = MatchBSwapHWordLow(N, N0, N1);
3035 if (BSwap.getNode() != 0)
3036 return BSwap;
3037
Nate Begemancd4d58c2006-02-03 06:46:56 +00003038 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00003039 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003040 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003041 return ROR;
3042 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003043 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003044 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003045 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003046 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003047 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003048 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3049 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3050 N0.getOperand(0), N1),
3051 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003052 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003053 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3054 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3055 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3056 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003057
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003058 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003059 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003060 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3061 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003062 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003063 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003064 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
3065 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003066 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003067 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003068 }
Bill Wendling09025642009-01-30 20:59:34 +00003069 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3070 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003071 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003072 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003073 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
3074 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003075 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003076 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003077 }
3078 }
3079 // canonicalize equivalent to ll == rl
3080 if (LL == RR && LR == RL) {
3081 Op1 = ISD::getSetCCSwappedOperands(Op1);
3082 std::swap(RL, RR);
3083 }
3084 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003085 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003086 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003087 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003088 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00003089 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
3090 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003091 }
3092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003093
Bill Wendling09025642009-01-30 20:59:34 +00003094 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003095 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003097 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003099
Bill Wendling09025642009-01-30 20:59:34 +00003100 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003101 if (N0.getOpcode() == ISD::AND &&
3102 N1.getOpcode() == ISD::AND &&
3103 N0.getOperand(1).getOpcode() == ISD::Constant &&
3104 N1.getOperand(1).getOpcode() == ISD::Constant &&
3105 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003106 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003107 // We can only do this xform if we know that bits from X that are set in C2
3108 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003109 const APInt &LHSMask =
3110 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3111 const APInt &RHSMask =
3112 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003113
Dan Gohmanea859be2007-06-22 14:59:07 +00003114 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3115 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00003116 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3117 N0.getOperand(0), N1.getOperand(0));
3118 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
3119 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003120 }
3121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003122
Chris Lattner516b9622006-09-14 20:50:57 +00003123 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00003124 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00003125 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003126
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003127 // Simplify the operands using demanded-bits information.
3128 if (!VT.isVector() &&
3129 SimplifyDemandedBits(SDValue(N, 0)))
3130 return SDValue(N, 0);
3131
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003132 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003133}
3134
Chris Lattner516b9622006-09-14 20:50:57 +00003135/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003136static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003137 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003138 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003139 Mask = Op.getOperand(1);
3140 Op = Op.getOperand(0);
3141 } else {
3142 return false;
3143 }
3144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003145
Chris Lattner516b9622006-09-14 20:50:57 +00003146 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3147 Shift = Op;
3148 return true;
3149 }
Bill Wendling09025642009-01-30 20:59:34 +00003150
Scott Michelfdc40a02009-02-17 22:15:04 +00003151 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003152}
3153
Chris Lattner516b9622006-09-14 20:50:57 +00003154// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3155// idioms for rotate, and if the target supports rotation instructions, generate
3156// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00003157SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003158 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003159 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003160 if (!TLI.isTypeLegal(VT)) return 0;
3161
3162 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003163 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3164 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003165 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003166
Chris Lattner516b9622006-09-14 20:50:57 +00003167 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003168 SDValue LHSShift; // The shift.
3169 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003170 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3171 return 0; // Not part of a rotate.
3172
Dan Gohman475871a2008-07-27 21:46:04 +00003173 SDValue RHSShift; // The shift.
3174 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003175 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3176 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003177
Chris Lattner516b9622006-09-14 20:50:57 +00003178 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3179 return 0; // Not shifting the same value.
3180
3181 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3182 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003183
Chris Lattner516b9622006-09-14 20:50:57 +00003184 // Canonicalize shl to left side in a shl/srl pair.
3185 if (RHSShift.getOpcode() == ISD::SHL) {
3186 std::swap(LHS, RHS);
3187 std::swap(LHSShift, RHSShift);
3188 std::swap(LHSMask , RHSMask );
3189 }
3190
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue LHSShiftArg = LHSShift.getOperand(0);
3193 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3194 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003195
3196 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3197 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003198 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3199 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003200 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3201 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003202 if ((LShVal + RShVal) != OpSizeInBits)
3203 return 0;
3204
Dan Gohman475871a2008-07-27 21:46:04 +00003205 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00003206 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003207 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00003208 else
Bill Wendling317bd702009-01-30 21:14:50 +00003209 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003210
Chris Lattner516b9622006-09-14 20:50:57 +00003211 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003212 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003213 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003214
Gabor Greifba36cb52008-08-28 21:40:38 +00003215 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003216 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3217 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003218 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003219 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003220 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3221 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003222 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003223
Bill Wendling317bd702009-01-30 21:14:50 +00003224 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003226
Gabor Greifba36cb52008-08-28 21:40:38 +00003227 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003229
Chris Lattner516b9622006-09-14 20:50:57 +00003230 // If there is a mask here, and we have a variable shift, we can't be sure
3231 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003232 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003233 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003234
Chris Lattner516b9622006-09-14 20:50:57 +00003235 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3236 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003237 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3238 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003239 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003240 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003241 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00003242 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003243 return DAG.getNode(ISD::ROTL, DL, VT,
3244 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003245 else
Bill Wendling317bd702009-01-30 21:14:50 +00003246 return DAG.getNode(ISD::ROTR, DL, VT,
3247 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003248 }
Chris Lattner516b9622006-09-14 20:50:57 +00003249 }
3250 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003251
Chris Lattner516b9622006-09-14 20:50:57 +00003252 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3253 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003254 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3255 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003256 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003257 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003258 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00003259 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00003260 return DAG.getNode(ISD::ROTR, DL, VT,
3261 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00003262 else
Bill Wendling317bd702009-01-30 21:14:50 +00003263 return DAG.getNode(ISD::ROTL, DL, VT,
3264 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003265 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003266 }
3267 }
3268
Dan Gohman74feef22008-10-17 01:23:35 +00003269 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00003270 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3271 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003272 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3273 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00003274 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3275 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003276 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3277 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003278 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3279 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003280 if (RExtOp0.getOpcode() == ISD::SUB &&
3281 RExtOp0.getOperand(1) == LExtOp0) {
3282 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003283 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003284 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003285 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003286 if (ConstantSDNode *SUBC =
3287 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003288 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003289 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3290 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003291 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003292 }
3293 }
3294 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3295 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003296 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003297 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003298 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003299 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003300 if (ConstantSDNode *SUBC =
3301 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003302 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003303 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3304 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003305 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003306 }
3307 }
Chris Lattner516b9622006-09-14 20:50:57 +00003308 }
3309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003310
Chris Lattner516b9622006-09-14 20:50:57 +00003311 return 0;
3312}
3313
Dan Gohman475871a2008-07-27 21:46:04 +00003314SDValue DAGCombiner::visitXOR(SDNode *N) {
3315 SDValue N0 = N->getOperand(0);
3316 SDValue N1 = N->getOperand(1);
3317 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003318 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003320 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003321
Dan Gohman7f321562007-06-25 16:23:39 +00003322 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003324 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003325 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003326 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003327
Evan Cheng26471c42008-03-25 20:08:07 +00003328 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3329 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3330 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003331 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003332 if (N0.getOpcode() == ISD::UNDEF)
3333 return N0;
3334 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003335 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003337 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003338 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003339 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003340 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003341 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003342 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003343 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003344 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003345 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003346 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003347 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003348 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003349
Nate Begeman1d4d4142005-09-01 00:19:25 +00003350 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003351 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003352 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003353 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3354 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003355
Duncan Sands25cf2272008-11-24 14:53:14 +00003356 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003357 switch (N0.getOpcode()) {
3358 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003359 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003360 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003361 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003362 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003363 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003364 N0.getOperand(3), NotCC);
3365 }
3366 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003367 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003368
Chris Lattner61c5ff42007-09-10 21:39:07 +00003369 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003370 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003371 N0.getNode()->hasOneUse() &&
3372 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003373 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003374 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003375 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003376 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003377 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003378 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003379
Bill Wendling317bd702009-01-30 21:14:50 +00003380 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003382 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003383 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003384 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3385 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003386 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3387 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003388 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003389 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003390 }
3391 }
Bill Wendling317bd702009-01-30 21:14:50 +00003392 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003393 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003394 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003395 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003396 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3397 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003398 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3399 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003400 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003401 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003402 }
3403 }
Bill Wendling317bd702009-01-30 21:14:50 +00003404 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003405 if (N1C && N0.getOpcode() == ISD::XOR) {
3406 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3407 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3408 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003409 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3410 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003411 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003412 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003413 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3414 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003415 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003416 }
3417 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003418 if (N0 == N1)
3419 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003420
Chris Lattner35e5c142006-05-05 05:51:50 +00003421 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3422 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003423 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003424 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003426
Chris Lattner3e104b12006-04-08 04:15:24 +00003427 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003428 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003429 SimplifyDemandedBits(SDValue(N, 0)))
3430 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003431
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003432 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003433}
3434
Chris Lattnere70da202007-12-06 07:33:36 +00003435/// visitShiftByConstant - Handle transforms common to the three shifts, when
3436/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003437SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003438 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003439 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003440
Chris Lattnere70da202007-12-06 07:33:36 +00003441 // We want to pull some binops through shifts, so that we have (and (shift))
3442 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3443 // thing happens with address calculations, so it's important to canonicalize
3444 // it.
3445 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003446
Chris Lattnere70da202007-12-06 07:33:36 +00003447 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003448 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003449 case ISD::OR:
3450 case ISD::XOR:
3451 HighBitSet = false; // We can only transform sra if the high bit is clear.
3452 break;
3453 case ISD::AND:
3454 HighBitSet = true; // We can only transform sra if the high bit is set.
3455 break;
3456 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003457 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003458 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003459 HighBitSet = false; // We can only transform sra if the high bit is clear.
3460 break;
3461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003462
Chris Lattnere70da202007-12-06 07:33:36 +00003463 // We require the RHS of the binop to be a constant as well.
3464 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003465 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003466
3467 // FIXME: disable this unless the input to the binop is a shift by a constant.
3468 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003469 //
Bill Wendling88103372009-01-30 21:37:17 +00003470 // void foo(int *X, int i) { X[i & 1235] = 1; }
3471 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003472 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003473 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003474 BinOpLHSVal->getOpcode() != ISD::SRA &&
3475 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3476 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003477 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003478
Owen Andersone50ed302009-08-10 22:56:29 +00003479 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003480
Bill Wendling88103372009-01-30 21:37:17 +00003481 // If this is a signed shift right, and the high bit is modified by the
3482 // logical operation, do not perform the transformation. The highBitSet
3483 // boolean indicates the value of the high bit of the constant which would
3484 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003485 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003486 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3487 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003488 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003490
Chris Lattnere70da202007-12-06 07:33:36 +00003491 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003492 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3493 N->getValueType(0),
3494 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003495
3496 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003497 SDValue NewShift = DAG.getNode(N->getOpcode(),
3498 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003499 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003500
3501 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003502 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003503}
3504
Dan Gohman475871a2008-07-27 21:46:04 +00003505SDValue DAGCombiner::visitSHL(SDNode *N) {
3506 SDValue N0 = N->getOperand(0);
3507 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003508 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3509 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003510 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003511 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003512
Nate Begeman1d4d4142005-09-01 00:19:25 +00003513 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003514 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003515 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003516 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003517 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003518 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003519 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003520 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003521 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003522 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003523 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003524 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003525 // fold (shl undef, x) -> 0
3526 if (N0.getOpcode() == ISD::UNDEF)
3527 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003528 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003529 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003530 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003531 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003532 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003533 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003534 N1.getOperand(0).getOpcode() == ISD::AND &&
3535 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003536 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003537 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003538 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003539 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003540 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003541 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003542 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003543 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3544 DAG.getNode(ISD::TRUNCATE,
3545 N->getDebugLoc(),
3546 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003547 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003548 }
3549 }
3550
Dan Gohman475871a2008-07-27 21:46:04 +00003551 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3552 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003553
3554 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003555 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003556 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003557 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3558 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003559 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003560 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003561 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003562 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003563 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003564
3565 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3566 // For this to be valid, the second form must not preserve any of the bits
3567 // that are shifted out by the inner shift in the first form. This means
3568 // the outer shift size must be >= the number of bits added by the ext.
3569 // As a corollary, we don't care what kind of ext it is.
3570 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3571 N0.getOpcode() == ISD::ANY_EXTEND ||
3572 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3573 N0.getOperand(0).getOpcode() == ISD::SHL &&
3574 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003575 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003576 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3577 uint64_t c2 = N1C->getZExtValue();
3578 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3579 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3580 if (c2 >= OpSizeInBits - InnerShiftSize) {
3581 if (c1 + c2 >= OpSizeInBits)
3582 return DAG.getConstant(0, VT);
3583 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3584 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3585 N0.getOperand(0)->getOperand(0)),
3586 DAG.getConstant(c1 + c2, N1.getValueType()));
3587 }
3588 }
3589
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003590 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3591 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003592 // Only fold this if the inner shift has no other uses -- if it does, folding
3593 // this will increase the total number of instructions.
3594 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003595 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003596 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003597 if (c1 < VT.getSizeInBits()) {
3598 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003599 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3600 VT.getSizeInBits() - c1);
3601 SDValue Shift;
3602 if (c2 > c1) {
3603 Mask = Mask.shl(c2-c1);
3604 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3605 DAG.getConstant(c2-c1, N1.getValueType()));
3606 } else {
3607 Mask = Mask.lshr(c1-c2);
3608 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3609 DAG.getConstant(c1-c2, N1.getValueType()));
3610 }
3611 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3612 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003613 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003614 }
Bill Wendling88103372009-01-30 21:37:17 +00003615 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003616 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3617 SDValue HiBitsMask =
3618 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3619 VT.getSizeInBits() -
3620 N1C->getZExtValue()),
3621 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003622 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003623 HiBitsMask);
3624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003625
Evan Chenge5b51ac2010-04-17 06:13:15 +00003626 if (N1C) {
3627 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3628 if (NewSHL.getNode())
3629 return NewSHL;
3630 }
3631
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003632 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003633}
3634
Dan Gohman475871a2008-07-27 21:46:04 +00003635SDValue DAGCombiner::visitSRA(SDNode *N) {
3636 SDValue N0 = N->getOperand(0);
3637 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003638 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3639 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003640 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003641 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003642
Bill Wendling88103372009-01-30 21:37:17 +00003643 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003644 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003645 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003646 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003647 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003648 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003649 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003650 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003651 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003652 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003653 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003654 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003655 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003656 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003657 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003658 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3659 // sext_inreg.
3660 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003661 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003662 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3663 if (VT.isVector())
3664 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3665 ExtVT, VT.getVectorNumElements());
3666 if ((!LegalOperations ||
3667 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003668 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003669 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003670 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003671
Bill Wendling88103372009-01-30 21:37:17 +00003672 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003673 if (N1C && N0.getOpcode() == ISD::SRA) {
3674 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003675 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003676 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003677 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003678 DAG.getConstant(Sum, N1C->getValueType(0)));
3679 }
3680 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003681
Bill Wendling88103372009-01-30 21:37:17 +00003682 // fold (sra (shl X, m), (sub result_size, n))
3683 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003684 // result_size - n != m.
3685 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003686 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003687 if (N0.getOpcode() == ISD::SHL) {
3688 // Get the two constanst of the shifts, CN0 = m, CN = n.
3689 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3690 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003691 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003692 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003693 EVT::getIntegerVT(*DAG.getContext(),
3694 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003695 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003696 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003697
Scott Michelfdc40a02009-02-17 22:15:04 +00003698 // If the shift is not a no-op (in which case this should be just a sign
3699 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003700 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003701 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003702 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003703 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3704 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003705 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003706
Owen Anderson95771af2011-02-25 21:41:48 +00003707 SDValue Amt = DAG.getConstant(ShiftAmt,
3708 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003709 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3710 N0.getOperand(0), Amt);
3711 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3712 Shift);
3713 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3714 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003715 }
3716 }
3717 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003718
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003719 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003720 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003721 N1.getOperand(0).getOpcode() == ISD::AND &&
3722 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003723 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003724 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003725 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003726 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003727 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003728 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003729 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003730 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003731 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003732 DAG.getNode(ISD::TRUNCATE,
3733 N->getDebugLoc(),
3734 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003735 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003736 }
3737 }
3738
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003739 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3740 // if c1 is equal to the number of bits the trunc removes
3741 if (N0.getOpcode() == ISD::TRUNCATE &&
3742 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3743 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3744 N0.getOperand(0).hasOneUse() &&
3745 N0.getOperand(0).getOperand(1).hasOneUse() &&
3746 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3747 EVT LargeVT = N0.getOperand(0).getValueType();
3748 ConstantSDNode *LargeShiftAmt =
3749 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3750
3751 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3752 LargeShiftAmt->getZExtValue()) {
3753 SDValue Amt =
3754 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003755 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003756 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3757 N0.getOperand(0).getOperand(0), Amt);
3758 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3759 }
3760 }
3761
Scott Michelfdc40a02009-02-17 22:15:04 +00003762 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003763 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3764 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003765
3766
Nate Begeman1d4d4142005-09-01 00:19:25 +00003767 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003768 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003769 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003770
Evan Chenge5b51ac2010-04-17 06:13:15 +00003771 if (N1C) {
3772 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3773 if (NewSRA.getNode())
3774 return NewSRA;
3775 }
3776
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003777 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003778}
3779
Dan Gohman475871a2008-07-27 21:46:04 +00003780SDValue DAGCombiner::visitSRL(SDNode *N) {
3781 SDValue N0 = N->getOperand(0);
3782 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003783 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3784 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003785 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003786 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003787
Nate Begeman1d4d4142005-09-01 00:19:25 +00003788 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003789 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003790 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003791 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003792 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003793 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003794 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003795 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003796 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003797 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003798 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003799 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003800 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003801 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003802 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003803 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003804
Bill Wendling88103372009-01-30 21:37:17 +00003805 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003806 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003808 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3809 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003810 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003811 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003812 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003813 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003814 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003815
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003816 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003817 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3818 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003819 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003820 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003821 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3822 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003823 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3824 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003825 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003826 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003827 if (c1 + OpSizeInBits == InnerShiftSize) {
3828 if (c1 + c2 >= InnerShiftSize)
3829 return DAG.getConstant(0, VT);
3830 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003831 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003832 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003833 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003834 }
3835 }
3836
Chris Lattnerefcddc32010-04-15 05:28:43 +00003837 // fold (srl (shl x, c), c) -> (and x, cst2)
3838 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3839 N0.getValueSizeInBits() <= 64) {
3840 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3841 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3842 DAG.getConstant(~0ULL >> ShAmt, VT));
3843 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003844
Scott Michelfdc40a02009-02-17 22:15:04 +00003845
Chris Lattner06afe072006-05-05 22:53:17 +00003846 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3847 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3848 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003849 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003850 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003851 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003852
Evan Chenge5b51ac2010-04-17 06:13:15 +00003853 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003854 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003855 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003856 N0.getOperand(0),
3857 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003858 AddToWorkList(SmallShift.getNode());
3859 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3860 }
Chris Lattner06afe072006-05-05 22:53:17 +00003861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003862
Chris Lattner3657ffe2006-10-12 20:23:19 +00003863 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3864 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003865 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003866 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003867 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003869
Chris Lattner350bec02006-04-02 06:11:11 +00003870 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003871 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003873 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003874 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003875
Chris Lattner350bec02006-04-02 06:11:11 +00003876 // If any of the input bits are KnownOne, then the input couldn't be all
3877 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003878 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003879
Chris Lattner350bec02006-04-02 06:11:11 +00003880 // If all of the bits input the to ctlz node are known to be zero, then
3881 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003882 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003883 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003884
Chris Lattner350bec02006-04-02 06:11:11 +00003885 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003886 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003887 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003888 // could be set on input to the CTLZ node. If this bit is set, the SRL
3889 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3890 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003891 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003892 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003893
Chris Lattner350bec02006-04-02 06:11:11 +00003894 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003895 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003896 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003897 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003898 }
Bill Wendling88103372009-01-30 21:37:17 +00003899
3900 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3901 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003902 }
3903 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003904
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003905 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003906 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003907 N1.getOperand(0).getOpcode() == ISD::AND &&
3908 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003909 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003910 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003911 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003912 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003913 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003914 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003915 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003916 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003917 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003918 DAG.getNode(ISD::TRUNCATE,
3919 N->getDebugLoc(),
3920 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003921 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003922 }
3923 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003924
Chris Lattner61a4c072007-04-18 03:06:49 +00003925 // fold operands of srl based on knowledge that the low bits are not
3926 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003927 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3928 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Evan Cheng9ab2b982009-12-18 21:31:31 +00003930 if (N1C) {
3931 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3932 if (NewSRL.getNode())
3933 return NewSRL;
3934 }
3935
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003936 // Attempt to convert a srl of a load into a narrower zero-extending load.
3937 SDValue NarrowLoad = ReduceLoadWidth(N);
3938 if (NarrowLoad.getNode())
3939 return NarrowLoad;
3940
Evan Cheng9ab2b982009-12-18 21:31:31 +00003941 // Here is a common situation. We want to optimize:
3942 //
3943 // %a = ...
3944 // %b = and i32 %a, 2
3945 // %c = srl i32 %b, 1
3946 // brcond i32 %c ...
3947 //
3948 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003949 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00003950 // %a = ...
3951 // %b = and %a, 2
3952 // %c = setcc eq %b, 0
3953 // brcond %c ...
3954 //
3955 // However when after the source operand of SRL is optimized into AND, the SRL
3956 // itself may not be optimized further. Look for it and add the BRCOND into
3957 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003958 if (N->hasOneUse()) {
3959 SDNode *Use = *N->use_begin();
3960 if (Use->getOpcode() == ISD::BRCOND)
3961 AddToWorkList(Use);
3962 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3963 // Also look pass the truncate.
3964 Use = *Use->use_begin();
3965 if (Use->getOpcode() == ISD::BRCOND)
3966 AddToWorkList(Use);
3967 }
3968 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003969
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003970 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00003971}
3972
Dan Gohman475871a2008-07-27 21:46:04 +00003973SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3974 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003975 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003976
3977 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003978 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003979 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003980 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003981}
3982
Chandler Carruth63974b22011-12-13 01:56:10 +00003983SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
3985 EVT VT = N->getValueType(0);
3986
3987 // fold (ctlz_zero_undef c1) -> c2
3988 if (isa<ConstantSDNode>(N0))
3989 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
3990 return SDValue();
3991}
3992
Dan Gohman475871a2008-07-27 21:46:04 +00003993SDValue DAGCombiner::visitCTTZ(SDNode *N) {
3994 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003995 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003996
Nate Begeman1d4d4142005-09-01 00:19:25 +00003997 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003998 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003999 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004000 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004001}
4002
Chandler Carruth63974b22011-12-13 01:56:10 +00004003SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4004 SDValue N0 = N->getOperand(0);
4005 EVT VT = N->getValueType(0);
4006
4007 // fold (cttz_zero_undef c1) -> c2
4008 if (isa<ConstantSDNode>(N0))
4009 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4010 return SDValue();
4011}
4012
Dan Gohman475871a2008-07-27 21:46:04 +00004013SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4014 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004015 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004016
Nate Begeman1d4d4142005-09-01 00:19:25 +00004017 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004018 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004019 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004020 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004021}
4022
Dan Gohman475871a2008-07-27 21:46:04 +00004023SDValue DAGCombiner::visitSELECT(SDNode *N) {
4024 SDValue N0 = N->getOperand(0);
4025 SDValue N1 = N->getOperand(1);
4026 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004027 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4028 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4029 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004030 EVT VT = N->getValueType(0);
4031 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004032
Bill Wendling34584e62009-01-30 22:02:18 +00004033 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004034 if (N1 == N2)
4035 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004036 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004037 if (N0C && !N0C->isNullValue())
4038 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004039 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00004040 if (N0C && N0C->isNullValue())
4041 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004042 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004043 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00004044 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4045 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004046 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004047 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004048 (VT0.isInteger() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00004049 TLI.getBooleanContents(false) == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004050 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004051 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004052 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00004053 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
4054 N0, DAG.getConstant(1, VT0));
4055 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
4056 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004057 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004058 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00004059 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
4060 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004061 }
Bill Wendling34584e62009-01-30 22:02:18 +00004062 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00004064 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004065 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004066 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004067 }
Bill Wendling34584e62009-01-30 22:02:18 +00004068 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004069 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004070 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004071 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004072 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004073 }
Bill Wendling34584e62009-01-30 22:02:18 +00004074 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00004076 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
4077 // fold (select X, X, Y) -> (or X, Y)
4078 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00004080 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4081 // fold (select X, Y, X) -> (and X, Y)
4082 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00004084 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004085
Chris Lattner40c62d52005-10-18 06:04:22 +00004086 // If we can fold this based on the true/false value, do so.
4087 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004088 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004089
Nate Begeman44728a72005-09-19 22:34:01 +00004090 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004091 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004092 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004094 // having to say they don't support SELECT_CC on every type the DAG knows
4095 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004097 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00004098 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
4099 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004100 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00004101 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004102 }
Bill Wendling34584e62009-01-30 22:02:18 +00004103
Dan Gohman475871a2008-07-27 21:46:04 +00004104 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00004105}
4106
Dan Gohman475871a2008-07-27 21:46:04 +00004107SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4108 SDValue N0 = N->getOperand(0);
4109 SDValue N1 = N->getOperand(1);
4110 SDValue N2 = N->getOperand(2);
4111 SDValue N3 = N->getOperand(3);
4112 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004113 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004114
Nate Begeman44728a72005-09-19 22:34:01 +00004115 // fold select_cc lhs, rhs, x, x, cc -> x
4116 if (N2 == N3)
4117 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004118
Chris Lattner5f42a242006-09-20 06:19:26 +00004119 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00004120 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004121 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004122 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004123
Gabor Greifba36cb52008-08-28 21:40:38 +00004124 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004125 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004126 return N2; // cond always true -> true val
4127 else
4128 return N3; // cond always false -> false val
4129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004130
Chris Lattner5f42a242006-09-20 06:19:26 +00004131 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00004133 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
4134 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004135 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004136
Chris Lattner40c62d52005-10-18 06:04:22 +00004137 // If we can fold this based on the true/false value, do so.
4138 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004139 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004140
Nate Begeman44728a72005-09-19 22:34:01 +00004141 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00004142 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004143}
4144
Dan Gohman475871a2008-07-27 21:46:04 +00004145SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00004146 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004147 cast<CondCodeSDNode>(N->getOperand(2))->get(),
4148 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00004149}
4150
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004151// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004152// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004153// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004154// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004155static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004156 unsigned ExtOpc,
4157 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004158 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004159 bool HasCopyToRegUses = false;
4160 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004161 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4162 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004163 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004164 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004165 if (User == N)
4166 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004167 if (UI.getUse().getResNo() != N0.getResNo())
4168 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004169 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004170 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004171 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4172 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4173 // Sign bits will be lost after a zext.
4174 return false;
4175 bool Add = false;
4176 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004177 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004178 if (UseOp == N0)
4179 continue;
4180 if (!isa<ConstantSDNode>(UseOp))
4181 return false;
4182 Add = true;
4183 }
4184 if (Add)
4185 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004186 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004187 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004188 // If truncates aren't free and there are users we can't
4189 // extend, it isn't worthwhile.
4190 if (!isTruncFree)
4191 return false;
4192 // Remember if this value is live-out.
4193 if (User->getOpcode() == ISD::CopyToReg)
4194 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004195 }
4196
4197 if (HasCopyToRegUses) {
4198 bool BothLiveOut = false;
4199 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4200 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004201 SDUse &Use = UI.getUse();
4202 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4203 BothLiveOut = true;
4204 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004205 }
4206 }
4207 if (BothLiveOut)
4208 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004209 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004210 return ExtendNodes.size();
4211 }
4212 return true;
4213}
4214
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004215void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4216 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4217 ISD::NodeType ExtType) {
4218 // Extend SetCC uses if necessary.
4219 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4220 SDNode *SetCC = SetCCs[i];
4221 SmallVector<SDValue, 4> Ops;
4222
4223 for (unsigned j = 0; j != 2; ++j) {
4224 SDValue SOp = SetCC->getOperand(j);
4225 if (SOp == Trunc)
4226 Ops.push_back(ExtLoad);
4227 else
4228 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4229 }
4230
4231 Ops.push_back(SetCC->getOperand(2));
4232 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4233 &Ops[0], Ops.size()));
4234 }
4235}
4236
Dan Gohman475871a2008-07-27 21:46:04 +00004237SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4238 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004239 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004240
Nate Begeman1d4d4142005-09-01 00:19:25 +00004241 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004242 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004243 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Nate Begeman1d4d4142005-09-01 00:19:25 +00004245 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004246 // fold (sext (aext x)) -> (sext x)
4247 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004248 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4249 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004250
Chris Lattner22558872007-02-26 03:13:59 +00004251 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004252 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4253 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004254 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4255 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004256 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4257 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004258 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004259 // CombineTo deleted the truncate, if needed, but not what's under it.
4260 AddToWorkList(oye);
4261 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004262 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004263 }
Evan Chengc88138f2007-03-22 01:54:19 +00004264
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004265 // See if the value being truncated is already sign extended. If so, just
4266 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004267 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004268 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4269 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4270 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004271 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004272
Chris Lattner22558872007-02-26 03:13:59 +00004273 if (OpBits == DestBits) {
4274 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4275 // bits, it is already ready.
4276 if (NumSignBits > DestBits-MidBits)
4277 return Op;
4278 } else if (OpBits < DestBits) {
4279 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4280 // bits, just sext from i32.
4281 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004282 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004283 } else {
4284 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4285 // bits, just truncate to i32.
4286 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004287 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004289
Chris Lattner22558872007-02-26 03:13:59 +00004290 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004291 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4292 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004293 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004294 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004295 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004296 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4297 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004298 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004299 }
Chris Lattner6007b842006-09-21 06:00:20 +00004300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004301
Evan Cheng110dec22005-12-14 02:19:23 +00004302 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004303 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004304 // on vectors in one instruction. We only perform this transformation on
4305 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004306 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004307 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004308 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004309 bool DoXform = true;
4310 SmallVector<SDNode*, 4> SetCCs;
4311 if (!N0.hasOneUse())
4312 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4313 if (DoXform) {
4314 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004315 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004316 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004317 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004318 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004319 LN0->isVolatile(), LN0->isNonTemporal(),
4320 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004321 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004322 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4323 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004324 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004325 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4326 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004327 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004328 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004329 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004330
4331 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4332 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004333 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4334 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004335 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004336 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004337 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004338 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004339 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004340 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004341 LN0->getBasePtr(), LN0->getPointerInfo(),
4342 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004343 LN0->isVolatile(), LN0->isNonTemporal(),
4344 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004345 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004346 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004347 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4348 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004349 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004350 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004351 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004353
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004354 // fold (sext (and/or/xor (load x), cst)) ->
4355 // (and/or/xor (sextload x), (sext cst))
4356 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4357 N0.getOpcode() == ISD::XOR) &&
4358 isa<LoadSDNode>(N0.getOperand(0)) &&
4359 N0.getOperand(1).getOpcode() == ISD::Constant &&
4360 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4361 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4362 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4363 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4364 bool DoXform = true;
4365 SmallVector<SDNode*, 4> SetCCs;
4366 if (!N0.hasOneUse())
4367 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4368 SetCCs, TLI);
4369 if (DoXform) {
4370 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4371 LN0->getChain(), LN0->getBasePtr(),
4372 LN0->getPointerInfo(),
4373 LN0->getMemoryVT(),
4374 LN0->isVolatile(),
4375 LN0->isNonTemporal(),
4376 LN0->getAlignment());
4377 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4378 Mask = Mask.sext(VT.getSizeInBits());
4379 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4380 ExtLoad, DAG.getConstant(Mask, VT));
4381 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4382 N0.getOperand(0).getDebugLoc(),
4383 N0.getOperand(0).getValueType(), ExtLoad);
4384 CombineTo(N, And);
4385 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4386 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4387 ISD::SIGN_EXTEND);
4388 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4389 }
4390 }
4391 }
4392
Chris Lattner20a35c32007-04-11 05:32:27 +00004393 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004394 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004395 // Only do this before legalize for now.
4396 if (VT.isVector() && !LegalOperations) {
4397 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004398 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4399 // of the same size as the compared operands. Only optimize sext(setcc())
4400 // if this is the case.
4401 EVT SVT = TLI.getSetCCResultType(N0VT);
4402
4403 // We know that the # elements of the results is the same as the
4404 // # elements of the compare (and the # elements of the compare result
4405 // for that matter). Check to see that they are the same size. If so,
4406 // we know that the element size of the sext'd result matches the
4407 // element size of the compare operands.
4408 if (VT.getSizeInBits() == SVT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004409 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004410 N0.getOperand(1),
4411 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Dan Gohman3ce89f42010-04-30 17:19:19 +00004412 // If the desired elements are smaller or larger than the source
4413 // elements we can use a matching integer vector type and then
4414 // truncate/sign extend
4415 else {
Duncan Sands34727662010-07-12 08:16:59 +00004416 EVT MatchingElementType =
4417 EVT::getIntegerVT(*DAG.getContext(),
4418 N0VT.getScalarType().getSizeInBits());
4419 EVT MatchingVectorType =
4420 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4421 N0VT.getVectorNumElements());
Nadav Rotem2e506192012-04-11 08:26:11 +00004422
4423 if (SVT == MatchingVectorType) {
4424 SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType,
4425 N0.getOperand(0), N0.getOperand(1),
4426 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4427 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
4428 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004429 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004430 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004431
Chris Lattner2b7a2712009-07-08 00:31:33 +00004432 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004433 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004434 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004435 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004436 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004437 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004438 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004439 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004441 if (!LegalOperations ||
4442 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
4443 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4444 DAG.getSetCC(N->getDebugLoc(),
4445 TLI.getSetCCResultType(VT),
4446 N0.getOperand(0), N0.getOperand(1),
4447 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4448 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004450
Dan Gohman8f0ad582008-04-28 16:58:24 +00004451 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004452 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004453 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004454 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004455
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004456 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004457}
4458
Rafael Espindoladecbc432012-04-09 16:06:03 +00004459// isTruncateOf - If N is a truncate of some other value, return true, record
4460// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4461// This function computes KnownZero to avoid a duplicated call to
4462// ComputeMaskedBits in the caller.
4463static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4464 APInt &KnownZero) {
4465 APInt KnownOne;
4466 if (N->getOpcode() == ISD::TRUNCATE) {
4467 Op = N->getOperand(0);
4468 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4469 return true;
4470 }
4471
4472 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4473 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4474 return false;
4475
4476 SDValue Op0 = N->getOperand(0);
4477 SDValue Op1 = N->getOperand(1);
4478 assert(Op0.getValueType() == Op1.getValueType());
4479
4480 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4481 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004482 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004483 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004484 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004485 Op = Op0;
4486 else
4487 return false;
4488
4489 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4490
4491 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4492 return false;
4493
4494 return true;
4495}
4496
Dan Gohman475871a2008-07-27 21:46:04 +00004497SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4498 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004499 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004500
Nate Begeman1d4d4142005-09-01 00:19:25 +00004501 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004502 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004503 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004504 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004505 // fold (zext (aext x)) -> (zext x)
4506 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004507 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4508 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004509
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004510 // fold (zext (truncate x)) -> (zext x) or
4511 // (zext (truncate x)) -> (truncate x)
4512 // This is valid when the truncated bits of x are already zero.
4513 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004514 SDValue Op;
4515 APInt KnownZero;
4516 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4517 APInt TruncatedBits =
4518 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4519 APInt(Op.getValueSizeInBits(), 0) :
4520 APInt::getBitsSet(Op.getValueSizeInBits(),
4521 N0.getValueSizeInBits(),
4522 std::min(Op.getValueSizeInBits(),
4523 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004524 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004525 if (VT.bitsGT(Op.getValueType()))
4526 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4527 if (VT.bitsLT(Op.getValueType()))
4528 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4529
4530 return Op;
4531 }
4532 }
4533
Evan Chengc88138f2007-03-22 01:54:19 +00004534 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4535 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004536 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004537 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4538 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004539 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4540 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004541 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004542 // CombineTo deleted the truncate, if needed, but not what's under it.
4543 AddToWorkList(oye);
4544 }
Eli Friedmane545d382011-04-16 23:25:34 +00004545 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004546 }
Evan Chengc88138f2007-03-22 01:54:19 +00004547 }
4548
Chris Lattner6007b842006-09-21 06:00:20 +00004549 // fold (zext (truncate x)) -> (and x, mask)
4550 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004551 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004552
4553 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4554 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4555 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4556 if (NarrowLoad.getNode()) {
4557 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4558 if (NarrowLoad.getNode() != N0.getNode()) {
4559 CombineTo(N0.getNode(), NarrowLoad);
4560 // CombineTo deleted the truncate, if needed, but not what's under it.
4561 AddToWorkList(oye);
4562 }
4563 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4564 }
4565
Dan Gohman475871a2008-07-27 21:46:04 +00004566 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004567 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004568 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004569 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004570 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004571 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004572 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004573 }
Dan Gohman87862e72009-12-11 21:31:27 +00004574 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4575 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004576 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004577
Dan Gohman97121ba2009-04-08 00:15:30 +00004578 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4579 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004580 if (N0.getOpcode() == ISD::AND &&
4581 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004582 N0.getOperand(1).getOpcode() == ISD::Constant &&
4583 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4584 N0.getValueType()) ||
4585 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004586 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004587 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004588 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004589 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004590 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004591 }
Dan Gohman220a8232008-03-03 23:51:38 +00004592 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004593 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004594 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4595 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004596 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004597
Evan Cheng110dec22005-12-14 02:19:23 +00004598 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004599 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004600 // on vectors in one instruction. We only perform this transformation on
4601 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004602 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004603 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004604 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004605 bool DoXform = true;
4606 SmallVector<SDNode*, 4> SetCCs;
4607 if (!N0.hasOneUse())
4608 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4609 if (DoXform) {
4610 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004611 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004612 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004613 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004614 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004615 LN0->isVolatile(), LN0->isNonTemporal(),
4616 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004617 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004618 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4619 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004620 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004621
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004622 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4623 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004624 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004625 }
Evan Cheng110dec22005-12-14 02:19:23 +00004626 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004627
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004628 // fold (zext (and/or/xor (load x), cst)) ->
4629 // (and/or/xor (zextload x), (zext cst))
4630 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4631 N0.getOpcode() == ISD::XOR) &&
4632 isa<LoadSDNode>(N0.getOperand(0)) &&
4633 N0.getOperand(1).getOpcode() == ISD::Constant &&
4634 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4635 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4636 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4637 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4638 bool DoXform = true;
4639 SmallVector<SDNode*, 4> SetCCs;
4640 if (!N0.hasOneUse())
4641 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4642 SetCCs, TLI);
4643 if (DoXform) {
4644 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4645 LN0->getChain(), LN0->getBasePtr(),
4646 LN0->getPointerInfo(),
4647 LN0->getMemoryVT(),
4648 LN0->isVolatile(),
4649 LN0->isNonTemporal(),
4650 LN0->getAlignment());
4651 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4652 Mask = Mask.zext(VT.getSizeInBits());
4653 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4654 ExtLoad, DAG.getConstant(Mask, VT));
4655 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4656 N0.getOperand(0).getDebugLoc(),
4657 N0.getOperand(0).getValueType(), ExtLoad);
4658 CombineTo(N, And);
4659 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4660 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4661 ISD::ZERO_EXTEND);
4662 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4663 }
4664 }
4665 }
4666
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004667 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4668 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004669 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4670 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004671 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004672 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004673 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004674 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004675 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004676 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004677 LN0->getBasePtr(), LN0->getPointerInfo(),
4678 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004679 LN0->isVolatile(), LN0->isNonTemporal(),
4680 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004681 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004682 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004683 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4684 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004685 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004686 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004687 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004688 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004689
Chris Lattner20a35c32007-04-11 05:32:27 +00004690 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004691 if (!LegalOperations && VT.isVector()) {
4692 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4693 // Only do this before legalize for now.
4694 EVT N0VT = N0.getOperand(0).getValueType();
4695 EVT EltVT = VT.getVectorElementType();
4696 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4697 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004698 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004699 // We know that the # elements of the results is the same as the
4700 // # elements of the compare (and the # elements of the compare result
4701 // for that matter). Check to see that they are the same size. If so,
4702 // we know that the element size of the sext'd result matches the
4703 // element size of the compare operands.
4704 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004705 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004706 N0.getOperand(1),
4707 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4708 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4709 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004710
4711 // If the desired elements are smaller or larger than the source
4712 // elements we can use a matching integer vector type and then
4713 // truncate/sign extend
4714 EVT MatchingElementType =
4715 EVT::getIntegerVT(*DAG.getContext(),
4716 N0VT.getScalarType().getSizeInBits());
4717 EVT MatchingVectorType =
4718 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4719 N0VT.getVectorNumElements());
4720 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004721 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004722 N0.getOperand(1),
4723 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4724 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4725 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4726 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4727 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004728 }
4729
4730 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004731 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004732 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004733 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004734 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004735 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004737
Evan Cheng9818c042009-12-15 03:00:32 +00004738 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004739 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004740 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004741 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4742 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004743 SDValue ShAmt = N0.getOperand(1);
4744 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004745 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004746 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004747 // If the original shl may be shifting out bits, do not perform this
4748 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004749 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4750 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4751 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004752 return SDValue();
4753 }
Chris Lattnere0751182011-02-13 19:09:16 +00004754
4755 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004756
4757 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004758 if (VT.getSizeInBits() >= 256)
4759 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004760
Chris Lattnere0751182011-02-13 19:09:16 +00004761 return DAG.getNode(N0.getOpcode(), DL, VT,
4762 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4763 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004764 }
4765
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004766 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004767}
4768
Dan Gohman475871a2008-07-27 21:46:04 +00004769SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4770 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004771 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004772
Chris Lattner5ffc0662006-05-05 05:58:59 +00004773 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004774 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004775 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004776 // fold (aext (aext x)) -> (aext x)
4777 // fold (aext (zext x)) -> (zext x)
4778 // fold (aext (sext x)) -> (sext x)
4779 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4780 N0.getOpcode() == ISD::ZERO_EXTEND ||
4781 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004782 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004783
Evan Chengc88138f2007-03-22 01:54:19 +00004784 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4785 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4786 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004787 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4788 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004789 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4790 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004791 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004792 // CombineTo deleted the truncate, if needed, but not what's under it.
4793 AddToWorkList(oye);
4794 }
Eli Friedmane545d382011-04-16 23:25:34 +00004795 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004796 }
Evan Chengc88138f2007-03-22 01:54:19 +00004797 }
4798
Chris Lattner84750582006-09-20 06:29:17 +00004799 // fold (aext (truncate x))
4800 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004802 if (TruncOp.getValueType() == VT)
4803 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004804 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004805 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4806 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004808
Dan Gohman97121ba2009-04-08 00:15:30 +00004809 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4810 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004811 if (N0.getOpcode() == ISD::AND &&
4812 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004813 N0.getOperand(1).getOpcode() == ISD::Constant &&
4814 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4815 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004816 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004817 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004818 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004819 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004820 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004821 }
Dan Gohman220a8232008-03-03 23:51:38 +00004822 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004823 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004824 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4825 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004827
Chris Lattner5ffc0662006-05-05 05:58:59 +00004828 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004829 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004830 // on vectors in one instruction. We only perform this transformation on
4831 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004832 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004833 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004834 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004835 bool DoXform = true;
4836 SmallVector<SDNode*, 4> SetCCs;
4837 if (!N0.hasOneUse())
4838 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4839 if (DoXform) {
4840 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004841 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004842 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004843 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004844 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004845 LN0->isVolatile(), LN0->isNonTemporal(),
4846 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004847 CombineTo(N, ExtLoad);
4848 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4849 N0.getValueType(), ExtLoad);
4850 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004851 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4852 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004853 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4854 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004855 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004856
Chris Lattner5ffc0662006-05-05 05:58:59 +00004857 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4858 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4859 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004860 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004861 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004862 N0.hasOneUse()) {
4863 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004864 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004865 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4866 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004867 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004868 LN0->isVolatile(), LN0->isNonTemporal(),
4869 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004870 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004871 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004872 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4873 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004874 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004875 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Chris Lattner20a35c32007-04-11 05:32:27 +00004878 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004879 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4880 // Only do this before legalize for now.
4881 if (VT.isVector() && !LegalOperations) {
4882 EVT N0VT = N0.getOperand(0).getValueType();
4883 // We know that the # elements of the results is the same as the
4884 // # elements of the compare (and the # elements of the compare result
4885 // for that matter). Check to see that they are the same size. If so,
4886 // we know that the element size of the sext'd result matches the
4887 // element size of the compare operands.
4888 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004889 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004890 N0.getOperand(1),
4891 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004892 // If the desired elements are smaller or larger than the source
4893 // elements we can use a matching integer vector type and then
4894 // truncate/sign extend
4895 else {
Duncan Sands34727662010-07-12 08:16:59 +00004896 EVT MatchingElementType =
4897 EVT::getIntegerVT(*DAG.getContext(),
4898 N0VT.getScalarType().getSizeInBits());
4899 EVT MatchingVectorType =
4900 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4901 N0VT.getVectorNumElements());
4902 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004903 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004904 N0.getOperand(1),
4905 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4906 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00004907 }
4908 }
4909
4910 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004911 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004912 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004913 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00004914 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004915 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004916 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004917 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004918
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004919 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00004920}
4921
Chris Lattner2b4c2792007-10-13 06:35:54 +00004922/// GetDemandedBits - See if the specified operand can be simplified with the
4923/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00004924/// simpler operand, otherwise return a null SDValue.
4925SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004926 switch (V.getOpcode()) {
4927 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00004928 case ISD::Constant: {
4929 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
4930 assert(CV != 0 && "Const value should be ConstSDNode.");
4931 const APInt &CVal = CV->getAPIntValue();
4932 APInt NewVal = CVal & Mask;
4933 if (NewVal != CVal) {
4934 return DAG.getConstant(NewVal, V.getValueType());
4935 }
4936 break;
4937 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004938 case ISD::OR:
4939 case ISD::XOR:
4940 // If the LHS or RHS don't contribute bits to the or, drop them.
4941 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
4942 return V.getOperand(1);
4943 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
4944 return V.getOperand(0);
4945 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00004946 case ISD::SRL:
4947 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004948 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00004949 break;
4950 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
4951 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004952 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00004953
Dan Gohmancc91d632009-01-03 19:22:06 +00004954 // Watch out for shift count overflow though.
4955 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004956 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00004957 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00004958 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004959 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00004960 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00004961 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004962 }
Dan Gohman475871a2008-07-27 21:46:04 +00004963 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00004964}
4965
Evan Chengc88138f2007-03-22 01:54:19 +00004966/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
4967/// bits and then truncated to a narrower type and where N is a multiple
4968/// of number of bits of the narrower type, transform it to a narrower load
4969/// from address + N / num of bits of new type. If the result is to be
4970/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00004971SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00004972 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004973
Evan Chengc88138f2007-03-22 01:54:19 +00004974 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00004975 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004976 EVT VT = N->getValueType(0);
4977 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00004978
Dan Gohman7f8613e2008-08-14 20:04:46 +00004979 // This transformation isn't valid for vector loads.
4980 if (VT.isVector())
4981 return SDValue();
4982
Dan Gohmand1996362010-01-09 02:13:55 +00004983 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00004984 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00004985 if (Opc == ISD::SIGN_EXTEND_INREG) {
4986 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00004987 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004988 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00004989 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004990 ExtType = ISD::ZEXTLOAD;
4991 N0 = SDValue(N, 0);
4992 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4993 if (!N01) return SDValue();
4994 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
4995 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00004996 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00004997 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
4998 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00004999
Owen Andersone50ed302009-08-10 22:56:29 +00005000 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005001
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005002 // Do not generate loads of non-round integer types since these can
5003 // be expensive (and would be wrong if the type is not byte sized).
5004 if (!ExtVT.isRound())
5005 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005006
Evan Chengc88138f2007-03-22 01:54:19 +00005007 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005008 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005009 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005010 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005011 // Is the shift amount a multiple of size of VT?
5012 if ((ShAmt & (EVTBits-1)) == 0) {
5013 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005014 // Is the load width a multiple of size of VT?
5015 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005016 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005017 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005018
Chris Lattnercbf68df2010-12-22 08:02:57 +00005019 // At this point, we must have a load or else we can't do the transform.
5020 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005021
Chris Lattner2831a192010-10-01 05:36:09 +00005022 // If the shift amount is larger than the input type then we're not
5023 // accessing any of the loaded bytes. If the load was a zextload/extload
5024 // then the result of the shift+trunc is zero/undef (handled elsewhere).
5025 // If the load was a sextload then the result is a splat of the sign bit
5026 // of the extended byte. This is not worth optimizing for.
Chris Lattnercbf68df2010-12-22 08:02:57 +00005027 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005028 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005029 }
5030 }
5031
Dan Gohman394d6292010-11-03 01:47:46 +00005032 // If the load is shifted left (and the result isn't shifted back right),
5033 // we can fold the truncate through the shift.
5034 unsigned ShLeftAmt = 0;
5035 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005036 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005037 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5038 ShLeftAmt = N01->getZExtValue();
5039 N0 = N0.getOperand(0);
5040 }
5041 }
Owen Anderson95771af2011-02-25 21:41:48 +00005042
Chris Lattner4c32bc22010-12-22 07:36:50 +00005043 // If we haven't found a load, we can't narrow it. Don't transform one with
5044 // multiple uses, this would require adding a new load.
5045 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() ||
5046 // Don't change the width of a volatile load.
5047 cast<LoadSDNode>(N0)->isVolatile())
5048 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005049
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005050 // Verify that we are actually reducing a load width here.
5051 if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005052 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005053
Chris Lattner4c32bc22010-12-22 07:36:50 +00005054 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5055 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005056
Evan Cheng16436df2012-06-26 01:19:33 +00005057 if (PtrType == MVT::Untyped || PtrType.isExtended())
5058 // It's not possible to generate a constant of extended or untyped type.
5059 return SDValue();
5060
Chris Lattner4c32bc22010-12-22 07:36:50 +00005061 // For big endian targets, we need to adjust the offset to the pointer to
5062 // load the correct bytes.
5063 if (TLI.isBigEndian()) {
5064 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5065 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5066 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005067 }
5068
Chris Lattner4c32bc22010-12-22 07:36:50 +00005069 uint64_t PtrOff = ShAmt / 8;
5070 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
5071 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
5072 PtrType, LN0->getBasePtr(),
5073 DAG.getConstant(PtrOff, PtrType));
5074 AddToWorkList(NewPtr.getNode());
5075
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005076 SDValue Load;
5077 if (ExtType == ISD::NON_EXTLOAD)
5078 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
5079 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005080 LN0->isVolatile(), LN0->isNonTemporal(),
5081 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005082 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00005083 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005084 LN0->getPointerInfo().getWithOffset(PtrOff),
5085 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5086 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005087
5088 // Replace the old load's chain with the new load's chain.
5089 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005090 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005091
5092 // Shift the result left, if we've swallowed a left shift.
5093 SDValue Result = Load;
5094 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005095 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005096 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5097 ShImmTy = VT;
5098 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
5099 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
5100 }
5101
5102 // Return the new loaded value.
5103 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005104}
5105
Dan Gohman475871a2008-07-27 21:46:04 +00005106SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5107 SDValue N0 = N->getOperand(0);
5108 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005109 EVT VT = N->getValueType(0);
5110 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005111 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005112 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005113
Nate Begeman1d4d4142005-09-01 00:19:25 +00005114 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005115 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00005116 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005117
Chris Lattner541a24f2006-05-06 22:43:44 +00005118 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005119 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005120 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005121
Nate Begeman646d7e22005-09-02 21:18:40 +00005122 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5123 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005124 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00005125 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5126 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005127 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005128
Dan Gohman75dcf082008-07-31 00:50:31 +00005129 // fold (sext_in_reg (sext x)) -> (sext x)
5130 // fold (sext_in_reg (aext x)) -> (sext x)
5131 // if x is small enough.
5132 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5133 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005134 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5135 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00005136 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005137 }
5138
Chris Lattner95a5e052007-04-17 19:03:21 +00005139 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005140 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005141 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattner95a5e052007-04-17 19:03:21 +00005143 // fold operands of sext_in_reg based on knowledge that the top bits are not
5144 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005145 if (SimplifyDemandedBits(SDValue(N, 0)))
5146 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005147
Evan Chengc88138f2007-03-22 01:54:19 +00005148 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5149 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005150 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005151 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005152 return NarrowLoad;
5153
Bill Wendling8509c902009-01-30 22:33:24 +00005154 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
5155 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005156 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5157 if (N0.getOpcode() == ISD::SRL) {
5158 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005159 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00005160 // We can turn this into an SRA iff the input to the SRL is already sign
5161 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005162 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005163 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00005164 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
5165 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005166 }
5167 }
Evan Chengc88138f2007-03-22 01:54:19 +00005168
Nate Begemanded49632005-10-13 03:11:28 +00005169 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005170 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005171 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005172 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005173 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005174 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005175 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005176 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005177 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005178 LN0->getBasePtr(), LN0->getPointerInfo(),
5179 EVT,
David Greene1e559442010-02-15 17:00:31 +00005180 LN0->isVolatile(), LN0->isNonTemporal(),
5181 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005182 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005183 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005184 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005185 }
5186 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005187 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005188 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005189 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005190 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005191 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005192 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005193 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005194 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005195 LN0->getBasePtr(), LN0->getPointerInfo(),
5196 EVT,
David Greene1e559442010-02-15 17:00:31 +00005197 LN0->isVolatile(), LN0->isNonTemporal(),
5198 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005199 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005200 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005201 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005202 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005203
5204 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5205 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5206 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5207 N0.getOperand(1), false);
5208 if (BSwap.getNode() != 0)
5209 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5210 BSwap, N1);
5211 }
5212
Dan Gohman475871a2008-07-27 21:46:04 +00005213 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005214}
5215
Dan Gohman475871a2008-07-27 21:46:04 +00005216SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5217 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005218 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005219 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005220
5221 // noop truncate
5222 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005223 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005224 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005225 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00005226 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005227 // fold (truncate (truncate x)) -> (truncate x)
5228 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00005229 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005230 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005231 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5232 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005233 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005234 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005235 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005236 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5237 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005238 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005239 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005240 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005241 else
5242 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00005243 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00005244 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005245 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005246
Nadav Rotemcc870a82012-02-05 11:39:23 +00005247 // Fold extract-and-trunc into a narrow extract. For example:
5248 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5249 // i32 y = TRUNCATE(i64 x)
5250 // -- becomes --
5251 // v16i8 b = BITCAST (v2i64 val)
5252 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5253 //
5254 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005255 // creates this pattern) and before operation legalization after which
5256 // we need to be more careful about the vector instructions that we generate.
5257 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5258 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5259
5260 EVT VecTy = N0.getOperand(0).getValueType();
5261 EVT ExTy = N0.getValueType();
5262 EVT TrTy = N->getValueType(0);
5263
5264 unsigned NumElem = VecTy.getVectorNumElements();
5265 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5266
5267 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5268 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5269
5270 SDValue EltNo = N0->getOperand(1);
5271 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5272 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005273 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005274 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5275
5276 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5277 NVT, N0.getOperand(0));
5278
5279 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5280 N->getDebugLoc(), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005281 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005282 }
5283 }
5284
Chris Lattner2b4c2792007-10-13 06:35:54 +00005285 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005286 // only the low bits are being used.
5287 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005288 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005289 // may have different active low bits.
5290 if (!VT.isVector()) {
5291 SDValue Shorter =
5292 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5293 VT.getSizeInBits()));
5294 if (Shorter.getNode())
5295 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5296 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005297 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005298 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005299 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5300 SDValue Reduced = ReduceLoadWidth(N);
5301 if (Reduced.getNode())
5302 return Reduced;
5303 }
5304
5305 // Simplify the operands using demanded-bits information.
5306 if (!VT.isVector() &&
5307 SimplifyDemandedBits(SDValue(N, 0)))
5308 return SDValue(N, 0);
5309
Evan Chenge5b51ac2010-04-17 06:13:15 +00005310 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005311}
5312
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005313static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005314 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005315 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005316 return Elt.getNode();
5317 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005318}
5319
5320/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005321/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005322SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005323 assert(N->getOpcode() == ISD::BUILD_PAIR);
5324
Nate Begemanabc01992009-06-05 21:37:30 +00005325 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5326 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005327 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5328 LD1->getPointerInfo().getAddrSpace() !=
5329 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005330 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005331 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005332
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005333 if (ISD::isNON_EXTLoad(LD2) &&
5334 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005335 // If both are volatile this would reduce the number of volatile loads.
5336 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005337 !LD1->isVolatile() &&
5338 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005339 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005340 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00005341 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005342 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005343
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005344 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005345 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005346 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005347 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005348 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005349 }
Bill Wendling67a67682009-01-30 22:44:24 +00005350
Dan Gohman475871a2008-07-27 21:46:04 +00005351 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005352}
5353
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005354SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005355 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005356 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005357
Dan Gohman7f321562007-06-25 16:23:39 +00005358 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5359 // Only do this before legalize, since afterward the target may be depending
5360 // on the bitconvert.
5361 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005362 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005363 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005364 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005365 bool isSimple = true;
5366 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5367 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5368 N0.getOperand(i).getOpcode() != ISD::Constant &&
5369 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005370 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005371 break;
5372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005373
Owen Andersone50ed302009-08-10 22:56:29 +00005374 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005375 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005376 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005377 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005378 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005379 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005380
Dan Gohman3dd168d2008-09-05 01:58:21 +00005381 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005382 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005383 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005384 if (Res.getNode() != N) {
5385 if (!LegalOperations ||
5386 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5387 return Res;
5388
5389 // Folding it resulted in an illegal node, and it's too late to
5390 // do that. Clean up the old node and forego the transformation.
5391 // Ideally this won't happen very often, because instcombine
5392 // and the earlier dagcombine runs (where illegal nodes are
5393 // permitted) should have folded most of them already.
5394 DAG.DeleteNode(Res.getNode());
5395 }
Chris Lattner94683772005-12-23 05:30:37 +00005396 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005397
Bill Wendling67a67682009-01-30 22:44:24 +00005398 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005399 if (N0.getOpcode() == ISD::BITCAST)
5400 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005401 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005402
Chris Lattner57104102005-12-23 05:44:41 +00005403 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005404 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005405 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005406 // Do not change the width of a volatile load.
5407 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005408 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005409 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00005410 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005411 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005412 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005413
Evan Cheng59d5b682007-05-07 21:27:48 +00005414 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005415 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005416 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005417 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005418 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005419 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005420 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005421 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005422 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005423 Load.getValue(1));
5424 return Load;
5425 }
Chris Lattner57104102005-12-23 05:44:41 +00005426 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005427
Bill Wendling67a67682009-01-30 22:44:24 +00005428 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5429 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005430 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005431 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5432 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005433 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005434 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005435 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005436 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005437
Duncan Sands83ec4b62008-06-06 12:08:01 +00005438 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005439 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005440 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5441 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005442 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005443 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5444 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005446
Bill Wendling67a67682009-01-30 22:44:24 +00005447 // fold (bitconvert (fcopysign cst, x)) ->
5448 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5449 // Note that we don't handle (copysign x, cst) because this can always be
5450 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005451 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005452 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005453 VT.isInteger() && !VT.isVector()) {
5454 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005455 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005456 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005457 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005458 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005459 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005460
Duncan Sands25cf2272008-11-24 14:53:14 +00005461 // If X has a different width than the result/lhs, sext it or truncate it.
5462 unsigned VTWidth = VT.getSizeInBits();
5463 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005464 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005465 AddToWorkList(X.getNode());
5466 } else if (OrigXWidth > VTWidth) {
5467 // To get the sign bit in the right place, we have to shift it right
5468 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005469 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005470 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005471 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5472 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005473 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005474 AddToWorkList(X.getNode());
5475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005476
Duncan Sands25cf2272008-11-24 14:53:14 +00005477 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005478 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005479 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005480 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005481
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005482 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005483 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005484 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005485 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005486 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005487
Bill Wendling67a67682009-01-30 22:44:24 +00005488 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005489 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005490 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005491
Scott Michelfdc40a02009-02-17 22:15:04 +00005492 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005493 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005494 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5495 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005496 return CombineLD;
5497 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005498
Dan Gohman475871a2008-07-27 21:46:04 +00005499 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005500}
5501
Dan Gohman475871a2008-07-27 21:46:04 +00005502SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005503 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005504 return CombineConsecutiveLoads(N, VT);
5505}
5506
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005507/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005508/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005509/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005510SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005511ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005512 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005513
Chris Lattner6258fb22006-04-02 02:53:43 +00005514 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005515 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Duncan Sands83ec4b62008-06-06 12:08:01 +00005517 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5518 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005519
Chris Lattner6258fb22006-04-02 02:53:43 +00005520 // If this is a conversion of N elements of one type to N elements of another
5521 // type, convert each element. This handles FP<->INT cases.
5522 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005523 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5524 BV->getValueType(0).getVectorNumElements());
5525
5526 // Due to the FP element handling below calling this routine recursively,
5527 // we can end up with a scalar-to-vector node here.
5528 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005529 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5530 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005531 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005532
Dan Gohman475871a2008-07-27 21:46:04 +00005533 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005534 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005535 SDValue Op = BV->getOperand(i);
5536 // If the vector element type is not legal, the BUILD_VECTOR operands
5537 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005538 if (Op.getValueType() != SrcEltVT)
5539 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005540 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005541 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005542 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005543 }
Evan Chenga87008d2009-02-25 22:49:59 +00005544 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5545 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005546 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005547
Chris Lattner6258fb22006-04-02 02:53:43 +00005548 // Otherwise, we're growing or shrinking the elements. To avoid having to
5549 // handle annoying details of growing/shrinking FP values, we convert them to
5550 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005551 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005552 // Convert the input float vector to a int vector where the elements are the
5553 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005555 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005556 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005557 SrcEltVT = IntVT;
5558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005559
Chris Lattner6258fb22006-04-02 02:53:43 +00005560 // Now we know the input is an integer vector. If the output is a FP type,
5561 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005562 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005563 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005564 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005565 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005566
Chris Lattner6258fb22006-04-02 02:53:43 +00005567 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005568 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005569 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005570
Chris Lattner6258fb22006-04-02 02:53:43 +00005571 // Okay, we know the src/dst types are both integers of differing types.
5572 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005573 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005574 if (SrcBitSize < DstBitSize) {
5575 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005576
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005578 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005579 i += NumInputsPerOutput) {
5580 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005581 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005582 bool EltIsUndef = true;
5583 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5584 // Shift the previously computed bits over.
5585 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005587 if (Op.getOpcode() == ISD::UNDEF) continue;
5588 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005589
Jay Foad40f8f622010-12-07 08:25:19 +00005590 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005591 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005593
Chris Lattner6258fb22006-04-02 02:53:43 +00005594 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005595 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005596 else
5597 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5598 }
5599
Owen Anderson23b9b192009-08-12 00:36:31 +00005600 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005601 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5602 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005604
Chris Lattner6258fb22006-04-02 02:53:43 +00005605 // Finally, this must be the case where we are shrinking elements: each input
5606 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005607 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005608 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005609 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5610 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005611 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005612
Dan Gohman7f321562007-06-25 16:23:39 +00005613 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005614 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5615 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005616 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005617 continue;
5618 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005619
Jay Foad40f8f622010-12-07 08:25:19 +00005620 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5621 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005622
Chris Lattner6258fb22006-04-02 02:53:43 +00005623 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005624 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005625 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005626 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005627 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005628 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5629 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005630 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005631 }
5632
5633 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005634 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005635 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5636 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005637
Evan Chenga87008d2009-02-25 22:49:59 +00005638 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5639 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005640}
5641
Dan Gohman475871a2008-07-27 21:46:04 +00005642SDValue DAGCombiner::visitFADD(SDNode *N) {
5643 SDValue N0 = N->getOperand(0);
5644 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005645 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5646 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005647 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005648
Dan Gohman7f321562007-06-25 16:23:39 +00005649 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005650 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005651 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005652 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005653 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005654
Lang Hames01806942012-06-14 20:37:15 +00005655 // fold (fadd c1, c2) -> c1 + c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005657 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005658 // canonicalize constant to RHS
5659 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005660 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5661 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005662 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5663 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005664 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005665 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005666 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5667 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005668 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005669 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005670 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005671 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5672 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005673 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005674 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005675
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005676 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005677 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5678 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5679 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005680 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005681 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5682 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005683
Owen Anderson43da6c72012-08-30 23:35:16 +00005684 // In unsafe math mode, we can fold chains of FADD's of the same value
5685 // into multiplications. This transform is not safe in general because
5686 // we are reducing the number of rounding steps.
5687 if (DAG.getTarget().Options.UnsafeFPMath &&
5688 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5689 !N0CFP && !N1CFP) {
5690 if (N0.getOpcode() == ISD::FMUL) {
5691 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5692 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5693
5694 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5695 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
5696 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5697 SDValue(CFP00, 0),
5698 DAG.getConstantFP(1.0, VT));
5699 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5700 N1, NewCFP);
5701 }
5702
5703 // (fadd (fmul x, c), x) -> (fmul c+1, x)
5704 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
5705 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5706 SDValue(CFP01, 0),
5707 DAG.getConstantFP(1.0, VT));
5708 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5709 N1, NewCFP);
5710 }
5711
5712 // (fadd (fadd x, x), x) -> (fmul 3.0, x)
5713 if (!CFP00 && !CFP01 && N0.getOperand(0) == N0.getOperand(1) &&
5714 N0.getOperand(0) == N1) {
5715 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5716 N1, DAG.getConstantFP(3.0, VT));
5717 }
5718
5719 // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x)
5720 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5721 N1.getOperand(0) == N1.getOperand(1) &&
5722 N0.getOperand(1) == N1.getOperand(0)) {
5723 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5724 SDValue(CFP00, 0),
5725 DAG.getConstantFP(2.0, VT));
5726 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5727 N0.getOperand(1), NewCFP);
5728 }
5729
5730 // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x)
5731 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5732 N1.getOperand(0) == N1.getOperand(1) &&
5733 N0.getOperand(0) == N1.getOperand(0)) {
5734 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5735 SDValue(CFP01, 0),
5736 DAG.getConstantFP(2.0, VT));
5737 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5738 N0.getOperand(0), NewCFP);
5739 }
5740 }
5741
5742 if (N1.getOpcode() == ISD::FMUL) {
5743 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5744 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5745
5746 // (fadd x, (fmul c, x)) -> (fmul c+1, x)
5747 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
5748 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5749 SDValue(CFP10, 0),
5750 DAG.getConstantFP(1.0, VT));
5751 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5752 N0, NewCFP);
5753 }
5754
5755 // (fadd x, (fmul x, c)) -> (fmul c+1, x)
5756 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
5757 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5758 SDValue(CFP11, 0),
5759 DAG.getConstantFP(1.0, VT));
5760 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5761 N0, NewCFP);
5762 }
5763
5764 // (fadd x, (fadd x, x)) -> (fmul 3.0, x)
5765 if (!CFP10 && !CFP11 && N1.getOperand(0) == N1.getOperand(1) &&
5766 N1.getOperand(0) == N0) {
5767 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5768 N0, DAG.getConstantFP(3.0, VT));
5769 }
5770
5771 // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x)
5772 if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD &&
5773 N1.getOperand(0) == N1.getOperand(1) &&
5774 N0.getOperand(1) == N1.getOperand(0)) {
5775 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5776 SDValue(CFP10, 0),
5777 DAG.getConstantFP(2.0, VT));
5778 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5779 N0.getOperand(1), NewCFP);
5780 }
5781
5782 // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x)
5783 if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD &&
5784 N1.getOperand(0) == N1.getOperand(1) &&
5785 N0.getOperand(0) == N1.getOperand(0)) {
5786 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5787 SDValue(CFP11, 0),
5788 DAG.getConstantFP(2.0, VT));
5789 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5790 N0.getOperand(0), NewCFP);
5791 }
5792 }
5793
5794 // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x)
5795 if (N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
5796 N0.getOperand(0) == N0.getOperand(1) &&
5797 N1.getOperand(0) == N1.getOperand(1) &&
5798 N0.getOperand(0) == N1.getOperand(0)) {
5799 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5800 N0.getOperand(0),
5801 DAG.getConstantFP(4.0, VT));
5802 }
5803 }
5804
Lang Hamesd693caf2012-06-19 22:51:23 +00005805 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005806 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005807 DAG.getTarget().Options.UnsafeFPMath) &&
5808 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005809 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005810
5811 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
5812 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
5813 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5814 N0.getOperand(0), N0.getOperand(1), N1);
5815 }
Owen Anderson43da6c72012-08-30 23:35:16 +00005816
Michael Liaob79bff52012-09-01 04:09:16 +00005817 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00005818 // Note: Commutes FADD operands.
5819 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
5820 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5821 N1.getOperand(0), N1.getOperand(1), N0);
5822 }
5823 }
5824
Dan Gohman475871a2008-07-27 21:46:04 +00005825 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005826}
5827
Dan Gohman475871a2008-07-27 21:46:04 +00005828SDValue DAGCombiner::visitFSUB(SDNode *N) {
5829 SDValue N0 = N->getOperand(0);
5830 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005831 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5832 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005833 EVT VT = N->getValueType(0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005834 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005835
Dan Gohman7f321562007-06-25 16:23:39 +00005836 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005837 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005838 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005839 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005841
Nate Begemana0e221d2005-10-18 00:28:13 +00005842 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005843 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005844 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005845 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005846 if (DAG.getTarget().Options.UnsafeFPMath &&
5847 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00005848 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005849 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005850 if (DAG.getTarget().Options.UnsafeFPMath &&
5851 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005852 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005853 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00005854 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005855 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00005856 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005857 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005858 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005859 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005860 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005861
Bill Wendling5a894342012-03-15 05:12:00 +00005862 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00005863 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00005864 // (fsub x, (fadd x, y)) -> (fneg y) &
5865 // (fsub x, (fadd y, x)) -> (fneg y)
5866 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00005867 if (N0 == N1)
5868 return DAG.getConstantFP(0.0f, VT);
5869
Bill Wendling5a894342012-03-15 05:12:00 +00005870 if (N1.getOpcode() == ISD::FADD) {
5871 SDValue N10 = N1->getOperand(0);
5872 SDValue N11 = N1->getOperand(1);
5873
5874 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
5875 &DAG.getTarget().Options))
5876 return GetNegatedExpression(N11, DAG, LegalOperations);
5877 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
5878 &DAG.getTarget().Options))
5879 return GetNegatedExpression(N10, DAG, LegalOperations);
5880 }
5881 }
5882
Lang Hamesd693caf2012-06-19 22:51:23 +00005883 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005884 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005885 DAG.getTarget().Options.UnsafeFPMath) &&
5886 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005887 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005888
5889 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
5890 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005891 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005892 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005893 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00005894 }
5895
5896 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
5897 // Note: Commutes FSUB operands.
5898 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005899 return DAG.getNode(ISD::FMA, dl, VT,
5900 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005901 N1.getOperand(0)),
5902 N1.getOperand(1), N0);
5903 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005904
5905 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
5906 if (N0.getOpcode() == ISD::FNEG &&
5907 N0.getOperand(0).getOpcode() == ISD::FMUL &&
5908 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
5909 SDValue N00 = N0.getOperand(0).getOperand(0);
5910 SDValue N01 = N0.getOperand(0).getOperand(1);
5911 return DAG.getNode(ISD::FMA, dl, VT,
5912 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
5913 DAG.getNode(ISD::FNEG, dl, VT, N1));
5914 }
Lang Hamesd693caf2012-06-19 22:51:23 +00005915 }
5916
Dan Gohman475871a2008-07-27 21:46:04 +00005917 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005918}
5919
Dan Gohman475871a2008-07-27 21:46:04 +00005920SDValue DAGCombiner::visitFMUL(SDNode *N) {
5921 SDValue N0 = N->getOperand(0);
5922 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005923 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5924 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005925 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00005926 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00005927
Dan Gohman7f321562007-06-25 16:23:39 +00005928 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005929 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005930 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005931 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005933
Nate Begeman11af4ea2005-10-17 20:40:11 +00005934 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005935 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005936 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005937 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00005938 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005939 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
5940 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005941 if (DAG.getTarget().Options.UnsafeFPMath &&
5942 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005943 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00005944 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005945 if (DAG.getTarget().Options.UnsafeFPMath &&
5946 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00005947 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00005948 // fold (fmul A, 1.0) -> A
5949 if (N1CFP && N1CFP->isExactlyValue(1.0))
5950 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00005951 // fold (fmul X, 2.0) -> (fadd X, X)
5952 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005953 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00005954 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00005955 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00005956 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005957 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005958
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005959 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00005960 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005961 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005962 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005963 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005964 // Both can be negated for free, check to see if at least one is cheaper
5965 // negated.
5966 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005967 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005968 GetNegatedExpression(N0, DAG, LegalOperations),
5969 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005970 }
5971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005972
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005973 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005974 if (DAG.getTarget().Options.UnsafeFPMath &&
5975 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005976 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005977 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005978 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00005979 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005980
Dan Gohman475871a2008-07-27 21:46:04 +00005981 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005982}
5983
Owen Anderson062c0a52012-05-02 22:17:40 +00005984SDValue DAGCombiner::visitFMA(SDNode *N) {
5985 SDValue N0 = N->getOperand(0);
5986 SDValue N1 = N->getOperand(1);
5987 SDValue N2 = N->getOperand(2);
5988 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5989 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
5990 EVT VT = N->getValueType(0);
Owen Anderson58d57292012-09-01 06:04:27 +00005991 DebugLoc dl = N->getDebugLoc();
Owen Anderson062c0a52012-05-02 22:17:40 +00005992
5993 if (N0CFP && N0CFP->isExactlyValue(1.0))
5994 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2);
5995 if (N1CFP && N1CFP->isExactlyValue(1.0))
5996 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2);
5997
Owen Anderson85ef6f42012-05-30 18:50:39 +00005998 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00005999 if (N0CFP && !N1CFP)
Owen Anderson85ef6f42012-05-30 18:50:39 +00006000 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2);
6001
Owen Anderson58d57292012-09-01 06:04:27 +00006002 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6003 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6004 N2.getOpcode() == ISD::FMUL &&
6005 N0 == N2.getOperand(0) &&
6006 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6007 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6008 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6009 }
6010
6011
6012 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6013 if (DAG.getTarget().Options.UnsafeFPMath &&
6014 N0.getOpcode() == ISD::FMUL && N1CFP &&
6015 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6016 return DAG.getNode(ISD::FMA, dl, VT,
6017 N0.getOperand(0),
6018 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6019 N2);
6020 }
6021
6022 // (fma x, 1, y) -> (fadd x, y)
6023 // (fma x, -1, y) -> (fadd (fneg x), y)
6024 if (N1CFP) {
6025 if (N1CFP->isExactlyValue(1.0))
6026 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6027
6028 if (N1CFP->isExactlyValue(-1.0) &&
6029 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6030 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6031 AddToWorkList(RHSNeg.getNode());
6032 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6033 }
6034 }
6035
6036 // (fma x, c, x) -> (fmul x, (c+1))
6037 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6038 return DAG.getNode(ISD::FMUL, dl, VT,
6039 N0,
6040 DAG.getNode(ISD::FADD, dl, VT,
6041 N1, DAG.getConstantFP(1.0, VT)));
6042 }
6043
6044 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6045 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6046 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6047 return DAG.getNode(ISD::FMUL, dl, VT,
6048 N0,
6049 DAG.getNode(ISD::FADD, dl, VT,
6050 N1, DAG.getConstantFP(-1.0, VT)));
6051 }
6052
6053
Owen Anderson062c0a52012-05-02 22:17:40 +00006054 return SDValue();
6055}
6056
Dan Gohman475871a2008-07-27 21:46:04 +00006057SDValue DAGCombiner::visitFDIV(SDNode *N) {
6058 SDValue N0 = N->getOperand(0);
6059 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006060 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6061 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006062 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006063 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006064
Dan Gohman7f321562007-06-25 16:23:39 +00006065 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006066 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006067 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006068 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006069 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006070
Nate Begemana148d982006-01-18 22:35:16 +00006071 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00006072 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006073 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006074
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006075 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
6076 if (N1CFP && VT != MVT::ppcf128 && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006077 // Compute the reciprocal 1.0 / c2.
6078 APFloat N1APF = N1CFP->getValueAPF();
6079 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6080 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006081 // Only do the transform if the reciprocal is a legal fp immediate that
6082 // isn't too nasty (eg NaN, denormal, ...).
6083 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006084 (!LegalOperations ||
6085 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6086 // backend)... we should handle this gracefully after Legalize.
6087 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6088 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6089 TLI.isFPImmLegal(Recip, VT)))
Duncan Sands961d6662012-04-07 20:04:00 +00006090 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0,
6091 DAG.getConstantFP(Recip, VT));
6092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006093
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006094 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006095 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006096 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006097 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006098 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006099 // Both can be negated for free, check to see if at least one is cheaper
6100 // negated.
6101 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00006102 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006103 GetNegatedExpression(N0, DAG, LegalOperations),
6104 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006105 }
6106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006107
Dan Gohman475871a2008-07-27 21:46:04 +00006108 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006109}
6110
Dan Gohman475871a2008-07-27 21:46:04 +00006111SDValue DAGCombiner::visitFREM(SDNode *N) {
6112 SDValue N0 = N->getOperand(0);
6113 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006114 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6115 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006116 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006117
Nate Begemana148d982006-01-18 22:35:16 +00006118 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006120 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006121
Dan Gohman475871a2008-07-27 21:46:04 +00006122 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006123}
6124
Dan Gohman475871a2008-07-27 21:46:04 +00006125SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6126 SDValue N0 = N->getOperand(0);
6127 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006128 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6129 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006130 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006131
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006133 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006134
Chris Lattner12d83032006-03-05 05:30:57 +00006135 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006136 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00006137 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6138 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006139 if (!V.isNegative()) {
6140 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006141 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006142 } else {
6143 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006144 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00006145 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006146 }
Chris Lattner12d83032006-03-05 05:30:57 +00006147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006148
Chris Lattner12d83032006-03-05 05:30:57 +00006149 // copysign(fabs(x), y) -> copysign(x, y)
6150 // copysign(fneg(x), y) -> copysign(x, y)
6151 // copysign(copysign(x,z), y) -> copysign(x, y)
6152 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6153 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006154 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6155 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006156
6157 // copysign(x, abs(y)) -> abs(x)
6158 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006159 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006160
Chris Lattner12d83032006-03-05 05:30:57 +00006161 // copysign(x, copysign(y,z)) -> copysign(x, z)
6162 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006163 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6164 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006165
Chris Lattner12d83032006-03-05 05:30:57 +00006166 // copysign(x, fp_extend(y)) -> copysign(x, y)
6167 // copysign(x, fp_round(y)) -> copysign(x, y)
6168 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006169 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6170 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006171
Dan Gohman475871a2008-07-27 21:46:04 +00006172 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006173}
6174
Dan Gohman475871a2008-07-27 21:46:04 +00006175SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6176 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006177 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006178 EVT VT = N->getValueType(0);
6179 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006180
Nate Begeman1d4d4142005-09-01 00:19:25 +00006181 // fold (sint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006182 if (N0C && OpVT != MVT::ppcf128 &&
6183 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006184 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006185 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006186 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006187
Chris Lattnercda88752008-06-26 00:16:49 +00006188 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6189 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006190 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6191 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006192 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006193 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006194 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006195 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006196
Nadav Rotemed1a3352012-07-23 07:59:50 +00006197 // The next optimizations are desireable only if SELECT_CC can be lowered.
6198 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6199 // having to say they don't support SELECT_CC on every type the DAG knows
6200 // about, since there is no way to mark an opcode illegal at all value types
6201 // (See also visitSELECT)
6202 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6203 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6204 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6205 !VT.isVector() &&
6206 (!LegalOperations ||
6207 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6208 SDValue Ops[] =
6209 { N0.getOperand(0), N0.getOperand(1),
6210 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6211 N0.getOperand(2) };
6212 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6213 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006214
Nadav Rotemed1a3352012-07-23 07:59:50 +00006215 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6216 // (select_cc x, y, 1.0, 0.0,, cc)
6217 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6218 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6219 (!LegalOperations ||
6220 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6221 SDValue Ops[] =
6222 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6223 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6224 N0.getOperand(0).getOperand(2) };
6225 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6226 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006227 }
6228
Dan Gohman475871a2008-07-27 21:46:04 +00006229 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006230}
6231
Dan Gohman475871a2008-07-27 21:46:04 +00006232SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6233 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006234 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006235 EVT VT = N->getValueType(0);
6236 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006237
Nate Begeman1d4d4142005-09-01 00:19:25 +00006238 // fold (uint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006239 if (N0C && OpVT != MVT::ppcf128 &&
6240 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006241 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006242 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006243 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006244
Chris Lattnercda88752008-06-26 00:16:49 +00006245 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6246 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006247 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6248 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006249 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006250 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006251 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006253
Nadav Rotemed1a3352012-07-23 07:59:50 +00006254 // The next optimizations are desireable only if SELECT_CC can be lowered.
6255 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6256 // having to say they don't support SELECT_CC on every type the DAG knows
6257 // about, since there is no way to mark an opcode illegal at all value types
6258 // (See also visitSELECT)
6259 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6260 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006261
Nadav Rotemed1a3352012-07-23 07:59:50 +00006262 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6263 (!LegalOperations ||
6264 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6265 SDValue Ops[] =
6266 { N0.getOperand(0), N0.getOperand(1),
6267 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6268 N0.getOperand(2) };
6269 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6270 }
6271 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006272
Dan Gohman475871a2008-07-27 21:46:04 +00006273 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006274}
6275
Dan Gohman475871a2008-07-27 21:46:04 +00006276SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6277 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006278 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006279 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006280
Nate Begeman1d4d4142005-09-01 00:19:25 +00006281 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006282 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006283 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
6284
Dan Gohman475871a2008-07-27 21:46:04 +00006285 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006286}
6287
Dan Gohman475871a2008-07-27 21:46:04 +00006288SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6289 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006290 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006291 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006292
Nate Begeman1d4d4142005-09-01 00:19:25 +00006293 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00006294 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006295 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
6296
Dan Gohman475871a2008-07-27 21:46:04 +00006297 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006298}
6299
Dan Gohman475871a2008-07-27 21:46:04 +00006300SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6301 SDValue N0 = N->getOperand(0);
6302 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006303 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006304 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006305
Nate Begeman1d4d4142005-09-01 00:19:25 +00006306 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006307 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006308 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006309
Chris Lattner79dbea52006-03-13 06:26:26 +00006310 // fold (fp_round (fp_extend x)) -> x
6311 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6312 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006313
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006314 // fold (fp_round (fp_round x)) -> (fp_round x)
6315 if (N0.getOpcode() == ISD::FP_ROUND) {
6316 // This is a value preserving truncation if both round's are.
6317 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006318 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00006319 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006320 DAG.getIntPtrConstant(IsTrunc));
6321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006322
Chris Lattner79dbea52006-03-13 06:26:26 +00006323 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006324 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00006325 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
6326 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006327 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00006328 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6329 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006331
Dan Gohman475871a2008-07-27 21:46:04 +00006332 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006333}
6334
Dan Gohman475871a2008-07-27 21:46:04 +00006335SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6336 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006337 EVT VT = N->getValueType(0);
6338 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006339 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006340
Nate Begeman1d4d4142005-09-01 00:19:25 +00006341 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006342 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006343 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006344 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006345 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006346
Dan Gohman475871a2008-07-27 21:46:04 +00006347 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006348}
6349
Dan Gohman475871a2008-07-27 21:46:04 +00006350SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6351 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006352 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006353 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006354
Chris Lattner5938bef2007-12-29 06:55:23 +00006355 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006356 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006357 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006358 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006359
Nate Begeman1d4d4142005-09-01 00:19:25 +00006360 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006361 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006362 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006363
6364 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6365 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006366 if (N0.getOpcode() == ISD::FP_ROUND
6367 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006368 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006369 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006370 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006371 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
6372 In, N0.getOperand(1));
6373 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006375
Chris Lattner0bd48932008-01-17 07:00:52 +00006376 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006377 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006378 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006379 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006380 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00006381 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006382 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006383 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006384 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006385 LN0->isVolatile(), LN0->isNonTemporal(),
6386 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006387 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006388 CombineTo(N0.getNode(),
6389 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
6390 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006391 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006392 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006393 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006394
Dan Gohman475871a2008-07-27 21:46:04 +00006395 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006396}
6397
Dan Gohman475871a2008-07-27 21:46:04 +00006398SDValue DAGCombiner::visitFNEG(SDNode *N) {
6399 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006400 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006401
Owen Andersonafd3d562012-03-06 00:29:31 +00006402 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6403 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006404 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006405
Chris Lattner3bd39d42008-01-27 17:42:27 +00006406 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6407 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006408 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006409 !VT.isVector() &&
6410 N0.getNode()->hasOneUse() &&
6411 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006412 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006413 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006414 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006415 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
6416 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006417 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006418 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006419 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006420 }
6421 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006422
Owen Anderson58d57292012-09-01 06:04:27 +00006423 // (fneg (fmul c, x)) -> (fmul -c, x)
6424 if (N0.getOpcode() == ISD::FMUL) {
6425 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6426 if (CFP1) {
6427 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6428 N0.getOperand(0),
6429 DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
6430 N0.getOperand(1)));
6431 }
6432 }
6433
Dan Gohman475871a2008-07-27 21:46:04 +00006434 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006435}
6436
Owen Anderson7c626d32012-08-13 23:32:49 +00006437SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6438 SDValue N0 = N->getOperand(0);
6439 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6440 EVT VT = N->getValueType(0);
6441
6442 // fold (fceil c1) -> fceil(c1)
6443 if (N0CFP && VT != MVT::ppcf128)
6444 return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0);
6445
6446 return SDValue();
6447}
6448
6449SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6450 SDValue N0 = N->getOperand(0);
6451 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6452 EVT VT = N->getValueType(0);
6453
6454 // fold (ftrunc c1) -> ftrunc(c1)
6455 if (N0CFP && VT != MVT::ppcf128)
6456 return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0);
6457
6458 return SDValue();
6459}
6460
6461SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6462 SDValue N0 = N->getOperand(0);
6463 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6464 EVT VT = N->getValueType(0);
6465
6466 // fold (ffloor c1) -> ffloor(c1)
6467 if (N0CFP && VT != MVT::ppcf128)
6468 return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0);
6469
6470 return SDValue();
6471}
6472
Dan Gohman475871a2008-07-27 21:46:04 +00006473SDValue DAGCombiner::visitFABS(SDNode *N) {
6474 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006476 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006477
Nate Begeman1d4d4142005-09-01 00:19:25 +00006478 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00006479 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006480 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006481 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006482 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006483 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006484 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006485 // fold (fabs (fcopysign x, y)) -> (fabs x)
6486 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006487 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006488
Chris Lattner3bd39d42008-01-27 17:42:27 +00006489 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6490 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006491 if (!TLI.isFAbsFree(VT) &&
6492 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006493 N0.getOperand(0).getValueType().isInteger() &&
6494 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006495 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006496 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006497 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006498 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006499 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006500 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006501 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006502 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006503 }
6504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006505
Dan Gohman475871a2008-07-27 21:46:04 +00006506 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006507}
6508
Dan Gohman475871a2008-07-27 21:46:04 +00006509SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6510 SDValue Chain = N->getOperand(0);
6511 SDValue N1 = N->getOperand(1);
6512 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006513
Dan Gohmane0f06c72009-11-17 00:47:23 +00006514 // If N is a constant we could fold this into a fallthrough or unconditional
6515 // branch. However that doesn't happen very often in normal code, because
6516 // Instcombine/SimplifyCFG should have handled the available opportunities.
6517 // If we did this folding here, it would be necessary to update the
6518 // MachineBasicBlock CFG, which is awkward.
6519
Nate Begeman750ac1b2006-02-01 07:19:44 +00006520 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6521 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006522 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006523 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
6524 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006525 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006526 N1.getOperand(0), N1.getOperand(1), N2);
6527 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006528
Evan Cheng2a135ae2010-10-04 22:41:01 +00006529 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6530 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6531 (N1.getOperand(0).hasOneUse() &&
6532 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6533 SDNode *Trunc = 0;
6534 if (N1.getOpcode() == ISD::TRUNCATE) {
6535 // Look pass the truncate.
6536 Trunc = N1.getNode();
6537 N1 = N1.getOperand(0);
6538 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006539
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006540 // Match this pattern so that we can generate simpler code:
6541 //
6542 // %a = ...
6543 // %b = and i32 %a, 2
6544 // %c = srl i32 %b, 1
6545 // brcond i32 %c ...
6546 //
6547 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006548 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006549 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006550 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006551 // %c = setcc eq %b, 0
6552 // brcond %c ...
6553 //
6554 // This applies only when the AND constant value has one bit set and the
6555 // SRL constant is equal to the log2 of the AND constant. The back-end is
6556 // smart enough to convert the result into a TEST/JMP sequence.
6557 SDValue Op0 = N1.getOperand(0);
6558 SDValue Op1 = N1.getOperand(1);
6559
6560 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006561 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006562 SDValue AndOp1 = Op0.getOperand(1);
6563
6564 if (AndOp1.getOpcode() == ISD::Constant) {
6565 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6566
6567 if (AndConst.isPowerOf2() &&
6568 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6569 SDValue SetCC =
6570 DAG.getSetCC(N->getDebugLoc(),
6571 TLI.getSetCCResultType(Op0.getValueType()),
6572 Op0, DAG.getConstant(0, Op0.getValueType()),
6573 ISD::SETNE);
6574
Evan Chengd40d03e2010-01-06 19:38:29 +00006575 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6576 MVT::Other, Chain, SetCC, N2);
6577 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6578 // will convert it back to (X & C1) >> C2.
6579 CombineTo(N, NewBRCond, false);
6580 // Truncate is dead.
6581 if (Trunc) {
6582 removeFromWorkList(Trunc);
6583 DAG.DeleteNode(Trunc);
6584 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006585 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006586 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006587 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006588 removeFromWorkList(N1.getNode());
6589 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006590 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006591 }
6592 }
6593 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006594
6595 if (Trunc)
6596 // Restore N1 if the above transformation doesn't match.
6597 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006598 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006599
Evan Cheng2c755ba2010-02-27 07:36:59 +00006600 // Transform br(xor(x, y)) -> br(x != y)
6601 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6602 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6603 SDNode *TheXor = N1.getNode();
6604 SDValue Op0 = TheXor->getOperand(0);
6605 SDValue Op1 = TheXor->getOperand(1);
6606 if (Op0.getOpcode() == Op1.getOpcode()) {
6607 // Avoid missing important xor optimizations.
6608 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00006609 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006610 DEBUG(dbgs() << "\nReplacing.8 ";
6611 TheXor->dump(&DAG);
6612 dbgs() << "\nWith: ";
6613 Tmp.getNode()->dump(&DAG);
6614 dbgs() << '\n');
6615 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006616 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006617 removeFromWorkList(TheXor);
6618 DAG.DeleteNode(TheXor);
6619 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6620 MVT::Other, Chain, Tmp, N2);
6621 }
6622 }
6623
6624 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6625 bool Equal = false;
6626 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6627 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6628 Op0.getOpcode() == ISD::XOR) {
6629 TheXor = Op0.getNode();
6630 Equal = true;
6631 }
6632
Evan Cheng2a135ae2010-10-04 22:41:01 +00006633 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006634 if (LegalTypes)
6635 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6636 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6637 SetCCVT,
6638 Op0, Op1,
6639 Equal ? ISD::SETEQ : ISD::SETNE);
6640 // Replace the uses of XOR with SETCC
6641 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006642 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006643 removeFromWorkList(N1.getNode());
6644 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006645 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6646 MVT::Other, Chain, SetCC, N2);
6647 }
6648 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006649
Dan Gohman475871a2008-07-27 21:46:04 +00006650 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006651}
6652
Chris Lattner3ea0b472005-10-05 06:47:48 +00006653// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6654//
Dan Gohman475871a2008-07-27 21:46:04 +00006655SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006656 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006657 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006658
Dan Gohmane0f06c72009-11-17 00:47:23 +00006659 // If N is a constant we could fold this into a fallthrough or unconditional
6660 // branch. However that doesn't happen very often in normal code, because
6661 // Instcombine/SimplifyCFG should have handled the available opportunities.
6662 // If we did this folding here, it would be necessary to update the
6663 // MachineBasicBlock CFG, which is awkward.
6664
Duncan Sands8eab8a22008-06-09 11:32:28 +00006665 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006666 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006667 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6668 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006669 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006670
Nate Begemane17daeb2005-10-05 21:43:42 +00006671 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006672 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006673 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006674 N->getOperand(0), Simp.getOperand(2),
6675 Simp.getOperand(0), Simp.getOperand(1),
6676 N->getOperand(4));
6677
Dan Gohman475871a2008-07-27 21:46:04 +00006678 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006679}
6680
Evan Chengc4b527a2012-01-13 01:37:24 +00006681/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6682/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006683/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006684static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6685 SelectionDAG &DAG,
6686 const TargetLowering &TLI) {
6687 EVT VT;
6688 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6689 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6690 return false;
6691 VT = Use->getValueType(0);
6692 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6693 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6694 return false;
6695 VT = ST->getValue().getValueType();
6696 } else
6697 return false;
6698
6699 TargetLowering::AddrMode AM;
6700 if (N->getOpcode() == ISD::ADD) {
6701 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6702 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006703 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006704 AM.BaseOffs = Offset->getSExtValue();
6705 else
Evan Cheng03be3622012-03-06 23:33:32 +00006706 // [reg +/- reg]
6707 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006708 } else if (N->getOpcode() == ISD::SUB) {
6709 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6710 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006711 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006712 AM.BaseOffs = -Offset->getSExtValue();
6713 else
Evan Cheng03be3622012-03-06 23:33:32 +00006714 // [reg +/- reg]
6715 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006716 } else
6717 return false;
6718
6719 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6720}
6721
Duncan Sandsec87aa82008-06-15 20:12:31 +00006722/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6723/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006724/// and it has other uses besides the load / store. After the
6725/// transformation, the new indexed load / store has effectively folded
6726/// the add / subtract in and all of its other uses are redirected to the
6727/// new load / store.
6728bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006729 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006730 return false;
6731
6732 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006733 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006734 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006735 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006736 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006737 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006738 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006739 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006740 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6741 return false;
6742 Ptr = LD->getBasePtr();
6743 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006744 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006745 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006746 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006747 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
6748 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
6749 return false;
6750 Ptr = ST->getBasePtr();
6751 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006752 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006753 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006754 }
Chris Lattner448f2192006-11-11 00:39:41 +00006755
Chris Lattner9f1794e2006-11-11 00:56:29 +00006756 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
6757 // out. There is no reason to make this a preinc/predec.
6758 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00006759 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006760 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006761
Chris Lattner9f1794e2006-11-11 00:56:29 +00006762 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00006763 SDValue BasePtr;
6764 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006765 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6766 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
6767 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00006768 // Don't create a indexed load / store with zero offset.
6769 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006770 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006771 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006772
Chris Lattner41e53fd2006-11-11 01:00:15 +00006773 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00006774 // 1) The new base ptr is a frame index.
6775 // 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 +00006776 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00006777 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00006778 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00006779 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00006780
Chris Lattner41e53fd2006-11-11 01:00:15 +00006781 // Check #1. Preinc'ing a frame index would require copying the stack pointer
6782 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00006783 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00006784 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006785
Chris Lattner41e53fd2006-11-11 01:00:15 +00006786 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006787 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006788 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006789 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006790 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006791 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006792
Evan Chengc843abe2007-05-24 02:35:39 +00006793 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006794 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00006795
6796 // Caches for hasPredecessorHelper
6797 SmallPtrSet<const SDNode *, 32> Visited;
6798 SmallVector<const SDNode *, 16> Worklist;
6799
Gabor Greifba36cb52008-08-28 21:40:38 +00006800 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6801 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006802 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006803 if (Use == N)
6804 continue;
Lang Hames944520f2011-07-07 04:31:51 +00006805 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006806 return false;
6807
Evan Chengc4b527a2012-01-13 01:37:24 +00006808 // If Ptr may be folded in addressing mode of other use, then it's
6809 // not profitable to do this transformation.
6810 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006811 RealUse = true;
6812 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006813
Chris Lattner9f1794e2006-11-11 00:56:29 +00006814 if (!RealUse)
6815 return false;
6816
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006818 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006819 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6820 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006821 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00006822 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6823 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006824 ++PreIndexedNodes;
6825 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006826 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006827 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006828 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006829 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006830 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006831 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006832 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006833 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6834 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006835 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006836 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006837 }
6838
Chris Lattner9f1794e2006-11-11 00:56:29 +00006839 // Finally, since the node is now dead, remove it from the graph.
6840 DAG.DeleteNode(N);
6841
6842 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006843 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006844 removeFromWorkList(Ptr.getNode());
6845 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00006846
6847 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006848}
6849
Duncan Sandsec87aa82008-06-15 20:12:31 +00006850/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00006851/// add / sub of the base pointer node into a post-indexed load / store.
6852/// The transformation folded the add / subtract into the new indexed
6853/// load / store effectively and all of its uses are redirected to the
6854/// new load / store.
6855bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006856 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006857 return false;
6858
6859 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006861 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006862 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006863 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006864 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006865 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006866 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
6867 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
6868 return false;
6869 Ptr = LD->getBasePtr();
6870 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006871 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006872 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006873 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006874 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
6875 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
6876 return false;
6877 Ptr = ST->getBasePtr();
6878 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006879 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006880 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006881 }
Chris Lattner448f2192006-11-11 00:39:41 +00006882
Gabor Greifba36cb52008-08-28 21:40:38 +00006883 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006884 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006885
Gabor Greifba36cb52008-08-28 21:40:38 +00006886 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6887 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006888 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006889 if (Op == N ||
6890 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
6891 continue;
6892
Dan Gohman475871a2008-07-27 21:46:04 +00006893 SDValue BasePtr;
6894 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006895 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6896 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00006897 // Don't create a indexed load / store with zero offset.
6898 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006899 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006900 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006901
Chris Lattner9f1794e2006-11-11 00:56:29 +00006902 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00006903 // 1) All uses are load / store ops that use it as base ptr (and
6904 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00006905 // 2) Op must be independent of N, i.e. Op is neither a predecessor
6906 // nor a successor of N. Otherwise, if Op is folded that would
6907 // create a cycle.
6908
Evan Chengcaab1292009-05-06 18:25:01 +00006909 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
6910 continue;
6911
Chris Lattner9f1794e2006-11-11 00:56:29 +00006912 // Check for #1.
6913 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006914 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
6915 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00006916 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00006917 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00006918 continue;
6919
Chris Lattner9f1794e2006-11-11 00:56:29 +00006920 // If all the uses are load / store addresses, then don't do the
6921 // transformation.
6922 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
6923 bool RealUse = false;
6924 for (SDNode::use_iterator III = Use->use_begin(),
6925 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00006926 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00006927 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006928 RealUse = true;
6929 }
Chris Lattner448f2192006-11-11 00:39:41 +00006930
Chris Lattner9f1794e2006-11-11 00:56:29 +00006931 if (!RealUse) {
6932 TryNext = true;
6933 break;
Chris Lattner448f2192006-11-11 00:39:41 +00006934 }
6935 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006936 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006937
Chris Lattner9f1794e2006-11-11 00:56:29 +00006938 if (TryNext)
6939 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006940
Chris Lattner9f1794e2006-11-11 00:56:29 +00006941 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00006942 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006943 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00006944 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6945 BasePtr, Offset, AM)
6946 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6947 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006948 ++PostIndexedNodes;
6949 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006950 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006951 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006952 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006953 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006954 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006955 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006956 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006957 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6958 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006959 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006960 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00006961 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006962
Chris Lattner9f1794e2006-11-11 00:56:29 +00006963 // Finally, since the node is now dead, remove it from the graph.
6964 DAG.DeleteNode(N);
6965
6966 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00006967 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006968 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006969 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006970 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006971 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006972 }
6973 }
6974 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006975
Chris Lattner448f2192006-11-11 00:39:41 +00006976 return false;
6977}
6978
Dan Gohman475871a2008-07-27 21:46:04 +00006979SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00006980 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00006981 SDValue Chain = LD->getChain();
6982 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00006983
Evan Cheng45a7ca92007-05-01 00:38:21 +00006984 // If load is not volatile and there are no uses of the loaded value (and
6985 // the updated indexed value in case of indexed loads), change uses of the
6986 // chain value into uses of the chain input (i.e. delete the dead load).
6987 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00006989 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00006990 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00006991 // It's not safe to use the two value CombineTo variant here. e.g.
6992 // v1, chain2 = load chain1, loc
6993 // v2, chain3 = load chain2, loc
6994 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00006995 // Now we replace use of chain2 with chain1. This makes the second load
6996 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00006997 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006998 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006999 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007000 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007001 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007002 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007003 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007004
Chris Lattner125991a2008-01-24 07:57:06 +00007005 if (N->use_empty()) {
7006 removeFromWorkList(N);
7007 DAG.DeleteNode(N);
7008 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007009
Dan Gohman475871a2008-07-27 21:46:04 +00007010 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007011 }
Evan Cheng498f5592007-05-01 08:53:39 +00007012 } else {
7013 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007014 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007015 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007016 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007017 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007018 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007019 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007020 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007021 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007022 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007023 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007024 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007025 DAG.getUNDEF(N->getValueType(1)));
7026 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007027 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007028 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007029 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007030 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007031 }
7032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007033
Chris Lattner01a22022005-10-10 22:04:48 +00007034 // If this load is directly stored, replace the load value with the stored
7035 // value.
7036 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007037 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007038 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007039 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007040 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7041 if (PrevST->getBasePtr() == Ptr &&
7042 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007043 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007044 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007045 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007046
Evan Cheng255f20f2010-04-01 06:04:33 +00007047 // Try to infer better alignment information than the load already has.
7048 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007049 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7050 if (Align > LD->getAlignment())
7051 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
7052 LD->getValueType(0),
7053 Chain, Ptr, LD->getPointerInfo(),
7054 LD->getMemoryVT(),
7055 LD->isVolatile(), LD->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007056 }
7057 }
7058
Jim Laskey7ca56af2006-10-11 13:47:09 +00007059 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007060 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007061 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007062
Jim Laskey6ff23e52006-10-04 16:53:27 +00007063 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007064 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007065 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007066
Jim Laskey279f0532006-09-25 16:29:54 +00007067 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007068 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00007069 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00007070 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007071 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007072 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007073 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00007074 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
7075 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007076 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007077 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007078 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007079 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007080 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007081 }
Jim Laskey279f0532006-09-25 16:29:54 +00007082
Jim Laskey6ff23e52006-10-04 16:53:27 +00007083 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00007084 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007086
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007087 // Make sure the new and old chains are cleaned up.
7088 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007089
Jim Laskey274062c2006-10-13 23:32:28 +00007090 // Replace uses with load result and token factor. Don't add users
7091 // to work list.
7092 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007093 }
7094 }
7095
Evan Cheng7fc033a2006-11-03 03:06:21 +00007096 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007097 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007098 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007099
Dan Gohman475871a2008-07-27 21:46:04 +00007100 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007101}
7102
Chris Lattner2392ae72010-04-15 04:48:01 +00007103/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7104/// load is having specific bytes cleared out. If so, return the byte size
7105/// being masked out and the shift amount.
7106static std::pair<unsigned, unsigned>
7107CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7108 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007109
Chris Lattner2392ae72010-04-15 04:48:01 +00007110 // Check for the structure we're looking for.
7111 if (V->getOpcode() != ISD::AND ||
7112 !isa<ConstantSDNode>(V->getOperand(1)) ||
7113 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7114 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007115
Chris Lattnere6987582010-04-15 06:10:49 +00007116 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007117 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007118 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007119
Chris Lattnere6987582010-04-15 06:10:49 +00007120 // The store should be chained directly to the load or be an operand of a
7121 // tokenfactor.
7122 if (LD == Chain.getNode())
7123 ; // ok.
7124 else if (Chain->getOpcode() != ISD::TokenFactor)
7125 return Result; // Fail.
7126 else {
7127 bool isOk = false;
7128 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7129 if (Chain->getOperand(i).getNode() == LD) {
7130 isOk = true;
7131 break;
7132 }
7133 if (!isOk) return Result;
7134 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007135
Chris Lattner2392ae72010-04-15 04:48:01 +00007136 // This only handles simple types.
7137 if (V.getValueType() != MVT::i16 &&
7138 V.getValueType() != MVT::i32 &&
7139 V.getValueType() != MVT::i64)
7140 return Result;
7141
7142 // Check the constant mask. Invert it so that the bits being masked out are
7143 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7144 // follow the sign bit for uniformity.
7145 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
7146 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
7147 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
7148 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
7149 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7150 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007151
Chris Lattner2392ae72010-04-15 04:48:01 +00007152 // See if we have a continuous run of bits. If so, we have 0*1+0*
7153 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7154 return Result;
7155
7156 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7157 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7158 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007159
Chris Lattner2392ae72010-04-15 04:48:01 +00007160 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7161 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007162 case 1:
7163 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007164 case 4: break;
7165 default: return Result; // All one mask, or 5-byte mask.
7166 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007167
Chris Lattner2392ae72010-04-15 04:48:01 +00007168 // Verify that the first bit starts at a multiple of mask so that the access
7169 // is aligned the same as the access width.
7170 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007171
Chris Lattner2392ae72010-04-15 04:48:01 +00007172 Result.first = MaskedBytes;
7173 Result.second = NotMaskTZ/8;
7174 return Result;
7175}
7176
7177
7178/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7179/// provides a value as specified by MaskInfo. If so, replace the specified
7180/// store with a narrower store of truncated IVal.
7181static SDNode *
7182ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7183 SDValue IVal, StoreSDNode *St,
7184 DAGCombiner *DC) {
7185 unsigned NumBytes = MaskInfo.first;
7186 unsigned ByteShift = MaskInfo.second;
7187 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007188
Chris Lattner2392ae72010-04-15 04:48:01 +00007189 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7190 // that uses this. If not, this is not a replacement.
7191 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7192 ByteShift*8, (ByteShift+NumBytes)*8);
7193 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007194
Chris Lattner2392ae72010-04-15 04:48:01 +00007195 // Check that it is legal on the target to do this. It is legal if the new
7196 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7197 // legalization.
7198 MVT VT = MVT::getIntegerVT(NumBytes*8);
7199 if (!DC->isTypeLegal(VT))
7200 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007201
Chris Lattner2392ae72010-04-15 04:48:01 +00007202 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7203 // shifted by ByteShift and truncated down to NumBytes.
7204 if (ByteShift)
7205 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007206 DAG.getConstant(ByteShift*8,
7207 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007208
7209 // Figure out the offset for the store and the alignment of the access.
7210 unsigned StOffset;
7211 unsigned NewAlign = St->getAlignment();
7212
7213 if (DAG.getTargetLoweringInfo().isLittleEndian())
7214 StOffset = ByteShift;
7215 else
7216 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007217
Chris Lattner2392ae72010-04-15 04:48:01 +00007218 SDValue Ptr = St->getBasePtr();
7219 if (StOffset) {
7220 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
7221 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7222 NewAlign = MinAlign(NewAlign, StOffset);
7223 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007224
Chris Lattner2392ae72010-04-15 04:48:01 +00007225 // Truncate down to the new size.
7226 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007227
Chris Lattner2392ae72010-04-15 04:48:01 +00007228 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007229 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007230 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007231 false, false, NewAlign).getNode();
7232}
7233
Evan Cheng8b944d32009-05-28 00:35:15 +00007234
7235/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7236/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7237/// of the loaded bits, try narrowing the load and store if it would end up
7238/// being a win for performance or code size.
7239SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7240 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007241 if (ST->isVolatile())
7242 return SDValue();
7243
Evan Cheng8b944d32009-05-28 00:35:15 +00007244 SDValue Chain = ST->getChain();
7245 SDValue Value = ST->getValue();
7246 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007247 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007248
7249 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007250 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007251
7252 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007253
Chris Lattner2392ae72010-04-15 04:48:01 +00007254 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7255 // is a byte mask indicating a consecutive number of bytes, check to see if
7256 // Y is known to provide just those bytes. If so, we try to replace the
7257 // load + replace + store sequence with a single (narrower) store, which makes
7258 // the load dead.
7259 if (Opc == ISD::OR) {
7260 std::pair<unsigned, unsigned> MaskedLoad;
7261 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7262 if (MaskedLoad.first)
7263 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7264 Value.getOperand(1), ST,this))
7265 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007266
Chris Lattner2392ae72010-04-15 04:48:01 +00007267 // Or is commutative, so try swapping X and Y.
7268 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7269 if (MaskedLoad.first)
7270 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7271 Value.getOperand(0), ST,this))
7272 return SDValue(NewST, 0);
7273 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007274
Evan Cheng8b944d32009-05-28 00:35:15 +00007275 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7276 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007277 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007278
7279 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007280 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7281 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007282 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007283 if (LD->getBasePtr() != Ptr ||
7284 LD->getPointerInfo().getAddrSpace() !=
7285 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007286 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007287
7288 // Find the type to narrow it the load / op / store to.
7289 SDValue N1 = Value.getOperand(1);
7290 unsigned BitWidth = N1.getValueSizeInBits();
7291 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7292 if (Opc == ISD::AND)
7293 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007294 if (Imm == 0 || Imm.isAllOnesValue())
7295 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007296 unsigned ShAmt = Imm.countTrailingZeros();
7297 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7298 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007299 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007300 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007301 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007302 TLI.isNarrowingProfitable(VT, NewVT))) {
7303 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007304 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007305 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007306 if (NewBW >= BitWidth)
7307 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007308
7309 // If the lsb changed does not start at the type bitwidth boundary,
7310 // start at the previous one.
7311 if (ShAmt % NewBW)
7312 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
7313 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
7314 if ((Imm & Mask) == Imm) {
7315 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7316 if (Opc == ISD::AND)
7317 NewImm ^= APInt::getAllOnesValue(NewBW);
7318 uint64_t PtrOff = ShAmt / 8;
7319 // For big endian targets, we need to adjust the offset to the pointer to
7320 // load the correct bytes.
7321 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007322 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007323
7324 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007325 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Chris Lattner2392ae72010-04-15 04:48:01 +00007326 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007327 return SDValue();
7328
Evan Cheng8b944d32009-05-28 00:35:15 +00007329 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
7330 Ptr.getValueType(), Ptr,
7331 DAG.getConstant(PtrOff, Ptr.getValueType()));
7332 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
7333 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007334 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007335 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007336 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007337 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
7338 DAG.getConstant(NewImm, NewVT));
7339 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
7340 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007341 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007342 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007343
7344 AddToWorkList(NewPtr.getNode());
7345 AddToWorkList(NewLD.getNode());
7346 AddToWorkList(NewVal.getNode());
7347 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007348 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007349 ++OpsNarrowed;
7350 return NewST;
7351 }
7352 }
7353
Evan Chengcdcecc02009-05-28 18:41:02 +00007354 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007355}
7356
Evan Cheng31959b12011-02-02 01:06:55 +00007357/// TransformFPLoadStorePair - For a given floating point load / store pair,
7358/// if the load value isn't used by any other operations, then consider
7359/// transforming the pair to integer load / store operations if the target
7360/// deems the transformation profitable.
7361SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7362 StoreSDNode *ST = cast<StoreSDNode>(N);
7363 SDValue Chain = ST->getChain();
7364 SDValue Value = ST->getValue();
7365 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7366 Value.hasOneUse() &&
7367 Chain == SDValue(Value.getNode(), 1)) {
7368 LoadSDNode *LD = cast<LoadSDNode>(Value);
7369 EVT VT = LD->getMemoryVT();
7370 if (!VT.isFloatingPoint() ||
7371 VT != ST->getMemoryVT() ||
7372 LD->isNonTemporal() ||
7373 ST->isNonTemporal() ||
7374 LD->getPointerInfo().getAddrSpace() != 0 ||
7375 ST->getPointerInfo().getAddrSpace() != 0)
7376 return SDValue();
7377
7378 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7379 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7380 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7381 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7382 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7383 return SDValue();
7384
7385 unsigned LDAlign = LD->getAlignment();
7386 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007387 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Evan Cheng31959b12011-02-02 01:06:55 +00007388 unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy);
7389 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7390 return SDValue();
7391
7392 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
7393 LD->getChain(), LD->getBasePtr(),
7394 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007395 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007396
7397 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
7398 NewLD, ST->getBasePtr(),
7399 ST->getPointerInfo(),
7400 false, false, STAlign);
7401
7402 AddToWorkList(NewLD.getNode());
7403 AddToWorkList(NewST.getNode());
7404 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007405 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007406 ++LdStFP2Int;
7407 return NewST;
7408 }
7409
7410 return SDValue();
7411}
7412
Dan Gohman475871a2008-07-27 21:46:04 +00007413SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007414 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007415 SDValue Chain = ST->getChain();
7416 SDValue Value = ST->getValue();
7417 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007418
Evan Cheng59d5b682007-05-07 21:27:48 +00007419 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00007420 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007421 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007422 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00007423 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007424 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00007425 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00007426 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007427 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00007428 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007429 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00007430 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00007431 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007432 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00007433 }
Owen Andersona34d9362011-04-14 17:30:49 +00007434
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007435 // Turn 'store undef, Ptr' -> nothing.
7436 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
7437 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007438
Nate Begeman2cbba892006-12-11 02:23:46 +00007439 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00007440 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007441 // NOTE: If the original store is volatile, this transform must not increase
7442 // the number of stores. For example, on x86-32 an f64 can be stored in one
7443 // processor operation but an i64 (which is not legal) requires two. So the
7444 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00007445 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00007446 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007448 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00007449 case MVT::f16: // We don't do this for these yet.
7450 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00007451 case MVT::f128:
7452 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00007453 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007454 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00007455 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00007457 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00007459 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007460 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007461 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00007462 }
7463 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00007465 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007466 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00007468 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00007469 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00007470 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007471 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007472 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007473 }
Owen Andersona34d9362011-04-14 17:30:49 +00007474
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007475 if (!ST->isVolatile() &&
7476 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00007477 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00007478 // argument passing. Since this is so common, custom legalize the
7479 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00007480 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007481 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
7482 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00007483 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00007484
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007485 unsigned Alignment = ST->getAlignment();
7486 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00007487 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007488
Bill Wendlingc144a572009-01-30 23:36:47 +00007489 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007490 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007491 isVolatile, isNonTemporal,
7492 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00007493 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00007494 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00007495 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00007496 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007497 Ptr, ST->getPointerInfo().getWithOffset(4),
7498 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00007499 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00007500 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00007501 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00007502 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007503
Chris Lattner62be1a72006-12-12 04:16:14 +00007504 break;
Evan Cheng25ece662006-12-11 17:25:19 +00007505 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007506 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007507 }
7508
Evan Cheng255f20f2010-04-01 06:04:33 +00007509 // Try to infer better alignment information than the store already has.
7510 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007511 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7512 if (Align > ST->getAlignment())
7513 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
7514 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
7515 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007516 }
7517 }
7518
Evan Cheng31959b12011-02-02 01:06:55 +00007519 // Try transforming a pair floating point load / store ops to integer
7520 // load / store ops.
7521 SDValue NewST = TransformFPLoadStorePair(N);
7522 if (NewST.getNode())
7523 return NewST;
7524
Scott Michelfdc40a02009-02-17 22:15:04 +00007525 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007526 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007527 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007528
Jim Laskey6ff23e52006-10-04 16:53:27 +00007529 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007530 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007531 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007532
7533 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007534 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007535 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007536 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007537 ST->getMemoryVT(), ST->isVolatile(),
7538 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007539 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00007540 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007541 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007542 ST->isVolatile(), ST->isNonTemporal(),
7543 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007544 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007545
Jim Laskey279f0532006-09-25 16:29:54 +00007546 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00007547 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007548 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00007549
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007550 // Make sure the new and old chains are cleaned up.
7551 AddToWorkList(Token.getNode());
7552
Jim Laskey274062c2006-10-13 23:32:28 +00007553 // Don't add users to work list.
7554 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007555 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00007556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007557
Evan Cheng33dbedc2006-11-05 09:31:14 +00007558 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007559 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007560 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00007561
Chris Lattner3c872852007-12-29 06:26:16 +00007562 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00007563 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00007564 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00007565 // See if we can simplify the input to this truncstore with knowledge that
7566 // only the low bits are being used. For example:
7567 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00007568 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007569 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00007570 APInt::getLowBitsSet(
7571 Value.getValueType().getScalarType().getSizeInBits(),
7572 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00007573 AddToWorkList(Value.getNode());
7574 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00007575 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007576 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007577 ST->isVolatile(), ST->isNonTemporal(),
7578 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00007579
Chris Lattnere33544c2007-10-13 06:58:48 +00007580 // Otherwise, see if we can simplify the operation with
7581 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00007582 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00007583 APInt::getLowBitsSet(
7584 Value.getValueType().getScalarType().getSizeInBits(),
7585 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00007586 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00007587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007588
Chris Lattner3c872852007-12-29 06:26:16 +00007589 // If this is a load followed by a store to the same location, then the store
7590 // is dead/noop.
7591 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007592 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007593 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00007594 // There can't be any side effects between the load and store, such as
7595 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00007596 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00007597 // The store is dead, remove it.
7598 return Chain;
7599 }
7600 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007601
Chris Lattnerddf89562008-01-17 19:59:44 +00007602 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
7603 // truncating store. We can do this even if this is already a truncstore.
7604 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00007605 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007606 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007607 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007608 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007609 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007610 ST->isVolatile(), ST->isNonTemporal(),
7611 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00007612 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007613
Evan Cheng8b944d32009-05-28 00:35:15 +00007614 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00007615}
7616
Dan Gohman475871a2008-07-27 21:46:04 +00007617SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
7618 SDValue InVec = N->getOperand(0);
7619 SDValue InVal = N->getOperand(1);
7620 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00007621 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007622
Bob Wilson492fd452010-05-19 23:42:58 +00007623 // If the inserted element is an UNDEF, just use the input vector.
7624 if (InVal.getOpcode() == ISD::UNDEF)
7625 return InVec;
7626
Nadav Rotem609d54e2011-02-12 14:40:33 +00007627 EVT VT = InVec.getValueType();
7628
Owen Anderson95771af2011-02-25 21:41:48 +00007629 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00007630 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
7631 return SDValue();
7632
Eli Friedman9db817f2011-09-09 21:04:06 +00007633 // Check that we know which element is being inserted
7634 if (!isa<ConstantSDNode>(EltNo))
7635 return SDValue();
7636 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007637
Eli Friedman9db817f2011-09-09 21:04:06 +00007638 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
7639 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
7640 // vector elements.
7641 SmallVector<SDValue, 8> Ops;
7642 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
7643 Ops.append(InVec.getNode()->op_begin(),
7644 InVec.getNode()->op_end());
7645 } else if (InVec.getOpcode() == ISD::UNDEF) {
7646 unsigned NElts = VT.getVectorNumElements();
7647 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
7648 } else {
7649 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007650 }
Eli Friedman9db817f2011-09-09 21:04:06 +00007651
7652 // Insert the element
7653 if (Elt < Ops.size()) {
7654 // All the operands of BUILD_VECTOR must have the same type;
7655 // we enforce that here.
7656 EVT OpVT = Ops[0].getValueType();
7657 if (InVal.getValueType() != OpVT)
7658 InVal = OpVT.bitsGT(InVal.getValueType()) ?
7659 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
7660 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
7661 Ops[Elt] = InVal;
7662 }
7663
7664 // Return the new vector
7665 return DAG.getNode(ISD::BUILD_VECTOR, dl,
7666 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00007667}
7668
Dan Gohman475871a2008-07-27 21:46:04 +00007669SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007670 // (vextract (scalar_to_vector val, 0) -> val
7671 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00007672 EVT VT = InVec.getValueType();
7673 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007674
Duncan Sandsc356f332011-05-09 08:03:33 +00007675 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
7676 // Check if the result type doesn't match the inserted element type. A
7677 // SCALAR_TO_VECTOR may truncate the inserted element and the
7678 // EXTRACT_VECTOR_ELT may widen the extracted vector.
7679 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00007680 if (InOp.getValueType() != NVT) {
7681 assert(InOp.getValueType().isInteger() && NVT.isInteger());
7682 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
7683 }
7684 return InOp;
7685 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007686
Nadav Rotemba05c912012-01-17 21:44:01 +00007687 SDValue EltNo = N->getOperand(1);
7688 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
7689
7690 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
7691 // We only perform this optimization before the op legalization phase because
7692 // we may introduce new vector instructions which are not backed by TD patterns.
7693 // For example on AVX, extracting elements from a wide vector without using
7694 // extract_subvector.
7695 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
7696 && ConstEltNo && !LegalOperations) {
7697 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
7698 int NumElem = VT.getVectorNumElements();
7699 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
7700 // Find the new index to extract from.
7701 int OrigElt = SVOp->getMaskElt(Elt);
7702
7703 // Extracting an undef index is undef.
7704 if (OrigElt == -1)
7705 return DAG.getUNDEF(NVT);
7706
7707 // Select the right vector half to extract from.
7708 if (OrigElt < NumElem) {
7709 InVec = InVec->getOperand(0);
7710 } else {
7711 InVec = InVec->getOperand(1);
7712 OrigElt -= NumElem;
7713 }
7714
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007715 EVT IndexTy = N->getOperand(1).getValueType();
Nadav Rotemba05c912012-01-17 21:44:01 +00007716 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007717 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00007718 }
7719
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007720 // Perform only after legalization to ensure build_vector / vector_shuffle
7721 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00007722 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007723
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007724 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
7725 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
7726 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00007727
Nadav Rotemba05c912012-01-17 21:44:01 +00007728 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00007729 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00007730 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00007731 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00007732 EVT ExtVT = VT.getVectorElementType();
7733 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00007734
Evan Cheng84387ea2012-03-13 22:00:52 +00007735 // If the result of load has to be truncated, then it's not necessarily
7736 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00007737 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00007738 return SDValue();
7739
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007740 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007741 // Don't duplicate a load with other uses.
7742 if (!InVec.hasOneUse())
7743 return SDValue();
7744
Owen Andersone50ed302009-08-10 22:56:29 +00007745 EVT BCVT = InVec.getOperand(0).getValueType();
7746 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007747 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00007748 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
7749 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007750 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007751 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007752 NewLoad = true;
7753 }
Evan Cheng513da432007-10-06 08:19:55 +00007754
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007755 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00007756 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00007757 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007758 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00007759 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00007760 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00007761 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007762 // Don't duplicate a load with other uses.
7763 if (!InVec.hasOneUse())
7764 return SDValue();
7765
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007766 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00007767 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007768 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
7769 // =>
7770 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00007771
Eli Friedmand6e25602011-12-26 22:49:32 +00007772 // Don't duplicate a load with other uses.
7773 if (!InVec.hasOneUse())
7774 return SDValue();
7775
Mon P Wanga60b5232008-12-11 00:26:16 +00007776 // If the bit convert changed the number of elements, it is unsafe
7777 // to examine the mask.
7778 if (BCNumEltsChanged)
7779 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00007780
7781 // Select the input vector, guarding against out of range extract vector.
7782 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00007783 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00007784 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
7785
Eli Friedmand6e25602011-12-26 22:49:32 +00007786 if (InVec.getOpcode() == ISD::BITCAST) {
7787 // Don't duplicate a load with other uses.
7788 if (!InVec.hasOneUse())
7789 return SDValue();
7790
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007791 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00007792 }
Gabor Greifba36cb52008-08-28 21:40:38 +00007793 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007794 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00007795 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00007796 }
7797 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007798
Eli Friedmand6e25602011-12-26 22:49:32 +00007799 // Make sure we found a non-volatile load and the extractelement is
7800 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00007801 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00007802 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007803
Eric Christopherd81f17a2010-11-03 20:44:42 +00007804 // If Idx was -1 above, Elt is going to be -1, so just return undef.
7805 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00007806 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00007807
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007808 unsigned Align = LN0->getAlignment();
7809 if (NewLoad) {
7810 // Check the resultant load doesn't need a higher alignment than the
7811 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00007812 unsigned NewAlign =
Eric Christopher503a64d2010-12-09 04:48:06 +00007813 TLI.getTargetData()
7814 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00007815
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007816 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00007817 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00007818
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007819 Align = NewAlign;
7820 }
7821
Dan Gohman475871a2008-07-27 21:46:04 +00007822 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00007823 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007824
Eric Christopherd81f17a2010-11-03 20:44:42 +00007825 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00007826 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007827 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007828 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007829 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00007830 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007831 DAG.getConstant(PtrOff, PtrType));
7832 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007833
Eli Friedman4db4add2011-11-16 23:50:22 +00007834 // The replacement we need to do here is a little tricky: we need to
7835 // replace an extractelement of a load with a load.
7836 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00007837 // Note that this replacement assumes that the extractvalue is the only
7838 // use of the load; that's okay because we don't want to perform this
7839 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00007840 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00007841 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00007842 if (NVT.bitsGT(LVT)) {
7843 // If the result type of vextract is wider than the load, then issue an
7844 // extending load instead.
7845 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
7846 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7847 Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(),
7848 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
7849 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007850 Chain = Load.getValue(1);
7851 } else {
Evan Cheng84387ea2012-03-13 22:00:52 +00007852 Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
7853 LN0->getPointerInfo().getWithOffset(PtrOff),
7854 LN0->isVolatile(), LN0->isNonTemporal(),
7855 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007856 Chain = Load.getValue(1);
7857 if (NVT.bitsLT(LVT))
7858 Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load);
7859 else
7860 Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load);
7861 }
Eli Friedman4db4add2011-11-16 23:50:22 +00007862 WorkListRemover DeadNodes(*this);
7863 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00007864 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007865 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00007866 // Since we're explcitly calling ReplaceAllUses, add the new node to the
7867 // worklist explicitly as well.
7868 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00007869 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00007870 // Make sure to revisit this node to clean it up; it will usually be dead.
7871 AddToWorkList(N);
7872 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00007873 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007874
Dan Gohman475871a2008-07-27 21:46:04 +00007875 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00007876}
Evan Cheng513da432007-10-06 08:19:55 +00007877
Dan Gohman475871a2008-07-27 21:46:04 +00007878SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007879 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007880 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007881 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007882
7883 // A vector built entirely of undefs is undef.
7884 if (ISD::allOperandsUndef(N))
7885 return DAG.getUNDEF(VT);
7886
Nadav Rotemb00418a2011-10-29 21:23:04 +00007887 // Check to see if this is a BUILD_VECTOR of a bunch of values
7888 // which come from any_extend or zero_extend nodes. If so, we can create
7889 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00007890 // optimizations. We do not handle sign-extend because we can't fill the sign
7891 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007892 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00007893 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007894
Craig Topperd3b58892012-01-17 09:09:48 +00007895 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007896 SDValue In = N->getOperand(i);
7897 // Ignore undef inputs.
7898 if (In.getOpcode() == ISD::UNDEF) continue;
7899
7900 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
7901 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
7902
Nadav Rotemf47368b2011-10-31 20:08:25 +00007903 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007904 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00007905 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007906 break;
7907 }
7908
7909 // The input is a ZeroExt or AnyExt. Check the original type.
7910 EVT InTy = In.getOperand(0).getValueType();
7911
7912 // Check that all of the widened source types are the same.
7913 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007914 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007915 SourceType = InTy;
7916 else if (InTy != SourceType) {
7917 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007918 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007919 break;
7920 }
7921
7922 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00007923 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007924 }
7925
Nadav Rotemf47368b2011-10-31 20:08:25 +00007926 // In order to have valid types, all of the inputs must be extended from the
7927 // same source type and all of the inputs must be any or zero extend.
7928 // Scalar sizes must be a power of two.
7929 EVT OutScalarTy = N->getValueType(0).getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007930 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00007931 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
7932 isPowerOf2_32(SourceType.getSizeInBits());
7933
7934 // We perform this optimization post type-legalization because
7935 // the type-legalizer often scalarizes integer-promoted vectors.
7936 // Performing this optimization before may create bit-casts which
7937 // will be type-legalized to complex code sequences.
7938 // We perform this optimization only before the operation legalizer because we
7939 // may introduce illegal operations.
Nadav Rotem6431ff92012-03-15 08:49:06 +00007940 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
7941 // turn into a single shuffle instruction.
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007942 if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) &&
7943 ValidTypes) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007944 bool isLE = TLI.isLittleEndian();
Nadav Rotemf47368b2011-10-31 20:08:25 +00007945 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007946 assert(ElemRatio > 1 && "Invalid element size ratio");
Craig Topperd3b58892012-01-17 09:09:48 +00007947 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
Nadav Rotemf47368b2011-10-31 20:08:25 +00007948 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007949
7950 unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements();
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007951 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007952
7953 // Populate the new build_vector
7954 for (unsigned i=0; i < N->getNumOperands(); ++i) {
7955 SDValue Cast = N->getOperand(i);
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007956 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
7957 Cast.getOpcode() == ISD::ZERO_EXTEND ||
7958 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
Nadav Rotemb00418a2011-10-29 21:23:04 +00007959 SDValue In;
7960 if (Cast.getOpcode() == ISD::UNDEF)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007961 In = DAG.getUNDEF(SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007962 else
7963 In = Cast->getOperand(0);
7964 unsigned Index = isLE ? (i * ElemRatio) :
7965 (i * ElemRatio + (ElemRatio - 1));
7966
7967 assert(Index < Ops.size() && "Invalid index");
7968 Ops[Index] = In;
7969 }
7970
7971 // The type of the new BUILD_VECTOR node.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007972 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007973 assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() &&
7974 "Invalid vector size");
Nadav Rotemf47368b2011-10-31 20:08:25 +00007975 // Check if the new vector type is legal.
7976 if (!isTypeLegal(VecVT)) return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007977
7978 // Make the new BUILD_VECTOR.
7979 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
7980 VecVT, &Ops[0], Ops.size());
7981
Nadav Rotem6431ff92012-03-15 08:49:06 +00007982 // The new BUILD_VECTOR node has the potential to be further optimized.
7983 AddToWorkList(BV.getNode());
Nadav Rotemb00418a2011-10-29 21:23:04 +00007984 // Bitcast to the desired type.
7985 return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV);
7986 }
Chris Lattnerca242442006-03-19 01:27:56 +00007987
Dan Gohman7f321562007-06-25 16:23:39 +00007988 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
7989 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
7990 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00007991
7992 // May only combine to shuffle after legalize if shuffle is legal.
7993 if (LegalOperations &&
7994 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
7995 return SDValue();
7996
Dan Gohman475871a2008-07-27 21:46:04 +00007997 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00007998 for (unsigned i = 0; i != NumInScalars; ++i) {
7999 // Ignore undef inputs.
8000 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008001
Dan Gohman7f321562007-06-25 16:23:39 +00008002 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00008003 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00008004 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00008005 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00008006 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008007 break;
8008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008009
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008010 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00008011 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008012 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
8013 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008014
Gabor Greifba36cb52008-08-28 21:40:38 +00008015 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008016 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00008017 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008018 VecIn2 = ExtractedFromVec;
8019 } else {
8020 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008021 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008022 break;
8023 }
8024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008025
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008026 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00008027 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008028 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008029 for (unsigned i = 0; i != NumInScalars; ++i) {
8030 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008031 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008032 continue;
8033 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008034
Rafael Espindola15684b22009-04-24 12:40:33 +00008035 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00008036 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00008037 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008038 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00008039 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
8040 if (ExtIndex > VT.getVectorNumElements())
8041 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008042
Nate Begeman5a5ca152009-04-29 05:20:52 +00008043 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008044 continue;
8045 }
8046
8047 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00008048 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008049 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008051
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008052 // We can't generate a shuffle node with mismatched input and output types.
8053 // Attempt to transform a single input vector to the correct type.
8054 if ((VT != VecIn1.getValueType())) {
8055 // We don't support shuffeling between TWO values of different types.
8056 if (VecIn2.getNode() != 0)
8057 return SDValue();
8058
8059 // We only support widening of vectors which are half the size of the
8060 // output registers. For example XMM->YMM widening on X86 with AVX.
8061 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
8062 return SDValue();
8063
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00008064 // Widen the input vector by adding undef values.
8065 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8066 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008067 }
8068
8069 // If VecIn2 is unused then change it to undef.
8070 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
8071
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008072 // Check that we were able to transform all incoming values to the same type.
8073 if (VecIn2.getValueType() != VecIn1.getValueType() ||
8074 VecIn1.getValueType() != VT)
8075 return SDValue();
8076
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008077 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008078 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00008079 return SDValue();
8080
Dan Gohman7f321562007-06-25 16:23:39 +00008081 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00008082 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00008083 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008084 Ops[1] = VecIn2;
Nate Begeman9008ca62009-04-27 18:41:29 +00008085 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008086 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008087
Dan Gohman475871a2008-07-27 21:46:04 +00008088 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00008089}
8090
Dan Gohman475871a2008-07-27 21:46:04 +00008091SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008092 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
8093 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
8094 // inputs come from at most two distinct vectors, turn this into a shuffle
8095 // node.
8096
8097 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00008098 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00008099 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008100
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008101 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008102 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008103 return DAG.getUNDEF(N->getValueType(0));
8104
Dan Gohman475871a2008-07-27 21:46:04 +00008105 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008106}
8107
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008108SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
8109 EVT NVT = N->getValueType(0);
8110 SDValue V = N->getOperand(0);
8111
8112 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
8113 // Handle only simple case where vector being inserted and vector
8114 // being extracted are of same type, and are half size of larger vectors.
8115 EVT BigVT = V->getOperand(0).getValueType();
8116 EVT SmallVT = V->getOperand(1).getValueType();
8117 if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
8118 return SDValue();
8119
Eli Friedman26323442011-12-07 00:11:56 +00008120 // Only handle cases where both indexes are constants with the same type.
8121 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
8122 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008123
Eli Friedman26323442011-12-07 00:11:56 +00008124 if (InsIdx && ExtIdx &&
8125 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
8126 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
8127 // Combine:
8128 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
8129 // Into:
8130 // indices are equal => V1
8131 // otherwise => (extract_subvec V1, ExtIdx)
8132 if (InsIdx->getZExtValue() == ExtIdx->getZExtValue())
8133 return V->getOperand(1);
8134 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT,
8135 V->getOperand(0), N->getOperand(1));
8136 }
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008137 }
8138
8139 return SDValue();
8140}
8141
Dan Gohman475871a2008-07-27 21:46:04 +00008142SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008143 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008144 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008145
Mon P Wangaeb06d22008-11-10 04:46:22 +00008146 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00008147 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00008148
Craig Topperae1bec52012-04-09 05:16:56 +00008149 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00008150
Craig Topper481b79c2012-01-04 08:07:43 +00008151 // Canonicalize shuffle undef, undef -> undef
8152 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
8153 return DAG.getUNDEF(VT);
8154
8155 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8156
8157 // Canonicalize shuffle v, v -> v, undef
8158 if (N0 == N1) {
8159 SmallVector<int, 8> NewMask;
8160 for (unsigned i = 0; i != NumElts; ++i) {
8161 int Idx = SVN->getMaskElt(i);
8162 if (Idx >= (int)NumElts) Idx -= NumElts;
8163 NewMask.push_back(Idx);
8164 }
8165 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
8166 &NewMask[0]);
8167 }
8168
8169 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
8170 if (N0.getOpcode() == ISD::UNDEF) {
8171 SmallVector<int, 8> NewMask;
8172 for (unsigned i = 0; i != NumElts; ++i) {
8173 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00008174 if (Idx >= 0) {
8175 if (Idx < (int)NumElts)
8176 Idx += NumElts;
8177 else
8178 Idx -= NumElts;
8179 }
8180 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00008181 }
8182 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
8183 &NewMask[0]);
8184 }
8185
8186 // Remove references to rhs if it is undef
8187 if (N1.getOpcode() == ISD::UNDEF) {
8188 bool Changed = false;
8189 SmallVector<int, 8> NewMask;
8190 for (unsigned i = 0; i != NumElts; ++i) {
8191 int Idx = SVN->getMaskElt(i);
8192 if (Idx >= (int)NumElts) {
8193 Idx = -1;
8194 Changed = true;
8195 }
8196 NewMask.push_back(Idx);
8197 }
8198 if (Changed)
8199 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
8200 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00008201
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008202 // If it is a splat, check if the argument vector is another splat or a
8203 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008204 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008205 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00008206
Dan Gohman7f321562007-06-25 16:23:39 +00008207 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00008208 // not the number of vector elements, look through it. Be careful not to
8209 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008210 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00008211 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00008212 if (ConvInput.getValueType().isVector() &&
8213 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00008214 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00008215 }
8216
Dan Gohman7f321562007-06-25 16:23:39 +00008217 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008218 assert(V->getNumOperands() == NumElts &&
8219 "BUILD_VECTOR has wrong number of operands");
8220 SDValue Base;
8221 bool AllSame = true;
8222 for (unsigned i = 0; i != NumElts; ++i) {
8223 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
8224 Base = V->getOperand(i);
8225 break;
Evan Cheng917ec982006-07-21 08:25:53 +00008226 }
Evan Cheng917ec982006-07-21 08:25:53 +00008227 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008228 // Splat of <u, u, u, u>, return <u, u, u, u>
8229 if (!Base.getNode())
8230 return N0;
8231 for (unsigned i = 0; i != NumElts; ++i) {
8232 if (V->getOperand(i) != Base) {
8233 AllSame = false;
8234 break;
8235 }
8236 }
8237 // Splat of <x, x, x, x>, return <x, x, x, x>
8238 if (AllSame)
8239 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00008240 }
8241 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00008242
8243 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008244 // and it reverses the swizzle of the previous shuffle then we can
8245 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00008246 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
8247 N1.getOpcode() == ISD::UNDEF) {
8248
Nadav Rotem4ac90812012-04-01 19:31:22 +00008249 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
8250
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008251 // Shuffle nodes can only reverse shuffles with a single non-undef value.
8252 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
8253 return SDValue();
8254
Craig Topperae1bec52012-04-09 05:16:56 +00008255 // The incoming shuffle must be of the same type as the result of the
8256 // current shuffle.
8257 assert(OtherSV->getOperand(0).getValueType() == VT &&
8258 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008259
8260 for (unsigned i = 0; i != NumElts; ++i) {
8261 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00008262 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008263 // Next, this index comes from the first value, which is the incoming
8264 // shuffle. Adopt the incoming index.
8265 if (Idx >= 0)
8266 Idx = OtherSV->getMaskElt(Idx);
8267
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008268 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00008269 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008270 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00008271 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008272
8273 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00008274 }
8275
Dan Gohman475871a2008-07-27 21:46:04 +00008276 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008277}
8278
Jim Grosbach9a526492010-06-23 16:07:42 +00008279SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) {
8280 if (!TLI.getShouldFoldAtomicFences())
8281 return SDValue();
8282
8283 SDValue atomic = N->getOperand(0);
8284 switch (atomic.getOpcode()) {
8285 case ISD::ATOMIC_CMP_SWAP:
8286 case ISD::ATOMIC_SWAP:
8287 case ISD::ATOMIC_LOAD_ADD:
8288 case ISD::ATOMIC_LOAD_SUB:
8289 case ISD::ATOMIC_LOAD_AND:
8290 case ISD::ATOMIC_LOAD_OR:
8291 case ISD::ATOMIC_LOAD_XOR:
8292 case ISD::ATOMIC_LOAD_NAND:
8293 case ISD::ATOMIC_LOAD_MIN:
8294 case ISD::ATOMIC_LOAD_MAX:
8295 case ISD::ATOMIC_LOAD_UMIN:
8296 case ISD::ATOMIC_LOAD_UMAX:
8297 break;
8298 default:
8299 return SDValue();
8300 }
8301
8302 SDValue fence = atomic.getOperand(0);
8303 if (fence.getOpcode() != ISD::MEMBARRIER)
8304 return SDValue();
8305
8306 switch (atomic.getOpcode()) {
8307 case ISD::ATOMIC_CMP_SWAP:
8308 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8309 fence.getOperand(0),
8310 atomic.getOperand(1), atomic.getOperand(2),
8311 atomic.getOperand(3)), atomic.getResNo());
8312 case ISD::ATOMIC_SWAP:
8313 case ISD::ATOMIC_LOAD_ADD:
8314 case ISD::ATOMIC_LOAD_SUB:
8315 case ISD::ATOMIC_LOAD_AND:
8316 case ISD::ATOMIC_LOAD_OR:
8317 case ISD::ATOMIC_LOAD_XOR:
8318 case ISD::ATOMIC_LOAD_NAND:
8319 case ISD::ATOMIC_LOAD_MIN:
8320 case ISD::ATOMIC_LOAD_MAX:
8321 case ISD::ATOMIC_LOAD_UMIN:
8322 case ISD::ATOMIC_LOAD_UMAX:
8323 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8324 fence.getOperand(0),
8325 atomic.getOperand(1), atomic.getOperand(2)),
8326 atomic.getResNo());
8327 default:
8328 return SDValue();
8329 }
8330}
8331
Evan Cheng44f1f092006-04-20 08:56:16 +00008332/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00008333/// an AND to a vector_shuffle with the destination vector and a zero vector.
8334/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00008335/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00008336SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008337 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008338 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008339 SDValue LHS = N->getOperand(0);
8340 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00008341 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008342 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00008343 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008344 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008345 SmallVector<int, 8> Indices;
8346 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00008347 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008348 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008349 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00008350 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00008351
8352 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008353 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008354 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008355 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00008356 else
Dan Gohman475871a2008-07-27 21:46:04 +00008357 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008358 }
8359
8360 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00008361 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008362 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008363 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008364
Dan Gohman7f321562007-06-25 16:23:39 +00008365 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00008366 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008367 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00008368 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00008369 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8370 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008371 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00008372 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008373 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00008374 }
8375 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008376
Dan Gohman475871a2008-07-27 21:46:04 +00008377 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008378}
8379
Dan Gohman7f321562007-06-25 16:23:39 +00008380/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00008381SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008382 // After legalize, the target may be depending on adds and other
8383 // binary ops to provide legal ways to construct constants or other
8384 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00008385 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008386
Bob Wilsond7273432010-12-17 23:06:49 +00008387 assert(N->getValueType(0).isVector() &&
8388 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00008389
Dan Gohman475871a2008-07-27 21:46:04 +00008390 SDValue LHS = N->getOperand(0);
8391 SDValue RHS = N->getOperand(1);
8392 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00008393 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00008394
Dan Gohman7f321562007-06-25 16:23:39 +00008395 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00008396 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00008397 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00008398 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00008399 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00008400 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008401 SDValue LHSOp = LHS.getOperand(i);
8402 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00008403 // If these two elements can't be folded, bail out.
8404 if ((LHSOp.getOpcode() != ISD::UNDEF &&
8405 LHSOp.getOpcode() != ISD::Constant &&
8406 LHSOp.getOpcode() != ISD::ConstantFP) ||
8407 (RHSOp.getOpcode() != ISD::UNDEF &&
8408 RHSOp.getOpcode() != ISD::Constant &&
8409 RHSOp.getOpcode() != ISD::ConstantFP))
8410 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008411
Evan Cheng7b336a82006-05-31 06:08:35 +00008412 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00008413 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
8414 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00008415 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008416 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00008417 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008418 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00008419 break;
8420 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008421
Bob Wilsond7273432010-12-17 23:06:49 +00008422 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00008423 EVT RVT = RHSOp.getValueType();
8424 if (RVT != VT) {
8425 // Integer BUILD_VECTOR operands may have types larger than the element
8426 // size (e.g., when the element type is not legal). Prior to type
8427 // legalization, the types may not match between the two BUILD_VECTORS.
8428 // Truncate one of the operands to make them match.
8429 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
8430 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
8431 } else {
8432 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
8433 VT = RVT;
8434 }
8435 }
Bob Wilsond7273432010-12-17 23:06:49 +00008436 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00008437 LHSOp, RHSOp);
8438 if (FoldOp.getOpcode() != ISD::UNDEF &&
8439 FoldOp.getOpcode() != ISD::Constant &&
8440 FoldOp.getOpcode() != ISD::ConstantFP)
8441 break;
8442 Ops.push_back(FoldOp);
8443 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00008444 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008445
Bob Wilsond7273432010-12-17 23:06:49 +00008446 if (Ops.size() == LHS.getNumOperands())
8447 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8448 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00008449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008450
Dan Gohman475871a2008-07-27 21:46:04 +00008451 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00008452}
8453
Bill Wendling836ca7d2009-01-30 23:59:18 +00008454SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
8455 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00008456 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00008457
Bill Wendling836ca7d2009-01-30 23:59:18 +00008458 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00008459 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008460
Nate Begemanf845b452005-10-08 00:29:44 +00008461 // If we got a simplified select_cc node back from SimplifySelectCC, then
8462 // break it down into a new SETCC node, and a new SELECT node, and then return
8463 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00008464 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008465 // Check to see if we got a select_cc back (to turn into setcc/select).
8466 // Otherwise, just return whatever node we got back, like fabs.
8467 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008468 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
8469 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008470 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008471 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00008472 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008473 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
8474 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00008475 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008476
Nate Begemanf845b452005-10-08 00:29:44 +00008477 return SCC;
8478 }
Dan Gohman475871a2008-07-27 21:46:04 +00008479 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008480}
8481
Chris Lattner40c62d52005-10-18 06:04:22 +00008482/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
8483/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00008484/// select. Callers of this should assume that TheSelect is deleted if this
8485/// returns true. As such, they should return the appropriate thing (e.g. the
8486/// node) back to the top-level of the DAG combiner loop to avoid it being
8487/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00008488bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00008489 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008490
Nadav Rotemf94fdb62011-02-11 19:57:47 +00008491 // Cannot simplify select with vector condition
8492 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
8493
Chris Lattner40c62d52005-10-18 06:04:22 +00008494 // If this is a select from two identical things, try to pull the operation
8495 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00008496 if (LHS.getOpcode() != RHS.getOpcode() ||
8497 !LHS.hasOneUse() || !RHS.hasOneUse())
8498 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008499
Chris Lattner18061612010-09-21 15:46:59 +00008500 // If this is a load and the token chain is identical, replace the select
8501 // of two loads with a load through a select of the address to load from.
8502 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
8503 // constants have been dropped into the constant pool.
8504 if (LHS.getOpcode() == ISD::LOAD) {
8505 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
8506 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008507
Chris Lattner18061612010-09-21 15:46:59 +00008508 // Token chains must be identical.
8509 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008510 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00008511 LLD->isVolatile() || RLD->isVolatile() ||
8512 // If this is an EXTLOAD, the VT's must match.
8513 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00008514 // If this is an EXTLOAD, the kind of extension must match.
8515 (LLD->getExtensionType() != RLD->getExtensionType() &&
8516 // The only exception is if one of the extensions is anyext.
8517 LLD->getExtensionType() != ISD::EXTLOAD &&
8518 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00008519 // FIXME: this discards src value information. This is
8520 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00008521 // both potential memory locations. Since we are discarding
8522 // src value info, don't do the transformation if the memory
8523 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00008524 LLD->getPointerInfo().getAddrSpace() != 0 ||
8525 RLD->getPointerInfo().getAddrSpace() != 0)
8526 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008527
Chris Lattnerf1658062010-09-21 15:58:55 +00008528 // Check that the select condition doesn't reach either load. If so,
8529 // folding this will induce a cycle into the DAG. If not, this is safe to
8530 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00008531 SDValue Addr;
8532 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00008533 SDNode *CondNode = TheSelect->getOperand(0).getNode();
8534 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
8535 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
8536 return false;
8537 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
8538 LLD->getBasePtr().getValueType(),
8539 TheSelect->getOperand(0), LLD->getBasePtr(),
8540 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00008541 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00008542 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
8543 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
8544
8545 if ((LLD->hasAnyUseOfValue(1) &&
8546 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00008547 (RLD->hasAnyUseOfValue(1) &&
8548 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00008549 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008550
Chris Lattnerf1658062010-09-21 15:58:55 +00008551 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
8552 LLD->getBasePtr().getValueType(),
8553 TheSelect->getOperand(0),
8554 TheSelect->getOperand(1),
8555 LLD->getBasePtr(), RLD->getBasePtr(),
8556 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00008557 }
8558
Chris Lattnerf1658062010-09-21 15:58:55 +00008559 SDValue Load;
8560 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
8561 Load = DAG.getLoad(TheSelect->getValueType(0),
8562 TheSelect->getDebugLoc(),
8563 // FIXME: Discards pointer info.
8564 LLD->getChain(), Addr, MachinePointerInfo(),
8565 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008566 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00008567 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00008568 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
8569 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00008570 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00008571 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00008572 // FIXME: Discards pointer info.
8573 LLD->getChain(), Addr, MachinePointerInfo(),
8574 LLD->getMemoryVT(), LLD->isVolatile(),
8575 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00008576 }
Chris Lattnerf1658062010-09-21 15:58:55 +00008577
8578 // Users of the select now use the result of the load.
8579 CombineTo(TheSelect, Load);
8580
8581 // Users of the old loads now use the new load's chain. We know the
8582 // old-load value is dead now.
8583 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
8584 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
8585 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00008586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008587
Chris Lattner40c62d52005-10-18 06:04:22 +00008588 return false;
8589}
8590
Chris Lattner600fec32009-03-11 05:08:08 +00008591/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
8592/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00008593SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00008594 SDValue N2, SDValue N3,
8595 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00008596 // (x ? y : y) -> y.
8597 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008598
Owen Andersone50ed302009-08-10 22:56:29 +00008599 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00008600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
8601 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
8602 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008603
8604 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00008605 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008606 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00008607 if (SCC.getNode()) AddToWorkList(SCC.getNode());
8608 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008609
8610 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00008611 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008612 return N2;
8613 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00008614 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008615 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00008616
Nate Begemanf845b452005-10-08 00:29:44 +00008617 // Check to see if we can simplify the select into an fabs node
8618 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
8619 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00008620 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008621 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
8622 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
8623 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
8624 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008625 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008626
Nate Begemanf845b452005-10-08 00:29:44 +00008627 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
8628 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
8629 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
8630 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008631 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00008632 }
8633 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008634
Chris Lattner600fec32009-03-11 05:08:08 +00008635 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
8636 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
8637 // in it. This is a win when the constant is not otherwise available because
8638 // it replaces two constant pool loads with one. We only do this if the FP
8639 // type is known to be legal, because if it isn't, then we are before legalize
8640 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00008641 // messing with soft float) and if the ConstantFP is not legal, because if
8642 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00008643 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
8644 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
8645 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00008646 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
8647 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00008648 // If both constants have multiple uses, then we won't need to do an
8649 // extra load, they are likely around in registers for other users.
8650 (TV->hasOneUse() || FV->hasOneUse())) {
8651 Constant *Elts[] = {
8652 const_cast<ConstantFP*>(FV->getConstantFPValue()),
8653 const_cast<ConstantFP*>(TV->getConstantFPValue())
8654 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008655 Type *FPTy = Elts[0]->getType();
Chris Lattner600fec32009-03-11 05:08:08 +00008656 const TargetData &TD = *TLI.getTargetData();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008657
Chris Lattner600fec32009-03-11 05:08:08 +00008658 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00008659 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00008660 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
8661 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00008662 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00008663
8664 // Get the offsets to the 0 and 1 element of the array so that we can
8665 // select between them.
8666 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00008667 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00008668 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008669
Chris Lattner600fec32009-03-11 05:08:08 +00008670 SDValue Cond = DAG.getSetCC(DL,
8671 TLI.getSetCCResultType(N0.getValueType()),
8672 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00008673 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008674 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
8675 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00008676 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008677 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
8678 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00008679 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008680 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00008681 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00008682 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00008683
8684 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008685 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008686
Nate Begemanf845b452005-10-08 00:29:44 +00008687 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00008688 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00008689 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00008690 (N1C->isNullValue() || // (a < 0) ? b : 0
8691 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00008692 EVT XType = N0.getValueType();
8693 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00008694 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00008695 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00008696 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00008697 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
8698 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00008699 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00008700 SDValue ShCt = DAG.getConstant(ShCtV,
8701 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00008702 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008703 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00008704 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008705
Duncan Sands8e4eb092008-06-08 20:54:56 +00008706 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008707 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008708 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008709 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008710
8711 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008712 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008713
Bill Wendling9729c5a2009-01-31 03:12:48 +00008714 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008715 XType, N0,
8716 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008717 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00008718 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008719
Duncan Sands8e4eb092008-06-08 20:54:56 +00008720 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008721 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008722 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008723 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008724
8725 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008726 }
8727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008728
Owen Andersoned1088a2010-09-22 22:58:22 +00008729 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
8730 // where y is has a single bit set.
8731 // A plaintext description would be, we can turn the SELECT_CC into an AND
8732 // when the condition can be materialized as an all-ones register. Any
8733 // single bit-test can be materialized as an all-ones register with
8734 // shift-left and shift-right-arith.
8735 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
8736 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008737 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00008738 N2C && N2C->isNullValue()) {
8739 SDValue AndLHS = N0->getOperand(0);
8740 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
8741 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
8742 // Shift the tested bit over the sign bit.
8743 APInt AndMask = ConstAndRHS->getAPIntValue();
8744 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008745 DAG.getConstant(AndMask.countLeadingZeros(),
8746 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008747 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008748
Owen Andersoned1088a2010-09-22 22:58:22 +00008749 // Now arithmetic right shift it all the way over, so the result is either
8750 // all-ones, or zero.
8751 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008752 DAG.getConstant(AndMask.getBitWidth()-1,
8753 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008754 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008755
Owen Andersoned1088a2010-09-22 22:58:22 +00008756 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
8757 }
8758 }
8759
Nate Begeman07ed4172005-10-10 21:26:48 +00008760 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00008761 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00008762 TLI.getBooleanContents(N0.getValueType().isVector()) ==
8763 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008764
Chris Lattner1eba01e2007-04-11 06:50:51 +00008765 // If the caller doesn't want us to simplify this into a zext of a compare,
8766 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00008767 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00008768 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008769
Nate Begeman07ed4172005-10-10 21:26:48 +00008770 // Get a SetCC of the condition
8771 // FIXME: Should probably make sure that setcc is legal if we ever have a
8772 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00008773 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00008774 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00008775 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008776 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00008777 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00008778 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00008779 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00008780 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00008781 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008782 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008783 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00008784 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00008785 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008786 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008787 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008788
Gabor Greifba36cb52008-08-28 21:40:38 +00008789 AddToWorkList(SCC.getNode());
8790 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00008791
Dan Gohman002e5d02008-03-13 22:13:53 +00008792 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00008793 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008794
Nate Begeman07ed4172005-10-10 21:26:48 +00008795 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00008796 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00008797 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00008798 getShiftAmountTy(Temp.getValueType())));
Nate Begeman07ed4172005-10-10 21:26:48 +00008799 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008800
Nate Begemanf845b452005-10-08 00:29:44 +00008801 // Check to see if this is the equivalent of setcc
8802 // FIXME: Turn all of these into setcc if setcc if setcc is legal
8803 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00008804 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00008805 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00008806 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00008807 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008808 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00008809 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008810 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00008811 return Res;
8812 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008813
Bill Wendling836ca7d2009-01-30 23:59:18 +00008814 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00008815 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008816 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00008817 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008818 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008819 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008820 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00008821 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00008822 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008823 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00008824 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008825 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
8826 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00008827 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00008828 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00008829 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00008830 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008831 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00008832 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008833 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00008834 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008835 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00008836 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008837 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00008838 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00008839 }
8840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008841
Benjamin Kramercde51102010-07-08 12:09:56 +00008842 // Check to see if this is an integer abs.
8843 // select_cc setg[te] X, 0, X, -X ->
8844 // select_cc setgt X, -1, X, -X ->
8845 // select_cc setl[te] X, 0, -X, X ->
8846 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00008847 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00008848 if (N1C) {
8849 ConstantSDNode *SubC = NULL;
8850 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
8851 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
8852 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
8853 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
8854 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
8855 (N1C->isOne() && CC == ISD::SETLT)) &&
8856 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
8857 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
8858
Owen Andersone50ed302009-08-10 22:56:29 +00008859 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00008860 if (SubC && SubC->isNullValue() && XType.isInteger()) {
8861 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
8862 N0,
8863 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008864 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +00008865 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
8866 XType, N0, Shift);
8867 AddToWorkList(Shift.getNode());
8868 AddToWorkList(Add.getNode());
8869 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00008870 }
8871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008872
Dan Gohman475871a2008-07-27 21:46:04 +00008873 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008874}
8875
Evan Chengfa1eb272007-02-08 22:13:59 +00008876/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00008877SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00008878 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008879 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008880 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00008881 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008882 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +00008883}
8884
Nate Begeman69575232005-10-20 02:15:44 +00008885/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
8886/// return a DAG expression to select that will generate the same value by
8887/// multiplying by a magic number. See:
8888/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008889SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008890 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008891 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008892
Andrew Lenharth232c9102006-06-12 16:07:18 +00008893 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008894 ii != ee; ++ii)
8895 AddToWorkList(*ii);
8896 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008897}
8898
8899/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
8900/// return a DAG expression to select that will generate the same value by
8901/// multiplying by a magic number. See:
8902/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008903SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008904 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008905 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00008906
Andrew Lenharth232c9102006-06-12 16:07:18 +00008907 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008908 ii != ee; ++ii)
8909 AddToWorkList(*ii);
8910 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008911}
8912
Nate Begemancc66cdd2009-09-25 06:05:26 +00008913/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +00008914// to alias with anything but itself. Provides base object and offset as
8915// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008916static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Dan Gohman46510a72010-04-15 01:51:59 +00008917 const GlobalValue *&GV, void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00008918 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008919 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008920
Jim Laskey71382342006-10-07 23:37:56 +00008921 // If it's an adding a simple constant then integrate the offset.
8922 if (Base.getOpcode() == ISD::ADD) {
8923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
8924 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008925 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00008926 }
8927 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008928
Nate Begemancc66cdd2009-09-25 06:05:26 +00008929 // Return the underlying GlobalValue, and update the Offset. Return false
8930 // for GlobalAddressSDNode since the same GlobalAddress may be represented
8931 // by multiple nodes with different offsets.
8932 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
8933 GV = G->getGlobal();
8934 Offset += G->getOffset();
8935 return false;
8936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008937
Nate Begemancc66cdd2009-09-25 06:05:26 +00008938 // Return the underlying Constant value, and update the Offset. Return false
8939 // for ConstantSDNodes since the same constant pool entry may be represented
8940 // by multiple nodes with different offsets.
8941 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
8942 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
8943 : (void *)C->getConstVal();
8944 Offset += C->getOffset();
8945 return false;
8946 }
Jim Laskey71382342006-10-07 23:37:56 +00008947 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008948 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00008949}
8950
8951/// isAlias - Return true if there is any possibility that the two addresses
8952/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00008953bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00008954 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008955 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008956 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +00008957 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008958 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008959 unsigned SrcValueAlign2,
8960 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +00008961 // If they are the same then they must be aliases.
8962 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00008963
Jim Laskey71382342006-10-07 23:37:56 +00008964 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00008965 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00008966 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00008967 const GlobalValue *GV1, *GV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00008968 void *CV1, *CV2;
8969 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
8970 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008971
Nate Begemancc66cdd2009-09-25 06:05:26 +00008972 // If they have a same base address then check to see if they overlap.
8973 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008974 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008975
Owen Anderson4a9f1502010-09-20 20:39:59 +00008976 // It is possible for different frame indices to alias each other, mostly
8977 // when tail call optimization reuses return address slots for arguments.
8978 // To catch this case, look up the actual index of frame indices to compute
8979 // the real alias relationship.
8980 if (isFrameIndex1 && isFrameIndex2) {
8981 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8982 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
8983 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
8984 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
8985 }
8986
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008987 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +00008988 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008989 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
8990 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00008991
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008992 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
8993 // compared to the size and offset of the access, we may be able to prove they
8994 // do not alias. This check is conservative for now to catch cases created by
8995 // splitting vector types.
8996 if ((SrcValueAlign1 == SrcValueAlign2) &&
8997 (SrcValueOffset1 != SrcValueOffset2) &&
8998 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
8999 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
9000 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009001
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009002 // There is no overlap between these relatively aligned accesses of similar
9003 // size, return no alias.
9004 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
9005 return false;
9006 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009007
Jim Laskey07a27092006-10-18 19:08:31 +00009008 if (CombinerGlobalAA) {
9009 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00009010 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
9011 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
9012 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00009013 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009014 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
9015 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +00009016 if (AAResult == AliasAnalysis::NoAlias)
9017 return false;
9018 }
Jim Laskey096c22e2006-10-18 12:29:57 +00009019
9020 // Otherwise we have to assume they alias.
9021 return true;
Jim Laskey71382342006-10-07 23:37:56 +00009022}
9023
9024/// FindAliasInfo - Extracts the relevant alias information from the memory
9025/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00009026bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +00009027 SDValue &Ptr, int64_t &Size,
9028 const Value *&SrcValue,
9029 int &SrcValueOffset,
9030 unsigned &SrcValueAlign,
9031 const MDNode *&TBAAInfo) const {
9032 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
9033
9034 Ptr = LS->getBasePtr();
9035 Size = LS->getMemoryVT().getSizeInBits() >> 3;
9036 SrcValue = LS->getSrcValue();
9037 SrcValueOffset = LS->getSrcValueOffset();
9038 SrcValueAlign = LS->getOriginalAlignment();
9039 TBAAInfo = LS->getTBAAInfo();
9040 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +00009041}
9042
Jim Laskey6ff23e52006-10-04 16:53:27 +00009043/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
9044/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00009045void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
9046 SmallVector<SDValue, 8> &Aliases) {
9047 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009048 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00009049
Jim Laskey279f0532006-09-25 16:29:54 +00009050 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00009051 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009052 int64_t Size;
9053 const Value *SrcValue;
9054 int SrcValueOffset;
9055 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009056 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009057 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009058 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +00009059
Jim Laskey6ff23e52006-10-04 16:53:27 +00009060 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00009061 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00009062 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009063
Jim Laskeybc588b82006-10-05 15:07:25 +00009064 // Look at each chain and determine if it is an alias. If so, add it to the
9065 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00009066 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00009067 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009068 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00009069 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009070
9071 // For TokenFactor nodes, look at each operand and only continue up the
9072 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +00009073 // find more and revert to original chain since the xform is unlikely to be
9074 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009075 //
9076 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +00009077 // chain we found before we hit a tokenfactor rather than the original
9078 // chain.
9079 if (Depth > 6 || Aliases.size() == 2) {
9080 Aliases.clear();
9081 Aliases.push_back(OriginalChain);
9082 break;
9083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009084
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009085 // Don't bother if we've been before.
9086 if (!Visited.insert(Chain.getNode()))
9087 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009088
Jim Laskeybc588b82006-10-05 15:07:25 +00009089 switch (Chain.getOpcode()) {
9090 case ISD::EntryToken:
9091 // Entry token is ideal chain operand, but handled in FindBetterChain.
9092 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009093
Jim Laskeybc588b82006-10-05 15:07:25 +00009094 case ISD::LOAD:
9095 case ISD::STORE: {
9096 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00009097 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009098 int64_t OpSize;
9099 const Value *OpSrcValue;
9100 int OpSrcValueOffset;
9101 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009102 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +00009103 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009104 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009105 OpSrcValueAlign,
9106 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +00009107
Jim Laskeybc588b82006-10-05 15:07:25 +00009108 // If chain is alias then stop here.
9109 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009110 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009111 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009112 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009113 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00009114 Aliases.push_back(Chain);
9115 } else {
9116 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00009117 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00009118 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00009119 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009120 break;
9121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009122
Jim Laskeybc588b82006-10-05 15:07:25 +00009123 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009124 // We have to check each of the operands of the token factor for "small"
9125 // token factors, so we queue them up. Adding the operands to the queue
9126 // (stack) in reverse order maintains the original order and increases the
9127 // likelihood that getNode will find a matching token factor (CSE.)
9128 if (Chain.getNumOperands() > 16) {
9129 Aliases.push_back(Chain);
9130 break;
9131 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009132 for (unsigned n = Chain.getNumOperands(); n;)
9133 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00009134 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00009135 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009136
Jim Laskeybc588b82006-10-05 15:07:25 +00009137 default:
9138 // For all other instructions we will just have to take what we can get.
9139 Aliases.push_back(Chain);
9140 break;
Jim Laskey279f0532006-09-25 16:29:54 +00009141 }
9142 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00009143}
9144
9145/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
9146/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00009147SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
9148 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00009149
Jim Laskey6ff23e52006-10-04 16:53:27 +00009150 // Accumulate all the aliases to this node.
9151 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00009152
Dan Gohman71dc7c92011-05-17 22:20:36 +00009153 // If no operands then chain to entry token.
9154 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009155 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +00009156
9157 // If a single operand then chain to it. We don't need to revisit it.
9158 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009159 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009160
Jim Laskey6ff23e52006-10-04 16:53:27 +00009161 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009162 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009163 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00009164}
9165
Nate Begeman1d4d4142005-09-01 00:19:25 +00009166// SelectionDAG::Combine - This is the entry point for the file.
9167//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009168void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00009169 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00009170 /// run - This is the main entry point to this class.
9171 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009172 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00009173}