blob: dbc173ed65b462f5243b540f4f12bb9b2d97385f [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>
James Molloy6660c052012-02-16 09:17:04 +000038#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039using namespace llvm;
40
Chris Lattnercd3245a2006-12-19 22:41:21 +000041STATISTIC(NodesCombined , "Number of dag nodes combined");
42STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
43STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000044STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000045STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046
Nate Begeman1d4d4142005-09-01 00:19:25 +000047namespace {
Jim Laskey71382342006-10-07 23:37:56 +000048 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000049 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000050 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000051
Jim Laskey07a27092006-10-18 19:08:31 +000052 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
Jim Laskeybc588b82006-10-05 15:07:25 +000056//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000059 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000065
66 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000067 //
68 // This has the semantics that when adding to the worklist,
69 // the item added must be next to be processed. It should
70 // also only appear once. The naive approach to this takes
71 // linear time.
72 //
73 // To reduce the insert/remove time to logarithmic, we use
74 // a set and a vector to maintain our worklist.
75 //
76 // The set contains the items on the worklist, but does not
77 // maintain the order they should be visited.
78 //
79 // The vector maintains the order nodes should be visited, but may
80 // contain duplicate or removed nodes. When choosing a node to
81 // visit, we pop off the order stack until we find an item that is
82 // also in the contents set. All operations are O(log N).
83 SmallPtrSet<SDNode*, 64> WorkListContents;
84 std::vector<SDNode*> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
Jim Laskeyc7c3f112006-10-16 20:52:31 +000086 // AA - Used for DAG load/store alias analysis.
87 AliasAnalysis &AA;
88
Nate Begeman1d4d4142005-09-01 00:19:25 +000089 /// AddUsersToWorkList - When an instruction is simplified, add all users of
90 /// the instruction to the work lists because they might get more simplified
91 /// now.
92 ///
93 void AddUsersToWorkList(SDNode *N) {
94 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000095 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000096 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000097 }
98
Dan Gohman389079b2007-10-08 17:57:15 +000099 /// visit - call the node-specific routine that knows how to fold each
100 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 public:
James Molloy6660c052012-02-16 09:17:04 +0000104 /// AddToWorkList - Add to the work list making sure i'ts instance is at the
105 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000106 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000107 WorkListContents.insert(N);
108 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000111 /// removeFromWorkList - remove all instances of N from the worklist.
112 ///
113 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000114 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000115 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000118 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000121 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Dan Gohman475871a2008-07-27 21:46:04 +0000124 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000125 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000126 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000127 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000128 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000129
130 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132 private:
133
Chris Lattner012f2412006-02-17 21:58:01 +0000134 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000135 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000136 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000138 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
139 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000140 return SimplifyDemandedBits(Op, Demanded);
141 }
142
Dan Gohman475871a2008-07-27 21:46:04 +0000143 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000144
Chris Lattner448f2192006-11-11 00:39:41 +0000145 bool CombineToPreIndexedLoadStore(SDNode *N);
146 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Evan Cheng95c57ea2010-04-24 04:43:44 +0000148 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
149 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
150 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
151 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000152 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000153 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000154 SDValue PromoteExtend(SDValue Op);
155 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000157 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
158 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
159 ISD::NodeType ExtType);
160
Dan Gohman389079b2007-10-08 17:57:15 +0000161 /// combine - call the node-specific routine that knows how to fold each
162 /// particular type of node. If that doesn't do anything, try the
163 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165
166 // Visitation implementation - Implement dag node combining for different
167 // node types. The semantics are as follows:
168 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000169 // SDValue.getNode() == 0 - No change was made
170 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
171 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000172 //
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDValue visitTokenFactor(SDNode *N);
174 SDValue visitMERGE_VALUES(SDNode *N);
175 SDValue visitADD(SDNode *N);
176 SDValue visitSUB(SDNode *N);
177 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000178 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000180 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue visitMUL(SDNode *N);
182 SDValue visitSDIV(SDNode *N);
183 SDValue visitUDIV(SDNode *N);
184 SDValue visitSREM(SDNode *N);
185 SDValue visitUREM(SDNode *N);
186 SDValue visitMULHU(SDNode *N);
187 SDValue visitMULHS(SDNode *N);
188 SDValue visitSMUL_LOHI(SDNode *N);
189 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000190 SDValue visitSMULO(SDNode *N);
191 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitSDIVREM(SDNode *N);
193 SDValue visitUDIVREM(SDNode *N);
194 SDValue visitAND(SDNode *N);
195 SDValue visitOR(SDNode *N);
196 SDValue visitXOR(SDNode *N);
197 SDValue SimplifyVBinOp(SDNode *N);
198 SDValue visitSHL(SDNode *N);
199 SDValue visitSRA(SDNode *N);
200 SDValue visitSRL(SDNode *N);
201 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000202 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000203 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000204 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitCTPOP(SDNode *N);
206 SDValue visitSELECT(SDNode *N);
207 SDValue visitSELECT_CC(SDNode *N);
208 SDValue visitSETCC(SDNode *N);
209 SDValue visitSIGN_EXTEND(SDNode *N);
210 SDValue visitZERO_EXTEND(SDNode *N);
211 SDValue visitANY_EXTEND(SDNode *N);
212 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
213 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000214 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue visitBUILD_PAIR(SDNode *N);
216 SDValue visitFADD(SDNode *N);
217 SDValue visitFSUB(SDNode *N);
218 SDValue visitFMUL(SDNode *N);
219 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);
231 SDValue visitBRCOND(SDNode *N);
232 SDValue visitBR_CC(SDNode *N);
233 SDValue visitLOAD(SDNode *N);
234 SDValue visitSTORE(SDNode *N);
235 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
236 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
237 SDValue visitBUILD_VECTOR(SDNode *N);
238 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000239 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000240 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Jim Grosbach9a526492010-06-23 16:07:42 +0000241 SDValue visitMEMBARRIER(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000242
Dan Gohman475871a2008-07-27 21:46:04 +0000243 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000244 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000245
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000247
Dan Gohman475871a2008-07-27 21:46:04 +0000248 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
249 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000250 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000251 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
252 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000253 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000254 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000255 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000256 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000257 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000258 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000260 SDValue BuildSDIV(SDNode *N);
261 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000262 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
263 bool DemandHighBits = true);
264 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000265 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000266 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000267 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000268 SDValue TransformFPLoadStorePair(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000269
Dan Gohman475871a2008-07-27 21:46:04 +0000270 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000271
Jim Laskey6ff23e52006-10-04 16:53:27 +0000272 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
273 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000274 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
275 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000276
Jim Laskey096c22e2006-10-18 12:29:57 +0000277 /// isAlias - Return true if there is any possibility that the two addresses
278 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000279 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000280 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000281 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000282 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000283 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000284 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000285 unsigned SrcValueAlign2,
286 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Jim Laskey7ca56af2006-10-11 13:47:09 +0000288 /// FindAliasInfo - Extracts the relevant alias information from the memory
289 /// node. Returns true if the operand was a load.
290 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000292 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000293 unsigned &SrcValueAlignment,
294 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000295
Jim Laskey279f0532006-09-25 16:29:54 +0000296 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000297 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000298 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000299
Chris Lattner2392ae72010-04-15 04:48:01 +0000300 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000301 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000302 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000303 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000304
Nate Begeman1d4d4142005-09-01 00:19:25 +0000305 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000306 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000307
Chris Lattner2392ae72010-04-15 04:48:01 +0000308 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000309
Chris Lattner2392ae72010-04-15 04:48:01 +0000310 /// getShiftAmountTy - Returns a type large enough to hold any valid
311 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000312 EVT getShiftAmountTy(EVT LHSTy) {
313 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000314 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 /// isTypeLegal - This method returns true if we are running before type
317 /// legalization or if the specified VT is legal.
318 bool isTypeLegal(const EVT &VT) {
319 if (!LegalTypes) return true;
320 return TLI.isTypeLegal(VT);
321 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 };
323}
324
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000325
326namespace {
327/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
328/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000329class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000330 DAGCombiner &DC;
331public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000332 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000333
Duncan Sandsedfcf592008-06-11 11:42:12 +0000334 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000335 DC.removeFromWorkList(N);
336 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000338 virtual void NodeUpdated(SDNode *N) {
339 // Ignore updates.
340 }
341};
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,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000385 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000386 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000387 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000389 return 0;
390
Chris Lattner29446522007-05-14 22:04:50 +0000391 // fneg is removable even if it has multiple uses.
392 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000393
Chris Lattner29446522007-05-14 22:04:50 +0000394 // Don't allow anything with multiple uses.
395 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000396
Chris Lattner3adf9512007-05-25 02:19:06 +0000397 // Don't recurse exponentially.
398 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000399
Chris Lattner29446522007-05-14 22:04:50 +0000400 switch (Op.getOpcode()) {
401 default: return false;
402 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000403 // Don't invert constant FP values after legalize. The negated constant
404 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000405 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000406 case ISD::FADD:
407 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000408 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000409
Bill Wendlingd34470c2009-01-30 23:10:18 +0000410 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000411 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Options,
412 Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000413 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000414 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000415 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Options,
416 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000417 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000418 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000419 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000420
Bill Wendlingd34470c2009-01-30 23:10:18 +0000421 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000422 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000423
Chris Lattner29446522007-05-14 22:04:50 +0000424 case ISD::FMUL:
425 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000426 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000427
Bill Wendlingd34470c2009-01-30 23:10:18 +0000428 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000429 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Options,
430 Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000431 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000432
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000433 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Options,
434 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000435
Chris Lattner29446522007-05-14 22:04:50 +0000436 case ISD::FP_EXTEND:
437 case ISD::FP_ROUND:
438 case ISD::FSIN:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000439 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Options,
440 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000441 }
442}
443
444/// GetNegatedExpression - If isNegatibleForFree returns true, this function
445/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000446static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000447 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000448 // fneg is removable even if it has multiple uses.
449 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000450
Chris Lattner29446522007-05-14 22:04:50 +0000451 // Don't allow anything with multiple uses.
452 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000453
Chris Lattner3adf9512007-05-25 02:19:06 +0000454 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000455 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000456 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000457 case ISD::ConstantFP: {
458 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
459 V.changeSign();
460 return DAG.getConstantFP(V, Op.getValueType());
461 }
Chris Lattner29446522007-05-14 22:04:50 +0000462 case ISD::FADD:
463 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000465
Bill Wendlingd34470c2009-01-30 23:10:18 +0000466 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000467 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
468 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000469 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000470 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000471 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000472 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000473 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000474 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000475 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000476 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000477 Op.getOperand(0));
478 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000479 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000480 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000481
Bill Wendlingd34470c2009-01-30 23:10:18 +0000482 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000483 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000484 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000485 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000486
Bill Wendlingd34470c2009-01-30 23:10:18 +0000487 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000488 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
489 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000490
Chris Lattner29446522007-05-14 22:04:50 +0000491 case ISD::FMUL:
492 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000493 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000494
Bill Wendlingd34470c2009-01-30 23:10:18 +0000495 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000496 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
497 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000498 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000499 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000500 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000501 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000502
Bill Wendlingd34470c2009-01-30 23:10:18 +0000503 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000504 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000505 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000506 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000507 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000508
Chris Lattner29446522007-05-14 22:04:50 +0000509 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000510 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000511 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000512 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000513 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000514 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000515 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000516 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000517 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000518 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000519 }
520}
Chris Lattner24664722006-03-01 04:53:38 +0000521
522
Nate Begeman4ebd8052005-09-01 23:24:04 +0000523// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
524// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000525// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000526// nodes based on the type of node we are checking. This simplifies life a
527// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000528static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
529 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000530 if (N.getOpcode() == ISD::SETCC) {
531 LHS = N.getOperand(0);
532 RHS = N.getOperand(1);
533 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000534 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000535 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000536 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000537 N.getOperand(2).getOpcode() == ISD::Constant &&
538 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000539 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
541 LHS = N.getOperand(0);
542 RHS = N.getOperand(1);
543 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000545 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 return false;
547}
548
Nate Begeman99801192005-09-07 23:25:52 +0000549// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
550// one use. If this is true, it allows the users to invert the operation for
551// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000552static bool isOneUseSetCC(SDValue N) {
553 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000554 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000555 return true;
556 return false;
557}
558
Bill Wendling35247c32009-01-30 00:45:56 +0000559SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
560 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000561 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000562 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
563 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000564 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000565 SDValue OpNode =
566 DAG.FoldConstantArithmetic(Opc, VT,
567 cast<ConstantSDNode>(N0.getOperand(1)),
568 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000569 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000570 }
571 if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000572 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
573 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
574 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000575 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000576 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000577 }
578 }
Bill Wendling35247c32009-01-30 00:45:56 +0000579
Nate Begemancd4d58c2006-02-03 06:46:56 +0000580 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
581 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000582 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000583 SDValue OpNode =
584 DAG.FoldConstantArithmetic(Opc, VT,
585 cast<ConstantSDNode>(N1.getOperand(1)),
586 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000587 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000588 }
589 if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000591 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000592 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000593 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000594 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000595 }
596 }
Bill Wendling35247c32009-01-30 00:45:56 +0000597
Dan Gohman475871a2008-07-27 21:46:04 +0000598 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000599}
600
Dan Gohman475871a2008-07-27 21:46:04 +0000601SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
602 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000603 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
604 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000605 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000606 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000607 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000608 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000609 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000610 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000611 assert((!To[i].getNode() ||
612 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000613 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000614 WorkListRemover DeadNodes(*this);
615 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000616
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000617 if (AddTo) {
618 // Push the new nodes and any users onto the worklist
619 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000620 if (To[i].getNode()) {
621 AddToWorkList(To[i].getNode());
622 AddUsersToWorkList(To[i].getNode());
623 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000624 }
625 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000626
Dan Gohmandbe664a2009-01-19 21:44:21 +0000627 // Finally, if the node is now dead, remove it from the graph. The node
628 // may not be dead if the replacement process recursively simplified to
629 // something else needing this node.
630 if (N->use_empty()) {
631 // Nodes can be reintroduced into the worklist. Make sure we do not
632 // process a node that has been replaced.
633 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000634
Dan Gohmandbe664a2009-01-19 21:44:21 +0000635 // Finally, since the node is now dead, remove it from the graph.
636 DAG.DeleteNode(N);
637 }
Dan Gohman475871a2008-07-27 21:46:04 +0000638 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000639}
640
Evan Chenge5b51ac2010-04-17 06:13:15 +0000641void DAGCombiner::
642CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000643 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000644 // are deleted, make sure to remove them from our worklist.
645 WorkListRemover DeadNodes(*this);
646 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000647
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000648 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000649 AddToWorkList(TLO.New.getNode());
650 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000652 // Finally, if the node is now dead, remove it from the graph. The node
653 // may not be dead if the replacement process recursively simplified to
654 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000655 if (TLO.Old.getNode()->use_empty()) {
656 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000657
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000658 // If the operands of this node are only used by the node, they will now
659 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000660 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
661 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
662 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000663
Gabor Greifba36cb52008-08-28 21:40:38 +0000664 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000666}
667
668/// SimplifyDemandedBits - Check the specified integer node value to see if
669/// it can be simplified or if things it uses can be simplified by bit
670/// propagation. If so, return true.
671bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000672 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673 APInt KnownZero, KnownOne;
674 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
675 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Dan Gohmane5af2d32009-01-29 01:59:02 +0000677 // Revisit the node.
678 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000679
Dan Gohmane5af2d32009-01-29 01:59:02 +0000680 // Replace the old value with the new one.
681 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000683 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000684 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000685 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000686 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000687
Dan Gohmane5af2d32009-01-29 01:59:02 +0000688 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000689 return true;
690}
691
Evan Cheng95c57ea2010-04-24 04:43:44 +0000692void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
693 DebugLoc dl = Load->getDebugLoc();
694 EVT VT = Load->getValueType(0);
695 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000696
Evan Cheng95c57ea2010-04-24 04:43:44 +0000697 DEBUG(dbgs() << "\nReplacing.9 ";
698 Load->dump(&DAG);
699 dbgs() << "\nWith: ";
700 Trunc.getNode()->dump(&DAG);
701 dbgs() << '\n');
702 WorkListRemover DeadNodes(*this);
703 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc, &DeadNodes);
704 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1),
705 &DeadNodes);
706 removeFromWorkList(Load);
707 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000708 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000709}
710
711SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
712 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000713 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000714 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000715 EVT MemVT = LD->getMemoryVT();
716 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000717 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000718 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000719 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000720 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000721 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000722 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000723 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000724 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000725 LD->isNonTemporal(), LD->getAlignment());
726 }
727
Evan Cheng4c26e932010-04-19 19:29:22 +0000728 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000729 switch (Opc) {
730 default: break;
731 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000732 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000733 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000734 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000735 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000736 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000737 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000738 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000739 case ISD::Constant: {
740 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000741 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000742 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743 }
Evan Chengcaf77402010-04-23 19:10:30 +0000744 }
745
746 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000747 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000748 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000749}
750
Evan Cheng95c57ea2010-04-24 04:43:44 +0000751SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000752 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
753 return SDValue();
754 EVT OldVT = Op.getValueType();
755 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000756 bool Replace = false;
757 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
758 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000759 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000760 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000761
762 if (Replace)
763 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
764 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000765 DAG.getValueType(OldVT));
766}
767
Evan Cheng95c57ea2010-04-24 04:43:44 +0000768SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000769 EVT OldVT = Op.getValueType();
770 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000771 bool Replace = false;
772 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
773 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000774 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000775 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000776
777 if (Replace)
778 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
779 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780}
781
Evan Cheng64b7bf72010-04-16 06:14:10 +0000782/// PromoteIntBinOp - Promote the specified integer binary operation if the
783/// target indicates it is beneficial. e.g. On x86, it's usually better to
784/// promote i16 operations to i32 since i16 instructions are longer.
785SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
786 if (!LegalOperations)
787 return SDValue();
788
789 EVT VT = Op.getValueType();
790 if (VT.isVector() || !VT.isInteger())
791 return SDValue();
792
Evan Chenge5b51ac2010-04-17 06:13:15 +0000793 // If operation type is 'undesirable', e.g. i16 on x86, consider
794 // promoting it.
795 unsigned Opc = Op.getOpcode();
796 if (TLI.isTypeDesirableForOp(Opc, VT))
797 return SDValue();
798
Evan Cheng64b7bf72010-04-16 06:14:10 +0000799 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000800 // Consult target whether it is a good idea to promote this operation and
801 // what's the right type to promote it to.
802 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000803 assert(PVT != VT && "Don't know what type to promote to!");
804
Evan Cheng95c57ea2010-04-24 04:43:44 +0000805 bool Replace0 = false;
806 SDValue N0 = Op.getOperand(0);
807 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
808 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000809 return SDValue();
810
Evan Cheng95c57ea2010-04-24 04:43:44 +0000811 bool Replace1 = false;
812 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000813 SDValue NN1;
814 if (N0 == N1)
815 NN1 = NN0;
816 else {
817 NN1 = PromoteOperand(N1, PVT, Replace1);
818 if (NN1.getNode() == 0)
819 return SDValue();
820 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000821
Evan Cheng95c57ea2010-04-24 04:43:44 +0000822 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000823 if (NN1.getNode())
824 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000825
826 if (Replace0)
827 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
828 if (Replace1)
829 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000830
Evan Chengac7eae52010-04-27 19:48:13 +0000831 DEBUG(dbgs() << "\nPromoting ";
832 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000833 DebugLoc dl = Op.getDebugLoc();
834 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000835 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000836 }
837 return SDValue();
838}
839
840/// PromoteIntShiftOp - Promote the specified integer shift operation if the
841/// target indicates it is beneficial. e.g. On x86, it's usually better to
842/// promote i16 operations to i32 since i16 instructions are longer.
843SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
844 if (!LegalOperations)
845 return SDValue();
846
847 EVT VT = Op.getValueType();
848 if (VT.isVector() || !VT.isInteger())
849 return SDValue();
850
851 // If operation type is 'undesirable', e.g. i16 on x86, consider
852 // promoting it.
853 unsigned Opc = Op.getOpcode();
854 if (TLI.isTypeDesirableForOp(Opc, VT))
855 return SDValue();
856
857 EVT PVT = VT;
858 // Consult target whether it is a good idea to promote this operation and
859 // what's the right type to promote it to.
860 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
861 assert(PVT != VT && "Don't know what type to promote to!");
862
Evan Cheng95c57ea2010-04-24 04:43:44 +0000863 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000864 SDValue N0 = Op.getOperand(0);
865 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000866 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000867 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000868 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000869 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000870 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000871 if (N0.getNode() == 0)
872 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000873
Evan Chenge5b51ac2010-04-17 06:13:15 +0000874 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000875 if (Replace)
876 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000877
Evan Chengac7eae52010-04-27 19:48:13 +0000878 DEBUG(dbgs() << "\nPromoting ";
879 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000880 DebugLoc dl = Op.getDebugLoc();
881 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000882 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000883 }
884 return SDValue();
885}
886
Evan Cheng4c26e932010-04-19 19:29:22 +0000887SDValue DAGCombiner::PromoteExtend(SDValue Op) {
888 if (!LegalOperations)
889 return SDValue();
890
891 EVT VT = Op.getValueType();
892 if (VT.isVector() || !VT.isInteger())
893 return SDValue();
894
895 // If operation type is 'undesirable', e.g. i16 on x86, consider
896 // promoting it.
897 unsigned Opc = Op.getOpcode();
898 if (TLI.isTypeDesirableForOp(Opc, VT))
899 return SDValue();
900
901 EVT PVT = VT;
902 // Consult target whether it is a good idea to promote this operation and
903 // what's the right type to promote it to.
904 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
905 assert(PVT != VT && "Don't know what type to promote to!");
906 // fold (aext (aext x)) -> (aext x)
907 // fold (aext (zext x)) -> (zext x)
908 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000909 DEBUG(dbgs() << "\nPromoting ";
910 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000911 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
912 }
913 return SDValue();
914}
915
916bool DAGCombiner::PromoteLoad(SDValue Op) {
917 if (!LegalOperations)
918 return false;
919
920 EVT VT = Op.getValueType();
921 if (VT.isVector() || !VT.isInteger())
922 return false;
923
924 // If operation type is 'undesirable', e.g. i16 on x86, consider
925 // promoting it.
926 unsigned Opc = Op.getOpcode();
927 if (TLI.isTypeDesirableForOp(Opc, VT))
928 return false;
929
930 EVT PVT = VT;
931 // Consult target whether it is a good idea to promote this operation and
932 // what's the right type to promote it to.
933 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
934 assert(PVT != VT && "Don't know what type to promote to!");
935
936 DebugLoc dl = Op.getDebugLoc();
937 SDNode *N = Op.getNode();
938 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000939 EVT MemVT = LD->getMemoryVT();
940 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000941 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000942 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000943 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000944 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000945 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000946 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000947 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000948 LD->isNonTemporal(), LD->getAlignment());
949 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
950
Evan Cheng95c57ea2010-04-24 04:43:44 +0000951 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000952 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000953 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000954 Result.getNode()->dump(&DAG);
955 dbgs() << '\n');
956 WorkListRemover DeadNodes(*this);
957 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result, &DeadNodes);
958 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1), &DeadNodes);
959 removeFromWorkList(N);
960 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000961 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000962 return true;
963 }
964 return false;
965}
966
Evan Chenge5b51ac2010-04-17 06:13:15 +0000967
Chris Lattner29446522007-05-14 22:04:50 +0000968//===----------------------------------------------------------------------===//
969// Main DAG Combiner implementation
970//===----------------------------------------------------------------------===//
971
Duncan Sands25cf2272008-11-24 14:53:14 +0000972void DAGCombiner::Run(CombineLevel AtLevel) {
973 // set the instance variables, so that the various visit routines may use it.
974 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000975 LegalOperations = Level >= AfterLegalizeVectorOps;
976 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000977
Evan Cheng17a568b2008-08-29 22:21:44 +0000978 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000979 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
980 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000981 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000982
Evan Cheng17a568b2008-08-29 22:21:44 +0000983 // Create a dummy node (which is not added to allnodes), that adds a reference
984 // to the root node, preventing it from being deleted, and tracking any
985 // changes of the root.
986 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000987
Jim Laskey26f7fa72006-10-17 19:33:52 +0000988 // The root of the dag may dangle to deleted nodes until the dag combiner is
989 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000990 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000991
James Molloy6660c052012-02-16 09:17:04 +0000992 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +0000993 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +0000994 while (!WorkListContents.empty()) {
995 SDNode *N;
996 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
997 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
998 // worklist *should* contain, and check the node we want to visit is should
999 // actually be visited.
1000 do {
1001 N = WorkListOrder.back();
1002 WorkListOrder.pop_back();
1003 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001004
Evan Cheng17a568b2008-08-29 22:21:44 +00001005 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1006 // N is deleted from the DAG, since they too may now be dead or may have a
1007 // reduced number of uses, allowing other xforms.
1008 if (N->use_empty() && N != &Dummy) {
1009 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1010 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001011
Evan Cheng17a568b2008-08-29 22:21:44 +00001012 DAG.DeleteNode(N);
1013 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001015
Evan Cheng17a568b2008-08-29 22:21:44 +00001016 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001017
Evan Cheng17a568b2008-08-29 22:21:44 +00001018 if (RV.getNode() == 0)
1019 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
Evan Cheng17a568b2008-08-29 22:21:44 +00001021 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001022
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 // If we get back the same node we passed in, rather than a new node or
1024 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001025 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 // mechanics for us, we have no work to do in this case.
1027 if (RV.getNode() == N)
1028 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001029
Evan Cheng17a568b2008-08-29 22:21:44 +00001030 assert(N->getOpcode() != ISD::DELETED_NODE &&
1031 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1032 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001033
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001034 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001035 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001036 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001037 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001038 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001039
Devang Patel9728ea22011-05-23 22:04:42 +00001040 // Transfer debug value.
1041 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001042 WorkListRemover DeadNodes(*this);
1043 if (N->getNumValues() == RV.getNode()->getNumValues())
1044 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
1045 else {
1046 assert(N->getValueType(0) == RV.getValueType() &&
1047 N->getNumValues() == 1 && "Type mismatch");
1048 SDValue OpV = RV;
1049 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
1050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001051
Evan Cheng17a568b2008-08-29 22:21:44 +00001052 // Push the new node and any users onto the worklist
1053 AddToWorkList(RV.getNode());
1054 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001055
Evan Cheng17a568b2008-08-29 22:21:44 +00001056 // Add any uses of the old node to the worklist in case this node is the
1057 // last one that uses them. They may become dead after this node is
1058 // deleted.
1059 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1060 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001061
Dan Gohmandbe664a2009-01-19 21:44:21 +00001062 // Finally, if the node is now dead, remove it from the graph. The node
1063 // may not be dead if the replacement process recursively simplified to
1064 // something else needing this node.
1065 if (N->use_empty()) {
1066 // Nodes can be reintroduced into the worklist. Make sure we do not
1067 // process a node that has been replaced.
1068 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001069
Dan Gohmandbe664a2009-01-19 21:44:21 +00001070 // Finally, since the node is now dead, remove it from the graph.
1071 DAG.DeleteNode(N);
1072 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001073 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Chris Lattner95038592005-10-05 06:35:28 +00001075 // If the root changed (e.g. it was a dead load, update the root).
1076 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +00001077}
1078
Dan Gohman475871a2008-07-27 21:46:04 +00001079SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001080 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001082 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001083 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 case ISD::ADD: return visitADD(N);
1085 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001086 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001087 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001088 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001089 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 case ISD::MUL: return visitMUL(N);
1091 case ISD::SDIV: return visitSDIV(N);
1092 case ISD::UDIV: return visitUDIV(N);
1093 case ISD::SREM: return visitSREM(N);
1094 case ISD::UREM: return visitUREM(N);
1095 case ISD::MULHU: return visitMULHU(N);
1096 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001097 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1098 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001099 case ISD::SMULO: return visitSMULO(N);
1100 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001101 case ISD::SDIVREM: return visitSDIVREM(N);
1102 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001103 case ISD::AND: return visitAND(N);
1104 case ISD::OR: return visitOR(N);
1105 case ISD::XOR: return visitXOR(N);
1106 case ISD::SHL: return visitSHL(N);
1107 case ISD::SRA: return visitSRA(N);
1108 case ISD::SRL: return visitSRL(N);
1109 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001110 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001112 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001114 case ISD::SELECT: return visitSELECT(N);
1115 case ISD::SELECT_CC: return visitSELECT_CC(N);
1116 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1118 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001119 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001120 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1121 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001122 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001123 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001124 case ISD::FADD: return visitFADD(N);
1125 case ISD::FSUB: return visitFSUB(N);
1126 case ISD::FMUL: return visitFMUL(N);
1127 case ISD::FDIV: return visitFDIV(N);
1128 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001129 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1131 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1132 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1133 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1134 case ISD::FP_ROUND: return visitFP_ROUND(N);
1135 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1136 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1137 case ISD::FNEG: return visitFNEG(N);
1138 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001139 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001140 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001141 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001142 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001143 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001144 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001145 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1146 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001147 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001148 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Jim Grosbach9a526492010-06-23 16:07:42 +00001149 case ISD::MEMBARRIER: return visitMEMBARRIER(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001150 }
Dan Gohman475871a2008-07-27 21:46:04 +00001151 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152}
1153
Dan Gohman475871a2008-07-27 21:46:04 +00001154SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001155 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001156
1157 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001159 assert(N->getOpcode() != ISD::DELETED_NODE &&
1160 "Node was deleted but visit returned NULL!");
1161
1162 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1163 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1164
1165 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001166 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001167 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001168
1169 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1170 }
1171 }
1172
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001173 // If nothing happened still, try promoting the operation.
1174 if (RV.getNode() == 0) {
1175 switch (N->getOpcode()) {
1176 default: break;
1177 case ISD::ADD:
1178 case ISD::SUB:
1179 case ISD::MUL:
1180 case ISD::AND:
1181 case ISD::OR:
1182 case ISD::XOR:
1183 RV = PromoteIntBinOp(SDValue(N, 0));
1184 break;
1185 case ISD::SHL:
1186 case ISD::SRA:
1187 case ISD::SRL:
1188 RV = PromoteIntShiftOp(SDValue(N, 0));
1189 break;
1190 case ISD::SIGN_EXTEND:
1191 case ISD::ZERO_EXTEND:
1192 case ISD::ANY_EXTEND:
1193 RV = PromoteExtend(SDValue(N, 0));
1194 break;
1195 case ISD::LOAD:
1196 if (PromoteLoad(SDValue(N, 0)))
1197 RV = SDValue(N, 0);
1198 break;
1199 }
1200 }
1201
Scott Michelfdc40a02009-02-17 22:15:04 +00001202 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001203 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001204 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001205 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1206 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001207 SDValue N0 = N->getOperand(0);
1208 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001209
Evan Cheng08b11732008-03-22 01:55:50 +00001210 // Constant operands are canonicalized to RHS.
1211 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001212 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001213 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1214 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001215 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001216 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001217 }
1218 }
1219
Dan Gohman389079b2007-10-08 17:57:15 +00001220 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001221}
Dan Gohman389079b2007-10-08 17:57:15 +00001222
Chris Lattner6270f682006-10-08 22:57:01 +00001223/// getInputChainForNode - Given a node, return its input chain if it has one,
1224/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001225static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001226 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001228 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001230 return N->getOperand(NumOps-1);
1231 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001233 return N->getOperand(i);
1234 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001235 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001236}
1237
Dan Gohman475871a2008-07-27 21:46:04 +00001238SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001239 // If N has two operands, where one has an input chain equal to the other,
1240 // the 'other' chain is redundant.
1241 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001242 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001243 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001245 return N->getOperand(1);
1246 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001247
Chris Lattnerc76d4412007-05-16 06:37:59 +00001248 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001249 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001250 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001251 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Jim Laskey6ff23e52006-10-04 16:53:27 +00001253 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001254 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Jim Laskey71382342006-10-07 23:37:56 +00001256 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001257 // encountered.
1258 for (unsigned i = 0; i < TFs.size(); ++i) {
1259 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
Jim Laskey6ff23e52006-10-04 16:53:27 +00001261 // Check each of the operands.
1262 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001263 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001264
Jim Laskey6ff23e52006-10-04 16:53:27 +00001265 switch (Op.getOpcode()) {
1266 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001267 // Entry tokens don't need to be added to the list. They are
1268 // rededundant.
1269 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001270 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001271
Jim Laskey6ff23e52006-10-04 16:53:27 +00001272 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001273 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001274 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001275 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001276 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001277 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001278 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001279 Changed = true;
1280 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001281 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Jim Laskey6ff23e52006-10-04 16:53:27 +00001284 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001285 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001287 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001288 else
1289 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001290 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001291 }
1292 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001293 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001294
Dan Gohman475871a2008-07-27 21:46:04 +00001295 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001296
1297 // If we've change things around then replace token factor.
1298 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001299 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 // The entry token is the only possible outcome.
1301 Result = DAG.getEntryNode();
1302 } else {
1303 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001304 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001305 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001306 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001307
Jim Laskey274062c2006-10-13 23:32:28 +00001308 // Don't add users to work list.
1309 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Jim Laskey6ff23e52006-10-04 16:53:27 +00001312 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313}
1314
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001315/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001316SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001317 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001318 // Replacing results may cause a different MERGE_VALUES to suddenly
1319 // be CSE'd with N, and carry its uses with it. Iterate until no
1320 // uses remain, to ensure that the node can be safely deleted.
1321 do {
1322 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1323 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
1324 &DeadNodes);
1325 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001326 removeFromWorkList(N);
1327 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001328 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001329}
1330
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001331static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001332SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1333 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001334 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001335 SDValue N00 = N0.getOperand(0);
1336 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001337 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001338
Gabor Greifba36cb52008-08-28 21:40:38 +00001339 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001340 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001341 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1342 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1343 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1344 N00.getOperand(0), N01),
1345 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1346 N00.getOperand(1), N01));
1347 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001348 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001349
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001351}
1352
Dan Gohman475871a2008-07-27 21:46:04 +00001353SDValue DAGCombiner::visitADD(SDNode *N) {
1354 SDValue N0 = N->getOperand(0);
1355 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1357 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001358 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001359
1360 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001361 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001363 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001364 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001365
Dan Gohman613e0d82007-07-03 14:03:57 +00001366 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001367 if (N0.getOpcode() == ISD::UNDEF)
1368 return N0;
1369 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001370 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001373 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001374 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001375 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001376 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001378 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001379 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001380 // fold (add Sym, c) -> Sym+c
1381 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001382 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001383 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001384 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001385 GA->getOffset() +
1386 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001387 // fold ((c1-A)+c2) -> (c1+c2)-A
1388 if (N1C && N0.getOpcode() == ISD::SUB)
1389 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001390 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001391 DAG.getConstant(N1C->getAPIntValue()+
1392 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001393 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001394 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001395 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001396 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001397 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398 // fold ((0-A) + B) -> B-A
1399 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1400 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001401 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402 // fold (A + (0-B)) -> A-B
1403 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1404 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001405 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001406 // fold (A+(B-A)) -> B
1407 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001409 // fold ((B-A)+A) -> B
1410 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1411 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001412 // fold (A+(B-(A+C))) to (B-C)
1413 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001414 N0 == N1.getOperand(1).getOperand(0))
1415 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001416 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001417 // fold (A+(B-(C+A))) to (B-C)
1418 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001419 N0 == N1.getOperand(1).getOperand(1))
1420 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001421 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001422 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001423 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1424 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001425 N0 == N1.getOperand(0).getOperand(1))
1426 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1427 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001428
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001429 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1430 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1431 SDValue N00 = N0.getOperand(0);
1432 SDValue N01 = N0.getOperand(1);
1433 SDValue N10 = N1.getOperand(0);
1434 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001435
1436 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1437 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1438 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1439 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001440 }
Chris Lattner947c2892006-03-13 06:51:27 +00001441
Dan Gohman475871a2008-07-27 21:46:04 +00001442 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1443 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001444
Chris Lattner947c2892006-03-13 06:51:27 +00001445 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001447 APInt LHSZero, LHSOne;
1448 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001449 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001450 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001451
Dan Gohman948d8ea2008-02-20 16:33:30 +00001452 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001453 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001454
Chris Lattner947c2892006-03-13 06:51:27 +00001455 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1456 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1457 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1458 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001459 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001460 }
1461 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001462
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001463 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001464 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001465 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001466 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001467 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001468 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001469 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001470 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001471 }
1472
Dan Gohmancd9e1552010-01-19 23:30:49 +00001473 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1474 if (N1.getOpcode() == ISD::SHL &&
1475 N1.getOperand(0).getOpcode() == ISD::SUB)
1476 if (ConstantSDNode *C =
1477 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1478 if (C->getAPIntValue() == 0)
1479 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1480 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1481 N1.getOperand(0).getOperand(1),
1482 N1.getOperand(1)));
1483 if (N0.getOpcode() == ISD::SHL &&
1484 N0.getOperand(0).getOpcode() == ISD::SUB)
1485 if (ConstantSDNode *C =
1486 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1487 if (C->getAPIntValue() == 0)
1488 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1489 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1490 N0.getOperand(0).getOperand(1),
1491 N0.getOperand(1)));
1492
Owen Andersonbc146b02010-09-21 20:42:50 +00001493 if (N1.getOpcode() == ISD::AND) {
1494 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001495 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001496 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1497 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001498
Owen Andersonbc146b02010-09-21 20:42:50 +00001499 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1500 // and similar xforms where the inner op is either ~0 or 0.
1501 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1502 DebugLoc DL = N->getDebugLoc();
1503 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1504 }
1505 }
1506
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001507 // add (sext i1), X -> sub X, (zext i1)
1508 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1509 N0.getOperand(0).getValueType() == MVT::i1 &&
1510 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1511 DebugLoc DL = N->getDebugLoc();
1512 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1513 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1514 }
1515
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001516 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001517}
1518
Dan Gohman475871a2008-07-27 21:46:04 +00001519SDValue DAGCombiner::visitADDC(SDNode *N) {
1520 SDValue N0 = N->getOperand(0);
1521 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001522 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1523 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001524 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001525
Chris Lattner91153682007-03-04 20:03:15 +00001526 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001527 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001528 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001529 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001530 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001531
Chris Lattner91153682007-03-04 20:03:15 +00001532 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001533 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001534 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001535
Chris Lattnerb6541762007-03-04 20:40:38 +00001536 // fold (addc x, 0) -> x + no carry out
1537 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001538 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001539 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001540
Dale Johannesen874ae252009-06-02 03:12:52 +00001541 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001542 APInt LHSZero, LHSOne;
1543 APInt RHSZero, RHSOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00001544 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001545 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001546
Dan Gohman948d8ea2008-02-20 16:33:30 +00001547 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001548 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Chris Lattnerb6541762007-03-04 20:40:38 +00001550 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1551 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1552 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1553 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling14036c02009-01-30 02:38:00 +00001554 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001555 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001556 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Dan Gohman475871a2008-07-27 21:46:04 +00001559 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001560}
1561
Dan Gohman475871a2008-07-27 21:46:04 +00001562SDValue DAGCombiner::visitADDE(SDNode *N) {
1563 SDValue N0 = N->getOperand(0);
1564 SDValue N1 = N->getOperand(1);
1565 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001566 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1567 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattner91153682007-03-04 20:03:15 +00001569 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001570 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001571 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1572 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001573
Chris Lattnerb6541762007-03-04 20:40:38 +00001574 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001575 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001576 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Dan Gohman475871a2008-07-27 21:46:04 +00001578 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001579}
1580
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001581// Since it may not be valid to emit a fold to zero for vector initializers
1582// check if we can before folding.
1583static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001584 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001585 if (!VT.isVector()) {
1586 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001587 }
1588 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001589 // Produce a vector of zeros.
1590 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1591 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1592 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1593 &Ops[0], Ops.size());
1594 }
1595 return SDValue();
1596}
1597
Dan Gohman475871a2008-07-27 21:46:04 +00001598SDValue DAGCombiner::visitSUB(SDNode *N) {
1599 SDValue N0 = N->getOperand(0);
1600 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1602 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001603 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1604 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Dan Gohman7f321562007-06-25 16:23:39 +00001607 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001608 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001609 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001610 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001611 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001612
Chris Lattner854077d2005-10-17 01:07:11 +00001613 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001614 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001615 if (N0 == N1)
1616 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001619 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001620 // fold (sub x, c) -> (add x, -c)
1621 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001622 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001623 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001624 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1625 if (N0C && N0C->isAllOnesValue())
1626 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001627 // fold A-(A-B) -> B
1628 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1629 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001631 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001632 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001633 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001634 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001635 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001636 // fold C2-(A+C1) -> (C2-C1)-A
1637 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
1638 SDValue NewC = DAG.getConstant((N0C->getAPIntValue() - N1C1->getAPIntValue()), VT);
1639 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
1640 N1.getOperand(0));
1641 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001642 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001643 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001644 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1645 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001646 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001647 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1648 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001649 // fold ((A+(C+B))-B) -> A+C
1650 if (N0.getOpcode() == ISD::ADD &&
1651 N0.getOperand(1).getOpcode() == ISD::ADD &&
1652 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001653 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1654 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001655 // fold ((A-(B-C))-C) -> A-B
1656 if (N0.getOpcode() == ISD::SUB &&
1657 N0.getOperand(1).getOpcode() == ISD::SUB &&
1658 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001659 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1660 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001661
Dan Gohman613e0d82007-07-03 14:03:57 +00001662 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001663 if (N0.getOpcode() == ISD::UNDEF)
1664 return N0;
1665 if (N1.getOpcode() == ISD::UNDEF)
1666 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001667
Dan Gohman6520e202008-10-18 02:06:02 +00001668 // If the relocation model supports it, consider symbol offsets.
1669 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001670 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001671 // fold (sub Sym, c) -> Sym-c
1672 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001673 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001674 GA->getOffset() -
1675 (uint64_t)N1C->getSExtValue());
1676 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1677 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1678 if (GA->getGlobal() == GB->getGlobal())
1679 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1680 VT);
1681 }
1682
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001683 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001684}
1685
Craig Toppercc274522012-01-07 09:06:39 +00001686SDValue DAGCombiner::visitSUBC(SDNode *N) {
1687 SDValue N0 = N->getOperand(0);
1688 SDValue N1 = N->getOperand(1);
1689 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1690 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1691 EVT VT = N0.getValueType();
1692
1693 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001694 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001695 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1696 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1697 MVT::Glue));
1698
1699 // fold (subc x, x) -> 0 + no borrow
1700 if (N0 == N1)
1701 return CombineTo(N, DAG.getConstant(0, VT),
1702 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1703 MVT::Glue));
1704
1705 // fold (subc x, 0) -> x + no borrow
1706 if (N1C && N1C->isNullValue())
1707 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1708 MVT::Glue));
1709
1710 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1711 if (N0C && N0C->isAllOnesValue())
1712 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1713 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1714 MVT::Glue));
1715
1716 return SDValue();
1717}
1718
1719SDValue DAGCombiner::visitSUBE(SDNode *N) {
1720 SDValue N0 = N->getOperand(0);
1721 SDValue N1 = N->getOperand(1);
1722 SDValue CarryIn = N->getOperand(2);
1723
1724 // fold (sube x, y, false) -> (subc x, y)
1725 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1726 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1727
1728 return SDValue();
1729}
1730
Dan Gohman475871a2008-07-27 21:46:04 +00001731SDValue DAGCombiner::visitMUL(SDNode *N) {
1732 SDValue N0 = N->getOperand(0);
1733 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001734 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1735 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001736 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Dan Gohman7f321562007-06-25 16:23:39 +00001738 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001741 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001742 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001743
Dan Gohman613e0d82007-07-03 14:03:57 +00001744 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001745 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001746 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001749 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001750 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001751 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001752 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001754 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001755 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001756 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001757 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001758 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1759 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001760 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001761 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001762 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001763 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001764 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001765 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001766 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1767 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001768 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001769 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001770 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1771 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001772 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001773 DAG.getConstant(Log2Val,
1774 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001775 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001776 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001777 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001778 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001779 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1780 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001781 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001782 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1783 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001784 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001785
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001786 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1787 // use.
1788 {
Dan Gohman475871a2008-07-27 21:46:04 +00001789 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001790 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1791 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001793 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001794 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001795 isa<ConstantSDNode>(N1.getOperand(1)) &&
1796 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001797 Sh = N1; Y = N0;
1798 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001799
Gabor Greifba36cb52008-08-28 21:40:38 +00001800 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001801 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1802 Sh.getOperand(0), Y);
1803 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1804 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001805 }
1806 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001807
Chris Lattnera1deca32006-03-04 23:33:26 +00001808 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001809 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001810 isa<ConstantSDNode>(N0.getOperand(1)))
1811 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1812 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1813 N0.getOperand(0), N1),
1814 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1815 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Nate Begemancd4d58c2006-02-03 06:46:56 +00001817 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001818 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001819 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001820 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001821
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001822 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823}
1824
Dan Gohman475871a2008-07-27 21:46:04 +00001825SDValue DAGCombiner::visitSDIV(SDNode *N) {
1826 SDValue N0 = N->getOperand(0);
1827 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1829 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001830 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831
Dan Gohman7f321562007-06-25 16:23:39 +00001832 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001833 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001835 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001837
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001839 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001840 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001841 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001842 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001843 return N0;
1844 // fold (sdiv X, -1) -> 0-X
1845 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001846 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1847 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001848 // If we know the sign bits of both operands are zero, strength reduce to a
1849 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001850 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001851 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001852 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1853 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001854 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001855 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001856 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001857 (N1C->getAPIntValue().isPowerOf2() ||
1858 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001859 // If dividing by powers of two is cheap, then don't perform the following
1860 // fold.
1861 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001862 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001863
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001864 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001865
Chris Lattner8f4880b2006-02-16 08:02:36 +00001866 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001867 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1868 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001869 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001870 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001871
Chris Lattner8f4880b2006-02-16 08:02:36 +00001872 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001873 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1874 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001875 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001876 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001877 AddToWorkList(SRL.getNode());
1878 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001879 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001880 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001881
Nate Begeman405e3ec2005-10-21 00:02:42 +00001882 // If we're dividing by a positive value, we're done. Otherwise, we must
1883 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001884 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001885 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001886
Gabor Greifba36cb52008-08-28 21:40:38 +00001887 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001888 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1889 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001890 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001891
Nate Begeman69575232005-10-20 02:15:44 +00001892 // if integer divide is expensive and we satisfy the requirements, emit an
1893 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001894 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001897 }
Dan Gohman7f321562007-06-25 16:23:39 +00001898
Dan Gohman613e0d82007-07-03 14:03:57 +00001899 // undef / X -> 0
1900 if (N0.getOpcode() == ISD::UNDEF)
1901 return DAG.getConstant(0, VT);
1902 // X / undef -> undef
1903 if (N1.getOpcode() == ISD::UNDEF)
1904 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001905
Dan Gohman475871a2008-07-27 21:46:04 +00001906 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907}
1908
Dan Gohman475871a2008-07-27 21:46:04 +00001909SDValue DAGCombiner::visitUDIV(SDNode *N) {
1910 SDValue N0 = N->getOperand(0);
1911 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001912 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001914 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001915
Dan Gohman7f321562007-06-25 16:23:39 +00001916 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001917 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001919 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001923 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001924 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001925 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001926 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001927 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001928 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001929 getShiftAmountTy(N0.getValueType())));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001930 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1931 if (N1.getOpcode() == ISD::SHL) {
1932 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001933 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001934 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001935 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1936 N1.getOperand(1),
1937 DAG.getConstant(SHC->getAPIntValue()
1938 .logBase2(),
1939 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001940 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001941 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001942 }
1943 }
1944 }
Nate Begeman69575232005-10-20 02:15:44 +00001945 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001946 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001948 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001949 }
Dan Gohman7f321562007-06-25 16:23:39 +00001950
Dan Gohman613e0d82007-07-03 14:03:57 +00001951 // undef / X -> 0
1952 if (N0.getOpcode() == ISD::UNDEF)
1953 return DAG.getConstant(0, VT);
1954 // X / undef -> undef
1955 if (N1.getOpcode() == ISD::UNDEF)
1956 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001957
Dan Gohman475871a2008-07-27 21:46:04 +00001958 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959}
1960
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue DAGCombiner::visitSREM(SDNode *N) {
1962 SDValue N0 = N->getOperand(0);
1963 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001964 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1965 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001966 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001967
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001969 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001970 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001971 // If we know the sign bits of both operands are zero, strength reduce to a
1972 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001973 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001974 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001975 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001976 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001977
Dan Gohman77003042007-11-26 23:46:11 +00001978 // If X/C can be simplified by the division-by-constant logic, lower
1979 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001980 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001981 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001982 AddToWorkList(Div.getNode());
1983 SDValue OptimizedDiv = combine(Div.getNode());
1984 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001985 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1986 OptimizedDiv, N1);
1987 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001988 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001989 return Sub;
1990 }
Chris Lattner26d29902006-10-12 20:58:32 +00001991 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001992
Dan Gohman613e0d82007-07-03 14:03:57 +00001993 // undef % X -> 0
1994 if (N0.getOpcode() == ISD::UNDEF)
1995 return DAG.getConstant(0, VT);
1996 // X % undef -> undef
1997 if (N1.getOpcode() == ISD::UNDEF)
1998 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001999
Dan Gohman475871a2008-07-27 21:46:04 +00002000 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001}
2002
Dan Gohman475871a2008-07-27 21:46:04 +00002003SDValue DAGCombiner::visitUREM(SDNode *N) {
2004 SDValue N0 = N->getOperand(0);
2005 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002006 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2007 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002008 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Nate Begeman1d4d4142005-09-01 00:19:25 +00002010 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002011 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002012 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002013 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002014 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002015 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002016 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002017 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2018 if (N1.getOpcode() == ISD::SHL) {
2019 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002020 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002021 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002022 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002023 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002024 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002025 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002026 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002027 }
2028 }
2029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002030
Dan Gohman77003042007-11-26 23:46:11 +00002031 // If X/C can be simplified by the division-by-constant logic, lower
2032 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002033 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002034 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002035 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002036 SDValue OptimizedDiv = combine(Div.getNode());
2037 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002038 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2039 OptimizedDiv, N1);
2040 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002041 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002042 return Sub;
2043 }
Chris Lattner26d29902006-10-12 20:58:32 +00002044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002045
Dan Gohman613e0d82007-07-03 14:03:57 +00002046 // undef % X -> 0
2047 if (N0.getOpcode() == ISD::UNDEF)
2048 return DAG.getConstant(0, VT);
2049 // X % undef -> undef
2050 if (N1.getOpcode() == ISD::UNDEF)
2051 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002052
Dan Gohman475871a2008-07-27 21:46:04 +00002053 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002054}
2055
Dan Gohman475871a2008-07-27 21:46:04 +00002056SDValue DAGCombiner::visitMULHS(SDNode *N) {
2057 SDValue N0 = N->getOperand(0);
2058 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002059 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002060 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002061 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002062
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002064 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002065 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002067 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002068 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2069 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002070 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002071 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002072 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002073 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002074
Chris Lattnerde1c3602010-12-13 08:39:01 +00002075 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2076 // plus a shift.
2077 if (VT.isSimple() && !VT.isVector()) {
2078 MVT Simple = VT.getSimpleVT();
2079 unsigned SimpleSize = Simple.getSizeInBits();
2080 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2081 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2082 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2083 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2084 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002085 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002086 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002087 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2088 }
2089 }
Owen Anderson95771af2011-02-25 21:41:48 +00002090
Dan Gohman475871a2008-07-27 21:46:04 +00002091 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092}
2093
Dan Gohman475871a2008-07-27 21:46:04 +00002094SDValue DAGCombiner::visitMULHU(SDNode *N) {
2095 SDValue N0 = N->getOperand(0);
2096 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002097 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002098 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002099 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002100
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002102 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002103 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002105 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002106 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002107 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002108 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002109 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002110
Chris Lattnerde1c3602010-12-13 08:39:01 +00002111 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2112 // plus a shift.
2113 if (VT.isSimple() && !VT.isVector()) {
2114 MVT Simple = VT.getSimpleVT();
2115 unsigned SimpleSize = Simple.getSizeInBits();
2116 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2117 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2118 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2119 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2120 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2121 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002122 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002123 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2124 }
2125 }
Owen Anderson95771af2011-02-25 21:41:48 +00002126
Dan Gohman475871a2008-07-27 21:46:04 +00002127 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128}
2129
Dan Gohman389079b2007-10-08 17:57:15 +00002130/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2131/// compute two values. LoOp and HiOp give the opcodes for the two computations
2132/// that are being performed. Return true if a simplification was made.
2133///
Scott Michelfdc40a02009-02-17 22:15:04 +00002134SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002135 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002136 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002137 bool HiExists = N->hasAnyUseOfValue(1);
2138 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002139 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002140 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002141 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2142 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002143 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002144 }
2145
2146 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002147 bool LoExists = N->hasAnyUseOfValue(0);
2148 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002149 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002150 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002151 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2152 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002153 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002154 }
2155
Evan Cheng44711942007-11-08 09:25:29 +00002156 // If both halves are used, return as it is.
2157 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002158 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002159
2160 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002161 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002162 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2163 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002164 AddToWorkList(Lo.getNode());
2165 SDValue LoOpt = combine(Lo.getNode());
2166 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002167 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002168 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002169 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002170 }
2171
Evan Cheng44711942007-11-08 09:25:29 +00002172 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002173 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002174 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002175 AddToWorkList(Hi.getNode());
2176 SDValue HiOpt = combine(Hi.getNode());
2177 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002178 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002179 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002180 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002181 }
Bill Wendling826d1142009-01-30 03:08:40 +00002182
Dan Gohman475871a2008-07-27 21:46:04 +00002183 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002184}
2185
Dan Gohman475871a2008-07-27 21:46:04 +00002186SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2187 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002188 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002189
Chris Lattner33e77d32010-12-15 06:04:19 +00002190 EVT VT = N->getValueType(0);
2191 DebugLoc DL = N->getDebugLoc();
2192
2193 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2194 // plus a shift.
2195 if (VT.isSimple() && !VT.isVector()) {
2196 MVT Simple = VT.getSimpleVT();
2197 unsigned SimpleSize = Simple.getSizeInBits();
2198 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2199 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2200 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2201 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2202 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2203 // Compute the high part as N1.
2204 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002205 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002206 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2207 // Compute the low part as N0.
2208 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2209 return CombineTo(N, Lo, Hi);
2210 }
2211 }
Owen Anderson95771af2011-02-25 21:41:48 +00002212
Dan Gohman475871a2008-07-27 21:46:04 +00002213 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002214}
2215
Dan Gohman475871a2008-07-27 21:46:04 +00002216SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2217 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002218 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002219
Chris Lattner33e77d32010-12-15 06:04:19 +00002220 EVT VT = N->getValueType(0);
2221 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002222
Chris Lattner33e77d32010-12-15 06:04:19 +00002223 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2224 // plus a shift.
2225 if (VT.isSimple() && !VT.isVector()) {
2226 MVT Simple = VT.getSimpleVT();
2227 unsigned SimpleSize = Simple.getSizeInBits();
2228 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2229 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2230 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2231 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2232 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2233 // Compute the high part as N1.
2234 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002235 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002236 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2237 // Compute the low part as N0.
2238 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2239 return CombineTo(N, Lo, Hi);
2240 }
2241 }
Owen Anderson95771af2011-02-25 21:41:48 +00002242
Dan Gohman475871a2008-07-27 21:46:04 +00002243 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002244}
2245
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002246SDValue DAGCombiner::visitSMULO(SDNode *N) {
2247 // (smulo x, 2) -> (saddo x, x)
2248 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2249 if (C2->getAPIntValue() == 2)
2250 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2251 N->getOperand(0), N->getOperand(0));
2252
2253 return SDValue();
2254}
2255
2256SDValue DAGCombiner::visitUMULO(SDNode *N) {
2257 // (umulo x, 2) -> (uaddo x, x)
2258 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2259 if (C2->getAPIntValue() == 2)
2260 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2261 N->getOperand(0), N->getOperand(0));
2262
2263 return SDValue();
2264}
2265
Dan Gohman475871a2008-07-27 21:46:04 +00002266SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2267 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002268 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002269
Dan Gohman475871a2008-07-27 21:46:04 +00002270 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002271}
2272
Dan Gohman475871a2008-07-27 21:46:04 +00002273SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2274 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002275 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Dan Gohman475871a2008-07-27 21:46:04 +00002277 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002278}
2279
Chris Lattner35e5c142006-05-05 05:51:50 +00002280/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2281/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002282SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2283 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002284 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002285 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dan Gohmanff00a552010-01-14 03:08:49 +00002287 // Bail early if none of these transforms apply.
2288 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2289
Chris Lattner540121f2006-05-05 06:31:05 +00002290 // For each of OP in AND/OR/XOR:
2291 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2292 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2293 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002294 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002295 //
2296 // do not sink logical op inside of a vector extend, since it may combine
2297 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002298 EVT Op0VT = N0.getOperand(0).getValueType();
2299 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002300 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002301 // Avoid infinite looping with PromoteIntBinOp.
2302 (N0.getOpcode() == ISD::ANY_EXTEND &&
2303 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002304 (N0.getOpcode() == ISD::TRUNCATE &&
2305 (!TLI.isZExtFree(VT, Op0VT) ||
2306 !TLI.isTruncateFree(Op0VT, VT)) &&
2307 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002308 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002309 Op0VT == N1.getOperand(0).getValueType() &&
2310 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002311 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2312 N0.getOperand(0).getValueType(),
2313 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002314 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002315 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002316 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002317
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002318 // For each of OP in SHL/SRL/SRA/AND...
2319 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2320 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2321 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002322 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002323 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002324 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002325 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2326 N0.getOperand(0).getValueType(),
2327 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002328 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002329 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2330 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002332
Dan Gohman475871a2008-07-27 21:46:04 +00002333 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002334}
2335
Dan Gohman475871a2008-07-27 21:46:04 +00002336SDValue DAGCombiner::visitAND(SDNode *N) {
2337 SDValue N0 = N->getOperand(0);
2338 SDValue N1 = N->getOperand(1);
2339 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002340 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2341 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002342 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002343 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002344
Dan Gohman7f321562007-06-25 16:23:39 +00002345 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002346 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002347 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002350
Dan Gohman613e0d82007-07-03 14:03:57 +00002351 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002352 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002353 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002356 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002357 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002358 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002359 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002360 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002361 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362 return N0;
2363 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002364 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002365 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002366 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002367 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002368 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002369 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002370 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002371 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002372 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002374 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002375 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002376 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2377 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002379 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002380 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002381 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002382 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2383 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002384
Chris Lattner1ec05d12006-03-01 21:47:21 +00002385 // Replace uses of the AND with uses of the Zero extend node.
2386 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002387
Chris Lattner3603cd62006-02-02 07:17:31 +00002388 // We actually want to replace all uses of the any_extend with the
2389 // zero_extend, to avoid duplicating things. This will later cause this
2390 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002391 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002392 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002393 }
2394 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002395 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2396 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2397 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2398 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002399
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002400 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002401 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002402 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002403 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002404 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2405 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002407 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002408 }
Bill Wendling2627a882009-01-30 20:43:18 +00002409 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002410 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002411 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2412 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002413 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002414 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002415 }
Bill Wendling2627a882009-01-30 20:43:18 +00002416 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002417 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002418 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2419 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002420 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002421 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002422 }
2423 }
2424 // canonicalize equivalent to ll == rl
2425 if (LL == RR && LR == RL) {
2426 Op1 = ISD::getSetCCSwappedOperands(Op1);
2427 std::swap(RL, RR);
2428 }
2429 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002430 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002431 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002432 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002433 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002434 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2435 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002436 }
2437 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002438
Bill Wendling2627a882009-01-30 20:43:18 +00002439 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002440 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002442 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002443 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002444
Nate Begemande996292006-02-03 22:24:05 +00002445 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2446 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002447 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SimplifyDemandedBits(SDValue(N, 0)))
2449 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002450
Nate Begemanded49632005-10-13 03:11:28 +00002451 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002452 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002453 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002454 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002455 // If we zero all the possible extended bits, then we can turn this into
2456 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002457 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002458 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002459 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002460 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002461 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002462 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002463 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002464 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002465 LN0->isVolatile(), LN0->isNonTemporal(),
2466 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002467 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002468 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002469 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002470 }
2471 }
2472 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002473 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002474 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002475 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002476 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002477 // If we zero all the possible extended bits, then we can turn this into
2478 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002479 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002480 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002481 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002482 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002483 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002484 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002485 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002486 LN0->getBasePtr(), LN0->getPointerInfo(),
2487 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002488 LN0->isVolatile(), LN0->isNonTemporal(),
2489 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002490 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002491 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002492 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002493 }
2494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002495
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002496 // fold (and (load x), 255) -> (zextload x, i8)
2497 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002498 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2499 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2500 (N0.getOpcode() == ISD::ANY_EXTEND &&
2501 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2502 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2503 LoadSDNode *LN0 = HasAnyExt
2504 ? cast<LoadSDNode>(N0.getOperand(0))
2505 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002506 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002507 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002508 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002509 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2510 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2511 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002512
Evan Chengd40d03e2010-01-06 19:38:29 +00002513 if (ExtVT == LoadedVT &&
2514 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002515 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002516
2517 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002518 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002519 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002520 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002521 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2522 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002523 AddToWorkList(N);
2524 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2525 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2526 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002527
Chris Lattneref7634c2010-01-07 21:53:27 +00002528 // Do not change the width of a volatile load.
2529 // Do not generate loads of non-round integer types since these can
2530 // be expensive (and would be wrong if the type is not byte sized).
2531 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2532 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2533 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002534
Chris Lattneref7634c2010-01-07 21:53:27 +00002535 unsigned Alignment = LN0->getAlignment();
2536 SDValue NewPtr = LN0->getBasePtr();
2537
2538 // For big endian targets, we need to add an offset to the pointer
2539 // to load the correct bytes. For little endian systems, we merely
2540 // need to read fewer bytes from the same pointer.
2541 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002542 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2543 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2544 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002545 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2546 NewPtr, DAG.getConstant(PtrOff, PtrType));
2547 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002548 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002549
2550 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002551
Chris Lattneref7634c2010-01-07 21:53:27 +00002552 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2553 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002554 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002555 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002556 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002557 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2558 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002559 AddToWorkList(N);
2560 CombineTo(LN0, Load, Load.getValue(1));
2561 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002562 }
Evan Cheng466685d2006-10-09 20:57:25 +00002563 }
Chris Lattner15045b62006-02-28 06:35:35 +00002564 }
2565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002566
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002567 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002568}
2569
Evan Cheng9568e5c2011-06-21 06:01:08 +00002570/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2571///
2572SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2573 bool DemandHighBits) {
2574 if (!LegalOperations)
2575 return SDValue();
2576
2577 EVT VT = N->getValueType(0);
2578 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2579 return SDValue();
2580 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2581 return SDValue();
2582
2583 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2584 bool LookPassAnd0 = false;
2585 bool LookPassAnd1 = false;
2586 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2587 std::swap(N0, N1);
2588 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2589 std::swap(N0, N1);
2590 if (N0.getOpcode() == ISD::AND) {
2591 if (!N0.getNode()->hasOneUse())
2592 return SDValue();
2593 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2594 if (!N01C || N01C->getZExtValue() != 0xFF00)
2595 return SDValue();
2596 N0 = N0.getOperand(0);
2597 LookPassAnd0 = true;
2598 }
2599
2600 if (N1.getOpcode() == ISD::AND) {
2601 if (!N1.getNode()->hasOneUse())
2602 return SDValue();
2603 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2604 if (!N11C || N11C->getZExtValue() != 0xFF)
2605 return SDValue();
2606 N1 = N1.getOperand(0);
2607 LookPassAnd1 = true;
2608 }
2609
2610 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2611 std::swap(N0, N1);
2612 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2613 return SDValue();
2614 if (!N0.getNode()->hasOneUse() ||
2615 !N1.getNode()->hasOneUse())
2616 return SDValue();
2617
2618 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2619 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2620 if (!N01C || !N11C)
2621 return SDValue();
2622 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2623 return SDValue();
2624
2625 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2626 SDValue N00 = N0->getOperand(0);
2627 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2628 if (!N00.getNode()->hasOneUse())
2629 return SDValue();
2630 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2631 if (!N001C || N001C->getZExtValue() != 0xFF)
2632 return SDValue();
2633 N00 = N00.getOperand(0);
2634 LookPassAnd0 = true;
2635 }
2636
2637 SDValue N10 = N1->getOperand(0);
2638 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2639 if (!N10.getNode()->hasOneUse())
2640 return SDValue();
2641 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2642 if (!N101C || N101C->getZExtValue() != 0xFF00)
2643 return SDValue();
2644 N10 = N10.getOperand(0);
2645 LookPassAnd1 = true;
2646 }
2647
2648 if (N00 != N10)
2649 return SDValue();
2650
2651 // Make sure everything beyond the low halfword is zero since the SRL 16
2652 // will clear the top bits.
2653 unsigned OpSizeInBits = VT.getSizeInBits();
2654 if (DemandHighBits && OpSizeInBits > 16 &&
2655 (!LookPassAnd0 || !LookPassAnd1) &&
2656 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2657 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002658
Evan Cheng9568e5c2011-06-21 06:01:08 +00002659 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2660 if (OpSizeInBits > 16)
2661 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2662 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2663 return Res;
2664}
2665
2666/// isBSwapHWordElement - Return true if the specified node is an element
2667/// that makes up a 32-bit packed halfword byteswap. i.e.
2668/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2669static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2670 if (!N.getNode()->hasOneUse())
2671 return false;
2672
2673 unsigned Opc = N.getOpcode();
2674 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2675 return false;
2676
2677 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2678 if (!N1C)
2679 return false;
2680
2681 unsigned Num;
2682 switch (N1C->getZExtValue()) {
2683 default:
2684 return false;
2685 case 0xFF: Num = 0; break;
2686 case 0xFF00: Num = 1; break;
2687 case 0xFF0000: Num = 2; break;
2688 case 0xFF000000: Num = 3; break;
2689 }
2690
2691 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2692 SDValue N0 = N.getOperand(0);
2693 if (Opc == ISD::AND) {
2694 if (Num == 0 || Num == 2) {
2695 // (x >> 8) & 0xff
2696 // (x >> 8) & 0xff0000
2697 if (N0.getOpcode() != ISD::SRL)
2698 return false;
2699 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2700 if (!C || C->getZExtValue() != 8)
2701 return false;
2702 } else {
2703 // (x << 8) & 0xff00
2704 // (x << 8) & 0xff000000
2705 if (N0.getOpcode() != ISD::SHL)
2706 return false;
2707 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2708 if (!C || C->getZExtValue() != 8)
2709 return false;
2710 }
2711 } else if (Opc == ISD::SHL) {
2712 // (x & 0xff) << 8
2713 // (x & 0xff0000) << 8
2714 if (Num != 0 && Num != 2)
2715 return false;
2716 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2717 if (!C || C->getZExtValue() != 8)
2718 return false;
2719 } else { // Opc == ISD::SRL
2720 // (x & 0xff00) >> 8
2721 // (x & 0xff000000) >> 8
2722 if (Num != 1 && Num != 3)
2723 return false;
2724 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2725 if (!C || C->getZExtValue() != 8)
2726 return false;
2727 }
2728
2729 if (Parts[Num])
2730 return false;
2731
2732 Parts[Num] = N0.getOperand(0).getNode();
2733 return true;
2734}
2735
2736/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2737/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2738/// => (rotl (bswap x), 16)
2739SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2740 if (!LegalOperations)
2741 return SDValue();
2742
2743 EVT VT = N->getValueType(0);
2744 if (VT != MVT::i32)
2745 return SDValue();
2746 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2747 return SDValue();
2748
2749 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2750 // Look for either
2751 // (or (or (and), (and)), (or (and), (and)))
2752 // (or (or (or (and), (and)), (and)), (and))
2753 if (N0.getOpcode() != ISD::OR)
2754 return SDValue();
2755 SDValue N00 = N0.getOperand(0);
2756 SDValue N01 = N0.getOperand(1);
2757
2758 if (N1.getOpcode() == ISD::OR) {
2759 // (or (or (and), (and)), (or (and), (and)))
2760 SDValue N000 = N00.getOperand(0);
2761 if (!isBSwapHWordElement(N000, Parts))
2762 return SDValue();
2763
2764 SDValue N001 = N00.getOperand(1);
2765 if (!isBSwapHWordElement(N001, Parts))
2766 return SDValue();
2767 SDValue N010 = N01.getOperand(0);
2768 if (!isBSwapHWordElement(N010, Parts))
2769 return SDValue();
2770 SDValue N011 = N01.getOperand(1);
2771 if (!isBSwapHWordElement(N011, Parts))
2772 return SDValue();
2773 } else {
2774 // (or (or (or (and), (and)), (and)), (and))
2775 if (!isBSwapHWordElement(N1, Parts))
2776 return SDValue();
2777 if (!isBSwapHWordElement(N01, Parts))
2778 return SDValue();
2779 if (N00.getOpcode() != ISD::OR)
2780 return SDValue();
2781 SDValue N000 = N00.getOperand(0);
2782 if (!isBSwapHWordElement(N000, Parts))
2783 return SDValue();
2784 SDValue N001 = N00.getOperand(1);
2785 if (!isBSwapHWordElement(N001, Parts))
2786 return SDValue();
2787 }
2788
2789 // Make sure the parts are all coming from the same node.
2790 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
2791 return SDValue();
2792
2793 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
2794 SDValue(Parts[0],0));
2795
2796 // Result of the bswap should be rotated by 16. If it's not legal, than
2797 // do (x << 16) | (x >> 16).
2798 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
2799 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
2800 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
2801 else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
2802 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
2803 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
2804 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
2805 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
2806}
2807
Dan Gohman475871a2008-07-27 21:46:04 +00002808SDValue DAGCombiner::visitOR(SDNode *N) {
2809 SDValue N0 = N->getOperand(0);
2810 SDValue N1 = N->getOperand(1);
2811 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002812 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2813 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002814 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002815
Dan Gohman7f321562007-06-25 16:23:39 +00002816 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002817 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002818 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002819 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002821
Dan Gohman613e0d82007-07-03 14:03:57 +00002822 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00002823 if (!LegalOperations &&
2824 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00002825 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
2826 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
2827 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002828 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002829 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002830 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002831 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002832 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00002833 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002834 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002835 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002836 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002837 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002838 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002839 return N1;
2840 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002841 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002842 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00002843
2844 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
2845 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
2846 if (BSwap.getNode() != 0)
2847 return BSwap;
2848 BSwap = MatchBSwapHWordLow(N, N0, N1);
2849 if (BSwap.getNode() != 0)
2850 return BSwap;
2851
Nate Begemancd4d58c2006-02-03 06:46:56 +00002852 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00002853 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002854 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002855 return ROR;
2856 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002857 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00002858 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00002859 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00002860 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00002861 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002862 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2863 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2864 N0.getOperand(0), N1),
2865 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00002866 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002867 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2868 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2869 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2870 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002871
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002872 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00002874 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2875 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002876 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002877 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002878 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2879 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002880 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002881 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002882 }
Bill Wendling09025642009-01-30 20:59:34 +00002883 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2884 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00002885 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002886 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002887 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2888 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002889 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002890 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002891 }
2892 }
2893 // canonicalize equivalent to ll == rl
2894 if (LL == RR && LR == RL) {
2895 Op1 = ISD::getSetCCSwappedOperands(Op1);
2896 std::swap(RL, RR);
2897 }
2898 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002899 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002900 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002901 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002902 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002903 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2904 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002905 }
2906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002907
Bill Wendling09025642009-01-30 20:59:34 +00002908 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002909 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002910 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002911 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002913
Bill Wendling09025642009-01-30 20:59:34 +00002914 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00002915 if (N0.getOpcode() == ISD::AND &&
2916 N1.getOpcode() == ISD::AND &&
2917 N0.getOperand(1).getOpcode() == ISD::Constant &&
2918 N1.getOperand(1).getOpcode() == ISD::Constant &&
2919 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002920 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002921 // We can only do this xform if we know that bits from X that are set in C2
2922 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002923 const APInt &LHSMask =
2924 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2925 const APInt &RHSMask =
2926 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00002927
Dan Gohmanea859be2007-06-22 14:59:07 +00002928 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2929 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00002930 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2931 N0.getOperand(0), N1.getOperand(0));
2932 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2933 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00002934 }
2935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002936
Chris Lattner516b9622006-09-14 20:50:57 +00002937 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002938 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002939 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002940
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002941 // Simplify the operands using demanded-bits information.
2942 if (!VT.isVector() &&
2943 SimplifyDemandedBits(SDValue(N, 0)))
2944 return SDValue(N, 0);
2945
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002946 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002947}
2948
Chris Lattner516b9622006-09-14 20:50:57 +00002949/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002950static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002951 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002952 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002953 Mask = Op.getOperand(1);
2954 Op = Op.getOperand(0);
2955 } else {
2956 return false;
2957 }
2958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002959
Chris Lattner516b9622006-09-14 20:50:57 +00002960 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2961 Shift = Op;
2962 return true;
2963 }
Bill Wendling09025642009-01-30 20:59:34 +00002964
Scott Michelfdc40a02009-02-17 22:15:04 +00002965 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00002966}
2967
Chris Lattner516b9622006-09-14 20:50:57 +00002968// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2969// idioms for rotate, and if the target supports rotation instructions, generate
2970// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00002971SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002972 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00002973 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002974 if (!TLI.isTypeLegal(VT)) return 0;
2975
2976 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002977 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2978 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002979 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002980
Chris Lattner516b9622006-09-14 20:50:57 +00002981 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002982 SDValue LHSShift; // The shift.
2983 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002984 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2985 return 0; // Not part of a rotate.
2986
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue RHSShift; // The shift.
2988 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002989 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2990 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002991
Chris Lattner516b9622006-09-14 20:50:57 +00002992 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2993 return 0; // Not shifting the same value.
2994
2995 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2996 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00002997
Chris Lattner516b9622006-09-14 20:50:57 +00002998 // Canonicalize shl to left side in a shl/srl pair.
2999 if (RHSShift.getOpcode() == ISD::SHL) {
3000 std::swap(LHS, RHS);
3001 std::swap(LHSShift, RHSShift);
3002 std::swap(LHSMask , RHSMask );
3003 }
3004
Duncan Sands83ec4b62008-06-06 12:08:01 +00003005 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003006 SDValue LHSShiftArg = LHSShift.getOperand(0);
3007 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3008 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003009
3010 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3011 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003012 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3013 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003014 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3015 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003016 if ((LShVal + RShVal) != OpSizeInBits)
3017 return 0;
3018
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00003020 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003021 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00003022 else
Bill Wendling317bd702009-01-30 21:14:50 +00003023 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003024
Chris Lattner516b9622006-09-14 20:50:57 +00003025 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003026 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003027 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003028
Gabor Greifba36cb52008-08-28 21:40:38 +00003029 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003030 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3031 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003032 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003033 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003034 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3035 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003036 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003037
Bill Wendling317bd702009-01-30 21:14:50 +00003038 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003040
Gabor Greifba36cb52008-08-28 21:40:38 +00003041 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003043
Chris Lattner516b9622006-09-14 20:50:57 +00003044 // If there is a mask here, and we have a variable shift, we can't be sure
3045 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003046 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003047 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003048
Chris Lattner516b9622006-09-14 20:50:57 +00003049 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3050 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003051 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3052 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003053 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003054 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003055 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00003056 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003057 return DAG.getNode(ISD::ROTL, DL, VT,
3058 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003059 else
Bill Wendling317bd702009-01-30 21:14:50 +00003060 return DAG.getNode(ISD::ROTR, DL, VT,
3061 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003062 }
Chris Lattner516b9622006-09-14 20:50:57 +00003063 }
3064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003065
Chris Lattner516b9622006-09-14 20:50:57 +00003066 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3067 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003068 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3069 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003070 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003071 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003072 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00003073 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00003074 return DAG.getNode(ISD::ROTR, DL, VT,
3075 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00003076 else
Bill Wendling317bd702009-01-30 21:14:50 +00003077 return DAG.getNode(ISD::ROTL, DL, VT,
3078 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003079 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003080 }
3081 }
3082
Dan Gohman74feef22008-10-17 01:23:35 +00003083 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00003084 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3085 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003086 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3087 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00003088 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3089 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003090 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3091 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003092 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3093 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003094 if (RExtOp0.getOpcode() == ISD::SUB &&
3095 RExtOp0.getOperand(1) == LExtOp0) {
3096 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003097 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003098 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003099 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003100 if (ConstantSDNode *SUBC =
3101 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003102 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003103 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3104 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003105 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003106 }
3107 }
3108 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3109 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003110 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003111 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003112 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003113 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003114 if (ConstantSDNode *SUBC =
3115 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003116 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003117 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3118 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003119 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003120 }
3121 }
Chris Lattner516b9622006-09-14 20:50:57 +00003122 }
3123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003124
Chris Lattner516b9622006-09-14 20:50:57 +00003125 return 0;
3126}
3127
Dan Gohman475871a2008-07-27 21:46:04 +00003128SDValue DAGCombiner::visitXOR(SDNode *N) {
3129 SDValue N0 = N->getOperand(0);
3130 SDValue N1 = N->getOperand(1);
3131 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003134 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003135
Dan Gohman7f321562007-06-25 16:23:39 +00003136 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003137 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003138 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003139 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003141
Evan Cheng26471c42008-03-25 20:08:07 +00003142 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3143 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3144 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003145 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003146 if (N0.getOpcode() == ISD::UNDEF)
3147 return N0;
3148 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003149 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003150 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003151 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003152 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003153 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003154 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003155 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003156 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003157 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003158 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003159 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003160 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003161 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003162 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003163
Nate Begeman1d4d4142005-09-01 00:19:25 +00003164 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003165 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003166 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003167 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3168 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003169
Duncan Sands25cf2272008-11-24 14:53:14 +00003170 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003171 switch (N0.getOpcode()) {
3172 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003173 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003174 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003175 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003176 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003177 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003178 N0.getOperand(3), NotCC);
3179 }
3180 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003181 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003182
Chris Lattner61c5ff42007-09-10 21:39:07 +00003183 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003184 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003185 N0.getNode()->hasOneUse() &&
3186 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003187 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003188 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003189 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003191 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003192 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003193
Bill Wendling317bd702009-01-30 21:14:50 +00003194 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003196 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003197 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003198 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3199 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003200 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3201 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003202 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003203 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003204 }
3205 }
Bill Wendling317bd702009-01-30 21:14:50 +00003206 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003207 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003208 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003210 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3211 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003212 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3213 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003215 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003216 }
3217 }
Bill Wendling317bd702009-01-30 21:14:50 +00003218 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003219 if (N1C && N0.getOpcode() == ISD::XOR) {
3220 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3221 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3222 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003223 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3224 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003225 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003226 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003227 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3228 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003229 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003230 }
3231 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003232 if (N0 == N1)
3233 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003234
Chris Lattner35e5c142006-05-05 05:51:50 +00003235 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3236 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003237 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003238 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003240
Chris Lattner3e104b12006-04-08 04:15:24 +00003241 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003242 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SimplifyDemandedBits(SDValue(N, 0)))
3244 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003245
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003246 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003247}
3248
Chris Lattnere70da202007-12-06 07:33:36 +00003249/// visitShiftByConstant - Handle transforms common to the three shifts, when
3250/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003251SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003252 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003253 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003254
Chris Lattnere70da202007-12-06 07:33:36 +00003255 // We want to pull some binops through shifts, so that we have (and (shift))
3256 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3257 // thing happens with address calculations, so it's important to canonicalize
3258 // it.
3259 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003260
Chris Lattnere70da202007-12-06 07:33:36 +00003261 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003262 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003263 case ISD::OR:
3264 case ISD::XOR:
3265 HighBitSet = false; // We can only transform sra if the high bit is clear.
3266 break;
3267 case ISD::AND:
3268 HighBitSet = true; // We can only transform sra if the high bit is set.
3269 break;
3270 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003271 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003272 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003273 HighBitSet = false; // We can only transform sra if the high bit is clear.
3274 break;
3275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003276
Chris Lattnere70da202007-12-06 07:33:36 +00003277 // We require the RHS of the binop to be a constant as well.
3278 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003279 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003280
3281 // FIXME: disable this unless the input to the binop is a shift by a constant.
3282 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003283 //
Bill Wendling88103372009-01-30 21:37:17 +00003284 // void foo(int *X, int i) { X[i & 1235] = 1; }
3285 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003286 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003287 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003288 BinOpLHSVal->getOpcode() != ISD::SRA &&
3289 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3290 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003291 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003292
Owen Andersone50ed302009-08-10 22:56:29 +00003293 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003294
Bill Wendling88103372009-01-30 21:37:17 +00003295 // If this is a signed shift right, and the high bit is modified by the
3296 // logical operation, do not perform the transformation. The highBitSet
3297 // boolean indicates the value of the high bit of the constant which would
3298 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003299 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003300 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3301 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003302 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003304
Chris Lattnere70da202007-12-06 07:33:36 +00003305 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003306 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3307 N->getValueType(0),
3308 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003309
3310 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003311 SDValue NewShift = DAG.getNode(N->getOpcode(),
3312 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003313 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003314
3315 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003316 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003317}
3318
Dan Gohman475871a2008-07-27 21:46:04 +00003319SDValue DAGCombiner::visitSHL(SDNode *N) {
3320 SDValue N0 = N->getOperand(0);
3321 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003322 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3323 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003324 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003325 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003326
Nate Begeman1d4d4142005-09-01 00:19:25 +00003327 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003328 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003329 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003330 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003331 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003332 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003333 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003334 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003335 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003337 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003338 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003339 // fold (shl undef, x) -> 0
3340 if (N0.getOpcode() == ISD::UNDEF)
3341 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003342 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003343 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003344 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003345 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003346 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003347 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003348 N1.getOperand(0).getOpcode() == ISD::AND &&
3349 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003350 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003351 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003352 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003353 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003354 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003355 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003356 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003357 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3358 DAG.getNode(ISD::TRUNCATE,
3359 N->getDebugLoc(),
3360 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003361 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003362 }
3363 }
3364
Dan Gohman475871a2008-07-27 21:46:04 +00003365 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3366 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003367
3368 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003369 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003370 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003371 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3372 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003373 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003374 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003375 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003376 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003377 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003378
3379 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3380 // For this to be valid, the second form must not preserve any of the bits
3381 // that are shifted out by the inner shift in the first form. This means
3382 // the outer shift size must be >= the number of bits added by the ext.
3383 // As a corollary, we don't care what kind of ext it is.
3384 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3385 N0.getOpcode() == ISD::ANY_EXTEND ||
3386 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3387 N0.getOperand(0).getOpcode() == ISD::SHL &&
3388 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003389 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003390 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3391 uint64_t c2 = N1C->getZExtValue();
3392 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3393 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3394 if (c2 >= OpSizeInBits - InnerShiftSize) {
3395 if (c1 + c2 >= OpSizeInBits)
3396 return DAG.getConstant(0, VT);
3397 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3398 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3399 N0.getOperand(0)->getOperand(0)),
3400 DAG.getConstant(c1 + c2, N1.getValueType()));
3401 }
3402 }
3403
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003404 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3405 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003406 // Only fold this if the inner shift has no other uses -- if it does, folding
3407 // this will increase the total number of instructions.
3408 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003409 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003410 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003411 if (c1 < VT.getSizeInBits()) {
3412 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003413 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3414 VT.getSizeInBits() - c1);
3415 SDValue Shift;
3416 if (c2 > c1) {
3417 Mask = Mask.shl(c2-c1);
3418 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3419 DAG.getConstant(c2-c1, N1.getValueType()));
3420 } else {
3421 Mask = Mask.lshr(c1-c2);
3422 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3423 DAG.getConstant(c1-c2, N1.getValueType()));
3424 }
3425 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3426 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003427 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003428 }
Bill Wendling88103372009-01-30 21:37:17 +00003429 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003430 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3431 SDValue HiBitsMask =
3432 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3433 VT.getSizeInBits() -
3434 N1C->getZExtValue()),
3435 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003436 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003437 HiBitsMask);
3438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003439
Evan Chenge5b51ac2010-04-17 06:13:15 +00003440 if (N1C) {
3441 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3442 if (NewSHL.getNode())
3443 return NewSHL;
3444 }
3445
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003446 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003447}
3448
Dan Gohman475871a2008-07-27 21:46:04 +00003449SDValue DAGCombiner::visitSRA(SDNode *N) {
3450 SDValue N0 = N->getOperand(0);
3451 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003452 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3453 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003454 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003455 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003456
Bill Wendling88103372009-01-30 21:37:17 +00003457 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003458 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003459 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003460 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003461 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003462 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003463 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003464 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003465 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003466 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003467 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003468 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003469 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003470 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003471 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003472 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3473 // sext_inreg.
3474 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003475 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003476 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3477 if (VT.isVector())
3478 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3479 ExtVT, VT.getVectorNumElements());
3480 if ((!LegalOperations ||
3481 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003482 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003483 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003484 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003485
Bill Wendling88103372009-01-30 21:37:17 +00003486 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003487 if (N1C && N0.getOpcode() == ISD::SRA) {
3488 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003489 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003490 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003491 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003492 DAG.getConstant(Sum, N1C->getValueType(0)));
3493 }
3494 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003495
Bill Wendling88103372009-01-30 21:37:17 +00003496 // fold (sra (shl X, m), (sub result_size, n))
3497 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003498 // result_size - n != m.
3499 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003500 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003501 if (N0.getOpcode() == ISD::SHL) {
3502 // Get the two constanst of the shifts, CN0 = m, CN = n.
3503 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3504 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003505 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003506 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003507 EVT::getIntegerVT(*DAG.getContext(),
3508 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003509 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003510 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003511
Scott Michelfdc40a02009-02-17 22:15:04 +00003512 // If the shift is not a no-op (in which case this should be just a sign
3513 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003514 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003515 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003516 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003517 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3518 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003519 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003520
Owen Anderson95771af2011-02-25 21:41:48 +00003521 SDValue Amt = DAG.getConstant(ShiftAmt,
3522 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003523 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3524 N0.getOperand(0), Amt);
3525 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3526 Shift);
3527 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3528 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003529 }
3530 }
3531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003532
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003533 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003534 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003535 N1.getOperand(0).getOpcode() == ISD::AND &&
3536 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003537 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003538 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003539 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003540 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003541 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003542 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003543 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003544 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003545 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003546 DAG.getNode(ISD::TRUNCATE,
3547 N->getDebugLoc(),
3548 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003549 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003550 }
3551 }
3552
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003553 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3554 // if c1 is equal to the number of bits the trunc removes
3555 if (N0.getOpcode() == ISD::TRUNCATE &&
3556 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3557 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3558 N0.getOperand(0).hasOneUse() &&
3559 N0.getOperand(0).getOperand(1).hasOneUse() &&
3560 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3561 EVT LargeVT = N0.getOperand(0).getValueType();
3562 ConstantSDNode *LargeShiftAmt =
3563 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3564
3565 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3566 LargeShiftAmt->getZExtValue()) {
3567 SDValue Amt =
3568 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003569 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003570 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3571 N0.getOperand(0).getOperand(0), Amt);
3572 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3573 }
3574 }
3575
Scott Michelfdc40a02009-02-17 22:15:04 +00003576 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003577 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3578 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003579
3580
Nate Begeman1d4d4142005-09-01 00:19:25 +00003581 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003582 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003583 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003584
Evan Chenge5b51ac2010-04-17 06:13:15 +00003585 if (N1C) {
3586 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3587 if (NewSRA.getNode())
3588 return NewSRA;
3589 }
3590
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003591 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003592}
3593
Dan Gohman475871a2008-07-27 21:46:04 +00003594SDValue DAGCombiner::visitSRL(SDNode *N) {
3595 SDValue N0 = N->getOperand(0);
3596 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003597 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3598 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003599 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003600 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003601
Nate Begeman1d4d4142005-09-01 00:19:25 +00003602 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003603 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003604 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003606 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003607 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003608 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003609 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003610 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003611 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003612 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003613 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003614 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003615 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003616 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003617 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003618
Bill Wendling88103372009-01-30 21:37:17 +00003619 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003620 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003621 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003622 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3623 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003624 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003625 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003626 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003627 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003628 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003629
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003630 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003631 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3632 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003633 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003634 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003635 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3636 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003637 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3638 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003639 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003640 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003641 if (c1 + OpSizeInBits == InnerShiftSize) {
3642 if (c1 + c2 >= InnerShiftSize)
3643 return DAG.getConstant(0, VT);
3644 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003645 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003646 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003647 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003648 }
3649 }
3650
Chris Lattnerefcddc32010-04-15 05:28:43 +00003651 // fold (srl (shl x, c), c) -> (and x, cst2)
3652 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3653 N0.getValueSizeInBits() <= 64) {
3654 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3655 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3656 DAG.getConstant(~0ULL >> ShAmt, VT));
3657 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003658
Scott Michelfdc40a02009-02-17 22:15:04 +00003659
Chris Lattner06afe072006-05-05 22:53:17 +00003660 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3661 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3662 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003663 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003664 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003665 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003666
Evan Chenge5b51ac2010-04-17 06:13:15 +00003667 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003668 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003669 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003670 N0.getOperand(0),
3671 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003672 AddToWorkList(SmallShift.getNode());
3673 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3674 }
Chris Lattner06afe072006-05-05 22:53:17 +00003675 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003676
Chris Lattner3657ffe2006-10-12 20:23:19 +00003677 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3678 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003679 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003680 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003681 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003682 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003683
Chris Lattner350bec02006-04-02 06:11:11 +00003684 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003685 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003686 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003687 APInt KnownZero, KnownOne;
Dan Gohman5b870af2010-03-02 02:14:38 +00003688 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00003689 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003690
Chris Lattner350bec02006-04-02 06:11:11 +00003691 // If any of the input bits are KnownOne, then the input couldn't be all
3692 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003693 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003694
Chris Lattner350bec02006-04-02 06:11:11 +00003695 // If all of the bits input the to ctlz node are known to be zero, then
3696 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003697 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00003698 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003699
Chris Lattner350bec02006-04-02 06:11:11 +00003700 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003701 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003702 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003703 // could be set on input to the CTLZ node. If this bit is set, the SRL
3704 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3705 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003706 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003707 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003708
Chris Lattner350bec02006-04-02 06:11:11 +00003709 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003710 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003711 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003712 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003713 }
Bill Wendling88103372009-01-30 21:37:17 +00003714
3715 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3716 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003717 }
3718 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003719
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003720 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003721 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003722 N1.getOperand(0).getOpcode() == ISD::AND &&
3723 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003724 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003725 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003726 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003727 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003728 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003729 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003730 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003731 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003732 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003733 DAG.getNode(ISD::TRUNCATE,
3734 N->getDebugLoc(),
3735 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003736 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003737 }
3738 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003739
Chris Lattner61a4c072007-04-18 03:06:49 +00003740 // fold operands of srl based on knowledge that the low bits are not
3741 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003742 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3743 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003744
Evan Cheng9ab2b982009-12-18 21:31:31 +00003745 if (N1C) {
3746 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3747 if (NewSRL.getNode())
3748 return NewSRL;
3749 }
3750
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003751 // Attempt to convert a srl of a load into a narrower zero-extending load.
3752 SDValue NarrowLoad = ReduceLoadWidth(N);
3753 if (NarrowLoad.getNode())
3754 return NarrowLoad;
3755
Evan Cheng9ab2b982009-12-18 21:31:31 +00003756 // Here is a common situation. We want to optimize:
3757 //
3758 // %a = ...
3759 // %b = and i32 %a, 2
3760 // %c = srl i32 %b, 1
3761 // brcond i32 %c ...
3762 //
3763 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003764 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00003765 // %a = ...
3766 // %b = and %a, 2
3767 // %c = setcc eq %b, 0
3768 // brcond %c ...
3769 //
3770 // However when after the source operand of SRL is optimized into AND, the SRL
3771 // itself may not be optimized further. Look for it and add the BRCOND into
3772 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003773 if (N->hasOneUse()) {
3774 SDNode *Use = *N->use_begin();
3775 if (Use->getOpcode() == ISD::BRCOND)
3776 AddToWorkList(Use);
3777 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3778 // Also look pass the truncate.
3779 Use = *Use->use_begin();
3780 if (Use->getOpcode() == ISD::BRCOND)
3781 AddToWorkList(Use);
3782 }
3783 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003784
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003785 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00003786}
3787
Dan Gohman475871a2008-07-27 21:46:04 +00003788SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3789 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003790 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003791
3792 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003793 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003794 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003795 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003796}
3797
Chandler Carruth63974b22011-12-13 01:56:10 +00003798SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
3799 SDValue N0 = N->getOperand(0);
3800 EVT VT = N->getValueType(0);
3801
3802 // fold (ctlz_zero_undef c1) -> c2
3803 if (isa<ConstantSDNode>(N0))
3804 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
3805 return SDValue();
3806}
3807
Dan Gohman475871a2008-07-27 21:46:04 +00003808SDValue DAGCombiner::visitCTTZ(SDNode *N) {
3809 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003810 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003811
Nate Begeman1d4d4142005-09-01 00:19:25 +00003812 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003813 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003814 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003815 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816}
3817
Chandler Carruth63974b22011-12-13 01:56:10 +00003818SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
3819 SDValue N0 = N->getOperand(0);
3820 EVT VT = N->getValueType(0);
3821
3822 // fold (cttz_zero_undef c1) -> c2
3823 if (isa<ConstantSDNode>(N0))
3824 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
3825 return SDValue();
3826}
3827
Dan Gohman475871a2008-07-27 21:46:04 +00003828SDValue DAGCombiner::visitCTPOP(SDNode *N) {
3829 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003830 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003831
Nate Begeman1d4d4142005-09-01 00:19:25 +00003832 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003833 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003834 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003835 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003836}
3837
Dan Gohman475871a2008-07-27 21:46:04 +00003838SDValue DAGCombiner::visitSELECT(SDNode *N) {
3839 SDValue N0 = N->getOperand(0);
3840 SDValue N1 = N->getOperand(1);
3841 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00003842 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3843 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3844 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00003845 EVT VT = N->getValueType(0);
3846 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00003847
Bill Wendling34584e62009-01-30 22:02:18 +00003848 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00003849 if (N1 == N2)
3850 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003851 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00003852 if (N0C && !N0C->isNullValue())
3853 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00003854 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00003855 if (N0C && N0C->isNullValue())
3856 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00003857 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00003859 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3860 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00003861 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00003863 (VT0.isInteger() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00003864 TLI.getBooleanContents(false) == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00003865 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003866 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00003867 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00003868 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
3869 N0, DAG.getConstant(1, VT0));
3870 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
3871 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003872 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00003873 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00003874 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
3875 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00003876 }
Bill Wendling34584e62009-01-30 22:02:18 +00003877 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00003879 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003880 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003881 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00003882 }
Bill Wendling34584e62009-01-30 22:02:18 +00003883 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003884 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00003885 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00003886 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00003887 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00003888 }
Bill Wendling34584e62009-01-30 22:02:18 +00003889 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00003891 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
3892 // fold (select X, X, Y) -> (or X, Y)
3893 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00003895 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
3896 // fold (select X, Y, X) -> (and X, Y)
3897 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00003899 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003900
Chris Lattner40c62d52005-10-18 06:04:22 +00003901 // If we can fold this based on the true/false value, do so.
3902 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00003903 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003904
Nate Begeman44728a72005-09-19 22:34:01 +00003905 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003906 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003907 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00003909 // having to say they don't support SELECT_CC on every type the DAG knows
3910 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00003912 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00003913 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
3914 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00003915 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00003916 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003917 }
Bill Wendling34584e62009-01-30 22:02:18 +00003918
Dan Gohman475871a2008-07-27 21:46:04 +00003919 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00003920}
3921
Dan Gohman475871a2008-07-27 21:46:04 +00003922SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
3923 SDValue N0 = N->getOperand(0);
3924 SDValue N1 = N->getOperand(1);
3925 SDValue N2 = N->getOperand(2);
3926 SDValue N3 = N->getOperand(3);
3927 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00003928 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Nate Begeman44728a72005-09-19 22:34:01 +00003930 // fold select_cc lhs, rhs, x, x, cc -> x
3931 if (N2 == N3)
3932 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003933
Chris Lattner5f42a242006-09-20 06:19:26 +00003934 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00003935 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003936 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00003938
Gabor Greifba36cb52008-08-28 21:40:38 +00003939 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003940 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00003941 return N2; // cond always true -> true val
3942 else
3943 return N3; // cond always false -> false val
3944 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003945
Chris Lattner5f42a242006-09-20 06:19:26 +00003946 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00003947 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00003948 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
3949 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00003950 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003951
Chris Lattner40c62d52005-10-18 06:04:22 +00003952 // If we can fold this based on the true/false value, do so.
3953 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00003954 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00003955
Nate Begeman44728a72005-09-19 22:34:01 +00003956 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00003957 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00003958}
3959
Dan Gohman475871a2008-07-27 21:46:04 +00003960SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00003961 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003962 cast<CondCodeSDNode>(N->getOperand(2))->get(),
3963 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00003964}
3965
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003966// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00003967// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003968// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00003969// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00003970static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003971 unsigned ExtOpc,
3972 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00003973 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003974 bool HasCopyToRegUses = false;
3975 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00003976 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
3977 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003978 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00003979 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003980 if (User == N)
3981 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00003982 if (UI.getUse().getResNo() != N0.getResNo())
3983 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003984 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00003985 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003986 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
3987 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
3988 // Sign bits will be lost after a zext.
3989 return false;
3990 bool Add = false;
3991 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003992 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003993 if (UseOp == N0)
3994 continue;
3995 if (!isa<ConstantSDNode>(UseOp))
3996 return false;
3997 Add = true;
3998 }
3999 if (Add)
4000 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004001 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004002 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004003 // If truncates aren't free and there are users we can't
4004 // extend, it isn't worthwhile.
4005 if (!isTruncFree)
4006 return false;
4007 // Remember if this value is live-out.
4008 if (User->getOpcode() == ISD::CopyToReg)
4009 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004010 }
4011
4012 if (HasCopyToRegUses) {
4013 bool BothLiveOut = false;
4014 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4015 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004016 SDUse &Use = UI.getUse();
4017 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4018 BothLiveOut = true;
4019 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004020 }
4021 }
4022 if (BothLiveOut)
4023 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004024 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004025 return ExtendNodes.size();
4026 }
4027 return true;
4028}
4029
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004030void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4031 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4032 ISD::NodeType ExtType) {
4033 // Extend SetCC uses if necessary.
4034 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4035 SDNode *SetCC = SetCCs[i];
4036 SmallVector<SDValue, 4> Ops;
4037
4038 for (unsigned j = 0; j != 2; ++j) {
4039 SDValue SOp = SetCC->getOperand(j);
4040 if (SOp == Trunc)
4041 Ops.push_back(ExtLoad);
4042 else
4043 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4044 }
4045
4046 Ops.push_back(SetCC->getOperand(2));
4047 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4048 &Ops[0], Ops.size()));
4049 }
4050}
4051
Dan Gohman475871a2008-07-27 21:46:04 +00004052SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4053 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004054 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004055
Nate Begeman1d4d4142005-09-01 00:19:25 +00004056 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004057 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004058 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004059
Nate Begeman1d4d4142005-09-01 00:19:25 +00004060 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004061 // fold (sext (aext x)) -> (sext x)
4062 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004063 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4064 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004065
Chris Lattner22558872007-02-26 03:13:59 +00004066 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004067 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4068 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004069 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4070 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004071 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4072 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004073 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004074 // CombineTo deleted the truncate, if needed, but not what's under it.
4075 AddToWorkList(oye);
4076 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004077 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004078 }
Evan Chengc88138f2007-03-22 01:54:19 +00004079
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004080 // See if the value being truncated is already sign extended. If so, just
4081 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004082 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004083 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4084 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4085 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004086 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004087
Chris Lattner22558872007-02-26 03:13:59 +00004088 if (OpBits == DestBits) {
4089 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4090 // bits, it is already ready.
4091 if (NumSignBits > DestBits-MidBits)
4092 return Op;
4093 } else if (OpBits < DestBits) {
4094 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4095 // bits, just sext from i32.
4096 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004097 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004098 } else {
4099 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4100 // bits, just truncate to i32.
4101 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004102 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004103 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004104
Chris Lattner22558872007-02-26 03:13:59 +00004105 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004106 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4107 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004108 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004109 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004110 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004111 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4112 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004113 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004114 }
Chris Lattner6007b842006-09-21 06:00:20 +00004115 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004116
Evan Cheng110dec22005-12-14 02:19:23 +00004117 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004118 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004119 // on vectors in one instruction. We only perform this transformation on
4120 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004121 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004122 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004123 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004124 bool DoXform = true;
4125 SmallVector<SDNode*, 4> SetCCs;
4126 if (!N0.hasOneUse())
4127 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4128 if (DoXform) {
4129 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004130 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004131 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004132 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004133 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004134 LN0->isVolatile(), LN0->isNonTemporal(),
4135 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004136 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004137 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4138 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004139 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004140 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4141 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004142 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004143 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004144 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004145
4146 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4147 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004148 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4149 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004150 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004151 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004152 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004153 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004154 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004155 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004156 LN0->getBasePtr(), LN0->getPointerInfo(),
4157 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004158 LN0->isVolatile(), LN0->isNonTemporal(),
4159 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004160 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004161 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004162 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4163 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004164 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004165 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004166 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004168
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004169 // fold (sext (and/or/xor (load x), cst)) ->
4170 // (and/or/xor (sextload x), (sext cst))
4171 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4172 N0.getOpcode() == ISD::XOR) &&
4173 isa<LoadSDNode>(N0.getOperand(0)) &&
4174 N0.getOperand(1).getOpcode() == ISD::Constant &&
4175 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4176 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4177 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4178 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4179 bool DoXform = true;
4180 SmallVector<SDNode*, 4> SetCCs;
4181 if (!N0.hasOneUse())
4182 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4183 SetCCs, TLI);
4184 if (DoXform) {
4185 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4186 LN0->getChain(), LN0->getBasePtr(),
4187 LN0->getPointerInfo(),
4188 LN0->getMemoryVT(),
4189 LN0->isVolatile(),
4190 LN0->isNonTemporal(),
4191 LN0->getAlignment());
4192 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4193 Mask = Mask.sext(VT.getSizeInBits());
4194 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4195 ExtLoad, DAG.getConstant(Mask, VT));
4196 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4197 N0.getOperand(0).getDebugLoc(),
4198 N0.getOperand(0).getValueType(), ExtLoad);
4199 CombineTo(N, And);
4200 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4201 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4202 ISD::SIGN_EXTEND);
4203 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4204 }
4205 }
4206 }
4207
Chris Lattner20a35c32007-04-11 05:32:27 +00004208 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004209 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004210 // Only do this before legalize for now.
4211 if (VT.isVector() && !LegalOperations) {
4212 EVT N0VT = N0.getOperand(0).getValueType();
Chris Lattner2b7a2712009-07-08 00:31:33 +00004213 // We know that the # elements of the results is the same as the
4214 // # elements of the compare (and the # elements of the compare result
4215 // for that matter). Check to see that they are the same size. If so,
4216 // we know that the element size of the sext'd result matches the
4217 // element size of the compare operands.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004218 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004219 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004220 N0.getOperand(1),
4221 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Dan Gohman3ce89f42010-04-30 17:19:19 +00004222 // If the desired elements are smaller or larger than the source
4223 // elements we can use a matching integer vector type and then
4224 // truncate/sign extend
4225 else {
Duncan Sands34727662010-07-12 08:16:59 +00004226 EVT MatchingElementType =
4227 EVT::getIntegerVT(*DAG.getContext(),
4228 N0VT.getScalarType().getSizeInBits());
4229 EVT MatchingVectorType =
4230 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4231 N0VT.getVectorNumElements());
4232 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004233 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004234 N0.getOperand(1),
4235 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4236 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004237 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004238 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004239
Chris Lattner2b7a2712009-07-08 00:31:33 +00004240 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004241 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004242 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004243 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004244 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004245 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004246 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004247 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004248 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004249 if (!LegalOperations ||
4250 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
4251 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4252 DAG.getSetCC(N->getDebugLoc(),
4253 TLI.getSetCCResultType(VT),
4254 N0.getOperand(0), N0.getOperand(1),
4255 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4256 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004258
Dan Gohman8f0ad582008-04-28 16:58:24 +00004259 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004260 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004261 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004262 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004263
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004264 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004265}
4266
Dan Gohman475871a2008-07-27 21:46:04 +00004267SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4268 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004269 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004270
Nate Begeman1d4d4142005-09-01 00:19:25 +00004271 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004272 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004273 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004274 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004275 // fold (zext (aext x)) -> (zext x)
4276 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004277 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4278 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004279
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004280 // fold (zext (truncate x)) -> (zext x) or
4281 // (zext (truncate x)) -> (truncate x)
4282 // This is valid when the truncated bits of x are already zero.
4283 // FIXME: We should extend this to work for vectors too.
4284 if (N0.getOpcode() == ISD::TRUNCATE && !VT.isVector()) {
4285 SDValue Op = N0.getOperand(0);
4286 APInt TruncatedBits
4287 = APInt::getBitsSet(Op.getValueSizeInBits(),
4288 N0.getValueSizeInBits(),
4289 std::min(Op.getValueSizeInBits(),
4290 VT.getSizeInBits()));
4291 APInt KnownZero, KnownOne;
4292 DAG.ComputeMaskedBits(Op, TruncatedBits, KnownZero, KnownOne);
4293 if (TruncatedBits == KnownZero) {
4294 if (VT.bitsGT(Op.getValueType()))
4295 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4296 if (VT.bitsLT(Op.getValueType()))
4297 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4298
4299 return Op;
4300 }
4301 }
4302
Evan Chengc88138f2007-03-22 01:54:19 +00004303 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4304 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004305 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004306 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4307 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004308 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4309 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004310 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004311 // CombineTo deleted the truncate, if needed, but not what's under it.
4312 AddToWorkList(oye);
4313 }
Eli Friedmane545d382011-04-16 23:25:34 +00004314 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004315 }
Evan Chengc88138f2007-03-22 01:54:19 +00004316 }
4317
Chris Lattner6007b842006-09-21 06:00:20 +00004318 // fold (zext (truncate x)) -> (and x, mask)
4319 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004320 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004321
4322 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4323 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4324 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4325 if (NarrowLoad.getNode()) {
4326 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4327 if (NarrowLoad.getNode() != N0.getNode()) {
4328 CombineTo(N0.getNode(), NarrowLoad);
4329 // CombineTo deleted the truncate, if needed, but not what's under it.
4330 AddToWorkList(oye);
4331 }
4332 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4333 }
4334
Dan Gohman475871a2008-07-27 21:46:04 +00004335 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004336 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004337 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004338 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004339 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004340 }
Dan Gohman87862e72009-12-11 21:31:27 +00004341 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4342 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004343 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004344
Dan Gohman97121ba2009-04-08 00:15:30 +00004345 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4346 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004347 if (N0.getOpcode() == ISD::AND &&
4348 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004349 N0.getOperand(1).getOpcode() == ISD::Constant &&
4350 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4351 N0.getValueType()) ||
4352 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004353 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004354 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004355 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004356 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004357 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004358 }
Dan Gohman220a8232008-03-03 23:51:38 +00004359 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004360 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004361 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4362 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004364
Evan Cheng110dec22005-12-14 02:19:23 +00004365 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004366 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004367 // on vectors in one instruction. We only perform this transformation on
4368 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004369 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004370 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004371 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004372 bool DoXform = true;
4373 SmallVector<SDNode*, 4> SetCCs;
4374 if (!N0.hasOneUse())
4375 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4376 if (DoXform) {
4377 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004378 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004379 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004380 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004381 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004382 LN0->isVolatile(), LN0->isNonTemporal(),
4383 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004384 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004385 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4386 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004387 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004388
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004389 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4390 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004391 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004392 }
Evan Cheng110dec22005-12-14 02:19:23 +00004393 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004394
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004395 // fold (zext (and/or/xor (load x), cst)) ->
4396 // (and/or/xor (zextload x), (zext cst))
4397 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4398 N0.getOpcode() == ISD::XOR) &&
4399 isa<LoadSDNode>(N0.getOperand(0)) &&
4400 N0.getOperand(1).getOpcode() == ISD::Constant &&
4401 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4402 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4403 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4404 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4405 bool DoXform = true;
4406 SmallVector<SDNode*, 4> SetCCs;
4407 if (!N0.hasOneUse())
4408 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4409 SetCCs, TLI);
4410 if (DoXform) {
4411 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4412 LN0->getChain(), LN0->getBasePtr(),
4413 LN0->getPointerInfo(),
4414 LN0->getMemoryVT(),
4415 LN0->isVolatile(),
4416 LN0->isNonTemporal(),
4417 LN0->getAlignment());
4418 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4419 Mask = Mask.zext(VT.getSizeInBits());
4420 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4421 ExtLoad, DAG.getConstant(Mask, VT));
4422 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4423 N0.getOperand(0).getDebugLoc(),
4424 N0.getOperand(0).getValueType(), ExtLoad);
4425 CombineTo(N, And);
4426 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4427 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4428 ISD::ZERO_EXTEND);
4429 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4430 }
4431 }
4432 }
4433
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004434 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4435 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004436 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4437 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004438 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004439 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004440 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004441 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004442 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004443 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004444 LN0->getBasePtr(), LN0->getPointerInfo(),
4445 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004446 LN0->isVolatile(), LN0->isNonTemporal(),
4447 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004448 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004449 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004450 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4451 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004452 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004453 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004454 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004456
Chris Lattner20a35c32007-04-11 05:32:27 +00004457 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004458 if (!LegalOperations && VT.isVector()) {
4459 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4460 // Only do this before legalize for now.
4461 EVT N0VT = N0.getOperand(0).getValueType();
4462 EVT EltVT = VT.getVectorElementType();
4463 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4464 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004465 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004466 // We know that the # elements of the results is the same as the
4467 // # elements of the compare (and the # elements of the compare result
4468 // for that matter). Check to see that they are the same size. If so,
4469 // we know that the element size of the sext'd result matches the
4470 // element size of the compare operands.
4471 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004472 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004473 N0.getOperand(1),
4474 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4475 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4476 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004477
4478 // If the desired elements are smaller or larger than the source
4479 // elements we can use a matching integer vector type and then
4480 // truncate/sign extend
4481 EVT MatchingElementType =
4482 EVT::getIntegerVT(*DAG.getContext(),
4483 N0VT.getScalarType().getSizeInBits());
4484 EVT MatchingVectorType =
4485 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4486 N0VT.getVectorNumElements());
4487 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004488 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004489 N0.getOperand(1),
4490 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4491 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4492 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4493 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4494 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004495 }
4496
4497 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004498 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004499 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004500 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004501 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004502 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004503 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004504
Evan Cheng9818c042009-12-15 03:00:32 +00004505 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004506 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004507 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004508 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4509 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004510 SDValue ShAmt = N0.getOperand(1);
4511 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004512 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004513 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004514 // If the original shl may be shifting out bits, do not perform this
4515 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004516 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4517 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4518 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004519 return SDValue();
4520 }
Chris Lattnere0751182011-02-13 19:09:16 +00004521
4522 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004523
4524 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004525 if (VT.getSizeInBits() >= 256)
4526 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004527
Chris Lattnere0751182011-02-13 19:09:16 +00004528 return DAG.getNode(N0.getOpcode(), DL, VT,
4529 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4530 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004531 }
4532
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004533 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004534}
4535
Dan Gohman475871a2008-07-27 21:46:04 +00004536SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4537 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004538 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Chris Lattner5ffc0662006-05-05 05:58:59 +00004540 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004541 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004542 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004543 // fold (aext (aext x)) -> (aext x)
4544 // fold (aext (zext x)) -> (zext x)
4545 // fold (aext (sext x)) -> (sext x)
4546 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4547 N0.getOpcode() == ISD::ZERO_EXTEND ||
4548 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004549 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004550
Evan Chengc88138f2007-03-22 01:54:19 +00004551 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4552 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4553 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004554 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4555 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004556 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4557 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004558 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004559 // CombineTo deleted the truncate, if needed, but not what's under it.
4560 AddToWorkList(oye);
4561 }
Eli Friedmane545d382011-04-16 23:25:34 +00004562 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004563 }
Evan Chengc88138f2007-03-22 01:54:19 +00004564 }
4565
Chris Lattner84750582006-09-20 06:29:17 +00004566 // fold (aext (truncate x))
4567 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004568 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004569 if (TruncOp.getValueType() == VT)
4570 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004571 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004572 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4573 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004575
Dan Gohman97121ba2009-04-08 00:15:30 +00004576 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4577 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004578 if (N0.getOpcode() == ISD::AND &&
4579 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004580 N0.getOperand(1).getOpcode() == ISD::Constant &&
4581 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4582 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004583 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004584 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004585 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004586 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004587 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004588 }
Dan Gohman220a8232008-03-03 23:51:38 +00004589 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004590 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004591 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4592 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004593 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004594
Chris Lattner5ffc0662006-05-05 05:58:59 +00004595 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004596 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004597 // on vectors in one instruction. We only perform this transformation on
4598 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004599 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004600 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004601 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004602 bool DoXform = true;
4603 SmallVector<SDNode*, 4> SetCCs;
4604 if (!N0.hasOneUse())
4605 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4606 if (DoXform) {
4607 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004608 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004609 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004610 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004611 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004612 LN0->isVolatile(), LN0->isNonTemporal(),
4613 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004614 CombineTo(N, ExtLoad);
4615 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4616 N0.getValueType(), ExtLoad);
4617 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004618 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4619 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004620 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4621 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004622 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004623
Chris Lattner5ffc0662006-05-05 05:58:59 +00004624 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4625 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4626 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004627 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004628 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004629 N0.hasOneUse()) {
4630 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004631 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004632 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4633 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004634 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004635 LN0->isVolatile(), LN0->isNonTemporal(),
4636 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004637 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004638 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004639 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4640 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004641 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004642 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004644
Chris Lattner20a35c32007-04-11 05:32:27 +00004645 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004646 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4647 // Only do this before legalize for now.
4648 if (VT.isVector() && !LegalOperations) {
4649 EVT N0VT = N0.getOperand(0).getValueType();
4650 // We know that the # elements of the results is the same as the
4651 // # elements of the compare (and the # elements of the compare result
4652 // for that matter). Check to see that they are the same size. If so,
4653 // we know that the element size of the sext'd result matches the
4654 // element size of the compare operands.
4655 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004656 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004657 N0.getOperand(1),
4658 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004659 // If the desired elements are smaller or larger than the source
4660 // elements we can use a matching integer vector type and then
4661 // truncate/sign extend
4662 else {
Duncan Sands34727662010-07-12 08:16:59 +00004663 EVT MatchingElementType =
4664 EVT::getIntegerVT(*DAG.getContext(),
4665 N0VT.getScalarType().getSizeInBits());
4666 EVT MatchingVectorType =
4667 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4668 N0VT.getVectorNumElements());
4669 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004670 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004671 N0.getOperand(1),
4672 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4673 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00004674 }
4675 }
4676
4677 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004678 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004679 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004680 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00004681 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004682 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004683 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004684 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004685
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004686 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00004687}
4688
Chris Lattner2b4c2792007-10-13 06:35:54 +00004689/// GetDemandedBits - See if the specified operand can be simplified with the
4690/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00004691/// simpler operand, otherwise return a null SDValue.
4692SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004693 switch (V.getOpcode()) {
4694 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00004695 case ISD::Constant: {
4696 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
4697 assert(CV != 0 && "Const value should be ConstSDNode.");
4698 const APInt &CVal = CV->getAPIntValue();
4699 APInt NewVal = CVal & Mask;
4700 if (NewVal != CVal) {
4701 return DAG.getConstant(NewVal, V.getValueType());
4702 }
4703 break;
4704 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004705 case ISD::OR:
4706 case ISD::XOR:
4707 // If the LHS or RHS don't contribute bits to the or, drop them.
4708 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
4709 return V.getOperand(1);
4710 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
4711 return V.getOperand(0);
4712 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00004713 case ISD::SRL:
4714 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004715 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00004716 break;
4717 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
4718 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004719 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00004720
Dan Gohmancc91d632009-01-03 19:22:06 +00004721 // Watch out for shift count overflow though.
4722 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004723 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00004724 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00004725 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004726 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00004727 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00004728 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004729 }
Dan Gohman475871a2008-07-27 21:46:04 +00004730 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00004731}
4732
Evan Chengc88138f2007-03-22 01:54:19 +00004733/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
4734/// bits and then truncated to a narrower type and where N is a multiple
4735/// of number of bits of the narrower type, transform it to a narrower load
4736/// from address + N / num of bits of new type. If the result is to be
4737/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00004738SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00004739 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004740
Evan Chengc88138f2007-03-22 01:54:19 +00004741 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00004742 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004743 EVT VT = N->getValueType(0);
4744 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00004745
Dan Gohman7f8613e2008-08-14 20:04:46 +00004746 // This transformation isn't valid for vector loads.
4747 if (VT.isVector())
4748 return SDValue();
4749
Dan Gohmand1996362010-01-09 02:13:55 +00004750 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00004751 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00004752 if (Opc == ISD::SIGN_EXTEND_INREG) {
4753 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00004754 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004755 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00004756 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004757 ExtType = ISD::ZEXTLOAD;
4758 N0 = SDValue(N, 0);
4759 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4760 if (!N01) return SDValue();
4761 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
4762 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00004763 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00004764 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
4765 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00004766
Owen Andersone50ed302009-08-10 22:56:29 +00004767 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00004768
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004769 // Do not generate loads of non-round integer types since these can
4770 // be expensive (and would be wrong if the type is not byte sized).
4771 if (!ExtVT.isRound())
4772 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00004773
Evan Chengc88138f2007-03-22 01:54:19 +00004774 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004775 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00004776 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004777 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00004778 // Is the shift amount a multiple of size of VT?
4779 if ((ShAmt & (EVTBits-1)) == 0) {
4780 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00004781 // Is the load width a multiple of size of VT?
4782 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00004783 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00004784 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004785
Chris Lattnercbf68df2010-12-22 08:02:57 +00004786 // At this point, we must have a load or else we can't do the transform.
4787 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00004788
Chris Lattner2831a192010-10-01 05:36:09 +00004789 // If the shift amount is larger than the input type then we're not
4790 // accessing any of the loaded bytes. If the load was a zextload/extload
4791 // then the result of the shift+trunc is zero/undef (handled elsewhere).
4792 // If the load was a sextload then the result is a splat of the sign bit
4793 // of the extended byte. This is not worth optimizing for.
Chris Lattnercbf68df2010-12-22 08:02:57 +00004794 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00004795 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00004796 }
4797 }
4798
Dan Gohman394d6292010-11-03 01:47:46 +00004799 // If the load is shifted left (and the result isn't shifted back right),
4800 // we can fold the truncate through the shift.
4801 unsigned ShLeftAmt = 0;
4802 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00004803 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00004804 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4805 ShLeftAmt = N01->getZExtValue();
4806 N0 = N0.getOperand(0);
4807 }
4808 }
Owen Anderson95771af2011-02-25 21:41:48 +00004809
Chris Lattner4c32bc22010-12-22 07:36:50 +00004810 // If we haven't found a load, we can't narrow it. Don't transform one with
4811 // multiple uses, this would require adding a new load.
4812 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() ||
4813 // Don't change the width of a volatile load.
4814 cast<LoadSDNode>(N0)->isVolatile())
4815 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00004816
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004817 // Verify that we are actually reducing a load width here.
4818 if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00004819 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00004820
Chris Lattner4c32bc22010-12-22 07:36:50 +00004821 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
4822 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00004823
Chris Lattner4c32bc22010-12-22 07:36:50 +00004824 // For big endian targets, we need to adjust the offset to the pointer to
4825 // load the correct bytes.
4826 if (TLI.isBigEndian()) {
4827 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
4828 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
4829 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00004830 }
4831
Chris Lattner4c32bc22010-12-22 07:36:50 +00004832 uint64_t PtrOff = ShAmt / 8;
4833 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
4834 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
4835 PtrType, LN0->getBasePtr(),
4836 DAG.getConstant(PtrOff, PtrType));
4837 AddToWorkList(NewPtr.getNode());
4838
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004839 SDValue Load;
4840 if (ExtType == ISD::NON_EXTLOAD)
4841 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
4842 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004843 LN0->isVolatile(), LN0->isNonTemporal(),
4844 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004845 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00004846 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00004847 LN0->getPointerInfo().getWithOffset(PtrOff),
4848 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
4849 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00004850
4851 // Replace the old load's chain with the new load's chain.
4852 WorkListRemover DeadNodes(*this);
4853 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
4854 &DeadNodes);
4855
4856 // Shift the result left, if we've swallowed a left shift.
4857 SDValue Result = Load;
4858 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00004859 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00004860 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
4861 ShImmTy = VT;
4862 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
4863 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
4864 }
4865
4866 // Return the new loaded value.
4867 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00004868}
4869
Dan Gohman475871a2008-07-27 21:46:04 +00004870SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
4871 SDValue N0 = N->getOperand(0);
4872 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004873 EVT VT = N->getValueType(0);
4874 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00004875 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00004876 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Nate Begeman1d4d4142005-09-01 00:19:25 +00004878 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00004879 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00004880 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004881
Chris Lattner541a24f2006-05-06 22:43:44 +00004882 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00004883 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00004884 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004885
Nate Begeman646d7e22005-09-02 21:18:40 +00004886 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
4887 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00004888 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00004889 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
4890 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00004891 }
Chris Lattner4b37e872006-05-08 21:18:59 +00004892
Dan Gohman75dcf082008-07-31 00:50:31 +00004893 // fold (sext_in_reg (sext x)) -> (sext x)
4894 // fold (sext_in_reg (aext x)) -> (sext x)
4895 // if x is small enough.
4896 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
4897 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00004898 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
4899 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00004900 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00004901 }
4902
Chris Lattner95a5e052007-04-17 19:03:21 +00004903 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004904 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004905 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004906
Chris Lattner95a5e052007-04-17 19:03:21 +00004907 // fold operands of sext_in_reg based on knowledge that the top bits are not
4908 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004909 if (SimplifyDemandedBits(SDValue(N, 0)))
4910 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004911
Evan Chengc88138f2007-03-22 01:54:19 +00004912 // fold (sext_in_reg (load x)) -> (smaller sextload x)
4913 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00004914 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004915 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00004916 return NarrowLoad;
4917
Bill Wendling8509c902009-01-30 22:33:24 +00004918 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
4919 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00004920 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
4921 if (N0.getOpcode() == ISD::SRL) {
4922 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00004923 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00004924 // We can turn this into an SRA iff the input to the SRL is already sign
4925 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00004926 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00004927 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00004928 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
4929 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00004930 }
4931 }
Evan Chengc88138f2007-03-22 01:54:19 +00004932
Nate Begemanded49632005-10-13 03:11:28 +00004933 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00004934 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004935 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004936 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004937 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004938 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004939 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004940 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00004941 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004942 LN0->getBasePtr(), LN0->getPointerInfo(),
4943 EVT,
David Greene1e559442010-02-15 17:00:31 +00004944 LN0->isVolatile(), LN0->isNonTemporal(),
4945 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004946 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004947 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004948 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004949 }
4950 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00004951 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00004952 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004953 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004954 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004955 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004956 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004957 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00004958 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004959 LN0->getBasePtr(), LN0->getPointerInfo(),
4960 EVT,
David Greene1e559442010-02-15 17:00:31 +00004961 LN0->isVolatile(), LN0->isNonTemporal(),
4962 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00004963 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004964 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004965 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00004966 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00004967
4968 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
4969 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
4970 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
4971 N0.getOperand(1), false);
4972 if (BSwap.getNode() != 0)
4973 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
4974 BSwap, N1);
4975 }
4976
Dan Gohman475871a2008-07-27 21:46:04 +00004977 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004978}
4979
Dan Gohman475871a2008-07-27 21:46:04 +00004980SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
4981 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004982 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00004983 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004984
4985 // noop truncate
4986 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004987 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004988 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004989 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00004990 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004991 // fold (truncate (truncate x)) -> (truncate x)
4992 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00004993 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004994 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00004995 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
4996 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00004997 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004998 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00004999 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005000 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5001 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005002 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005003 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005004 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005005 else
5006 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00005007 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00005008 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005009 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005010
Nadav Rotemcc870a82012-02-05 11:39:23 +00005011 // Fold extract-and-trunc into a narrow extract. For example:
5012 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5013 // i32 y = TRUNCATE(i64 x)
5014 // -- becomes --
5015 // v16i8 b = BITCAST (v2i64 val)
5016 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5017 //
5018 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005019 // creates this pattern) and before operation legalization after which
5020 // we need to be more careful about the vector instructions that we generate.
5021 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5022 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5023
5024 EVT VecTy = N0.getOperand(0).getValueType();
5025 EVT ExTy = N0.getValueType();
5026 EVT TrTy = N->getValueType(0);
5027
5028 unsigned NumElem = VecTy.getVectorNumElements();
5029 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5030
5031 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5032 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5033
5034 SDValue EltNo = N0->getOperand(1);
5035 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5036 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5037
5038 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5039
5040 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5041 NVT, N0.getOperand(0));
5042
5043 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5044 N->getDebugLoc(), TrTy, V,
5045 DAG.getConstant(Index, MVT::i32));
5046 }
5047 }
5048
Chris Lattner2b4c2792007-10-13 06:35:54 +00005049 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005050 // only the low bits are being used.
5051 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005052 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005053 // may have different active low bits.
5054 if (!VT.isVector()) {
5055 SDValue Shorter =
5056 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5057 VT.getSizeInBits()));
5058 if (Shorter.getNode())
5059 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5060 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005061 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005062 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005063 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5064 SDValue Reduced = ReduceLoadWidth(N);
5065 if (Reduced.getNode())
5066 return Reduced;
5067 }
5068
5069 // Simplify the operands using demanded-bits information.
5070 if (!VT.isVector() &&
5071 SimplifyDemandedBits(SDValue(N, 0)))
5072 return SDValue(N, 0);
5073
Evan Chenge5b51ac2010-04-17 06:13:15 +00005074 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005075}
5076
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005077static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005079 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005080 return Elt.getNode();
5081 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005082}
5083
5084/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005085/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005086SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005087 assert(N->getOpcode() == ISD::BUILD_PAIR);
5088
Nate Begemanabc01992009-06-05 21:37:30 +00005089 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5090 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005091 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5092 LD1->getPointerInfo().getAddrSpace() !=
5093 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005094 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005095 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005096
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005097 if (ISD::isNON_EXTLoad(LD2) &&
5098 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005099 // If both are volatile this would reduce the number of volatile loads.
5100 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005101 !LD1->isVolatile() &&
5102 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005103 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005104 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00005105 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005106 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005107
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005108 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005109 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005110 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005111 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005112 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005113 }
Bill Wendling67a67682009-01-30 22:44:24 +00005114
Dan Gohman475871a2008-07-27 21:46:04 +00005115 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005116}
5117
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005118SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005119 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005120 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005121
Dan Gohman7f321562007-06-25 16:23:39 +00005122 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5123 // Only do this before legalize, since afterward the target may be depending
5124 // on the bitconvert.
5125 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005126 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005127 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005128 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005129 bool isSimple = true;
5130 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5131 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5132 N0.getOperand(i).getOpcode() != ISD::Constant &&
5133 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005134 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005135 break;
5136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005137
Owen Andersone50ed302009-08-10 22:56:29 +00005138 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005139 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005140 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005141 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005142 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005143 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005144
Dan Gohman3dd168d2008-09-05 01:58:21 +00005145 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005146 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005147 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005148 if (Res.getNode() != N) {
5149 if (!LegalOperations ||
5150 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5151 return Res;
5152
5153 // Folding it resulted in an illegal node, and it's too late to
5154 // do that. Clean up the old node and forego the transformation.
5155 // Ideally this won't happen very often, because instcombine
5156 // and the earlier dagcombine runs (where illegal nodes are
5157 // permitted) should have folded most of them already.
5158 DAG.DeleteNode(Res.getNode());
5159 }
Chris Lattner94683772005-12-23 05:30:37 +00005160 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005161
Bill Wendling67a67682009-01-30 22:44:24 +00005162 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005163 if (N0.getOpcode() == ISD::BITCAST)
5164 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005165 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005166
Chris Lattner57104102005-12-23 05:44:41 +00005167 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005168 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005169 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005170 // Do not change the width of a volatile load.
5171 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005172 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005173 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00005174 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005175 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005176 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005177
Evan Cheng59d5b682007-05-07 21:27:48 +00005178 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005179 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005180 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005181 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005182 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005183 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005184 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005185 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005186 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005187 Load.getValue(1));
5188 return Load;
5189 }
Chris Lattner57104102005-12-23 05:44:41 +00005190 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005191
Bill Wendling67a67682009-01-30 22:44:24 +00005192 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5193 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005194 // This often reduces constant pool loads.
5195 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005196 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005197 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005198 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005199 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005200
Duncan Sands83ec4b62008-06-06 12:08:01 +00005201 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005202 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005203 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5204 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005205 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005206 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5207 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005208 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005209
Bill Wendling67a67682009-01-30 22:44:24 +00005210 // fold (bitconvert (fcopysign cst, x)) ->
5211 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5212 // Note that we don't handle (copysign x, cst) because this can always be
5213 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005214 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005215 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005216 VT.isInteger() && !VT.isVector()) {
5217 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005218 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005219 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005220 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005221 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005222 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005223
Duncan Sands25cf2272008-11-24 14:53:14 +00005224 // If X has a different width than the result/lhs, sext it or truncate it.
5225 unsigned VTWidth = VT.getSizeInBits();
5226 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005227 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005228 AddToWorkList(X.getNode());
5229 } else if (OrigXWidth > VTWidth) {
5230 // To get the sign bit in the right place, we have to shift it right
5231 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005232 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005233 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005234 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5235 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005236 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005237 AddToWorkList(X.getNode());
5238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005239
Duncan Sands25cf2272008-11-24 14:53:14 +00005240 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005241 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005242 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005243 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005244
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005245 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005246 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005247 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005248 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005249 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005250
Bill Wendling67a67682009-01-30 22:44:24 +00005251 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005252 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005253 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005254
Scott Michelfdc40a02009-02-17 22:15:04 +00005255 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005256 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005257 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5258 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005259 return CombineLD;
5260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005261
Dan Gohman475871a2008-07-27 21:46:04 +00005262 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005263}
5264
Dan Gohman475871a2008-07-27 21:46:04 +00005265SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005266 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005267 return CombineConsecutiveLoads(N, VT);
5268}
5269
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005270/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005271/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005272/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005273SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005274ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005275 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005276
Chris Lattner6258fb22006-04-02 02:53:43 +00005277 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005278 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005279
Duncan Sands83ec4b62008-06-06 12:08:01 +00005280 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5281 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Chris Lattner6258fb22006-04-02 02:53:43 +00005283 // If this is a conversion of N elements of one type to N elements of another
5284 // type, convert each element. This handles FP<->INT cases.
5285 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005286 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5287 BV->getValueType(0).getVectorNumElements());
5288
5289 // Due to the FP element handling below calling this routine recursively,
5290 // we can end up with a scalar-to-vector node here.
5291 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005292 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5293 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005294 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005295
Dan Gohman475871a2008-07-27 21:46:04 +00005296 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005297 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005298 SDValue Op = BV->getOperand(i);
5299 // If the vector element type is not legal, the BUILD_VECTOR operands
5300 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005301 if (Op.getValueType() != SrcEltVT)
5302 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005303 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005304 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005305 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005306 }
Evan Chenga87008d2009-02-25 22:49:59 +00005307 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5308 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005310
Chris Lattner6258fb22006-04-02 02:53:43 +00005311 // Otherwise, we're growing or shrinking the elements. To avoid having to
5312 // handle annoying details of growing/shrinking FP values, we convert them to
5313 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005314 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005315 // Convert the input float vector to a int vector where the elements are the
5316 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005317 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005318 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005319 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005320 SrcEltVT = IntVT;
5321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005322
Chris Lattner6258fb22006-04-02 02:53:43 +00005323 // Now we know the input is an integer vector. If the output is a FP type,
5324 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005325 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005326 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005327 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005328 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005329
Chris Lattner6258fb22006-04-02 02:53:43 +00005330 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005331 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005332 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Chris Lattner6258fb22006-04-02 02:53:43 +00005334 // Okay, we know the src/dst types are both integers of differing types.
5335 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005336 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005337 if (SrcBitSize < DstBitSize) {
5338 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005339
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005341 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005342 i += NumInputsPerOutput) {
5343 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005344 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005345 bool EltIsUndef = true;
5346 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5347 // Shift the previously computed bits over.
5348 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005350 if (Op.getOpcode() == ISD::UNDEF) continue;
5351 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005352
Jay Foad40f8f622010-12-07 08:25:19 +00005353 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005354 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005355 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005356
Chris Lattner6258fb22006-04-02 02:53:43 +00005357 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005358 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005359 else
5360 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5361 }
5362
Owen Anderson23b9b192009-08-12 00:36:31 +00005363 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005364 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5365 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005367
Chris Lattner6258fb22006-04-02 02:53:43 +00005368 // Finally, this must be the case where we are shrinking elements: each input
5369 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005370 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005371 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005372 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5373 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005375
Dan Gohman7f321562007-06-25 16:23:39 +00005376 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005377 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5378 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005379 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005380 continue;
5381 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005382
Jay Foad40f8f622010-12-07 08:25:19 +00005383 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5384 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005385
Chris Lattner6258fb22006-04-02 02:53:43 +00005386 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005387 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005388 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005389 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005390 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005391 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5392 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005393 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005394 }
5395
5396 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005397 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005398 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5399 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005400
Evan Chenga87008d2009-02-25 22:49:59 +00005401 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5402 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005403}
5404
Dan Gohman475871a2008-07-27 21:46:04 +00005405SDValue DAGCombiner::visitFADD(SDNode *N) {
5406 SDValue N0 = N->getOperand(0);
5407 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005408 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5409 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005410 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005411
Dan Gohman7f321562007-06-25 16:23:39 +00005412 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005413 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005417
Bill Wendlingb0162f52009-01-30 22:53:48 +00005418 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005420 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005421 // canonicalize constant to RHS
5422 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005423 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5424 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005425 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5426 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005427 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005428 // fold (fadd A, (fneg B)) -> (fsub A, B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005429 if (isNegatibleForFree(N1, LegalOperations, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005430 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005431 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005432 // fold (fadd (fneg A), B) -> (fsub B, A)
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005433 if (isNegatibleForFree(N0, LegalOperations, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005434 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005435 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005436
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005437 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005438 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5439 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5440 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005441 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005442 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5443 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005444
Dan Gohman475871a2008-07-27 21:46:04 +00005445 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005446}
5447
Dan Gohman475871a2008-07-27 21:46:04 +00005448SDValue DAGCombiner::visitFSUB(SDNode *N) {
5449 SDValue N0 = N->getOperand(0);
5450 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005451 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5452 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005453 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005454
Dan Gohman7f321562007-06-25 16:23:39 +00005455 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005456 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005457 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005458 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005460
Nate Begemana0e221d2005-10-18 00:28:13 +00005461 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005462 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005463 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005464 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005465 if (DAG.getTarget().Options.UnsafeFPMath &&
5466 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00005467 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005468 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005469 if (DAG.getTarget().Options.UnsafeFPMath &&
5470 N0CFP && N0CFP->getValueAPF().isZero()) {
5471 if (isNegatibleForFree(N1, LegalOperations, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005472 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00005473 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005474 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00005475 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005476 // fold (fsub A, (fneg B)) -> (fadd A, B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005477 if (isNegatibleForFree(N1, LegalOperations, &DAG.getTarget().Options))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005478 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005479 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005480
Dan Gohman475871a2008-07-27 21:46:04 +00005481 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005482}
5483
Dan Gohman475871a2008-07-27 21:46:04 +00005484SDValue DAGCombiner::visitFMUL(SDNode *N) {
5485 SDValue N0 = N->getOperand(0);
5486 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005487 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5488 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005489 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00005490
Dan Gohman7f321562007-06-25 16:23:39 +00005491 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005492 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005493 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005494 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005496
Nate Begeman11af4ea2005-10-17 20:40:11 +00005497 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005499 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005500 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00005501 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005502 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
5503 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005504 if (DAG.getTarget().Options.UnsafeFPMath &&
5505 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005506 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00005507 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005508 if (DAG.getTarget().Options.UnsafeFPMath &&
5509 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00005510 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00005511 // fold (fmul X, 2.0) -> (fadd X, X)
5512 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005513 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00005514 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00005515 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00005516 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005517 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005518
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005519 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005520 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations,
5521 &DAG.getTarget().Options)) {
5522 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations,
5523 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005524 // Both can be negated for free, check to see if at least one is cheaper
5525 // negated.
5526 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005527 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005528 GetNegatedExpression(N0, DAG, LegalOperations),
5529 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005530 }
5531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005532
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005533 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005534 if (DAG.getTarget().Options.UnsafeFPMath &&
5535 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005536 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005537 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005538 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00005539 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005540
Dan Gohman475871a2008-07-27 21:46:04 +00005541 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005542}
5543
Dan Gohman475871a2008-07-27 21:46:04 +00005544SDValue DAGCombiner::visitFDIV(SDNode *N) {
5545 SDValue N0 = N->getOperand(0);
5546 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00005547 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5548 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005549 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00005550
Dan Gohman7f321562007-06-25 16:23:39 +00005551 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005552 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005553 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005554 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005555 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005556
Nate Begemana148d982006-01-18 22:35:16 +00005557 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005559 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005560
5561
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005562 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005563 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations,
5564 &DAG.getTarget().Options)) {
5565 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations,
5566 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005567 // Both can be negated for free, check to see if at least one is cheaper
5568 // negated.
5569 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00005570 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005571 GetNegatedExpression(N0, DAG, LegalOperations),
5572 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005573 }
5574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005575
Dan Gohman475871a2008-07-27 21:46:04 +00005576 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005577}
5578
Dan Gohman475871a2008-07-27 21:46:04 +00005579SDValue DAGCombiner::visitFREM(SDNode *N) {
5580 SDValue N0 = N->getOperand(0);
5581 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00005582 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5583 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005584 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00005585
Nate Begemana148d982006-01-18 22:35:16 +00005586 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00005587 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005588 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00005589
Dan Gohman475871a2008-07-27 21:46:04 +00005590 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005591}
5592
Dan Gohman475871a2008-07-27 21:46:04 +00005593SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
5594 SDValue N0 = N->getOperand(0);
5595 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00005596 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5597 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005598 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00005599
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005601 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005602
Chris Lattner12d83032006-03-05 05:30:57 +00005603 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00005604 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00005605 // copysign(x, c1) -> fabs(x) iff ispos(c1)
5606 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00005607 if (!V.isNegative()) {
5608 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005609 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00005610 } else {
5611 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005612 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00005613 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00005614 }
Chris Lattner12d83032006-03-05 05:30:57 +00005615 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005616
Chris Lattner12d83032006-03-05 05:30:57 +00005617 // copysign(fabs(x), y) -> copysign(x, y)
5618 // copysign(fneg(x), y) -> copysign(x, y)
5619 // copysign(copysign(x,z), y) -> copysign(x, y)
5620 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
5621 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005622 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
5623 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00005624
5625 // copysign(x, abs(y)) -> abs(x)
5626 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005627 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005628
Chris Lattner12d83032006-03-05 05:30:57 +00005629 // copysign(x, copysign(y,z)) -> copysign(x, z)
5630 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005631 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
5632 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005633
Chris Lattner12d83032006-03-05 05:30:57 +00005634 // copysign(x, fp_extend(y)) -> copysign(x, y)
5635 // copysign(x, fp_round(y)) -> copysign(x, y)
5636 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005637 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
5638 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005639
Dan Gohman475871a2008-07-27 21:46:04 +00005640 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00005641}
5642
Dan Gohman475871a2008-07-27 21:46:04 +00005643SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
5644 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00005645 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005646 EVT VT = N->getValueType(0);
5647 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00005648
Nate Begeman1d4d4142005-09-01 00:19:25 +00005649 // fold (sint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00005650 if (N0C && OpVT != MVT::ppcf128 &&
5651 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00005652 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00005653 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005654 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005655
Chris Lattnercda88752008-06-26 00:16:49 +00005656 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
5657 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005658 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
5659 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005660 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00005661 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005662 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00005663 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00005664
Dan Gohman475871a2008-07-27 21:46:04 +00005665 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005666}
5667
Dan Gohman475871a2008-07-27 21:46:04 +00005668SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
5669 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00005670 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005671 EVT VT = N->getValueType(0);
5672 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00005673
Nate Begeman1d4d4142005-09-01 00:19:25 +00005674 // fold (uint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00005675 if (N0C && OpVT != MVT::ppcf128 &&
5676 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00005677 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00005678 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005679 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005680
Chris Lattnercda88752008-06-26 00:16:49 +00005681 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
5682 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005683 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
5684 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005685 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00005686 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005687 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00005688 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005689
Dan Gohman475871a2008-07-27 21:46:04 +00005690 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005691}
5692
Dan Gohman475871a2008-07-27 21:46:04 +00005693SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
5694 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00005695 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005696 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005697
Nate Begeman1d4d4142005-09-01 00:19:25 +00005698 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00005699 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005700 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
5701
Dan Gohman475871a2008-07-27 21:46:04 +00005702 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005703}
5704
Dan Gohman475871a2008-07-27 21:46:04 +00005705SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
5706 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00005707 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005708 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005709
Nate Begeman1d4d4142005-09-01 00:19:25 +00005710 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00005711 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005712 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
5713
Dan Gohman475871a2008-07-27 21:46:04 +00005714 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005715}
5716
Dan Gohman475871a2008-07-27 21:46:04 +00005717SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
5718 SDValue N0 = N->getOperand(0);
5719 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00005720 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005721 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005722
Nate Begeman1d4d4142005-09-01 00:19:25 +00005723 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005725 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005726
Chris Lattner79dbea52006-03-13 06:26:26 +00005727 // fold (fp_round (fp_extend x)) -> x
5728 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
5729 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005730
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00005731 // fold (fp_round (fp_round x)) -> (fp_round x)
5732 if (N0.getOpcode() == ISD::FP_ROUND) {
5733 // This is a value preserving truncation if both round's are.
5734 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005735 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00005736 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00005737 DAG.getIntPtrConstant(IsTrunc));
5738 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005739
Chris Lattner79dbea52006-03-13 06:26:26 +00005740 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00005741 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00005742 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
5743 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00005744 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00005745 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
5746 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00005747 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005748
Dan Gohman475871a2008-07-27 21:46:04 +00005749 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005750}
5751
Dan Gohman475871a2008-07-27 21:46:04 +00005752SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
5753 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005754 EVT VT = N->getValueType(0);
5755 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00005756 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005757
Nate Begeman1d4d4142005-09-01 00:19:25 +00005758 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00005759 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005760 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00005761 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005762 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00005763
Dan Gohman475871a2008-07-27 21:46:04 +00005764 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005765}
5766
Dan Gohman475871a2008-07-27 21:46:04 +00005767SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
5768 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00005769 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005770 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005771
Chris Lattner5938bef2007-12-29 06:55:23 +00005772 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00005773 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00005774 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00005775 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005776
Nate Begeman1d4d4142005-09-01 00:19:25 +00005777 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00005779 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00005780
5781 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
5782 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00005783 if (N0.getOpcode() == ISD::FP_ROUND
5784 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00005785 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00005786 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005787 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00005788 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
5789 In, N0.getOperand(1));
5790 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00005791 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005792
Chris Lattner0bd48932008-01-17 07:00:52 +00005793 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00005794 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005795 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005796 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005797 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005798 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00005799 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005800 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00005801 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005802 LN0->isVolatile(), LN0->isNonTemporal(),
5803 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00005804 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00005805 CombineTo(N0.getNode(),
5806 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
5807 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00005808 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005809 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00005810 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005811
Dan Gohman475871a2008-07-27 21:46:04 +00005812 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005813}
5814
Dan Gohman475871a2008-07-27 21:46:04 +00005815SDValue DAGCombiner::visitFNEG(SDNode *N) {
5816 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00005817 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00005818
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005819 if (isNegatibleForFree(N0, LegalOperations, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005820 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00005821
Chris Lattner3bd39d42008-01-27 17:42:27 +00005822 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
5823 // constant pool values.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005824 if (N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00005825 !VT.isVector() &&
5826 N0.getNode()->hasOneUse() &&
5827 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005828 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005829 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005830 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005831 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
5832 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00005833 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00005835 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00005836 }
5837 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005838
Dan Gohman475871a2008-07-27 21:46:04 +00005839 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005840}
5841
Dan Gohman475871a2008-07-27 21:46:04 +00005842SDValue DAGCombiner::visitFABS(SDNode *N) {
5843 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00005844 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00005845 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005846
Nate Begeman1d4d4142005-09-01 00:19:25 +00005847 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00005849 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005850 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00005851 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00005852 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005853 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00005854 // fold (fabs (fcopysign x, y)) -> (fabs x)
5855 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00005856 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005857
Chris Lattner3bd39d42008-01-27 17:42:27 +00005858 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
5859 // constant pool values.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005860 if (N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005861 N0.getOperand(0).getValueType().isInteger() &&
5862 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005863 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005864 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005865 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005866 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005867 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00005868 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005869 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00005870 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00005871 }
5872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005873
Dan Gohman475871a2008-07-27 21:46:04 +00005874 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005875}
5876
Dan Gohman475871a2008-07-27 21:46:04 +00005877SDValue DAGCombiner::visitBRCOND(SDNode *N) {
5878 SDValue Chain = N->getOperand(0);
5879 SDValue N1 = N->getOperand(1);
5880 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005881
Dan Gohmane0f06c72009-11-17 00:47:23 +00005882 // If N is a constant we could fold this into a fallthrough or unconditional
5883 // branch. However that doesn't happen very often in normal code, because
5884 // Instcombine/SimplifyCFG should have handled the available opportunities.
5885 // If we did this folding here, it would be necessary to update the
5886 // MachineBasicBlock CFG, which is awkward.
5887
Nate Begeman750ac1b2006-02-01 07:19:44 +00005888 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
5889 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005890 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
5892 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00005893 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00005894 N1.getOperand(0), N1.getOperand(1), N2);
5895 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00005896
Evan Cheng2a135ae2010-10-04 22:41:01 +00005897 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
5898 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
5899 (N1.getOperand(0).hasOneUse() &&
5900 N1.getOperand(0).getOpcode() == ISD::SRL))) {
5901 SDNode *Trunc = 0;
5902 if (N1.getOpcode() == ISD::TRUNCATE) {
5903 // Look pass the truncate.
5904 Trunc = N1.getNode();
5905 N1 = N1.getOperand(0);
5906 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005907
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005908 // Match this pattern so that we can generate simpler code:
5909 //
5910 // %a = ...
5911 // %b = and i32 %a, 2
5912 // %c = srl i32 %b, 1
5913 // brcond i32 %c ...
5914 //
5915 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005917 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00005918 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005919 // %c = setcc eq %b, 0
5920 // brcond %c ...
5921 //
5922 // This applies only when the AND constant value has one bit set and the
5923 // SRL constant is equal to the log2 of the AND constant. The back-end is
5924 // smart enough to convert the result into a TEST/JMP sequence.
5925 SDValue Op0 = N1.getOperand(0);
5926 SDValue Op1 = N1.getOperand(1);
5927
5928 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005929 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005930 SDValue AndOp1 = Op0.getOperand(1);
5931
5932 if (AndOp1.getOpcode() == ISD::Constant) {
5933 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
5934
5935 if (AndConst.isPowerOf2() &&
5936 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
5937 SDValue SetCC =
5938 DAG.getSetCC(N->getDebugLoc(),
5939 TLI.getSetCCResultType(Op0.getValueType()),
5940 Op0, DAG.getConstant(0, Op0.getValueType()),
5941 ISD::SETNE);
5942
Evan Chengd40d03e2010-01-06 19:38:29 +00005943 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
5944 MVT::Other, Chain, SetCC, N2);
5945 // Don't add the new BRCond into the worklist or else SimplifySelectCC
5946 // will convert it back to (X & C1) >> C2.
5947 CombineTo(N, NewBRCond, false);
5948 // Truncate is dead.
5949 if (Trunc) {
5950 removeFromWorkList(Trunc);
5951 DAG.DeleteNode(Trunc);
5952 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005953 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00005954 WorkListRemover DeadNodes(*this);
5955 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005956 removeFromWorkList(N1.getNode());
5957 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00005958 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005959 }
5960 }
5961 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00005962
5963 if (Trunc)
5964 // Restore N1 if the above transformation doesn't match.
5965 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00005966 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005967
Evan Cheng2c755ba2010-02-27 07:36:59 +00005968 // Transform br(xor(x, y)) -> br(x != y)
5969 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
5970 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
5971 SDNode *TheXor = N1.getNode();
5972 SDValue Op0 = TheXor->getOperand(0);
5973 SDValue Op1 = TheXor->getOperand(1);
5974 if (Op0.getOpcode() == Op1.getOpcode()) {
5975 // Avoid missing important xor optimizations.
5976 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00005977 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005978 DEBUG(dbgs() << "\nReplacing.8 ";
5979 TheXor->dump(&DAG);
5980 dbgs() << "\nWith: ";
5981 Tmp.getNode()->dump(&DAG);
5982 dbgs() << '\n');
5983 WorkListRemover DeadNodes(*this);
5984 DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes);
5985 removeFromWorkList(TheXor);
5986 DAG.DeleteNode(TheXor);
5987 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
5988 MVT::Other, Chain, Tmp, N2);
5989 }
5990 }
5991
5992 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
5993 bool Equal = false;
5994 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
5995 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
5996 Op0.getOpcode() == ISD::XOR) {
5997 TheXor = Op0.getNode();
5998 Equal = true;
5999 }
6000
Evan Cheng2a135ae2010-10-04 22:41:01 +00006001 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006002 if (LegalTypes)
6003 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6004 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6005 SetCCVT,
6006 Op0, Op1,
6007 Equal ? ISD::SETEQ : ISD::SETNE);
6008 // Replace the uses of XOR with SETCC
6009 WorkListRemover DeadNodes(*this);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006010 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
6011 removeFromWorkList(N1.getNode());
6012 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006013 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6014 MVT::Other, Chain, SetCC, N2);
6015 }
6016 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006017
Dan Gohman475871a2008-07-27 21:46:04 +00006018 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006019}
6020
Chris Lattner3ea0b472005-10-05 06:47:48 +00006021// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6022//
Dan Gohman475871a2008-07-27 21:46:04 +00006023SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006024 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006025 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006026
Dan Gohmane0f06c72009-11-17 00:47:23 +00006027 // If N is a constant we could fold this into a fallthrough or unconditional
6028 // branch. However that doesn't happen very often in normal code, because
6029 // Instcombine/SimplifyCFG should have handled the available opportunities.
6030 // If we did this folding here, it would be necessary to update the
6031 // MachineBasicBlock CFG, which is awkward.
6032
Duncan Sands8eab8a22008-06-09 11:32:28 +00006033 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006034 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006035 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6036 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006037 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006038
Nate Begemane17daeb2005-10-05 21:43:42 +00006039 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006040 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006041 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006042 N->getOperand(0), Simp.getOperand(2),
6043 Simp.getOperand(0), Simp.getOperand(1),
6044 N->getOperand(4));
6045
Dan Gohman475871a2008-07-27 21:46:04 +00006046 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006047}
6048
Evan Chengc4b527a2012-01-13 01:37:24 +00006049/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6050/// uses N as its base pointer and that N may be folded in the load / store
6051/// addressing mode. FIXME: This currently only looks for folding of
6052/// [reg +/- imm] addressing modes.
6053static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6054 SelectionDAG &DAG,
6055 const TargetLowering &TLI) {
6056 EVT VT;
6057 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6058 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6059 return false;
6060 VT = Use->getValueType(0);
6061 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6062 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6063 return false;
6064 VT = ST->getValue().getValueType();
6065 } else
6066 return false;
6067
6068 TargetLowering::AddrMode AM;
6069 if (N->getOpcode() == ISD::ADD) {
6070 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6071 if (Offset)
6072 AM.BaseOffs = Offset->getSExtValue();
6073 else
6074 return false;
6075 } else if (N->getOpcode() == ISD::SUB) {
6076 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6077 if (Offset)
6078 AM.BaseOffs = -Offset->getSExtValue();
6079 else
6080 return false;
6081 } else
6082 return false;
6083
6084 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6085}
6086
Duncan Sandsec87aa82008-06-15 20:12:31 +00006087/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6088/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006089/// and it has other uses besides the load / store. After the
6090/// transformation, the new indexed load / store has effectively folded
6091/// the add / subtract in and all of its other uses are redirected to the
6092/// new load / store.
6093bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006094 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006095 return false;
6096
6097 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006099 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006100 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006101 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006102 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006103 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006104 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006105 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6106 return false;
6107 Ptr = LD->getBasePtr();
6108 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006109 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006110 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006111 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006112 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
6113 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
6114 return false;
6115 Ptr = ST->getBasePtr();
6116 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006117 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006118 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006119 }
Chris Lattner448f2192006-11-11 00:39:41 +00006120
Chris Lattner9f1794e2006-11-11 00:56:29 +00006121 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
6122 // out. There is no reason to make this a preinc/predec.
6123 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00006124 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006125 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006126
Chris Lattner9f1794e2006-11-11 00:56:29 +00006127 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00006128 SDValue BasePtr;
6129 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006130 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6131 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
6132 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00006133 // Don't create a indexed load / store with zero offset.
6134 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006135 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006136 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006137
Chris Lattner41e53fd2006-11-11 01:00:15 +00006138 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00006139 // 1) The new base ptr is a frame index.
6140 // 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 +00006141 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00006142 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00006143 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00006144 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00006145
Chris Lattner41e53fd2006-11-11 01:00:15 +00006146 // Check #1. Preinc'ing a frame index would require copying the stack pointer
6147 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00006148 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00006149 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006150
Chris Lattner41e53fd2006-11-11 01:00:15 +00006151 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006152 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006153 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006154 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006155 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006156 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006157
Evan Chengc843abe2007-05-24 02:35:39 +00006158 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006159 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00006160
6161 // Caches for hasPredecessorHelper
6162 SmallPtrSet<const SDNode *, 32> Visited;
6163 SmallVector<const SDNode *, 16> Worklist;
6164
Gabor Greifba36cb52008-08-28 21:40:38 +00006165 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6166 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006167 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006168 if (Use == N)
6169 continue;
Lang Hames944520f2011-07-07 04:31:51 +00006170 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006171 return false;
6172
Evan Chengc4b527a2012-01-13 01:37:24 +00006173 // If Ptr may be folded in addressing mode of other use, then it's
6174 // not profitable to do this transformation.
6175 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006176 RealUse = true;
6177 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006178
Chris Lattner9f1794e2006-11-11 00:56:29 +00006179 if (!RealUse)
6180 return false;
6181
Dan Gohman475871a2008-07-27 21:46:04 +00006182 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006183 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006184 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6185 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006186 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00006187 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6188 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006189 ++PreIndexedNodes;
6190 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006191 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006192 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006193 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006194 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006195 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006196 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006197 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006198 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006199 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00006200 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006201 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006202 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00006203 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006204 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006205 }
6206
Chris Lattner9f1794e2006-11-11 00:56:29 +00006207 // Finally, since the node is now dead, remove it from the graph.
6208 DAG.DeleteNode(N);
6209
6210 // Replace the uses of Ptr with uses of the updated base value.
6211 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006212 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00006213 removeFromWorkList(Ptr.getNode());
6214 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00006215
6216 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006217}
6218
Duncan Sandsec87aa82008-06-15 20:12:31 +00006219/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00006220/// add / sub of the base pointer node into a post-indexed load / store.
6221/// The transformation folded the add / subtract into the new indexed
6222/// load / store effectively and all of its uses are redirected to the
6223/// new load / store.
6224bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006225 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006226 return false;
6227
6228 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006229 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006230 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006231 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006232 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006233 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006234 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006235 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
6236 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
6237 return false;
6238 Ptr = LD->getBasePtr();
6239 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006240 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006241 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006242 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006243 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
6244 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
6245 return false;
6246 Ptr = ST->getBasePtr();
6247 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006248 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006249 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006250 }
Chris Lattner448f2192006-11-11 00:39:41 +00006251
Gabor Greifba36cb52008-08-28 21:40:38 +00006252 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006253 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006254
Gabor Greifba36cb52008-08-28 21:40:38 +00006255 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6256 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006257 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006258 if (Op == N ||
6259 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
6260 continue;
6261
Dan Gohman475871a2008-07-27 21:46:04 +00006262 SDValue BasePtr;
6263 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006264 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6265 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00006266 // Don't create a indexed load / store with zero offset.
6267 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006268 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006269 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006270
Chris Lattner9f1794e2006-11-11 00:56:29 +00006271 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00006272 // 1) All uses are load / store ops that use it as base ptr (and
6273 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00006274 // 2) Op must be independent of N, i.e. Op is neither a predecessor
6275 // nor a successor of N. Otherwise, if Op is folded that would
6276 // create a cycle.
6277
Evan Chengcaab1292009-05-06 18:25:01 +00006278 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
6279 continue;
6280
Chris Lattner9f1794e2006-11-11 00:56:29 +00006281 // Check for #1.
6282 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006283 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
6284 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00006285 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00006286 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00006287 continue;
6288
Chris Lattner9f1794e2006-11-11 00:56:29 +00006289 // If all the uses are load / store addresses, then don't do the
6290 // transformation.
6291 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
6292 bool RealUse = false;
6293 for (SDNode::use_iterator III = Use->use_begin(),
6294 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00006295 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00006296 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006297 RealUse = true;
6298 }
Chris Lattner448f2192006-11-11 00:39:41 +00006299
Chris Lattner9f1794e2006-11-11 00:56:29 +00006300 if (!RealUse) {
6301 TryNext = true;
6302 break;
Chris Lattner448f2192006-11-11 00:39:41 +00006303 }
6304 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006305 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006306
Chris Lattner9f1794e2006-11-11 00:56:29 +00006307 if (TryNext)
6308 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006309
Chris Lattner9f1794e2006-11-11 00:56:29 +00006310 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00006311 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006312 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00006313 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6314 BasePtr, Offset, AM)
6315 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6316 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006317 ++PostIndexedNodes;
6318 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006319 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006320 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006321 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006322 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006323 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006324 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006325 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006326 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006327 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00006328 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006329 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006330 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00006331 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006332 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00006333 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006334
Chris Lattner9f1794e2006-11-11 00:56:29 +00006335 // Finally, since the node is now dead, remove it from the graph.
6336 DAG.DeleteNode(N);
6337
6338 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00006339 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00006340 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006341 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006342 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006343 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006344 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006345 }
6346 }
6347 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006348
Chris Lattner448f2192006-11-11 00:39:41 +00006349 return false;
6350}
6351
Dan Gohman475871a2008-07-27 21:46:04 +00006352SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00006353 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue Chain = LD->getChain();
6355 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00006356
Evan Cheng45a7ca92007-05-01 00:38:21 +00006357 // If load is not volatile and there are no uses of the loaded value (and
6358 // the updated indexed value in case of indexed loads), change uses of the
6359 // chain value into uses of the chain input (i.e. delete the dead load).
6360 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006361 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00006362 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00006363 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00006364 // It's not safe to use the two value CombineTo variant here. e.g.
6365 // v1, chain2 = load chain1, loc
6366 // v2, chain3 = load chain2, loc
6367 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00006368 // Now we replace use of chain2 with chain1. This makes the second load
6369 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00006370 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006371 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006372 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006373 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006374 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006375 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00006376 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendlingc0debad2009-01-30 23:27:35 +00006377
Chris Lattner125991a2008-01-24 07:57:06 +00006378 if (N->use_empty()) {
6379 removeFromWorkList(N);
6380 DAG.DeleteNode(N);
6381 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006382
Dan Gohman475871a2008-07-27 21:46:04 +00006383 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00006384 }
Evan Cheng498f5592007-05-01 08:53:39 +00006385 } else {
6386 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00006387 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00006388 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00006389 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00006390 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006391 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006392 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006393 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006394 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006395 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00006396 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
6397 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesene8d72302009-02-06 23:05:02 +00006398 DAG.getUNDEF(N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006399 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00006400 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00006401 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00006402 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00006403 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00006404 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00006405 }
6406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006407
Chris Lattner01a22022005-10-10 22:04:48 +00006408 // If this load is directly stored, replace the load value with the stored
6409 // value.
6410 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006411 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00006412 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006413 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00006414 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
6415 if (PrevST->getBasePtr() == Ptr &&
6416 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006417 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00006418 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006419 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006420
Evan Cheng255f20f2010-04-01 06:04:33 +00006421 // Try to infer better alignment information than the load already has.
6422 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00006423 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
6424 if (Align > LD->getAlignment())
6425 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
6426 LD->getValueType(0),
6427 Chain, Ptr, LD->getPointerInfo(),
6428 LD->getMemoryVT(),
6429 LD->isVolatile(), LD->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00006430 }
6431 }
6432
Jim Laskey7ca56af2006-10-11 13:47:09 +00006433 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00006434 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006435 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00006436
Jim Laskey6ff23e52006-10-04 16:53:27 +00006437 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00006438 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00006439 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006440
Jim Laskey279f0532006-09-25 16:29:54 +00006441 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006442 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00006443 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00006444 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00006445 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006446 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006447 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00006448 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
6449 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00006450 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006451 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006452 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006453 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00006454 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00006455 }
Jim Laskey279f0532006-09-25 16:29:54 +00006456
Jim Laskey6ff23e52006-10-04 16:53:27 +00006457 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00006458 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006459 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006460
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006461 // Make sure the new and old chains are cleaned up.
6462 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006463
Jim Laskey274062c2006-10-13 23:32:28 +00006464 // Replace uses with load result and token factor. Don't add users
6465 // to work list.
6466 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00006467 }
6468 }
6469
Evan Cheng7fc033a2006-11-03 03:06:21 +00006470 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00006471 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00006472 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00006473
Dan Gohman475871a2008-07-27 21:46:04 +00006474 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00006475}
6476
Chris Lattner2392ae72010-04-15 04:48:01 +00006477/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
6478/// load is having specific bytes cleared out. If so, return the byte size
6479/// being masked out and the shift amount.
6480static std::pair<unsigned, unsigned>
6481CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
6482 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006483
Chris Lattner2392ae72010-04-15 04:48:01 +00006484 // Check for the structure we're looking for.
6485 if (V->getOpcode() != ISD::AND ||
6486 !isa<ConstantSDNode>(V->getOperand(1)) ||
6487 !ISD::isNormalLoad(V->getOperand(0).getNode()))
6488 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006489
Chris Lattnere6987582010-04-15 06:10:49 +00006490 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00006491 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00006492 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006493
Chris Lattnere6987582010-04-15 06:10:49 +00006494 // The store should be chained directly to the load or be an operand of a
6495 // tokenfactor.
6496 if (LD == Chain.getNode())
6497 ; // ok.
6498 else if (Chain->getOpcode() != ISD::TokenFactor)
6499 return Result; // Fail.
6500 else {
6501 bool isOk = false;
6502 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
6503 if (Chain->getOperand(i).getNode() == LD) {
6504 isOk = true;
6505 break;
6506 }
6507 if (!isOk) return Result;
6508 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006509
Chris Lattner2392ae72010-04-15 04:48:01 +00006510 // This only handles simple types.
6511 if (V.getValueType() != MVT::i16 &&
6512 V.getValueType() != MVT::i32 &&
6513 V.getValueType() != MVT::i64)
6514 return Result;
6515
6516 // Check the constant mask. Invert it so that the bits being masked out are
6517 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
6518 // follow the sign bit for uniformity.
6519 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
6520 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
6521 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
6522 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
6523 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
6524 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006525
Chris Lattner2392ae72010-04-15 04:48:01 +00006526 // See if we have a continuous run of bits. If so, we have 0*1+0*
6527 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
6528 return Result;
6529
6530 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
6531 if (V.getValueType() != MVT::i64 && NotMaskLZ)
6532 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006533
Chris Lattner2392ae72010-04-15 04:48:01 +00006534 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
6535 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006536 case 1:
6537 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00006538 case 4: break;
6539 default: return Result; // All one mask, or 5-byte mask.
6540 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006541
Chris Lattner2392ae72010-04-15 04:48:01 +00006542 // Verify that the first bit starts at a multiple of mask so that the access
6543 // is aligned the same as the access width.
6544 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006545
Chris Lattner2392ae72010-04-15 04:48:01 +00006546 Result.first = MaskedBytes;
6547 Result.second = NotMaskTZ/8;
6548 return Result;
6549}
6550
6551
6552/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
6553/// provides a value as specified by MaskInfo. If so, replace the specified
6554/// store with a narrower store of truncated IVal.
6555static SDNode *
6556ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
6557 SDValue IVal, StoreSDNode *St,
6558 DAGCombiner *DC) {
6559 unsigned NumBytes = MaskInfo.first;
6560 unsigned ByteShift = MaskInfo.second;
6561 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006562
Chris Lattner2392ae72010-04-15 04:48:01 +00006563 // Check to see if IVal is all zeros in the part being masked in by the 'or'
6564 // that uses this. If not, this is not a replacement.
6565 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
6566 ByteShift*8, (ByteShift+NumBytes)*8);
6567 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006568
Chris Lattner2392ae72010-04-15 04:48:01 +00006569 // Check that it is legal on the target to do this. It is legal if the new
6570 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
6571 // legalization.
6572 MVT VT = MVT::getIntegerVT(NumBytes*8);
6573 if (!DC->isTypeLegal(VT))
6574 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006575
Chris Lattner2392ae72010-04-15 04:48:01 +00006576 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
6577 // shifted by ByteShift and truncated down to NumBytes.
6578 if (ByteShift)
6579 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00006580 DAG.getConstant(ByteShift*8,
6581 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00006582
6583 // Figure out the offset for the store and the alignment of the access.
6584 unsigned StOffset;
6585 unsigned NewAlign = St->getAlignment();
6586
6587 if (DAG.getTargetLoweringInfo().isLittleEndian())
6588 StOffset = ByteShift;
6589 else
6590 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006591
Chris Lattner2392ae72010-04-15 04:48:01 +00006592 SDValue Ptr = St->getBasePtr();
6593 if (StOffset) {
6594 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
6595 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
6596 NewAlign = MinAlign(NewAlign, StOffset);
6597 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006598
Chris Lattner2392ae72010-04-15 04:48:01 +00006599 // Truncate down to the new size.
6600 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006601
Chris Lattner2392ae72010-04-15 04:48:01 +00006602 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006603 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006604 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00006605 false, false, NewAlign).getNode();
6606}
6607
Evan Cheng8b944d32009-05-28 00:35:15 +00006608
6609/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
6610/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
6611/// of the loaded bits, try narrowing the load and store if it would end up
6612/// being a win for performance or code size.
6613SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
6614 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00006615 if (ST->isVolatile())
6616 return SDValue();
6617
Evan Cheng8b944d32009-05-28 00:35:15 +00006618 SDValue Chain = ST->getChain();
6619 SDValue Value = ST->getValue();
6620 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00006621 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00006622
6623 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00006624 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006625
6626 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006627
Chris Lattner2392ae72010-04-15 04:48:01 +00006628 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
6629 // is a byte mask indicating a consecutive number of bytes, check to see if
6630 // Y is known to provide just those bytes. If so, we try to replace the
6631 // load + replace + store sequence with a single (narrower) store, which makes
6632 // the load dead.
6633 if (Opc == ISD::OR) {
6634 std::pair<unsigned, unsigned> MaskedLoad;
6635 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
6636 if (MaskedLoad.first)
6637 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
6638 Value.getOperand(1), ST,this))
6639 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006640
Chris Lattner2392ae72010-04-15 04:48:01 +00006641 // Or is commutative, so try swapping X and Y.
6642 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
6643 if (MaskedLoad.first)
6644 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
6645 Value.getOperand(0), ST,this))
6646 return SDValue(NewST, 0);
6647 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006648
Evan Cheng8b944d32009-05-28 00:35:15 +00006649 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
6650 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00006651 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006652
6653 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00006654 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
6655 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00006656 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00006657 if (LD->getBasePtr() != Ptr ||
6658 LD->getPointerInfo().getAddrSpace() !=
6659 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00006660 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006661
6662 // Find the type to narrow it the load / op / store to.
6663 SDValue N1 = Value.getOperand(1);
6664 unsigned BitWidth = N1.getValueSizeInBits();
6665 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
6666 if (Opc == ISD::AND)
6667 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00006668 if (Imm == 0 || Imm.isAllOnesValue())
6669 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006670 unsigned ShAmt = Imm.countTrailingZeros();
6671 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
6672 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00006673 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00006674 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00006675 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00006676 TLI.isNarrowingProfitable(VT, NewVT))) {
6677 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00006678 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00006679 }
Evan Chengcdcecc02009-05-28 18:41:02 +00006680 if (NewBW >= BitWidth)
6681 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006682
6683 // If the lsb changed does not start at the type bitwidth boundary,
6684 // start at the previous one.
6685 if (ShAmt % NewBW)
6686 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
6687 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
6688 if ((Imm & Mask) == Imm) {
6689 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
6690 if (Opc == ISD::AND)
6691 NewImm ^= APInt::getAllOnesValue(NewBW);
6692 uint64_t PtrOff = ShAmt / 8;
6693 // For big endian targets, we need to adjust the offset to the pointer to
6694 // load the correct bytes.
6695 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00006696 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00006697
6698 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006699 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Chris Lattner2392ae72010-04-15 04:48:01 +00006700 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00006701 return SDValue();
6702
Evan Cheng8b944d32009-05-28 00:35:15 +00006703 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
6704 Ptr.getValueType(), Ptr,
6705 DAG.getConstant(PtrOff, Ptr.getValueType()));
6706 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
6707 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00006708 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00006709 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006710 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00006711 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
6712 DAG.getConstant(NewImm, NewVT));
6713 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
6714 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00006715 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00006716 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00006717
6718 AddToWorkList(NewPtr.getNode());
6719 AddToWorkList(NewLD.getNode());
6720 AddToWorkList(NewVal.getNode());
6721 WorkListRemover DeadNodes(*this);
6722 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
6723 &DeadNodes);
6724 ++OpsNarrowed;
6725 return NewST;
6726 }
6727 }
6728
Evan Chengcdcecc02009-05-28 18:41:02 +00006729 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00006730}
6731
Evan Cheng31959b12011-02-02 01:06:55 +00006732/// TransformFPLoadStorePair - For a given floating point load / store pair,
6733/// if the load value isn't used by any other operations, then consider
6734/// transforming the pair to integer load / store operations if the target
6735/// deems the transformation profitable.
6736SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
6737 StoreSDNode *ST = cast<StoreSDNode>(N);
6738 SDValue Chain = ST->getChain();
6739 SDValue Value = ST->getValue();
6740 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
6741 Value.hasOneUse() &&
6742 Chain == SDValue(Value.getNode(), 1)) {
6743 LoadSDNode *LD = cast<LoadSDNode>(Value);
6744 EVT VT = LD->getMemoryVT();
6745 if (!VT.isFloatingPoint() ||
6746 VT != ST->getMemoryVT() ||
6747 LD->isNonTemporal() ||
6748 ST->isNonTemporal() ||
6749 LD->getPointerInfo().getAddrSpace() != 0 ||
6750 ST->getPointerInfo().getAddrSpace() != 0)
6751 return SDValue();
6752
6753 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
6754 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
6755 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
6756 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
6757 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
6758 return SDValue();
6759
6760 unsigned LDAlign = LD->getAlignment();
6761 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006762 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Evan Cheng31959b12011-02-02 01:06:55 +00006763 unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy);
6764 if (LDAlign < ABIAlign || STAlign < ABIAlign)
6765 return SDValue();
6766
6767 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
6768 LD->getChain(), LD->getBasePtr(),
6769 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006770 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00006771
6772 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
6773 NewLD, ST->getBasePtr(),
6774 ST->getPointerInfo(),
6775 false, false, STAlign);
6776
6777 AddToWorkList(NewLD.getNode());
6778 AddToWorkList(NewST.getNode());
6779 WorkListRemover DeadNodes(*this);
6780 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1),
6781 &DeadNodes);
6782 ++LdStFP2Int;
6783 return NewST;
6784 }
6785
6786 return SDValue();
6787}
6788
Dan Gohman475871a2008-07-27 21:46:04 +00006789SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00006790 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00006791 SDValue Chain = ST->getChain();
6792 SDValue Value = ST->getValue();
6793 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00006794
Evan Cheng59d5b682007-05-07 21:27:48 +00006795 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00006796 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006797 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00006798 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00006799 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00006800 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00006801 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00006802 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006803 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006804 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006805 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00006806 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00006807 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006808 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00006809 }
Owen Andersona34d9362011-04-14 17:30:49 +00006810
Chris Lattnerb3452ea2011-04-09 02:32:02 +00006811 // Turn 'store undef, Ptr' -> nothing.
6812 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
6813 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006814
Nate Begeman2cbba892006-12-11 02:23:46 +00006815 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00006816 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006817 // NOTE: If the original store is volatile, this transform must not increase
6818 // the number of stores. For example, on x86-32 an f64 can be stored in one
6819 // processor operation but an i64 (which is not legal) requires two. So the
6820 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00006821 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00006822 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006823 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006824 default: llvm_unreachable("Unknown FP type");
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 case MVT::f80: // We don't do this for these yet.
6826 case MVT::f128:
6827 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00006828 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00006830 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00006832 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00006833 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00006834 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006835 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006836 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00006837 }
6838 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00006840 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006841 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006843 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00006845 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006846 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00006847 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00006848 }
Owen Andersona34d9362011-04-14 17:30:49 +00006849
Chris Lattnerb3452ea2011-04-09 02:32:02 +00006850 if (!ST->isVolatile() &&
6851 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00006852 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00006853 // argument passing. Since this is so common, custom legalize the
6854 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00006855 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
6857 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00006858 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00006859
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006860 unsigned Alignment = ST->getAlignment();
6861 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00006862 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006863
Bill Wendlingc144a572009-01-30 23:36:47 +00006864 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006865 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00006866 isVolatile, isNonTemporal,
6867 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00006868 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00006869 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00006870 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00006871 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006872 Ptr, ST->getPointerInfo().getWithOffset(4),
6873 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00006874 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00006876 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00006877 }
Bill Wendlingc144a572009-01-30 23:36:47 +00006878
Chris Lattner62be1a72006-12-12 04:16:14 +00006879 break;
Evan Cheng25ece662006-12-11 17:25:19 +00006880 }
Nate Begeman2cbba892006-12-11 02:23:46 +00006881 }
Nate Begeman2cbba892006-12-11 02:23:46 +00006882 }
6883
Evan Cheng255f20f2010-04-01 06:04:33 +00006884 // Try to infer better alignment information than the store already has.
6885 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00006886 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
6887 if (Align > ST->getAlignment())
6888 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
6889 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
6890 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00006891 }
6892 }
6893
Evan Cheng31959b12011-02-02 01:06:55 +00006894 // Try transforming a pair floating point load / store ops to integer
6895 // load / store ops.
6896 SDValue NewST = TransformFPLoadStorePair(N);
6897 if (NewST.getNode())
6898 return NewST;
6899
Scott Michelfdc40a02009-02-17 22:15:04 +00006900 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00006901 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006902 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00006903
Jim Laskey6ff23e52006-10-04 16:53:27 +00006904 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00006905 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00006906 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006907
6908 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00006909 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00006910 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00006911 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00006912 ST->getMemoryVT(), ST->isVolatile(),
6913 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00006914 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00006915 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00006916 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00006917 ST->isVolatile(), ST->isNonTemporal(),
6918 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00006919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006920
Jim Laskey279f0532006-09-25 16:29:54 +00006921 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00006922 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00006924
Nate Begemanb6aef5c2009-09-15 00:18:30 +00006925 // Make sure the new and old chains are cleaned up.
6926 AddToWorkList(Token.getNode());
6927
Jim Laskey274062c2006-10-13 23:32:28 +00006928 // Don't add users to work list.
6929 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00006930 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00006931 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006932
Evan Cheng33dbedc2006-11-05 09:31:14 +00006933 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00006934 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00006935 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00006936
Chris Lattner3c872852007-12-29 06:26:16 +00006937 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00006938 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00006939 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00006940 // See if we can simplify the input to this truncstore with knowledge that
6941 // only the low bits are being used. For example:
6942 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00006943 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006944 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00006945 APInt::getLowBitsSet(
6946 Value.getValueType().getScalarType().getSizeInBits(),
6947 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00006948 AddToWorkList(Value.getNode());
6949 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00006950 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00006951 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00006952 ST->isVolatile(), ST->isNonTemporal(),
6953 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00006954
Chris Lattnere33544c2007-10-13 06:58:48 +00006955 // Otherwise, see if we can simplify the operation with
6956 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00006957 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00006958 APInt::getLowBitsSet(
6959 Value.getValueType().getScalarType().getSizeInBits(),
6960 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00006961 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00006962 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006963
Chris Lattner3c872852007-12-29 06:26:16 +00006964 // If this is a load followed by a store to the same location, then the store
6965 // is dead/noop.
6966 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006967 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00006968 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00006969 // There can't be any side effects between the load and store, such as
6970 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00006971 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00006972 // The store is dead, remove it.
6973 return Chain;
6974 }
6975 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006976
Chris Lattnerddf89562008-01-17 19:59:44 +00006977 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
6978 // truncating store. We can do this even if this is already a truncstore.
6979 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00006980 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00006981 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006982 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00006983 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00006984 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00006985 ST->isVolatile(), ST->isNonTemporal(),
6986 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00006987 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006988
Evan Cheng8b944d32009-05-28 00:35:15 +00006989 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00006990}
6991
Dan Gohman475871a2008-07-27 21:46:04 +00006992SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
6993 SDValue InVec = N->getOperand(0);
6994 SDValue InVal = N->getOperand(1);
6995 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00006996 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006997
Bob Wilson492fd452010-05-19 23:42:58 +00006998 // If the inserted element is an UNDEF, just use the input vector.
6999 if (InVal.getOpcode() == ISD::UNDEF)
7000 return InVec;
7001
Nadav Rotem609d54e2011-02-12 14:40:33 +00007002 EVT VT = InVec.getValueType();
7003
Owen Anderson95771af2011-02-25 21:41:48 +00007004 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00007005 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
7006 return SDValue();
7007
Eli Friedman9db817f2011-09-09 21:04:06 +00007008 // Check that we know which element is being inserted
7009 if (!isa<ConstantSDNode>(EltNo))
7010 return SDValue();
7011 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007012
Eli Friedman9db817f2011-09-09 21:04:06 +00007013 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
7014 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
7015 // vector elements.
7016 SmallVector<SDValue, 8> Ops;
7017 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
7018 Ops.append(InVec.getNode()->op_begin(),
7019 InVec.getNode()->op_end());
7020 } else if (InVec.getOpcode() == ISD::UNDEF) {
7021 unsigned NElts = VT.getVectorNumElements();
7022 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
7023 } else {
7024 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007025 }
Eli Friedman9db817f2011-09-09 21:04:06 +00007026
7027 // Insert the element
7028 if (Elt < Ops.size()) {
7029 // All the operands of BUILD_VECTOR must have the same type;
7030 // we enforce that here.
7031 EVT OpVT = Ops[0].getValueType();
7032 if (InVal.getValueType() != OpVT)
7033 InVal = OpVT.bitsGT(InVal.getValueType()) ?
7034 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
7035 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
7036 Ops[Elt] = InVal;
7037 }
7038
7039 // Return the new vector
7040 return DAG.getNode(ISD::BUILD_VECTOR, dl,
7041 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00007042}
7043
Dan Gohman475871a2008-07-27 21:46:04 +00007044SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007045 // (vextract (scalar_to_vector val, 0) -> val
7046 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00007047 EVT VT = InVec.getValueType();
7048 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007049
Duncan Sandsc356f332011-05-09 08:03:33 +00007050 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
7051 // Check if the result type doesn't match the inserted element type. A
7052 // SCALAR_TO_VECTOR may truncate the inserted element and the
7053 // EXTRACT_VECTOR_ELT may widen the extracted vector.
7054 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00007055 if (InOp.getValueType() != NVT) {
7056 assert(InOp.getValueType().isInteger() && NVT.isInteger());
7057 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
7058 }
7059 return InOp;
7060 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007061
Nadav Rotemba05c912012-01-17 21:44:01 +00007062 SDValue EltNo = N->getOperand(1);
7063 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
7064
7065 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
7066 // We only perform this optimization before the op legalization phase because
7067 // we may introduce new vector instructions which are not backed by TD patterns.
7068 // For example on AVX, extracting elements from a wide vector without using
7069 // extract_subvector.
7070 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
7071 && ConstEltNo && !LegalOperations) {
7072 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
7073 int NumElem = VT.getVectorNumElements();
7074 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
7075 // Find the new index to extract from.
7076 int OrigElt = SVOp->getMaskElt(Elt);
7077
7078 // Extracting an undef index is undef.
7079 if (OrigElt == -1)
7080 return DAG.getUNDEF(NVT);
7081
7082 // Select the right vector half to extract from.
7083 if (OrigElt < NumElem) {
7084 InVec = InVec->getOperand(0);
7085 } else {
7086 InVec = InVec->getOperand(1);
7087 OrigElt -= NumElem;
7088 }
7089
7090 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
7091 InVec, DAG.getConstant(OrigElt, MVT::i32));
7092 }
7093
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007094 // Perform only after legalization to ensure build_vector / vector_shuffle
7095 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00007096 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007097
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007098 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
7099 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
7100 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00007101
Nadav Rotemba05c912012-01-17 21:44:01 +00007102 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00007103 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00007104 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00007105 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00007106 EVT ExtVT = VT.getVectorElementType();
7107 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00007108
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007109 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007110 // Don't duplicate a load with other uses.
7111 if (!InVec.hasOneUse())
7112 return SDValue();
7113
Owen Andersone50ed302009-08-10 22:56:29 +00007114 EVT BCVT = InVec.getOperand(0).getValueType();
7115 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007116 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00007117 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
7118 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007119 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007120 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007121 NewLoad = true;
7122 }
Evan Cheng513da432007-10-06 08:19:55 +00007123
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007124 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00007125 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00007126 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007127 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00007128 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00007129 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00007130 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007131 // Don't duplicate a load with other uses.
7132 if (!InVec.hasOneUse())
7133 return SDValue();
7134
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007135 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00007136 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007137 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
7138 // =>
7139 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00007140
Eli Friedmand6e25602011-12-26 22:49:32 +00007141 // Don't duplicate a load with other uses.
7142 if (!InVec.hasOneUse())
7143 return SDValue();
7144
Mon P Wanga60b5232008-12-11 00:26:16 +00007145 // If the bit convert changed the number of elements, it is unsafe
7146 // to examine the mask.
7147 if (BCNumEltsChanged)
7148 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00007149
7150 // Select the input vector, guarding against out of range extract vector.
7151 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00007152 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00007153 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
7154
Eli Friedmand6e25602011-12-26 22:49:32 +00007155 if (InVec.getOpcode() == ISD::BITCAST) {
7156 // Don't duplicate a load with other uses.
7157 if (!InVec.hasOneUse())
7158 return SDValue();
7159
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007160 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00007161 }
Gabor Greifba36cb52008-08-28 21:40:38 +00007162 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007163 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00007164 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00007165 }
7166 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007167
Eli Friedmand6e25602011-12-26 22:49:32 +00007168 // Make sure we found a non-volatile load and the extractelement is
7169 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00007170 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00007171 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007172
Eric Christopherd81f17a2010-11-03 20:44:42 +00007173 // If Idx was -1 above, Elt is going to be -1, so just return undef.
7174 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00007175 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00007176
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007177 unsigned Align = LN0->getAlignment();
7178 if (NewLoad) {
7179 // Check the resultant load doesn't need a higher alignment than the
7180 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00007181 unsigned NewAlign =
Eric Christopher503a64d2010-12-09 04:48:06 +00007182 TLI.getTargetData()
7183 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00007184
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007185 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00007186 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00007187
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007188 Align = NewAlign;
7189 }
7190
Dan Gohman475871a2008-07-27 21:46:04 +00007191 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00007192 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007193
Eric Christopherd81f17a2010-11-03 20:44:42 +00007194 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00007195 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007196 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007197 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007198 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00007199 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007200 DAG.getConstant(PtrOff, PtrType));
7201 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007202
Eli Friedman4db4add2011-11-16 23:50:22 +00007203 // The replacement we need to do here is a little tricky: we need to
7204 // replace an extractelement of a load with a load.
7205 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00007206 // Note that this replacement assumes that the extractvalue is the only
7207 // use of the load; that's okay because we don't want to perform this
7208 // transformation in other cases anyway.
Eli Friedman4db4add2011-11-16 23:50:22 +00007209 SDValue Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
7210 LN0->getPointerInfo().getWithOffset(PtrOff),
7211 LN0->isVolatile(), LN0->isNonTemporal(),
7212 LN0->isInvariant(), Align);
7213 WorkListRemover DeadNodes(*this);
7214 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
7215 SDValue To[] = { Load.getValue(0), Load.getValue(1) };
7216 DAG.ReplaceAllUsesOfValuesWith(From, To, 2, &DeadNodes);
7217 // Since we're explcitly calling ReplaceAllUses, add the new node to the
7218 // worklist explicitly as well.
7219 AddToWorkList(Load.getNode());
7220 // Make sure to revisit this node to clean it up; it will usually be dead.
7221 AddToWorkList(N);
7222 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00007223 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007224
Dan Gohman475871a2008-07-27 21:46:04 +00007225 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00007226}
Evan Cheng513da432007-10-06 08:19:55 +00007227
Dan Gohman475871a2008-07-27 21:46:04 +00007228SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007229 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007230 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007231 EVT VT = N->getValueType(0);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007232 // Check to see if this is a BUILD_VECTOR of a bunch of values
7233 // which come from any_extend or zero_extend nodes. If so, we can create
7234 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00007235 // optimizations. We do not handle sign-extend because we can't fill the sign
7236 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007237 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00007238 bool AllAnyExt = true;
7239 bool AllUndef = true;
7240 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007241 SDValue In = N->getOperand(i);
7242 // Ignore undef inputs.
7243 if (In.getOpcode() == ISD::UNDEF) continue;
Craig Topperd3b58892012-01-17 09:09:48 +00007244 AllUndef = false;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007245
7246 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
7247 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
7248
Nadav Rotemf47368b2011-10-31 20:08:25 +00007249 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007250 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00007251 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007252 break;
7253 }
7254
7255 // The input is a ZeroExt or AnyExt. Check the original type.
7256 EVT InTy = In.getOperand(0).getValueType();
7257
7258 // Check that all of the widened source types are the same.
7259 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007260 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007261 SourceType = InTy;
7262 else if (InTy != SourceType) {
7263 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007264 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007265 break;
7266 }
7267
7268 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00007269 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007270 }
7271
Craig Topperd3b58892012-01-17 09:09:48 +00007272 if (AllUndef)
7273 return DAG.getUNDEF(VT);
Nadav Rotemf47368b2011-10-31 20:08:25 +00007274
7275 // In order to have valid types, all of the inputs must be extended from the
7276 // same source type and all of the inputs must be any or zero extend.
7277 // Scalar sizes must be a power of two.
7278 EVT OutScalarTy = N->getValueType(0).getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007279 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00007280 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
7281 isPowerOf2_32(SourceType.getSizeInBits());
7282
7283 // We perform this optimization post type-legalization because
7284 // the type-legalizer often scalarizes integer-promoted vectors.
7285 // Performing this optimization before may create bit-casts which
7286 // will be type-legalized to complex code sequences.
7287 // We perform this optimization only before the operation legalizer because we
7288 // may introduce illegal operations.
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007289 if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) &&
7290 ValidTypes) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007291 bool isLE = TLI.isLittleEndian();
Nadav Rotemf47368b2011-10-31 20:08:25 +00007292 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007293 assert(ElemRatio > 1 && "Invalid element size ratio");
Craig Topperd3b58892012-01-17 09:09:48 +00007294 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
Nadav Rotemf47368b2011-10-31 20:08:25 +00007295 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007296
7297 unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements();
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007298 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007299
7300 // Populate the new build_vector
7301 for (unsigned i=0; i < N->getNumOperands(); ++i) {
7302 SDValue Cast = N->getOperand(i);
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007303 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
7304 Cast.getOpcode() == ISD::ZERO_EXTEND ||
7305 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
Nadav Rotemb00418a2011-10-29 21:23:04 +00007306 SDValue In;
7307 if (Cast.getOpcode() == ISD::UNDEF)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007308 In = DAG.getUNDEF(SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007309 else
7310 In = Cast->getOperand(0);
7311 unsigned Index = isLE ? (i * ElemRatio) :
7312 (i * ElemRatio + (ElemRatio - 1));
7313
7314 assert(Index < Ops.size() && "Invalid index");
7315 Ops[Index] = In;
7316 }
7317
7318 // The type of the new BUILD_VECTOR node.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007319 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007320 assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() &&
7321 "Invalid vector size");
Nadav Rotemf47368b2011-10-31 20:08:25 +00007322 // Check if the new vector type is legal.
7323 if (!isTypeLegal(VecVT)) return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007324
7325 // Make the new BUILD_VECTOR.
7326 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
7327 VecVT, &Ops[0], Ops.size());
7328
7329 // Bitcast to the desired type.
7330 return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV);
7331 }
Chris Lattnerca242442006-03-19 01:27:56 +00007332
Dan Gohman7f321562007-06-25 16:23:39 +00007333 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
7334 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
7335 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00007336 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00007337 for (unsigned i = 0; i != NumInScalars; ++i) {
7338 // Ignore undef inputs.
7339 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00007340
Dan Gohman7f321562007-06-25 16:23:39 +00007341 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00007342 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00007343 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00007344 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007345 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007346 break;
7347 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007348
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007349 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00007350 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007351 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
7352 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00007353
Gabor Greifba36cb52008-08-28 21:40:38 +00007354 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00007355 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00007356 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00007357 VecIn2 = ExtractedFromVec;
7358 } else {
7359 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007360 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007361 break;
7362 }
7363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007364
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007365 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00007366 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007367 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00007368 for (unsigned i = 0; i != NumInScalars; ++i) {
7369 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007370 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007371 continue;
7372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007373
Rafael Espindola15684b22009-04-24 12:40:33 +00007374 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00007375 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00007376 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007377 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00007378 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
7379 if (ExtIndex > VT.getVectorNumElements())
7380 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007381
Nate Begeman5a5ca152009-04-29 05:20:52 +00007382 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007383 continue;
7384 }
7385
7386 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00007387 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007388 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007390
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007391 // We can't generate a shuffle node with mismatched input and output types.
7392 // Attempt to transform a single input vector to the correct type.
7393 if ((VT != VecIn1.getValueType())) {
7394 // We don't support shuffeling between TWO values of different types.
7395 if (VecIn2.getNode() != 0)
7396 return SDValue();
7397
7398 // We only support widening of vectors which are half the size of the
7399 // output registers. For example XMM->YMM widening on X86 with AVX.
7400 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
7401 return SDValue();
7402
7403 // Widen the input vector by adding undef values.
7404 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7405 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
7406 }
7407
7408 // If VecIn2 is unused then change it to undef.
7409 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
7410
Nadav Rotem0877fdf2012-02-13 12:42:26 +00007411 // Check that we were able to transform all incoming values to the same type.
7412 if (VecIn2.getValueType() != VecIn1.getValueType() ||
7413 VecIn1.getValueType() != VT)
7414 return SDValue();
7415
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007416 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00007417 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00007418 return SDValue();
7419
Dan Gohman7f321562007-06-25 16:23:39 +00007420 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00007421 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00007422 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007423 Ops[1] = VecIn2;
Nate Begeman9008ca62009-04-27 18:41:29 +00007424 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00007425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007426
Dan Gohman475871a2008-07-27 21:46:04 +00007427 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00007428}
7429
Dan Gohman475871a2008-07-27 21:46:04 +00007430SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007431 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
7432 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
7433 // inputs come from at most two distinct vectors, turn this into a shuffle
7434 // node.
7435
7436 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00007437 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00007438 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00007439
Dan Gohman475871a2008-07-27 21:46:04 +00007440 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00007441}
7442
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00007443SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
7444 EVT NVT = N->getValueType(0);
7445 SDValue V = N->getOperand(0);
7446
7447 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
7448 // Handle only simple case where vector being inserted and vector
7449 // being extracted are of same type, and are half size of larger vectors.
7450 EVT BigVT = V->getOperand(0).getValueType();
7451 EVT SmallVT = V->getOperand(1).getValueType();
7452 if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
7453 return SDValue();
7454
Eli Friedman26323442011-12-07 00:11:56 +00007455 // Only handle cases where both indexes are constants with the same type.
7456 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
7457 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00007458
Eli Friedman26323442011-12-07 00:11:56 +00007459 if (InsIdx && ExtIdx &&
7460 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
7461 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
7462 // Combine:
7463 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
7464 // Into:
7465 // indices are equal => V1
7466 // otherwise => (extract_subvec V1, ExtIdx)
7467 if (InsIdx->getZExtValue() == ExtIdx->getZExtValue())
7468 return V->getOperand(1);
7469 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT,
7470 V->getOperand(0), N->getOperand(1));
7471 }
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00007472 }
7473
7474 return SDValue();
7475}
7476
Dan Gohman475871a2008-07-27 21:46:04 +00007477SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00007478 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00007479 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00007480
Mon P Wangaeb06d22008-11-10 04:46:22 +00007481 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00007482 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007483
7484 assert(N0.getValueType().getVectorNumElements() == NumElts &&
7485 "Vector shuffle must be normalized in DAG");
7486
Craig Topper481b79c2012-01-04 08:07:43 +00007487 // Canonicalize shuffle undef, undef -> undef
7488 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
7489 return DAG.getUNDEF(VT);
7490
7491 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7492
7493 // Canonicalize shuffle v, v -> v, undef
7494 if (N0 == N1) {
7495 SmallVector<int, 8> NewMask;
7496 for (unsigned i = 0; i != NumElts; ++i) {
7497 int Idx = SVN->getMaskElt(i);
7498 if (Idx >= (int)NumElts) Idx -= NumElts;
7499 NewMask.push_back(Idx);
7500 }
7501 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
7502 &NewMask[0]);
7503 }
7504
7505 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
7506 if (N0.getOpcode() == ISD::UNDEF) {
7507 SmallVector<int, 8> NewMask;
7508 for (unsigned i = 0; i != NumElts; ++i) {
7509 int Idx = SVN->getMaskElt(i);
7510 if (Idx < 0)
7511 NewMask.push_back(Idx);
7512 else if (Idx < (int)NumElts)
7513 NewMask.push_back(Idx + NumElts);
7514 else
7515 NewMask.push_back(Idx - NumElts);
7516 }
7517 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
7518 &NewMask[0]);
7519 }
7520
7521 // Remove references to rhs if it is undef
7522 if (N1.getOpcode() == ISD::UNDEF) {
7523 bool Changed = false;
7524 SmallVector<int, 8> NewMask;
7525 for (unsigned i = 0; i != NumElts; ++i) {
7526 int Idx = SVN->getMaskElt(i);
7527 if (Idx >= (int)NumElts) {
7528 Idx = -1;
7529 Changed = true;
7530 }
7531 NewMask.push_back(Idx);
7532 }
7533 if (Changed)
7534 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
7535 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00007536
Bob Wilson0f1db1a2010-10-28 17:06:14 +00007537 // If it is a splat, check if the argument vector is another splat or a
7538 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00007539 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007540 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00007541
Dan Gohman7f321562007-06-25 16:23:39 +00007542 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00007543 // not the number of vector elements, look through it. Be careful not to
7544 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007545 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00007546 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00007547 if (ConvInput.getValueType().isVector() &&
7548 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00007549 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00007550 }
7551
Dan Gohman7f321562007-06-25 16:23:39 +00007552 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00007553 assert(V->getNumOperands() == NumElts &&
7554 "BUILD_VECTOR has wrong number of operands");
7555 SDValue Base;
7556 bool AllSame = true;
7557 for (unsigned i = 0; i != NumElts; ++i) {
7558 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
7559 Base = V->getOperand(i);
7560 break;
Evan Cheng917ec982006-07-21 08:25:53 +00007561 }
Evan Cheng917ec982006-07-21 08:25:53 +00007562 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00007563 // Splat of <u, u, u, u>, return <u, u, u, u>
7564 if (!Base.getNode())
7565 return N0;
7566 for (unsigned i = 0; i != NumElts; ++i) {
7567 if (V->getOperand(i) != Base) {
7568 AllSame = false;
7569 break;
7570 }
7571 }
7572 // Splat of <x, x, x, x>, return <x, x, x, x>
7573 if (AllSame)
7574 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00007575 }
7576 }
Dan Gohman475871a2008-07-27 21:46:04 +00007577 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00007578}
7579
Jim Grosbach9a526492010-06-23 16:07:42 +00007580SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) {
7581 if (!TLI.getShouldFoldAtomicFences())
7582 return SDValue();
7583
7584 SDValue atomic = N->getOperand(0);
7585 switch (atomic.getOpcode()) {
7586 case ISD::ATOMIC_CMP_SWAP:
7587 case ISD::ATOMIC_SWAP:
7588 case ISD::ATOMIC_LOAD_ADD:
7589 case ISD::ATOMIC_LOAD_SUB:
7590 case ISD::ATOMIC_LOAD_AND:
7591 case ISD::ATOMIC_LOAD_OR:
7592 case ISD::ATOMIC_LOAD_XOR:
7593 case ISD::ATOMIC_LOAD_NAND:
7594 case ISD::ATOMIC_LOAD_MIN:
7595 case ISD::ATOMIC_LOAD_MAX:
7596 case ISD::ATOMIC_LOAD_UMIN:
7597 case ISD::ATOMIC_LOAD_UMAX:
7598 break;
7599 default:
7600 return SDValue();
7601 }
7602
7603 SDValue fence = atomic.getOperand(0);
7604 if (fence.getOpcode() != ISD::MEMBARRIER)
7605 return SDValue();
7606
7607 switch (atomic.getOpcode()) {
7608 case ISD::ATOMIC_CMP_SWAP:
7609 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
7610 fence.getOperand(0),
7611 atomic.getOperand(1), atomic.getOperand(2),
7612 atomic.getOperand(3)), atomic.getResNo());
7613 case ISD::ATOMIC_SWAP:
7614 case ISD::ATOMIC_LOAD_ADD:
7615 case ISD::ATOMIC_LOAD_SUB:
7616 case ISD::ATOMIC_LOAD_AND:
7617 case ISD::ATOMIC_LOAD_OR:
7618 case ISD::ATOMIC_LOAD_XOR:
7619 case ISD::ATOMIC_LOAD_NAND:
7620 case ISD::ATOMIC_LOAD_MIN:
7621 case ISD::ATOMIC_LOAD_MAX:
7622 case ISD::ATOMIC_LOAD_UMIN:
7623 case ISD::ATOMIC_LOAD_UMAX:
7624 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
7625 fence.getOperand(0),
7626 atomic.getOperand(1), atomic.getOperand(2)),
7627 atomic.getResNo());
7628 default:
7629 return SDValue();
7630 }
7631}
7632
Evan Cheng44f1f092006-04-20 08:56:16 +00007633/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00007634/// an AND to a vector_shuffle with the destination vector and a zero vector.
7635/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00007636/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00007637SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00007638 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00007639 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007640 SDValue LHS = N->getOperand(0);
7641 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00007642 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007643 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00007644 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00007645 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007646 SmallVector<int, 8> Indices;
7647 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00007648 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007649 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00007650 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00007651 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00007652 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00007653 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00007654 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00007655 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00007656 else
Dan Gohman475871a2008-07-27 21:46:04 +00007657 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00007658 }
7659
7660 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00007661 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007662 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00007663 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00007664
Dan Gohman7f321562007-06-25 16:23:39 +00007665 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00007666 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007667 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00007668 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00007669 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
7670 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007671 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00007672 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007673 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00007674 }
7675 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00007676
Dan Gohman475871a2008-07-27 21:46:04 +00007677 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00007678}
7679
Dan Gohman7f321562007-06-25 16:23:39 +00007680/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00007681SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007682 // After legalize, the target may be depending on adds and other
7683 // binary ops to provide legal ways to construct constants or other
7684 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00007685 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00007686
Bob Wilsond7273432010-12-17 23:06:49 +00007687 assert(N->getValueType(0).isVector() &&
7688 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00007689
Dan Gohman475871a2008-07-27 21:46:04 +00007690 SDValue LHS = N->getOperand(0);
7691 SDValue RHS = N->getOperand(1);
7692 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00007693 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00007694
Dan Gohman7f321562007-06-25 16:23:39 +00007695 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00007696 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00007697 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00007698 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00007699 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00007700 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007701 SDValue LHSOp = LHS.getOperand(i);
7702 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00007703 // If these two elements can't be folded, bail out.
7704 if ((LHSOp.getOpcode() != ISD::UNDEF &&
7705 LHSOp.getOpcode() != ISD::Constant &&
7706 LHSOp.getOpcode() != ISD::ConstantFP) ||
7707 (RHSOp.getOpcode() != ISD::UNDEF &&
7708 RHSOp.getOpcode() != ISD::Constant &&
7709 RHSOp.getOpcode() != ISD::ConstantFP))
7710 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00007711
Evan Cheng7b336a82006-05-31 06:08:35 +00007712 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00007713 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
7714 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00007715 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00007716 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00007717 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00007718 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00007719 break;
7720 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00007721
Bob Wilsond7273432010-12-17 23:06:49 +00007722 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00007723 EVT RVT = RHSOp.getValueType();
7724 if (RVT != VT) {
7725 // Integer BUILD_VECTOR operands may have types larger than the element
7726 // size (e.g., when the element type is not legal). Prior to type
7727 // legalization, the types may not match between the two BUILD_VECTORS.
7728 // Truncate one of the operands to make them match.
7729 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
7730 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
7731 } else {
7732 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
7733 VT = RVT;
7734 }
7735 }
Bob Wilsond7273432010-12-17 23:06:49 +00007736 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00007737 LHSOp, RHSOp);
7738 if (FoldOp.getOpcode() != ISD::UNDEF &&
7739 FoldOp.getOpcode() != ISD::Constant &&
7740 FoldOp.getOpcode() != ISD::ConstantFP)
7741 break;
7742 Ops.push_back(FoldOp);
7743 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00007744 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007745
Bob Wilsond7273432010-12-17 23:06:49 +00007746 if (Ops.size() == LHS.getNumOperands())
7747 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
7748 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00007749 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007750
Dan Gohman475871a2008-07-27 21:46:04 +00007751 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00007752}
7753
Bill Wendling836ca7d2009-01-30 23:59:18 +00007754SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
7755 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00007756 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007757
Bill Wendling836ca7d2009-01-30 23:59:18 +00007758 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00007759 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00007760
Nate Begemanf845b452005-10-08 00:29:44 +00007761 // If we got a simplified select_cc node back from SimplifySelectCC, then
7762 // break it down into a new SETCC node, and a new SELECT node, and then return
7763 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00007764 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00007765 // Check to see if we got a select_cc back (to turn into setcc/select).
7766 // Otherwise, just return whatever node we got back, like fabs.
7767 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00007768 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
7769 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007770 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00007771 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00007772 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00007773 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
7774 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00007775 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00007776
Nate Begemanf845b452005-10-08 00:29:44 +00007777 return SCC;
7778 }
Dan Gohman475871a2008-07-27 21:46:04 +00007779 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007780}
7781
Chris Lattner40c62d52005-10-18 06:04:22 +00007782/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
7783/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00007784/// select. Callers of this should assume that TheSelect is deleted if this
7785/// returns true. As such, they should return the appropriate thing (e.g. the
7786/// node) back to the top-level of the DAG combiner loop to avoid it being
7787/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00007788bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007790
Nadav Rotemf94fdb62011-02-11 19:57:47 +00007791 // Cannot simplify select with vector condition
7792 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
7793
Chris Lattner40c62d52005-10-18 06:04:22 +00007794 // If this is a select from two identical things, try to pull the operation
7795 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00007796 if (LHS.getOpcode() != RHS.getOpcode() ||
7797 !LHS.hasOneUse() || !RHS.hasOneUse())
7798 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007799
Chris Lattner18061612010-09-21 15:46:59 +00007800 // If this is a load and the token chain is identical, replace the select
7801 // of two loads with a load through a select of the address to load from.
7802 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
7803 // constants have been dropped into the constant pool.
7804 if (LHS.getOpcode() == ISD::LOAD) {
7805 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
7806 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007807
Chris Lattner18061612010-09-21 15:46:59 +00007808 // Token chains must be identical.
7809 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007810 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00007811 LLD->isVolatile() || RLD->isVolatile() ||
7812 // If this is an EXTLOAD, the VT's must match.
7813 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00007814 // If this is an EXTLOAD, the kind of extension must match.
7815 (LLD->getExtensionType() != RLD->getExtensionType() &&
7816 // The only exception is if one of the extensions is anyext.
7817 LLD->getExtensionType() != ISD::EXTLOAD &&
7818 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00007819 // FIXME: this discards src value information. This is
7820 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00007821 // both potential memory locations. Since we are discarding
7822 // src value info, don't do the transformation if the memory
7823 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00007824 LLD->getPointerInfo().getAddrSpace() != 0 ||
7825 RLD->getPointerInfo().getAddrSpace() != 0)
7826 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007827
Chris Lattnerf1658062010-09-21 15:58:55 +00007828 // Check that the select condition doesn't reach either load. If so,
7829 // folding this will induce a cycle into the DAG. If not, this is safe to
7830 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00007831 SDValue Addr;
7832 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00007833 SDNode *CondNode = TheSelect->getOperand(0).getNode();
7834 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
7835 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
7836 return false;
7837 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
7838 LLD->getBasePtr().getValueType(),
7839 TheSelect->getOperand(0), LLD->getBasePtr(),
7840 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00007841 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00007842 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
7843 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
7844
7845 if ((LLD->hasAnyUseOfValue(1) &&
7846 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
7847 (LLD->hasAnyUseOfValue(1) &&
7848 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))))
7849 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007850
Chris Lattnerf1658062010-09-21 15:58:55 +00007851 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
7852 LLD->getBasePtr().getValueType(),
7853 TheSelect->getOperand(0),
7854 TheSelect->getOperand(1),
7855 LLD->getBasePtr(), RLD->getBasePtr(),
7856 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00007857 }
7858
Chris Lattnerf1658062010-09-21 15:58:55 +00007859 SDValue Load;
7860 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
7861 Load = DAG.getLoad(TheSelect->getValueType(0),
7862 TheSelect->getDebugLoc(),
7863 // FIXME: Discards pointer info.
7864 LLD->getChain(), Addr, MachinePointerInfo(),
7865 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007866 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00007867 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00007868 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
7869 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00007870 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007871 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00007872 // FIXME: Discards pointer info.
7873 LLD->getChain(), Addr, MachinePointerInfo(),
7874 LLD->getMemoryVT(), LLD->isVolatile(),
7875 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00007876 }
Chris Lattnerf1658062010-09-21 15:58:55 +00007877
7878 // Users of the select now use the result of the load.
7879 CombineTo(TheSelect, Load);
7880
7881 // Users of the old loads now use the new load's chain. We know the
7882 // old-load value is dead now.
7883 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
7884 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
7885 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00007886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007887
Chris Lattner40c62d52005-10-18 06:04:22 +00007888 return false;
7889}
7890
Chris Lattner600fec32009-03-11 05:08:08 +00007891/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
7892/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00007893SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00007894 SDValue N2, SDValue N3,
7895 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00007896 // (x ? y : y) -> y.
7897 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007898
Owen Andersone50ed302009-08-10 22:56:29 +00007899 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00007900 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
7901 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
7902 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00007903
7904 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00007905 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00007906 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00007907 if (SCC.getNode()) AddToWorkList(SCC.getNode());
7908 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00007909
7910 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00007911 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00007912 return N2;
7913 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00007914 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00007915 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Nate Begemanf845b452005-10-08 00:29:44 +00007917 // Check to see if we can simplify the select into an fabs node
7918 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
7919 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00007920 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00007921 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
7922 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
7923 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
7924 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00007925 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00007926
Nate Begemanf845b452005-10-08 00:29:44 +00007927 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
7928 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
7929 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
7930 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00007931 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00007932 }
7933 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007934
Chris Lattner600fec32009-03-11 05:08:08 +00007935 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
7936 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
7937 // in it. This is a win when the constant is not otherwise available because
7938 // it replaces two constant pool loads with one. We only do this if the FP
7939 // type is known to be legal, because if it isn't, then we are before legalize
7940 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00007941 // messing with soft float) and if the ConstantFP is not legal, because if
7942 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00007943 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
7944 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
7945 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00007946 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
7947 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00007948 // If both constants have multiple uses, then we won't need to do an
7949 // extra load, they are likely around in registers for other users.
7950 (TV->hasOneUse() || FV->hasOneUse())) {
7951 Constant *Elts[] = {
7952 const_cast<ConstantFP*>(FV->getConstantFPValue()),
7953 const_cast<ConstantFP*>(TV->getConstantFPValue())
7954 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007955 Type *FPTy = Elts[0]->getType();
Chris Lattner600fec32009-03-11 05:08:08 +00007956 const TargetData &TD = *TLI.getTargetData();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007957
Chris Lattner600fec32009-03-11 05:08:08 +00007958 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00007959 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00007960 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
7961 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00007962 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00007963
7964 // Get the offsets to the 0 and 1 element of the array so that we can
7965 // select between them.
7966 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00007967 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00007968 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007969
Chris Lattner600fec32009-03-11 05:08:08 +00007970 SDValue Cond = DAG.getSetCC(DL,
7971 TLI.getSetCCResultType(N0.getValueType()),
7972 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00007973 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00007974 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
7975 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00007976 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00007977 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
7978 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00007979 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00007980 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00007981 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007982 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00007983
7984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007985 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007986
Nate Begemanf845b452005-10-08 00:29:44 +00007987 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00007988 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00007989 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007990 (N1C->isNullValue() || // (a < 0) ? b : 0
7991 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00007992 EVT XType = N0.getValueType();
7993 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00007994 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00007995 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00007996 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00007997 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
7998 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007999 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00008000 SDValue ShCt = DAG.getConstant(ShCtV,
8001 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00008002 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008003 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00008004 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008005
Duncan Sands8e4eb092008-06-08 20:54:56 +00008006 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008007 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008008 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008009 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008010
8011 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008012 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008013
Bill Wendling9729c5a2009-01-31 03:12:48 +00008014 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008015 XType, N0,
8016 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008017 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00008018 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008019
Duncan Sands8e4eb092008-06-08 20:54:56 +00008020 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008021 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008022 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008023 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008024
8025 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008026 }
8027 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008028
Owen Andersoned1088a2010-09-22 22:58:22 +00008029 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
8030 // where y is has a single bit set.
8031 // A plaintext description would be, we can turn the SELECT_CC into an AND
8032 // when the condition can be materialized as an all-ones register. Any
8033 // single bit-test can be materialized as an all-ones register with
8034 // shift-left and shift-right-arith.
8035 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
8036 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008037 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00008038 N2C && N2C->isNullValue()) {
8039 SDValue AndLHS = N0->getOperand(0);
8040 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
8041 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
8042 // Shift the tested bit over the sign bit.
8043 APInt AndMask = ConstAndRHS->getAPIntValue();
8044 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008045 DAG.getConstant(AndMask.countLeadingZeros(),
8046 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008047 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008048
Owen Andersoned1088a2010-09-22 22:58:22 +00008049 // Now arithmetic right shift it all the way over, so the result is either
8050 // all-ones, or zero.
8051 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008052 DAG.getConstant(AndMask.getBitWidth()-1,
8053 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008054 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008055
Owen Andersoned1088a2010-09-22 22:58:22 +00008056 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
8057 }
8058 }
8059
Nate Begeman07ed4172005-10-10 21:26:48 +00008060 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00008061 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00008062 TLI.getBooleanContents(N0.getValueType().isVector()) ==
8063 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008064
Chris Lattner1eba01e2007-04-11 06:50:51 +00008065 // If the caller doesn't want us to simplify this into a zext of a compare,
8066 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00008067 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00008068 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008069
Nate Begeman07ed4172005-10-10 21:26:48 +00008070 // Get a SetCC of the condition
8071 // FIXME: Should probably make sure that setcc is legal if we ever have a
8072 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00008073 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00008074 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00008075 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008076 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00008077 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00008078 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00008079 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00008080 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00008081 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008082 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008083 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00008084 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00008085 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008086 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008087 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008088
Gabor Greifba36cb52008-08-28 21:40:38 +00008089 AddToWorkList(SCC.getNode());
8090 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00008091
Dan Gohman002e5d02008-03-13 22:13:53 +00008092 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00008093 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008094
Nate Begeman07ed4172005-10-10 21:26:48 +00008095 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00008096 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00008097 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00008098 getShiftAmountTy(Temp.getValueType())));
Nate Begeman07ed4172005-10-10 21:26:48 +00008099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008100
Nate Begemanf845b452005-10-08 00:29:44 +00008101 // Check to see if this is the equivalent of setcc
8102 // FIXME: Turn all of these into setcc if setcc if setcc is legal
8103 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00008104 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00008105 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00008106 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00008107 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008108 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00008109 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008110 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00008111 return Res;
8112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008113
Bill Wendling836ca7d2009-01-30 23:59:18 +00008114 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00008115 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008116 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00008117 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008118 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008119 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008120 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00008121 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00008122 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008123 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00008124 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008125 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
8126 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00008127 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00008128 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00008129 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00008130 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008131 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00008132 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008133 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00008134 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008135 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00008136 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008137 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00008138 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00008139 }
8140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008141
Benjamin Kramercde51102010-07-08 12:09:56 +00008142 // Check to see if this is an integer abs.
8143 // select_cc setg[te] X, 0, X, -X ->
8144 // select_cc setgt X, -1, X, -X ->
8145 // select_cc setl[te] X, 0, -X, X ->
8146 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00008147 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00008148 if (N1C) {
8149 ConstantSDNode *SubC = NULL;
8150 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
8151 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
8152 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
8153 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
8154 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
8155 (N1C->isOne() && CC == ISD::SETLT)) &&
8156 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
8157 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
8158
Owen Andersone50ed302009-08-10 22:56:29 +00008159 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00008160 if (SubC && SubC->isNullValue() && XType.isInteger()) {
8161 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
8162 N0,
8163 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008164 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +00008165 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
8166 XType, N0, Shift);
8167 AddToWorkList(Shift.getNode());
8168 AddToWorkList(Add.getNode());
8169 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00008170 }
8171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008172
Dan Gohman475871a2008-07-27 21:46:04 +00008173 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008174}
8175
Evan Chengfa1eb272007-02-08 22:13:59 +00008176/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00008177SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00008178 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008179 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008180 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00008181 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008182 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +00008183}
8184
Nate Begeman69575232005-10-20 02:15:44 +00008185/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
8186/// return a DAG expression to select that will generate the same value by
8187/// multiplying by a magic number. See:
8188/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008189SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008190 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008191 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008192
Andrew Lenharth232c9102006-06-12 16:07:18 +00008193 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008194 ii != ee; ++ii)
8195 AddToWorkList(*ii);
8196 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008197}
8198
8199/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
8200/// return a DAG expression to select that will generate the same value by
8201/// multiplying by a magic number. See:
8202/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008203SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008204 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008205 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00008206
Andrew Lenharth232c9102006-06-12 16:07:18 +00008207 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008208 ii != ee; ++ii)
8209 AddToWorkList(*ii);
8210 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008211}
8212
Nate Begemancc66cdd2009-09-25 06:05:26 +00008213/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +00008214// to alias with anything but itself. Provides base object and offset as
8215// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008216static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Dan Gohman46510a72010-04-15 01:51:59 +00008217 const GlobalValue *&GV, void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00008218 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008219 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008220
Jim Laskey71382342006-10-07 23:37:56 +00008221 // If it's an adding a simple constant then integrate the offset.
8222 if (Base.getOpcode() == ISD::ADD) {
8223 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
8224 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008225 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00008226 }
8227 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008228
Nate Begemancc66cdd2009-09-25 06:05:26 +00008229 // Return the underlying GlobalValue, and update the Offset. Return false
8230 // for GlobalAddressSDNode since the same GlobalAddress may be represented
8231 // by multiple nodes with different offsets.
8232 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
8233 GV = G->getGlobal();
8234 Offset += G->getOffset();
8235 return false;
8236 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008237
Nate Begemancc66cdd2009-09-25 06:05:26 +00008238 // Return the underlying Constant value, and update the Offset. Return false
8239 // for ConstantSDNodes since the same constant pool entry may be represented
8240 // by multiple nodes with different offsets.
8241 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
8242 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
8243 : (void *)C->getConstVal();
8244 Offset += C->getOffset();
8245 return false;
8246 }
Jim Laskey71382342006-10-07 23:37:56 +00008247 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008248 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00008249}
8250
8251/// isAlias - Return true if there is any possibility that the two addresses
8252/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00008253bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00008254 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008255 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008256 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +00008257 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008258 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008259 unsigned SrcValueAlign2,
8260 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +00008261 // If they are the same then they must be aliases.
8262 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00008263
Jim Laskey71382342006-10-07 23:37:56 +00008264 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00008265 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00008266 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00008267 const GlobalValue *GV1, *GV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00008268 void *CV1, *CV2;
8269 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
8270 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008271
Nate Begemancc66cdd2009-09-25 06:05:26 +00008272 // If they have a same base address then check to see if they overlap.
8273 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008274 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008275
Owen Anderson4a9f1502010-09-20 20:39:59 +00008276 // It is possible for different frame indices to alias each other, mostly
8277 // when tail call optimization reuses return address slots for arguments.
8278 // To catch this case, look up the actual index of frame indices to compute
8279 // the real alias relationship.
8280 if (isFrameIndex1 && isFrameIndex2) {
8281 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8282 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
8283 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
8284 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
8285 }
8286
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008287 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +00008288 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008289 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
8290 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00008291
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008292 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
8293 // compared to the size and offset of the access, we may be able to prove they
8294 // do not alias. This check is conservative for now to catch cases created by
8295 // splitting vector types.
8296 if ((SrcValueAlign1 == SrcValueAlign2) &&
8297 (SrcValueOffset1 != SrcValueOffset2) &&
8298 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
8299 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
8300 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008301
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008302 // There is no overlap between these relatively aligned accesses of similar
8303 // size, return no alias.
8304 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
8305 return false;
8306 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008307
Jim Laskey07a27092006-10-18 19:08:31 +00008308 if (CombinerGlobalAA) {
8309 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00008310 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
8311 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
8312 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00008313 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008314 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
8315 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +00008316 if (AAResult == AliasAnalysis::NoAlias)
8317 return false;
8318 }
Jim Laskey096c22e2006-10-18 12:29:57 +00008319
8320 // Otherwise we have to assume they alias.
8321 return true;
Jim Laskey71382342006-10-07 23:37:56 +00008322}
8323
8324/// FindAliasInfo - Extracts the relevant alias information from the memory
8325/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00008326bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +00008327 SDValue &Ptr, int64_t &Size,
8328 const Value *&SrcValue,
8329 int &SrcValueOffset,
8330 unsigned &SrcValueAlign,
8331 const MDNode *&TBAAInfo) const {
8332 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
8333
8334 Ptr = LS->getBasePtr();
8335 Size = LS->getMemoryVT().getSizeInBits() >> 3;
8336 SrcValue = LS->getSrcValue();
8337 SrcValueOffset = LS->getSrcValueOffset();
8338 SrcValueAlign = LS->getOriginalAlignment();
8339 TBAAInfo = LS->getTBAAInfo();
8340 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +00008341}
8342
Jim Laskey6ff23e52006-10-04 16:53:27 +00008343/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
8344/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00008345void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
8346 SmallVector<SDValue, 8> &Aliases) {
8347 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008348 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00008349
Jim Laskey279f0532006-09-25 16:29:54 +00008350 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00008351 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008352 int64_t Size;
8353 const Value *SrcValue;
8354 int SrcValueOffset;
8355 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008356 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008357 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008358 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +00008359
Jim Laskey6ff23e52006-10-04 16:53:27 +00008360 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00008361 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00008362 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008363
Jim Laskeybc588b82006-10-05 15:07:25 +00008364 // Look at each chain and determine if it is an alias. If so, add it to the
8365 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00008366 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00008367 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008368 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00008369 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008370
8371 // For TokenFactor nodes, look at each operand and only continue up the
8372 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +00008373 // find more and revert to original chain since the xform is unlikely to be
8374 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008375 //
8376 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +00008377 // chain we found before we hit a tokenfactor rather than the original
8378 // chain.
8379 if (Depth > 6 || Aliases.size() == 2) {
8380 Aliases.clear();
8381 Aliases.push_back(OriginalChain);
8382 break;
8383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008384
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008385 // Don't bother if we've been before.
8386 if (!Visited.insert(Chain.getNode()))
8387 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008388
Jim Laskeybc588b82006-10-05 15:07:25 +00008389 switch (Chain.getOpcode()) {
8390 case ISD::EntryToken:
8391 // Entry token is ideal chain operand, but handled in FindBetterChain.
8392 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008393
Jim Laskeybc588b82006-10-05 15:07:25 +00008394 case ISD::LOAD:
8395 case ISD::STORE: {
8396 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00008397 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008398 int64_t OpSize;
8399 const Value *OpSrcValue;
8400 int OpSrcValueOffset;
8401 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008402 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +00008403 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008404 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008405 OpSrcValueAlign,
8406 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +00008407
Jim Laskeybc588b82006-10-05 15:07:25 +00008408 // If chain is alias then stop here.
8409 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008410 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008411 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008412 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008413 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00008414 Aliases.push_back(Chain);
8415 } else {
8416 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00008417 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00008418 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00008419 }
Jim Laskeybc588b82006-10-05 15:07:25 +00008420 break;
8421 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008422
Jim Laskeybc588b82006-10-05 15:07:25 +00008423 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008424 // We have to check each of the operands of the token factor for "small"
8425 // token factors, so we queue them up. Adding the operands to the queue
8426 // (stack) in reverse order maintains the original order and increases the
8427 // likelihood that getNode will find a matching token factor (CSE.)
8428 if (Chain.getNumOperands() > 16) {
8429 Aliases.push_back(Chain);
8430 break;
8431 }
Jim Laskeybc588b82006-10-05 15:07:25 +00008432 for (unsigned n = Chain.getNumOperands(); n;)
8433 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00008434 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00008435 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00008436
Jim Laskeybc588b82006-10-05 15:07:25 +00008437 default:
8438 // For all other instructions we will just have to take what we can get.
8439 Aliases.push_back(Chain);
8440 break;
Jim Laskey279f0532006-09-25 16:29:54 +00008441 }
8442 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00008443}
8444
8445/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
8446/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00008447SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
8448 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00008449
Jim Laskey6ff23e52006-10-04 16:53:27 +00008450 // Accumulate all the aliases to this node.
8451 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00008452
Dan Gohman71dc7c92011-05-17 22:20:36 +00008453 // If no operands then chain to entry token.
8454 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +00008455 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +00008456
8457 // If a single operand then chain to it. We don't need to revisit it.
8458 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +00008459 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008460
Jim Laskey6ff23e52006-10-04 16:53:27 +00008461 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008462 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008463 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00008464}
8465
Nate Begeman1d4d4142005-09-01 00:19:25 +00008466// SelectionDAG::Combine - This is the entry point for the file.
8467//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00008468void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00008469 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00008470 /// run - This is the main entry point to this class.
8471 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00008472 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00008473}