Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1 | //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7 | // |
| 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 Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12 | // |
Dan Gohman | 4128700 | 2009-04-25 17:09:45 +0000 | [diff] [blame] | 13 | // 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 Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #define DEBUG_TYPE "dagcombine" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
| 36 | #include "llvm/Target/TargetMachine.h" |
| 37 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 41 | STATISTIC(NodesCombined , "Number of dag nodes combined"); |
| 42 | STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created"); |
| 43 | STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created"); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 44 | STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 45 | STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 46 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 47 | namespace { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
Owen Anderson | 0dcc814 | 2010-09-19 21:01:26 +0000 | [diff] [blame] | 49 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 50 | cl::desc("Turn on alias analysis during testing")); |
Jim Laskey | 3ad175b | 2006-10-12 15:22:24 +0000 | [diff] [blame] | 51 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
| 53 | CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, |
| 54 | cl::desc("Include global information in alias analysis")); |
| 55 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 56 | //------------------------------ DAGCombiner ---------------------------------// |
| 57 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 58 | class DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 59 | SelectionDAG &DAG; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 60 | const TargetLowering &TLI; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 61 | CombineLevel Level; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 62 | CodeGenOpt::Level OptLevel; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 63 | bool LegalOperations; |
| 64 | bool LegalTypes; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 65 | |
| 66 | // Worklist of all of the nodes that need to be simplified. |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 67 | // |
| 68 | // This has the semantics that when adding to the worklist, |
| 69 | // the item added must be next to be processed. It should |
| 70 | // also only appear once. The naive approach to this takes |
| 71 | // linear time. |
| 72 | // |
| 73 | // To reduce the insert/remove time to logarithmic, we use |
| 74 | // a set and a vector to maintain our worklist. |
| 75 | // |
| 76 | // The set contains the items on the worklist, but does not |
| 77 | // maintain the order they should be visited. |
| 78 | // |
| 79 | // The vector maintains the order nodes should be visited, but may |
| 80 | // contain duplicate or removed nodes. When choosing a node to |
| 81 | // visit, we pop off the order stack until we find an item that is |
| 82 | // also in the contents set. All operations are O(log N). |
| 83 | SmallPtrSet<SDNode*, 64> WorkListContents; |
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 84 | SmallVector<SDNode*, 64> WorkListOrder; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 85 | |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 86 | // AA - Used for DAG load/store alias analysis. |
| 87 | AliasAnalysis &AA; |
| 88 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 89 | /// 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 Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 95 | UI != UE; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 96 | AddToWorkList(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 99 | /// visit - call the node-specific routine that knows how to fold each |
| 100 | /// particular type of node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 101 | SDValue visit(SDNode *N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 103 | public: |
James Molloy | 6afa3f7 | 2012-02-16 09:48:07 +0000 | [diff] [blame] | 104 | /// AddToWorkList - Add to the work list making sure its instance is at the |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 105 | /// back (next to be processed.) |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 106 | void AddToWorkList(SDNode *N) { |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 107 | WorkListContents.insert(N); |
| 108 | WorkListOrder.push_back(N); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 109 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 110 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 111 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 112 | /// |
| 113 | void removeFromWorkList(SDNode *N) { |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 114 | WorkListContents.erase(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 115 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 116 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 117 | SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 118 | bool AddTo = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 119 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 120 | SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 121 | return CombineTo(N, &Res, 1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 122 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 123 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 124 | SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 125 | bool AddTo = true) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 126 | SDValue To[] = { Res0, Res1 }; |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 127 | return CombineTo(N, To, 2, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 128 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 129 | |
| 130 | void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 134 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 135 | /// it can be simplified or if things it uses can be simplified by bit |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 136 | /// propagation. If so, return true. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 137 | bool SimplifyDemandedBits(SDValue Op) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 138 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
| 139 | APInt Demanded = APInt::getAllOnesValue(BitWidth); |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 140 | return SimplifyDemandedBits(Op, Demanded); |
| 141 | } |
| 142 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 143 | bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 145 | bool CombineToPreIndexedLoadStore(SDNode *N); |
| 146 | bool CombineToPostIndexedLoadStore(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 147 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 148 | 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 Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 152 | SDValue PromoteIntBinOp(SDValue Op); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 153 | SDValue PromoteIntShiftOp(SDValue Op); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 154 | SDValue PromoteExtend(SDValue Op); |
| 155 | bool PromoteLoad(SDValue Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 156 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 157 | void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs, |
| 158 | SDValue Trunc, SDValue ExtLoad, DebugLoc DL, |
| 159 | ISD::NodeType ExtType); |
| 160 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 161 | /// 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 Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 164 | SDValue combine(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 165 | |
| 166 | // Visitation implementation - Implement dag node combining for different |
| 167 | // node types. The semantics are as follows: |
| 168 | // Return Value: |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 169 | // 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 Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 172 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 173 | 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 Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 178 | SDValue visitSUBC(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 179 | SDValue visitADDE(SDNode *N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 180 | SDValue visitSUBE(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 181 | 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 Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 190 | SDValue visitSMULO(SDNode *N); |
| 191 | SDValue visitUMULO(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 192 | SDValue visitSDIVREM(SDNode *N); |
| 193 | SDValue visitUDIVREM(SDNode *N); |
| 194 | SDValue visitAND(SDNode *N); |
| 195 | SDValue visitOR(SDNode *N); |
| 196 | SDValue visitXOR(SDNode *N); |
| 197 | SDValue SimplifyVBinOp(SDNode *N); |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 198 | SDValue SimplifyVUnaryOp(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 199 | SDValue visitSHL(SDNode *N); |
| 200 | SDValue visitSRA(SDNode *N); |
| 201 | SDValue visitSRL(SDNode *N); |
| 202 | SDValue visitCTLZ(SDNode *N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 203 | SDValue visitCTLZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 204 | SDValue visitCTTZ(SDNode *N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 205 | SDValue visitCTTZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 206 | SDValue visitCTPOP(SDNode *N); |
| 207 | SDValue visitSELECT(SDNode *N); |
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 208 | SDValue visitVSELECT(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 209 | SDValue visitSELECT_CC(SDNode *N); |
| 210 | SDValue visitSETCC(SDNode *N); |
| 211 | SDValue visitSIGN_EXTEND(SDNode *N); |
| 212 | SDValue visitZERO_EXTEND(SDNode *N); |
| 213 | SDValue visitANY_EXTEND(SDNode *N); |
| 214 | SDValue visitSIGN_EXTEND_INREG(SDNode *N); |
| 215 | SDValue visitTRUNCATE(SDNode *N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 216 | SDValue visitBITCAST(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 217 | SDValue visitBUILD_PAIR(SDNode *N); |
| 218 | SDValue visitFADD(SDNode *N); |
| 219 | SDValue visitFSUB(SDNode *N); |
| 220 | SDValue visitFMUL(SDNode *N); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 221 | SDValue visitFMA(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 222 | SDValue visitFDIV(SDNode *N); |
| 223 | SDValue visitFREM(SDNode *N); |
| 224 | SDValue visitFCOPYSIGN(SDNode *N); |
| 225 | SDValue visitSINT_TO_FP(SDNode *N); |
| 226 | SDValue visitUINT_TO_FP(SDNode *N); |
| 227 | SDValue visitFP_TO_SINT(SDNode *N); |
| 228 | SDValue visitFP_TO_UINT(SDNode *N); |
| 229 | SDValue visitFP_ROUND(SDNode *N); |
| 230 | SDValue visitFP_ROUND_INREG(SDNode *N); |
| 231 | SDValue visitFP_EXTEND(SDNode *N); |
| 232 | SDValue visitFNEG(SDNode *N); |
| 233 | SDValue visitFABS(SDNode *N); |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 234 | SDValue visitFCEIL(SDNode *N); |
| 235 | SDValue visitFTRUNC(SDNode *N); |
| 236 | SDValue visitFFLOOR(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 237 | SDValue visitBRCOND(SDNode *N); |
| 238 | SDValue visitBR_CC(SDNode *N); |
| 239 | SDValue visitLOAD(SDNode *N); |
| 240 | SDValue visitSTORE(SDNode *N); |
| 241 | SDValue visitINSERT_VECTOR_ELT(SDNode *N); |
| 242 | SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); |
| 243 | SDValue visitBUILD_VECTOR(SDNode *N); |
| 244 | SDValue visitCONCAT_VECTORS(SDNode *N); |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 245 | SDValue visitEXTRACT_SUBVECTOR(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 246 | SDValue visitVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 247 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 248 | SDValue XformToShuffleWithZero(SDNode *N); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 249 | SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 250 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 251 | SDValue visitShiftByConstant(SDNode *N, unsigned Amt); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 252 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 253 | bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); |
| 254 | SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 255 | SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 256 | SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2, |
| 257 | SDValue N3, ISD::CondCode CC, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 258 | bool NotExtCompare = false); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 259 | SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 260 | DebugLoc DL, bool foldBooleans = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 261 | SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 262 | unsigned HiOp); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 263 | SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 264 | SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 265 | SDValue BuildSDIV(SDNode *N); |
| 266 | SDValue BuildUDIV(SDNode *N); |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 267 | SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 268 | bool DemandHighBits = true); |
| 269 | SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 270 | SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 271 | SDValue ReduceLoadWidth(SDNode *N); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 272 | SDValue ReduceLoadOpStoreWidth(SDNode *N); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 273 | SDValue TransformFPLoadStorePair(SDNode *N); |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 274 | SDValue reduceBuildVecExtToExtBuildVec(SDNode *N); |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 275 | SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 276 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 277 | SDValue GetDemandedBits(SDValue V, const APInt &Mask); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 278 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 279 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 280 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 281 | void GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 282 | SmallVector<SDValue, 8> &Aliases); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 283 | |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 284 | /// isAlias - Return true if there is any possibility that the two addresses |
| 285 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 286 | bool isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 287 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 288 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 289 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 290 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 291 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 292 | unsigned SrcValueAlign2, |
| 293 | const MDNode *TBAAInfo2) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 294 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 295 | /// isAlias - Return true if there is any possibility that the two addresses |
| 296 | /// overlap. |
| 297 | bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1); |
| 298 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 299 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 300 | /// node. Returns true if the operand was a load. |
| 301 | bool FindAliasInfo(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 302 | SDValue &Ptr, int64_t &Size, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 303 | const Value *&SrcValue, int &SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 304 | unsigned &SrcValueAlignment, |
| 305 | const MDNode *&TBAAInfo) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 306 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 307 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 308 | /// looking for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 309 | SDValue FindBetterChain(SDNode *N, SDValue Chain); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 310 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 311 | /// Merge consecutive store operations into a wide store. |
| 312 | /// This optimization uses wide integers or vectors when possible. |
| 313 | /// \return True if some memory operations were changed. |
| 314 | bool MergeConsecutiveStores(StoreSDNode *N); |
| 315 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 316 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 317 | DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 318 | : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 319 | OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 320 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 321 | /// Run - runs the dag combiner on all nodes in the work list |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 322 | void Run(CombineLevel AtLevel); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 324 | SelectionDAG &getDAG() const { return DAG; } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 326 | /// getShiftAmountTy - Returns a type large enough to hold any valid |
| 327 | /// shift amount - before type legalization these can be huge. |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 328 | EVT getShiftAmountTy(EVT LHSTy) { |
| 329 | return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy(); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 330 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 332 | /// isTypeLegal - This method returns true if we are running before type |
| 333 | /// legalization or if the specified VT is legal. |
| 334 | bool isTypeLegal(const EVT &VT) { |
| 335 | if (!LegalTypes) return true; |
| 336 | return TLI.isTypeLegal(VT); |
| 337 | } |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 338 | |
| 339 | /// getSetCCResultType - Convenience wrapper around |
| 340 | /// TargetLowering::getSetCCResultType |
| 341 | EVT getSetCCResultType(EVT VT) const { |
| 342 | return TLI.getSetCCResultType(*DAG.getContext(), VT); |
| 343 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 344 | }; |
| 345 | } |
| 346 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 347 | |
| 348 | namespace { |
| 349 | /// WorkListRemover - This class is a DAGUpdateListener that removes any deleted |
| 350 | /// nodes from the worklist. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 351 | class WorkListRemover : public SelectionDAG::DAGUpdateListener { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 352 | DAGCombiner &DC; |
| 353 | public: |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 354 | explicit WorkListRemover(DAGCombiner &dc) |
| 355 | : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 356 | |
Duncan Sands | edfcf59 | 2008-06-11 11:42:12 +0000 | [diff] [blame] | 357 | virtual void NodeDeleted(SDNode *N, SDNode *E) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 358 | DC.removeFromWorkList(N); |
| 359 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 360 | }; |
| 361 | } |
| 362 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 363 | //===----------------------------------------------------------------------===// |
| 364 | // TargetLowering::DAGCombinerInfo implementation |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 368 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 369 | } |
| 370 | |
Cameron Zwarich | ed3caf9 | 2011-04-02 02:40:26 +0000 | [diff] [blame] | 371 | void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) { |
| 372 | ((DAGCombiner*)DC)->removeFromWorkList(N); |
| 373 | } |
| 374 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 375 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 376 | CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) { |
| 377 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 380 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 381 | CombineTo(SDNode *N, SDValue Res, bool AddTo) { |
| 382 | return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 386 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 387 | CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { |
| 388 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 391 | void TargetLowering::DAGCombinerInfo:: |
| 392 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
| 393 | return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); |
| 394 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 396 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 397 | // Helper Functions |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | |
| 400 | /// isNegatibleForFree - Return 1 if we can compute the negated form of the |
| 401 | /// specified expression for the same cost as the expression itself, or 2 if we |
| 402 | /// can compute the negated form more cheaply than the expression itself. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 403 | static char isNegatibleForFree(SDValue Op, bool LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 404 | const TargetLowering &TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 405 | const TargetOptions *Options, |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 406 | unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 407 | // fneg is removable even if it has multiple uses. |
| 408 | if (Op.getOpcode() == ISD::FNEG) return 2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 410 | // Don't allow anything with multiple uses. |
| 411 | if (!Op.hasOneUse()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 413 | // Don't recurse exponentially. |
| 414 | if (Depth > 6) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 416 | switch (Op.getOpcode()) { |
| 417 | default: return false; |
| 418 | case ISD::ConstantFP: |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 419 | // Don't invert constant FP values after legalize. The negated constant |
| 420 | // isn't necessarily legal. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 421 | return LegalOperations ? 0 : 1; |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 422 | case ISD::FADD: |
| 423 | // FIXME: determine better conditions for this xform. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 424 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 425 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 426 | // After operation legalization, it might not be legal to create new FSUBs. |
| 427 | if (LegalOperations && |
| 428 | !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) |
| 429 | return 0; |
| 430 | |
Craig Topper | 956342b | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 431 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 432 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
| 433 | Options, Depth + 1)) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 434 | return V; |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 435 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 436 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 437 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 438 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 439 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 440 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 441 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 442 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 443 | return 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 445 | case ISD::FMUL: |
| 446 | case ISD::FDIV: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 447 | if (Options->HonorSignDependentRoundingFPMath()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 448 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 449 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y)) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 450 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
| 451 | Options, Depth + 1)) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 452 | return V; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 453 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 454 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 455 | Depth + 1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 457 | case ISD::FP_EXTEND: |
| 458 | case ISD::FP_ROUND: |
| 459 | case ISD::FSIN: |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 460 | return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 461 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | /// GetNegatedExpression - If isNegatibleForFree returns true, this function |
| 466 | /// returns the newly negated expression. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 467 | static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 468 | bool LegalOperations, unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 469 | // fneg is removable even if it has multiple uses. |
| 470 | if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 472 | // Don't allow anything with multiple uses. |
| 473 | assert(Op.hasOneUse() && "Unknown reuse!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 475 | assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 476 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 477 | default: llvm_unreachable("Unknown code"); |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 478 | case ISD::ConstantFP: { |
| 479 | APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); |
| 480 | V.changeSign(); |
| 481 | return DAG.getConstantFP(V, Op.getValueType()); |
| 482 | } |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 483 | case ISD::FADD: |
| 484 | // FIXME: determine better conditions for this xform. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 485 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 486 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 487 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 488 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 489 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 490 | &DAG.getTarget().Options, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 491 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 492 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 493 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 494 | Op.getOperand(1)); |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 495 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 496 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 497 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 498 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 499 | Op.getOperand(0)); |
| 500 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 501 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 502 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 503 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 504 | // fold (fneg (fsub 0, B)) -> B |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 505 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 506 | if (N0CFP->getValueAPF().isZero()) |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 507 | return Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 508 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 509 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 510 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
| 511 | Op.getOperand(1), Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 513 | case ISD::FMUL: |
| 514 | case ISD::FDIV: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 515 | assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 516 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 517 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 518 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 519 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 520 | &DAG.getTarget().Options, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 521 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 522 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 523 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 524 | Op.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 525 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 526 | // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 527 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 528 | Op.getOperand(0), |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 529 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 530 | LegalOperations, Depth+1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 532 | case ISD::FP_EXTEND: |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 533 | case ISD::FSIN: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 534 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 535 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 536 | LegalOperations, Depth+1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 537 | case ISD::FP_ROUND: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 538 | return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 539 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 540 | LegalOperations, Depth+1), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 541 | Op.getOperand(1)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 544 | |
| 545 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 546 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 547 | // that selects between the values 1 and 0, making it equivalent to a setcc. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 548 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 549 | // nodes based on the type of node we are checking. This simplifies life a |
| 550 | // bit for the callers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 551 | static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, |
| 552 | SDValue &CC) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 553 | if (N.getOpcode() == ISD::SETCC) { |
| 554 | LHS = N.getOperand(0); |
| 555 | RHS = N.getOperand(1); |
| 556 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 557 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 558 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 559 | if (N.getOpcode() == ISD::SELECT_CC && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 560 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 561 | N.getOperand(3).getOpcode() == ISD::Constant && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 562 | cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 563 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 564 | LHS = N.getOperand(0); |
| 565 | RHS = N.getOperand(1); |
| 566 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 567 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 568 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 572 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 573 | // one use. If this is true, it allows the users to invert the operation for |
| 574 | // free when it is profitable to do so. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 575 | static bool isOneUseSetCC(SDValue N) { |
| 576 | SDValue N0, N1, N2; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 577 | if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 578 | return true; |
| 579 | return false; |
| 580 | } |
| 581 | |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 582 | SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL, |
| 583 | SDValue N0, SDValue N1) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 584 | EVT VT = N0.getValueType(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 585 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
| 586 | if (isa<ConstantSDNode>(N1)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 587 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 588 | SDValue OpNode = |
| 589 | DAG.FoldConstantArithmetic(Opc, VT, |
| 590 | cast<ConstantSDNode>(N0.getOperand(1)), |
| 591 | cast<ConstantSDNode>(N1)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 592 | return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 593 | } |
| 594 | if (N0.hasOneUse()) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 595 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 596 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
| 597 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 598 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 599 | return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 602 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 603 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
| 604 | if (isa<ConstantSDNode>(N0)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 605 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 606 | SDValue OpNode = |
| 607 | DAG.FoldConstantArithmetic(Opc, VT, |
| 608 | cast<ConstantSDNode>(N1.getOperand(1)), |
| 609 | cast<ConstantSDNode>(N0)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 610 | return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 611 | } |
| 612 | if (N1.hasOneUse()) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 613 | // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 614 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 615 | N1.getOperand(0), N0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 616 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 617 | return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 620 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 621 | return SDValue(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 624 | SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
| 625 | bool AddTo) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 626 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
| 627 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 628 | DEBUG(dbgs() << "\nReplacing.1 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 629 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 630 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 631 | To[0].getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 632 | dbgs() << " and " << NumTo-1 << " other values\n"; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 633 | for (unsigned i = 0, e = NumTo; i != e; ++i) |
Jakob Stoklund Olesen | 9f0d4e6 | 2009-12-03 05:15:35 +0000 | [diff] [blame] | 634 | assert((!To[i].getNode() || |
| 635 | N->getValueType(i) == To[i].getValueType()) && |
Dan Gohman | 764fd0c | 2009-01-21 15:17:51 +0000 | [diff] [blame] | 636 | "Cannot combine value to value of different type!")); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 637 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 638 | DAG.ReplaceAllUsesWith(N, To); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 639 | if (AddTo) { |
| 640 | // Push the new nodes and any users onto the worklist |
| 641 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 642 | if (To[i].getNode()) { |
| 643 | AddToWorkList(To[i].getNode()); |
| 644 | AddUsersToWorkList(To[i].getNode()); |
| 645 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 648 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 649 | // Finally, if the node is now dead, remove it from the graph. The node |
| 650 | // may not be dead if the replacement process recursively simplified to |
| 651 | // something else needing this node. |
| 652 | if (N->use_empty()) { |
| 653 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 654 | // process a node that has been replaced. |
| 655 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 656 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 657 | // Finally, since the node is now dead, remove it from the graph. |
| 658 | DAG.DeleteNode(N); |
| 659 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 660 | return SDValue(N, 0); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 663 | void DAGCombiner:: |
| 664 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 665 | // Replace all uses. If any nodes become isomorphic to other nodes and |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 666 | // are deleted, make sure to remove them from our worklist. |
| 667 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 668 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 669 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 670 | // Push the new node and any (possibly new) users onto the worklist. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 671 | AddToWorkList(TLO.New.getNode()); |
| 672 | AddUsersToWorkList(TLO.New.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 673 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 674 | // Finally, if the node is now dead, remove it from the graph. The node |
| 675 | // may not be dead if the replacement process recursively simplified to |
| 676 | // something else needing this node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 677 | if (TLO.Old.getNode()->use_empty()) { |
| 678 | removeFromWorkList(TLO.Old.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 679 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 680 | // If the operands of this node are only used by the node, they will now |
| 681 | // be dead. Make sure to visit them first to delete dead nodes early. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 682 | for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i) |
| 683 | if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse()) |
| 684 | AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 685 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 686 | DAG.DeleteNode(TLO.Old.getNode()); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 687 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
| 691 | /// it can be simplified or if things it uses can be simplified by bit |
| 692 | /// propagation. If so, return true. |
| 693 | bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 694 | TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 695 | APInt KnownZero, KnownOne; |
| 696 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 697 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 698 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 699 | // Revisit the node. |
| 700 | AddToWorkList(Op.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 701 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 702 | // Replace the old value with the new one. |
| 703 | ++NodesCombined; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 704 | DEBUG(dbgs() << "\nReplacing.2 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 705 | TLO.Old.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 706 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 707 | TLO.New.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 708 | dbgs() << '\n'); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 709 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 710 | CommitTargetLoweringOpt(TLO); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 714 | void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { |
| 715 | DebugLoc dl = Load->getDebugLoc(); |
| 716 | EVT VT = Load->getValueType(0); |
| 717 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 718 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 719 | DEBUG(dbgs() << "\nReplacing.9 "; |
| 720 | Load->dump(&DAG); |
| 721 | dbgs() << "\nWith: "; |
| 722 | Trunc.getNode()->dump(&DAG); |
| 723 | dbgs() << '\n'); |
| 724 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 725 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc); |
| 726 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1)); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 727 | removeFromWorkList(Load); |
| 728 | DAG.DeleteNode(Load); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 729 | AddToWorkList(Trunc.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { |
| 733 | Replace = false; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 734 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 735 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 736 | EVT MemVT = LD->getMemoryVT(); |
| 737 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 738 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 739 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 740 | : LD->getExtensionType(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 741 | Replace = true; |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 742 | return DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 743 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 744 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 745 | MemVT, LD->isVolatile(), |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 746 | LD->isNonTemporal(), LD->getAlignment()); |
| 747 | } |
| 748 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 749 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 750 | switch (Opc) { |
| 751 | default: break; |
| 752 | case ISD::AssertSext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 753 | return DAG.getNode(ISD::AssertSext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 754 | SExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 755 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 756 | case ISD::AssertZext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 757 | return DAG.getNode(ISD::AssertZext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 758 | ZExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 759 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 760 | case ISD::Constant: { |
| 761 | unsigned ExtOpc = |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 762 | Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 763 | return DAG.getNode(ExtOpc, dl, PVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 764 | } |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 768 | return SDValue(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 769 | return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 772 | SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 773 | if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) |
| 774 | return SDValue(); |
| 775 | EVT OldVT = Op.getValueType(); |
| 776 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 777 | bool Replace = false; |
| 778 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 779 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 780 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 781 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 782 | |
| 783 | if (Replace) |
| 784 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 785 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 786 | DAG.getValueType(OldVT)); |
| 787 | } |
| 788 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 789 | SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 790 | EVT OldVT = Op.getValueType(); |
| 791 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 792 | bool Replace = false; |
| 793 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 794 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 795 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 796 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 797 | |
| 798 | if (Replace) |
| 799 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 800 | return DAG.getZeroExtendInReg(NewOp, dl, OldVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 803 | /// PromoteIntBinOp - Promote the specified integer binary operation if the |
| 804 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 805 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 806 | SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { |
| 807 | if (!LegalOperations) |
| 808 | return SDValue(); |
| 809 | |
| 810 | EVT VT = Op.getValueType(); |
| 811 | if (VT.isVector() || !VT.isInteger()) |
| 812 | return SDValue(); |
| 813 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 814 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 815 | // promoting it. |
| 816 | unsigned Opc = Op.getOpcode(); |
| 817 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 818 | return SDValue(); |
| 819 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 820 | EVT PVT = VT; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 821 | // Consult target whether it is a good idea to promote this operation and |
| 822 | // what's the right type to promote it to. |
| 823 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 824 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 825 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 826 | bool Replace0 = false; |
| 827 | SDValue N0 = Op.getOperand(0); |
| 828 | SDValue NN0 = PromoteOperand(N0, PVT, Replace0); |
| 829 | if (NN0.getNode() == 0) |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 830 | return SDValue(); |
| 831 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 832 | bool Replace1 = false; |
| 833 | SDValue N1 = Op.getOperand(1); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 834 | SDValue NN1; |
| 835 | if (N0 == N1) |
| 836 | NN1 = NN0; |
| 837 | else { |
| 838 | NN1 = PromoteOperand(N1, PVT, Replace1); |
| 839 | if (NN1.getNode() == 0) |
| 840 | return SDValue(); |
| 841 | } |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 842 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 843 | AddToWorkList(NN0.getNode()); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 844 | if (NN1.getNode()) |
| 845 | AddToWorkList(NN1.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 846 | |
| 847 | if (Replace0) |
| 848 | ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); |
| 849 | if (Replace1) |
| 850 | ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 851 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 852 | DEBUG(dbgs() << "\nPromoting "; |
| 853 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 854 | DebugLoc dl = Op.getDebugLoc(); |
| 855 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 856 | DAG.getNode(Opc, dl, PVT, NN0, NN1)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 857 | } |
| 858 | return SDValue(); |
| 859 | } |
| 860 | |
| 861 | /// PromoteIntShiftOp - Promote the specified integer shift operation if the |
| 862 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 863 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 864 | SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { |
| 865 | if (!LegalOperations) |
| 866 | return SDValue(); |
| 867 | |
| 868 | EVT VT = Op.getValueType(); |
| 869 | if (VT.isVector() || !VT.isInteger()) |
| 870 | return SDValue(); |
| 871 | |
| 872 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 873 | // promoting it. |
| 874 | unsigned Opc = Op.getOpcode(); |
| 875 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 876 | return SDValue(); |
| 877 | |
| 878 | EVT PVT = VT; |
| 879 | // Consult target whether it is a good idea to promote this operation and |
| 880 | // what's the right type to promote it to. |
| 881 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 882 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 883 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 884 | bool Replace = false; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 885 | SDValue N0 = Op.getOperand(0); |
| 886 | if (Opc == ISD::SRA) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 887 | N0 = SExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 888 | else if (Opc == ISD::SRL) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 889 | N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 890 | else |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 891 | N0 = PromoteOperand(N0, PVT, Replace); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 892 | if (N0.getNode() == 0) |
| 893 | return SDValue(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 894 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 895 | AddToWorkList(N0.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 896 | if (Replace) |
| 897 | ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 898 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 899 | DEBUG(dbgs() << "\nPromoting "; |
| 900 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 901 | DebugLoc dl = Op.getDebugLoc(); |
| 902 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 903 | DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1))); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 904 | } |
| 905 | return SDValue(); |
| 906 | } |
| 907 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 908 | SDValue DAGCombiner::PromoteExtend(SDValue Op) { |
| 909 | if (!LegalOperations) |
| 910 | return SDValue(); |
| 911 | |
| 912 | EVT VT = Op.getValueType(); |
| 913 | if (VT.isVector() || !VT.isInteger()) |
| 914 | return SDValue(); |
| 915 | |
| 916 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 917 | // promoting it. |
| 918 | unsigned Opc = Op.getOpcode(); |
| 919 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 920 | return SDValue(); |
| 921 | |
| 922 | EVT PVT = VT; |
| 923 | // Consult target whether it is a good idea to promote this operation and |
| 924 | // what's the right type to promote it to. |
| 925 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 926 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 927 | // fold (aext (aext x)) -> (aext x) |
| 928 | // fold (aext (zext x)) -> (zext x) |
| 929 | // fold (aext (sext x)) -> (sext x) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 930 | DEBUG(dbgs() << "\nPromoting "; |
| 931 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 932 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0)); |
| 933 | } |
| 934 | return SDValue(); |
| 935 | } |
| 936 | |
| 937 | bool DAGCombiner::PromoteLoad(SDValue Op) { |
| 938 | if (!LegalOperations) |
| 939 | return false; |
| 940 | |
| 941 | EVT VT = Op.getValueType(); |
| 942 | if (VT.isVector() || !VT.isInteger()) |
| 943 | return false; |
| 944 | |
| 945 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 946 | // promoting it. |
| 947 | unsigned Opc = Op.getOpcode(); |
| 948 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 949 | return false; |
| 950 | |
| 951 | EVT PVT = VT; |
| 952 | // Consult target whether it is a good idea to promote this operation and |
| 953 | // what's the right type to promote it to. |
| 954 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 955 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 956 | |
| 957 | DebugLoc dl = Op.getDebugLoc(); |
| 958 | SDNode *N = Op.getNode(); |
| 959 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 960 | EVT MemVT = LD->getMemoryVT(); |
| 961 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 962 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 963 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 964 | : LD->getExtensionType(); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 965 | SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 966 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 967 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 968 | MemVT, LD->isVolatile(), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 969 | LD->isNonTemporal(), LD->getAlignment()); |
| 970 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD); |
| 971 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 972 | DEBUG(dbgs() << "\nPromoting "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 973 | N->dump(&DAG); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 974 | dbgs() << "\nTo: "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 975 | Result.getNode()->dump(&DAG); |
| 976 | dbgs() << '\n'); |
| 977 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 978 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
| 979 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 980 | removeFromWorkList(N); |
| 981 | DAG.DeleteNode(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 982 | AddToWorkList(Result.getNode()); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 983 | return true; |
| 984 | } |
| 985 | return false; |
| 986 | } |
| 987 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 989 | //===----------------------------------------------------------------------===// |
| 990 | // Main DAG Combiner implementation |
| 991 | //===----------------------------------------------------------------------===// |
| 992 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 993 | void DAGCombiner::Run(CombineLevel AtLevel) { |
| 994 | // set the instance variables, so that the various visit routines may use it. |
| 995 | Level = AtLevel; |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 996 | LegalOperations = Level >= AfterLegalizeVectorOps; |
| 997 | LegalTypes = Level >= AfterLegalizeTypes; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 998 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 999 | // Add all the dag nodes to the worklist. |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1000 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 1001 | E = DAG.allnodes_end(); I != E; ++I) |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1002 | AddToWorkList(I); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1003 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1004 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 1005 | // to the root node, preventing it from being deleted, and tracking any |
| 1006 | // changes of the root. |
| 1007 | HandleSDNode Dummy(DAG.getRoot()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1008 | |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 1009 | // The root of the dag may dangle to deleted nodes until the dag combiner is |
| 1010 | // done. Set it to null to avoid confusion. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1011 | DAG.setRoot(SDValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1012 | |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1013 | // while the worklist isn't empty, find a node and |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1014 | // try and combine it. |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1015 | while (!WorkListContents.empty()) { |
| 1016 | SDNode *N; |
| 1017 | // The WorkListOrder holds the SDNodes in order, but it may contain duplicates. |
| 1018 | // In order to avoid a linear scan, we use a set (O(log N)) to hold what the |
| 1019 | // worklist *should* contain, and check the node we want to visit is should |
| 1020 | // actually be visited. |
| 1021 | do { |
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 1022 | N = WorkListOrder.pop_back_val(); |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1023 | } while (!WorkListContents.erase(N)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1024 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1025 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
| 1026 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 1027 | // reduced number of uses, allowing other xforms. |
| 1028 | if (N->use_empty() && N != &Dummy) { |
| 1029 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1030 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1031 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1032 | DAG.DeleteNode(N); |
| 1033 | continue; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1034 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1035 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1036 | SDValue RV = combine(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1037 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1038 | if (RV.getNode() == 0) |
| 1039 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1040 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1041 | ++NodesCombined; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1042 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1043 | // If we get back the same node we passed in, rather than a new node or |
| 1044 | // zero, we know that the node must have defined multiple values and |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1045 | // CombineTo was used. Since CombineTo takes care of the worklist |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1046 | // mechanics for us, we have no work to do in this case. |
| 1047 | if (RV.getNode() == N) |
| 1048 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1049 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1050 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1051 | RV.getNode()->getOpcode() != ISD::DELETED_NODE && |
| 1052 | "Node was deleted but visit returned new node!"); |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1053 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1054 | DEBUG(dbgs() << "\nReplacing.3 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1055 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1056 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1057 | RV.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1058 | dbgs() << '\n'); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1059 | |
Devang Patel | 9728ea2 | 2011-05-23 22:04:42 +0000 | [diff] [blame] | 1060 | // Transfer debug value. |
| 1061 | DAG.TransferDbgValues(SDValue(N, 0), RV); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1062 | WorkListRemover DeadNodes(*this); |
| 1063 | if (N->getNumValues() == RV.getNode()->getNumValues()) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1064 | DAG.ReplaceAllUsesWith(N, RV.getNode()); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1065 | else { |
| 1066 | assert(N->getValueType(0) == RV.getValueType() && |
| 1067 | N->getNumValues() == 1 && "Type mismatch"); |
| 1068 | SDValue OpV = RV; |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1069 | DAG.ReplaceAllUsesWith(N, &OpV); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1070 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1071 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1072 | // Push the new node and any users onto the worklist |
| 1073 | AddToWorkList(RV.getNode()); |
| 1074 | AddUsersToWorkList(RV.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1075 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1076 | // Add any uses of the old node to the worklist in case this node is the |
| 1077 | // last one that uses them. They may become dead after this node is |
| 1078 | // deleted. |
| 1079 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1080 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1082 | // Finally, if the node is now dead, remove it from the graph. The node |
| 1083 | // may not be dead if the replacement process recursively simplified to |
| 1084 | // something else needing this node. |
| 1085 | if (N->use_empty()) { |
| 1086 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 1087 | // process a node that has been replaced. |
| 1088 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1089 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1090 | // Finally, since the node is now dead, remove it from the graph. |
| 1091 | DAG.DeleteNode(N); |
| 1092 | } |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1093 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1094 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 1095 | // If the root changed (e.g. it was a dead load, update the root). |
| 1096 | DAG.setRoot(Dummy.getValue()); |
Hal Finkel | 31490ba | 2012-04-16 03:33:22 +0000 | [diff] [blame] | 1097 | DAG.RemoveDeadNodes(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1100 | SDValue DAGCombiner::visit(SDNode *N) { |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1101 | switch (N->getOpcode()) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1102 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 1103 | case ISD::TokenFactor: return visitTokenFactor(N); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1104 | case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1105 | case ISD::ADD: return visitADD(N); |
| 1106 | case ISD::SUB: return visitSUB(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1107 | case ISD::ADDC: return visitADDC(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1108 | case ISD::SUBC: return visitSUBC(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1109 | case ISD::ADDE: return visitADDE(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1110 | case ISD::SUBE: return visitSUBE(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1111 | case ISD::MUL: return visitMUL(N); |
| 1112 | case ISD::SDIV: return visitSDIV(N); |
| 1113 | case ISD::UDIV: return visitUDIV(N); |
| 1114 | case ISD::SREM: return visitSREM(N); |
| 1115 | case ISD::UREM: return visitUREM(N); |
| 1116 | case ISD::MULHU: return visitMULHU(N); |
| 1117 | case ISD::MULHS: return visitMULHS(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1118 | case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); |
| 1119 | case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 1120 | case ISD::SMULO: return visitSMULO(N); |
| 1121 | case ISD::UMULO: return visitUMULO(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1122 | case ISD::SDIVREM: return visitSDIVREM(N); |
| 1123 | case ISD::UDIVREM: return visitUDIVREM(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1124 | case ISD::AND: return visitAND(N); |
| 1125 | case ISD::OR: return visitOR(N); |
| 1126 | case ISD::XOR: return visitXOR(N); |
| 1127 | case ISD::SHL: return visitSHL(N); |
| 1128 | case ISD::SRA: return visitSRA(N); |
| 1129 | case ISD::SRL: return visitSRL(N); |
| 1130 | case ISD::CTLZ: return visitCTLZ(N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1131 | case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1132 | case ISD::CTTZ: return visitCTTZ(N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1133 | case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1134 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1135 | case ISD::SELECT: return visitSELECT(N); |
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 1136 | case ISD::VSELECT: return visitVSELECT(N); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1137 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 1138 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1139 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 1140 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1141 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1142 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 1143 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1144 | case ISD::BITCAST: return visitBITCAST(N); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 1145 | case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1146 | case ISD::FADD: return visitFADD(N); |
| 1147 | case ISD::FSUB: return visitFSUB(N); |
| 1148 | case ISD::FMUL: return visitFMUL(N); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 1149 | case ISD::FMA: return visitFMA(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1150 | case ISD::FDIV: return visitFDIV(N); |
| 1151 | case ISD::FREM: return visitFREM(N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 1152 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1153 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 1154 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 1155 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 1156 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 1157 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 1158 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 1159 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 1160 | case ISD::FNEG: return visitFNEG(N); |
| 1161 | case ISD::FABS: return visitFABS(N); |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1162 | case ISD::FFLOOR: return visitFFLOOR(N); |
| 1163 | case ISD::FCEIL: return visitFCEIL(N); |
| 1164 | case ISD::FTRUNC: return visitFTRUNC(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1165 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1166 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 1167 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 1168 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 1169 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 1170 | case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1171 | case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); |
| 1172 | case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 1173 | case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 1174 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1175 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1176 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1179 | SDValue DAGCombiner::combine(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1180 | SDValue RV = visit(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1181 | |
| 1182 | // If nothing happened, try a target-specific DAG combine. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1183 | if (RV.getNode() == 0) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1184 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1185 | "Node was deleted but visit returned NULL!"); |
| 1186 | |
| 1187 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 1188 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { |
| 1189 | |
| 1190 | // Expose the DAG combiner to the target combiner impls. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1191 | TargetLowering::DAGCombinerInfo |
Nadav Rotem | 444b4bf | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 1192 | DagCombineInfo(DAG, Level, false, this); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1193 | |
| 1194 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 1195 | } |
| 1196 | } |
| 1197 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1198 | // If nothing happened still, try promoting the operation. |
| 1199 | if (RV.getNode() == 0) { |
| 1200 | switch (N->getOpcode()) { |
| 1201 | default: break; |
| 1202 | case ISD::ADD: |
| 1203 | case ISD::SUB: |
| 1204 | case ISD::MUL: |
| 1205 | case ISD::AND: |
| 1206 | case ISD::OR: |
| 1207 | case ISD::XOR: |
| 1208 | RV = PromoteIntBinOp(SDValue(N, 0)); |
| 1209 | break; |
| 1210 | case ISD::SHL: |
| 1211 | case ISD::SRA: |
| 1212 | case ISD::SRL: |
| 1213 | RV = PromoteIntShiftOp(SDValue(N, 0)); |
| 1214 | break; |
| 1215 | case ISD::SIGN_EXTEND: |
| 1216 | case ISD::ZERO_EXTEND: |
| 1217 | case ISD::ANY_EXTEND: |
| 1218 | RV = PromoteExtend(SDValue(N, 0)); |
| 1219 | break; |
| 1220 | case ISD::LOAD: |
| 1221 | if (PromoteLoad(SDValue(N, 0))) |
| 1222 | RV = SDValue(N, 0); |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1227 | // If N is a commutative binary node, try commuting it to enable more |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1228 | // sdisel CSE. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1229 | if (RV.getNode() == 0 && |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1230 | SelectionDAG::isCommutativeBinOp(N->getOpcode()) && |
| 1231 | N->getNumValues() == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1232 | SDValue N0 = N->getOperand(0); |
| 1233 | SDValue N1 = N->getOperand(1); |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1234 | |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1235 | // Constant operands are canonicalized to RHS. |
| 1236 | if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1237 | SDValue Ops[] = { N1, N0 }; |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1238 | SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), |
| 1239 | Ops, 2); |
Evan Cheng | ea10046 | 2008-03-24 23:55:16 +0000 | [diff] [blame] | 1240 | if (CSENode) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1241 | return SDValue(CSENode, 0); |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1245 | return RV; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1246 | } |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1248 | /// getInputChainForNode - Given a node, return its input chain if it has one, |
| 1249 | /// otherwise return a null sd operand. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1250 | static SDValue getInputChainForNode(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1251 | if (unsigned NumOps = N->getNumOperands()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1252 | if (N->getOperand(0).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1253 | return N->getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1254 | else if (N->getOperand(NumOps-1).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1255 | return N->getOperand(NumOps-1); |
| 1256 | for (unsigned i = 1; i < NumOps-1; ++i) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1257 | if (N->getOperand(i).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1258 | return N->getOperand(i); |
| 1259 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1260 | return SDValue(); |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1263 | SDValue DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1264 | // If N has two operands, where one has an input chain equal to the other, |
| 1265 | // the 'other' chain is redundant. |
| 1266 | if (N->getNumOperands() == 2) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1267 | if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1268 | return N->getOperand(0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1269 | if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1270 | return N->getOperand(1); |
| 1271 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1273 | SmallVector<SDNode *, 8> TFs; // List of token factors to visit. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1274 | SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1275 | SmallPtrSet<SDNode*, 16> SeenOps; |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1276 | bool Changed = false; // If we should replace this token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1277 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1278 | // Start out with this token factor. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1279 | TFs.push_back(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1280 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 1281 | // Iterate through token factors. The TFs grows when new token factors are |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1282 | // encountered. |
| 1283 | for (unsigned i = 0; i < TFs.size(); ++i) { |
| 1284 | SDNode *TF = TFs[i]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1285 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1286 | // Check each of the operands. |
| 1287 | for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1288 | SDValue Op = TF->getOperand(i); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1289 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1290 | switch (Op.getOpcode()) { |
| 1291 | case ISD::EntryToken: |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1292 | // Entry tokens don't need to be added to the list. They are |
| 1293 | // rededundant. |
| 1294 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1295 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1296 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1297 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 1298 | if (Op.hasOneUse() && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1299 | std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1300 | // Queue up for processing. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1301 | TFs.push_back(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1302 | // Clean up in case the token factor is removed. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1303 | AddToWorkList(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1304 | Changed = true; |
| 1305 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1306 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1307 | // Fall thru |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1308 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1309 | default: |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1310 | // Only add if it isn't already in the list. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1311 | if (SeenOps.insert(Op.getNode())) |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1312 | Ops.push_back(Op); |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1313 | else |
| 1314 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1315 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1318 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1319 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1320 | SDValue Result; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1321 | |
| 1322 | // If we've change things around then replace token factor. |
| 1323 | if (Changed) { |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1324 | if (Ops.empty()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1325 | // The entry token is the only possible outcome. |
| 1326 | Result = DAG.getEntryNode(); |
| 1327 | } else { |
| 1328 | // New and improved token factor. |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1329 | Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1330 | MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1331 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1332 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 1333 | // Don't add users to work list. |
| 1334 | return CombineTo(N, Result, false); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1335 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1336 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1337 | return Result; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1340 | /// MERGE_VALUES can always be eliminated. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1341 | SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1342 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1343 | // Replacing results may cause a different MERGE_VALUES to suddenly |
| 1344 | // be CSE'd with N, and carry its uses with it. Iterate until no |
| 1345 | // uses remain, to ensure that the node can be safely deleted. |
Pete Cooper | 3affd9e | 2012-06-20 19:35:43 +0000 | [diff] [blame] | 1346 | // First add the users of this node to the work list so that they |
| 1347 | // can be tried again once they have new operands. |
| 1348 | AddUsersToWorkList(N); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1349 | do { |
| 1350 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1351 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i)); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1352 | } while (!N->use_empty()); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1353 | removeFromWorkList(N); |
| 1354 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1355 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1358 | static |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1359 | SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1, |
| 1360 | SelectionDAG &DAG) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1361 | EVT VT = N0.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1362 | SDValue N00 = N0.getOperand(0); |
| 1363 | SDValue N01 = N0.getOperand(1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1364 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1365 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1366 | if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() && |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1367 | isa<ConstantSDNode>(N00.getOperand(1))) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1368 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
| 1369 | N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, |
| 1370 | DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT, |
| 1371 | N00.getOperand(0), N01), |
| 1372 | DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT, |
| 1373 | N00.getOperand(1), N01)); |
| 1374 | return DAG.getNode(ISD::ADD, DL, VT, N0, N1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1375 | } |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1376 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1377 | return SDValue(); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1380 | SDValue DAGCombiner::visitADD(SDNode *N) { |
| 1381 | SDValue N0 = N->getOperand(0); |
| 1382 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1383 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1384 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1385 | EVT VT = N0.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1386 | |
| 1387 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1388 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1389 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1390 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 48b509c | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1391 | |
| 1392 | // fold (add x, 0) -> x, vector edition |
| 1393 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 1394 | return N0; |
| 1395 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 1396 | return N1; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1397 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1398 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1399 | // fold (add x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1400 | if (N0.getOpcode() == ISD::UNDEF) |
| 1401 | return N0; |
| 1402 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1403 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1404 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1405 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1406 | return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1407 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1408 | if (N0C && !N1C) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1409 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1410 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1411 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1412 | return N0; |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1413 | // fold (add Sym, c) -> Sym+c |
| 1414 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1415 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C && |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1416 | GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1417 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1418 | GA->getOffset() + |
| 1419 | (uint64_t)N1C->getSExtValue()); |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1420 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 1421 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 1422 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1423 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1424 | DAG.getConstant(N1C->getAPIntValue()+ |
| 1425 | N0C->getAPIntValue(), VT), |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1426 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1427 | // reassociate add |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1428 | SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1429 | if (RADD.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1430 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1431 | // fold ((0-A) + B) -> B-A |
| 1432 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 1433 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1434 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1435 | // fold (A + (0-B)) -> A-B |
| 1436 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 1437 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1438 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1439 | // fold (A+(B-A)) -> B |
| 1440 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1441 | return N1.getOperand(0); |
Dale Johannesen | 56eca91 | 2008-11-27 00:43:21 +0000 | [diff] [blame] | 1442 | // fold ((B-A)+A) -> B |
| 1443 | if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) |
| 1444 | return N0.getOperand(0); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1445 | // fold (A+(B-(A+C))) to (B-C) |
| 1446 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1447 | N0 == N1.getOperand(1).getOperand(0)) |
| 1448 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1449 | N1.getOperand(1).getOperand(1)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1450 | // fold (A+(B-(C+A))) to (B-C) |
| 1451 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1452 | N0 == N1.getOperand(1).getOperand(1)) |
| 1453 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1454 | N1.getOperand(1).getOperand(0)); |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1455 | // fold (A+((B-A)+or-C)) to (B+or-C) |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1456 | if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && |
| 1457 | N1.getOperand(0).getOpcode() == ISD::SUB && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1458 | N0 == N1.getOperand(0).getOperand(1)) |
| 1459 | return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT, |
| 1460 | N1.getOperand(0).getOperand(0), N1.getOperand(1)); |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1461 | |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1462 | // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant |
| 1463 | if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { |
| 1464 | SDValue N00 = N0.getOperand(0); |
| 1465 | SDValue N01 = N0.getOperand(1); |
| 1466 | SDValue N10 = N1.getOperand(0); |
| 1467 | SDValue N11 = N1.getOperand(1); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) |
| 1470 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1471 | DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10), |
| 1472 | DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1473 | } |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1474 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1475 | if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) |
| 1476 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1477 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1478 | // fold (a+b) -> (a|b) iff a and b share no bits. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1479 | if (VT.isInteger() && !VT.isVector()) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1480 | APInt LHSZero, LHSOne; |
| 1481 | APInt RHSZero, RHSOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1482 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1483 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1484 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1485 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1487 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1488 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1489 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1490 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Evan Cheng | 3ef554d | 2006-11-06 08:14:30 +0000 | [diff] [blame] | 1493 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1494 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1495 | if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1496 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1497 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1498 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1499 | if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1500 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1501 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1504 | // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) |
| 1505 | if (N1.getOpcode() == ISD::SHL && |
| 1506 | N1.getOperand(0).getOpcode() == ISD::SUB) |
| 1507 | if (ConstantSDNode *C = |
| 1508 | dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0))) |
| 1509 | if (C->getAPIntValue() == 0) |
| 1510 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, |
| 1511 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1512 | N1.getOperand(0).getOperand(1), |
| 1513 | N1.getOperand(1))); |
| 1514 | if (N0.getOpcode() == ISD::SHL && |
| 1515 | N0.getOperand(0).getOpcode() == ISD::SUB) |
| 1516 | if (ConstantSDNode *C = |
| 1517 | dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0))) |
| 1518 | if (C->getAPIntValue() == 0) |
| 1519 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, |
| 1520 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1521 | N0.getOperand(0).getOperand(1), |
| 1522 | N0.getOperand(1))); |
| 1523 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1524 | if (N1.getOpcode() == ISD::AND) { |
| 1525 | SDValue AndOp0 = N1.getOperand(0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1526 | ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1)); |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1527 | unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); |
| 1528 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1529 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1530 | // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) |
| 1531 | // and similar xforms where the inner op is either ~0 or 0. |
| 1532 | if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) { |
| 1533 | DebugLoc DL = N->getDebugLoc(); |
| 1534 | return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0); |
| 1535 | } |
| 1536 | } |
| 1537 | |
Benjamin Kramer | f50125e | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1538 | // add (sext i1), X -> sub X, (zext i1) |
| 1539 | if (N0.getOpcode() == ISD::SIGN_EXTEND && |
| 1540 | N0.getOperand(0).getValueType() == MVT::i1 && |
| 1541 | !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { |
| 1542 | DebugLoc DL = N->getDebugLoc(); |
| 1543 | SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); |
| 1544 | return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); |
| 1545 | } |
| 1546 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1547 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1550 | SDValue DAGCombiner::visitADDC(SDNode *N) { |
| 1551 | SDValue N0 = N->getOperand(0); |
| 1552 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1553 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1554 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1555 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1557 | // If the flag result is dead, turn this into an ADD. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1558 | if (!N->hasAnyUseOfValue(1)) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1559 | return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1560 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1561 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1562 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1563 | // canonicalize constant to RHS. |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1564 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1565 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1567 | // fold (addc x, 0) -> x + no carry out |
| 1568 | if (N1C && N1C->isNullValue()) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1569 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1570 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1571 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1572 | // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1573 | APInt LHSZero, LHSOne; |
| 1574 | APInt RHSZero, RHSOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1575 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1576 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1577 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1578 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1580 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1581 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1582 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1583 | return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1584 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1585 | N->getDebugLoc(), MVT::Glue)); |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1586 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1587 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1588 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1591 | SDValue DAGCombiner::visitADDE(SDNode *N) { |
| 1592 | SDValue N0 = N->getOperand(0); |
| 1593 | SDValue N1 = N->getOperand(1); |
| 1594 | SDValue CarryIn = N->getOperand(2); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1595 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1596 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1598 | // canonicalize constant to RHS |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1599 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1600 | return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(), |
| 1601 | N1, N0, CarryIn); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1603 | // fold (adde x, y, false) -> (addc x, y) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1604 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1605 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1606 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1607 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1610 | // Since it may not be valid to emit a fold to zero for vector initializers |
| 1611 | // check if we can before folding. |
| 1612 | static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1613 | SelectionDAG &DAG, bool LegalOperations) { |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1614 | if (!VT.isVector()) { |
| 1615 | return DAG.getConstant(0, VT); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 1616 | } |
| 1617 | if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) { |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1618 | // Produce a vector of zeros. |
| 1619 | SDValue El = DAG.getConstant(0, VT.getVectorElementType()); |
| 1620 | std::vector<SDValue> Ops(VT.getVectorNumElements(), El); |
| 1621 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, |
| 1622 | &Ops[0], Ops.size()); |
| 1623 | } |
| 1624 | return SDValue(); |
| 1625 | } |
| 1626 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1627 | SDValue DAGCombiner::visitSUB(SDNode *N) { |
| 1628 | SDValue N0 = N->getOperand(0); |
| 1629 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1630 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1631 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1632 | ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 : |
| 1633 | dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1634 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1635 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1636 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1637 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1638 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1639 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 48b509c | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1640 | |
| 1641 | // fold (sub x, 0) -> x, vector edition |
| 1642 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 1643 | return N0; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1644 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 1646 | // fold (sub x, x) -> 0 |
Eric Christopher | 169e155 | 2011-02-16 01:10:03 +0000 | [diff] [blame] | 1647 | // FIXME: Refactor this and xor and other similar operations together. |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1648 | if (N0 == N1) |
| 1649 | return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1650 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1651 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1652 | return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 1653 | // fold (sub x, c) -> (add x, -c) |
| 1654 | if (N1C) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1655 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1656 | DAG.getConstant(-N1C->getAPIntValue(), VT)); |
Evan Cheng | 1ad0e8b | 2010-01-18 21:38:44 +0000 | [diff] [blame] | 1657 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) |
| 1658 | if (N0C && N0C->isAllOnesValue()) |
| 1659 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Benjamin Kramer | 2c94b42 | 2011-01-29 12:34:05 +0000 | [diff] [blame] | 1660 | // fold A-(A-B) -> B |
| 1661 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) |
| 1662 | return N1.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1663 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1664 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1665 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1666 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1667 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1668 | return N0.getOperand(0); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1669 | // fold C2-(A+C1) -> (C2-C1)-A |
| 1670 | if (N1.getOpcode() == ISD::ADD && N0C && N1C1) { |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 1671 | SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(), |
| 1672 | VT); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1673 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC, |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 1674 | N1.getOperand(0)); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1675 | } |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1676 | // fold ((A+(B+or-C))-B) -> A+or-C |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1677 | if (N0.getOpcode() == ISD::ADD && |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1678 | (N0.getOperand(1).getOpcode() == ISD::SUB || |
| 1679 | N0.getOperand(1).getOpcode() == ISD::ADD) && |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1680 | N0.getOperand(1).getOperand(0) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1681 | return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT, |
| 1682 | N0.getOperand(0), N0.getOperand(1).getOperand(1)); |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1683 | // fold ((A+(C+B))-B) -> A+C |
| 1684 | if (N0.getOpcode() == ISD::ADD && |
| 1685 | N0.getOperand(1).getOpcode() == ISD::ADD && |
| 1686 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1687 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1688 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Dale Johannesen | 58e39b0 | 2008-12-23 01:59:54 +0000 | [diff] [blame] | 1689 | // fold ((A-(B-C))-C) -> A-B |
| 1690 | if (N0.getOpcode() == ISD::SUB && |
| 1691 | N0.getOperand(1).getOpcode() == ISD::SUB && |
| 1692 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1693 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1694 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1695 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1696 | // If either operand of a sub is undef, the result is undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1697 | if (N0.getOpcode() == ISD::UNDEF) |
| 1698 | return N0; |
| 1699 | if (N1.getOpcode() == ISD::UNDEF) |
| 1700 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1701 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1702 | // If the relocation model supports it, consider symbol offsets. |
| 1703 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1704 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1705 | // fold (sub Sym, c) -> Sym-c |
| 1706 | if (N1C && GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1707 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1708 | GA->getOffset() - |
| 1709 | (uint64_t)N1C->getSExtValue()); |
| 1710 | // fold (sub Sym+c1, Sym+c2) -> c1-c2 |
| 1711 | if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) |
| 1712 | if (GA->getGlobal() == GB->getGlobal()) |
| 1713 | return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), |
| 1714 | VT); |
| 1715 | } |
| 1716 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1717 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1720 | SDValue DAGCombiner::visitSUBC(SDNode *N) { |
| 1721 | SDValue N0 = N->getOperand(0); |
| 1722 | SDValue N1 = N->getOperand(1); |
| 1723 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1724 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1725 | EVT VT = N0.getValueType(); |
| 1726 | |
| 1727 | // If the flag result is dead, turn this into an SUB. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1728 | if (!N->hasAnyUseOfValue(1)) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1729 | return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1), |
| 1730 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1731 | MVT::Glue)); |
| 1732 | |
| 1733 | // fold (subc x, x) -> 0 + no borrow |
| 1734 | if (N0 == N1) |
| 1735 | return CombineTo(N, DAG.getConstant(0, VT), |
| 1736 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1737 | MVT::Glue)); |
| 1738 | |
| 1739 | // fold (subc x, 0) -> x + no borrow |
| 1740 | if (N1C && N1C->isNullValue()) |
| 1741 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1742 | MVT::Glue)); |
| 1743 | |
| 1744 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow |
| 1745 | if (N0C && N0C->isAllOnesValue()) |
| 1746 | return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0), |
| 1747 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1748 | MVT::Glue)); |
| 1749 | |
| 1750 | return SDValue(); |
| 1751 | } |
| 1752 | |
| 1753 | SDValue DAGCombiner::visitSUBE(SDNode *N) { |
| 1754 | SDValue N0 = N->getOperand(0); |
| 1755 | SDValue N1 = N->getOperand(1); |
| 1756 | SDValue CarryIn = N->getOperand(2); |
| 1757 | |
| 1758 | // fold (sube x, y, false) -> (subc x, y) |
| 1759 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
| 1760 | return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1); |
| 1761 | |
| 1762 | return SDValue(); |
| 1763 | } |
| 1764 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1765 | SDValue DAGCombiner::visitMUL(SDNode *N) { |
| 1766 | SDValue N0 = N->getOperand(0); |
| 1767 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1768 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1769 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1770 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1771 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1772 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1773 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1774 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1775 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1776 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1778 | // fold (mul x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1779 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1780 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1781 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1782 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1783 | return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1784 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1785 | if (N0C && !N1C) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1786 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1787 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1788 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1789 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1790 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1791 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1792 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1793 | DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1794 | // fold (mul x, (1 << c)) -> x << c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1795 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1796 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1797 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1798 | getShiftAmountTy(N0.getValueType()))); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1799 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1800 | if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) { |
| 1801 | unsigned Log2Val = (-N1C->getAPIntValue()).logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1802 | // FIXME: If the input is something that is easily negated (e.g. a |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1803 | // single-use add), we should put the negate there. |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1804 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1805 | DAG.getConstant(0, VT), |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1806 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1807 | DAG.getConstant(Log2Val, |
| 1808 | getShiftAmountTy(N0.getValueType())))); |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1809 | } |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1810 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1811 | if (N1C && N0.getOpcode() == ISD::SHL && |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1812 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1813 | SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1814 | N1, N0.getOperand(1)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1815 | AddToWorkList(C3.getNode()); |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1816 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1817 | N0.getOperand(0), C3); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1818 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1820 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 1821 | // use. |
| 1822 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1823 | SDValue Sh(0,0), Y(0,0); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1824 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| 1825 | if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1826 | N0.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1827 | Sh = N0; Y = N1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1828 | } else if (N1.getOpcode() == ISD::SHL && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 1829 | isa<ConstantSDNode>(N1.getOperand(1)) && |
| 1830 | N1.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1831 | Sh = N1; Y = N0; |
| 1832 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1833 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1834 | if (Sh.getNode()) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1835 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1836 | Sh.getOperand(0), Y); |
| 1837 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1838 | Mul, Sh.getOperand(1)); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1839 | } |
| 1840 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 1842 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1843 | if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1844 | isa<ConstantSDNode>(N0.getOperand(1))) |
| 1845 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1846 | DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT, |
| 1847 | N0.getOperand(0), N1), |
| 1848 | DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT, |
| 1849 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1850 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1851 | // reassociate mul |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1852 | SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1853 | if (RMUL.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1854 | return RMUL; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1855 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1856 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1859 | SDValue DAGCombiner::visitSDIV(SDNode *N) { |
| 1860 | SDValue N0 = N->getOperand(0); |
| 1861 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1862 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1863 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1864 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1865 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1866 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1867 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1868 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1869 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1870 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1871 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1872 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1873 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1874 | return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1875 | // fold (sdiv X, 1) -> X |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1876 | if (N1C && N1C->getAPIntValue() == 1LL) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1877 | return N0; |
| 1878 | // fold (sdiv X, -1) -> 0-X |
| 1879 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1880 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1881 | DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 1882 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1883 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1884 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1885 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1886 | return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(), |
| 1887 | N0, N1); |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 1888 | } |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 1889 | // fold (sdiv X, pow2) -> simple ops after legalize |
Eli Friedman | 1c663fe | 2011-12-07 03:55:52 +0000 | [diff] [blame] | 1890 | if (N1C && !N1C->isNullValue() && |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1891 | (N1C->getAPIntValue().isPowerOf2() || |
| 1892 | (-N1C->getAPIntValue()).isPowerOf2())) { |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1893 | // If dividing by powers of two is cheap, then don't perform the following |
| 1894 | // fold. |
| 1895 | if (TLI.isPow2DivCheap()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1896 | return SDValue(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1897 | |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1898 | unsigned lg2 = N1C->getAPIntValue().countTrailingZeros(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1900 | // Splat the sign bit into the register |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1901 | SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
| 1902 | DAG.getConstant(VT.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1903 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1904 | AddToWorkList(SGN.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1906 | // Add (N0 < 0) ? abs2 - 1 : 0; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1907 | SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN, |
| 1908 | DAG.getConstant(VT.getSizeInBits() - lg2, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1909 | getShiftAmountTy(SGN.getValueType()))); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1910 | SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1911 | AddToWorkList(SRL.getNode()); |
| 1912 | AddToWorkList(ADD.getNode()); // Divide by pow2 |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1913 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1914 | DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType()))); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1915 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1916 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 1917 | // negate the result. |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1918 | if (N1C->getAPIntValue().isNonNegative()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1919 | return SRA; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1920 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1921 | AddToWorkList(SRA.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1922 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1923 | DAG.getConstant(0, VT), SRA); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1924 | } |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1925 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1926 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 1927 | // alternate sequence. |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1928 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1929 | SDValue Op = BuildSDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1930 | if (Op.getNode()) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1931 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1932 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1933 | // undef / X -> 0 |
| 1934 | if (N0.getOpcode() == ISD::UNDEF) |
| 1935 | return DAG.getConstant(0, VT); |
| 1936 | // X / undef -> undef |
| 1937 | if (N1.getOpcode() == ISD::UNDEF) |
| 1938 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1939 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1940 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1943 | SDValue DAGCombiner::visitUDIV(SDNode *N) { |
| 1944 | SDValue N0 = N->getOperand(0); |
| 1945 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1946 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1947 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1948 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1949 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1950 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1951 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1952 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1953 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1954 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1955 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1956 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1957 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1958 | return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1959 | // fold (udiv x, (1 << c)) -> x >>u c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1960 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1961 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1962 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1963 | getShiftAmountTy(N0.getValueType()))); |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1964 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1965 | if (N1.getOpcode() == ISD::SHL) { |
| 1966 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1967 | if (SHC->getAPIntValue().isPowerOf2()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1968 | EVT ADDVT = N1.getOperand(1).getValueType(); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1969 | SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT, |
| 1970 | N1.getOperand(1), |
| 1971 | DAG.getConstant(SHC->getAPIntValue() |
| 1972 | .logBase2(), |
| 1973 | ADDVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1974 | AddToWorkList(Add.getNode()); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1975 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1979 | // fold (udiv x, c) -> alternate |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1980 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1981 | SDValue Op = BuildUDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1982 | if (Op.getNode()) return Op; |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 1983 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1984 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1985 | // undef / X -> 0 |
| 1986 | if (N0.getOpcode() == ISD::UNDEF) |
| 1987 | return DAG.getConstant(0, VT); |
| 1988 | // X / undef -> undef |
| 1989 | if (N1.getOpcode() == ISD::UNDEF) |
| 1990 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1991 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1992 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1995 | SDValue DAGCombiner::visitSREM(SDNode *N) { |
| 1996 | SDValue N0 = N->getOperand(0); |
| 1997 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1998 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1999 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2000 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2001 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2002 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2003 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2004 | return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2005 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 2006 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2007 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2008 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2009 | return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | ee339f4 | 2008-01-27 23:21:58 +0000 | [diff] [blame] | 2010 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2011 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2012 | // If X/C can be simplified by the division-by-constant logic, lower |
| 2013 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2014 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2015 | SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2016 | AddToWorkList(Div.getNode()); |
| 2017 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 2018 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2019 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 2020 | OptimizedDiv, N1); |
| 2021 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2022 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2023 | return Sub; |
| 2024 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2025 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2026 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2027 | // undef % X -> 0 |
| 2028 | if (N0.getOpcode() == ISD::UNDEF) |
| 2029 | return DAG.getConstant(0, VT); |
| 2030 | // X % undef -> undef |
| 2031 | if (N1.getOpcode() == ISD::UNDEF) |
| 2032 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2033 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2034 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2037 | SDValue DAGCombiner::visitUREM(SDNode *N) { |
| 2038 | SDValue N0 = N->getOperand(0); |
| 2039 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2040 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2041 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2042 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2043 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2044 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2045 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2046 | return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2047 | // fold (urem x, pow2) -> (and x, pow2-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2048 | if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2049 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2050 | DAG.getConstant(N1C->getAPIntValue()-1,VT)); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2051 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 2052 | if (N1.getOpcode() == ISD::SHL) { |
| 2053 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2054 | if (SHC->getAPIntValue().isPowerOf2()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2055 | SDValue Add = |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2056 | DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2057 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2058 | VT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2059 | AddToWorkList(Add.getNode()); |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2060 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2064 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2065 | // If X/C can be simplified by the division-by-constant logic, lower |
| 2066 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2067 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2068 | SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 942ca7f | 2008-09-08 16:59:01 +0000 | [diff] [blame] | 2069 | AddToWorkList(Div.getNode()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2070 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 2071 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2072 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 2073 | OptimizedDiv, N1); |
| 2074 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2075 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2076 | return Sub; |
| 2077 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2078 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2079 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2080 | // undef % X -> 0 |
| 2081 | if (N0.getOpcode() == ISD::UNDEF) |
| 2082 | return DAG.getConstant(0, VT); |
| 2083 | // X % undef -> undef |
| 2084 | if (N1.getOpcode() == ISD::UNDEF) |
| 2085 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2086 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2087 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2090 | SDValue DAGCombiner::visitMULHS(SDNode *N) { |
| 2091 | SDValue N0 = N->getOperand(0); |
| 2092 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2093 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2094 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2095 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2096 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2097 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2098 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2099 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2100 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2101 | if (N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 326411d | 2009-01-30 03:00:18 +0000 | [diff] [blame] | 2102 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0, |
| 2103 | DAG.getConstant(N0.getValueType().getSizeInBits() - 1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2104 | getShiftAmountTy(N0.getValueType()))); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2105 | // fold (mulhs x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2106 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2107 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2108 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2109 | // If the type twice as wide is legal, transform the mulhs to a wider multiply |
| 2110 | // plus a shift. |
| 2111 | if (VT.isSimple() && !VT.isVector()) { |
| 2112 | MVT Simple = VT.getSimpleVT(); |
| 2113 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2114 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2115 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2116 | N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); |
| 2117 | N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); |
| 2118 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
Chris Lattner | 1a0fbe2 | 2010-12-15 05:51:39 +0000 | [diff] [blame] | 2119 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2120 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2121 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2122 | } |
| 2123 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2124 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2125 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2128 | SDValue DAGCombiner::visitMULHU(SDNode *N) { |
| 2129 | SDValue N0 = N->getOperand(0); |
| 2130 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2131 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2132 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2133 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2134 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2135 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2136 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2137 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2138 | // fold (mulhu x, 1) -> 0 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2139 | if (N1C && N1C->getAPIntValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2140 | return DAG.getConstant(0, N0.getValueType()); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2141 | // fold (mulhu x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2142 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2143 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2145 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2146 | // plus a shift. |
| 2147 | if (VT.isSimple() && !VT.isVector()) { |
| 2148 | MVT Simple = VT.getSimpleVT(); |
| 2149 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2150 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2151 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2152 | N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); |
| 2153 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); |
| 2154 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
| 2155 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2156 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2157 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2158 | } |
| 2159 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2160 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2161 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2164 | /// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that |
| 2165 | /// compute two values. LoOp and HiOp give the opcodes for the two computations |
| 2166 | /// that are being performed. Return true if a simplification was made. |
| 2167 | /// |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2168 | SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2169 | unsigned HiOp) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2170 | // If the high half is not needed, just compute the low half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2171 | bool HiExists = N->hasAnyUseOfValue(1); |
| 2172 | if (!HiExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2173 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2174 | TLI.isOperationLegal(LoOp, N->getValueType(0)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2175 | SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2176 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2177 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | // If the low half is not needed, just compute the high half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2181 | bool LoExists = N->hasAnyUseOfValue(0); |
| 2182 | if (!LoExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2183 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2184 | TLI.isOperationLegal(HiOp, N->getValueType(1)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2185 | SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
| 2186 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2187 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2190 | // If both halves are used, return as it is. |
| 2191 | if (LoExists && HiExists) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2192 | return SDValue(); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2193 | |
| 2194 | // If the two computed results can be simplified separately, separate them. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2195 | if (LoExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2196 | SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2197 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2198 | AddToWorkList(Lo.getNode()); |
| 2199 | SDValue LoOpt = combine(Lo.getNode()); |
| 2200 | if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2201 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2202 | TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2203 | return CombineTo(N, LoOpt, LoOpt); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2206 | if (HiExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2207 | SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2208 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2209 | AddToWorkList(Hi.getNode()); |
| 2210 | SDValue HiOpt = combine(Hi.getNode()); |
| 2211 | if (HiOpt.getNode() && HiOpt != Hi && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2212 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2213 | TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2214 | return CombineTo(N, HiOpt, HiOpt); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2215 | } |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2216 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2217 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2220 | SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { |
| 2221 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2222 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2224 | EVT VT = N->getValueType(0); |
| 2225 | DebugLoc DL = N->getDebugLoc(); |
| 2226 | |
| 2227 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2228 | // plus a shift. |
| 2229 | if (VT.isSimple() && !VT.isVector()) { |
| 2230 | MVT Simple = VT.getSimpleVT(); |
| 2231 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2232 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2233 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2234 | SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2235 | SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2236 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2237 | // Compute the high part as N1. |
| 2238 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2239 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2240 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2241 | // Compute the low part as N0. |
| 2242 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2243 | return CombineTo(N, Lo, Hi); |
| 2244 | } |
| 2245 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2246 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2247 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2250 | SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { |
| 2251 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2252 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2253 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2254 | EVT VT = N->getValueType(0); |
| 2255 | DebugLoc DL = N->getDebugLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2257 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2258 | // plus a shift. |
| 2259 | if (VT.isSimple() && !VT.isVector()) { |
| 2260 | MVT Simple = VT.getSimpleVT(); |
| 2261 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2262 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2263 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2264 | SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2265 | SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2266 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2267 | // Compute the high part as N1. |
| 2268 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2269 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2270 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2271 | // Compute the low part as N0. |
| 2272 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2273 | return CombineTo(N, Lo, Hi); |
| 2274 | } |
| 2275 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2276 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2277 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2280 | SDValue DAGCombiner::visitSMULO(SDNode *N) { |
| 2281 | // (smulo x, 2) -> (saddo x, x) |
| 2282 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2283 | if (C2->getAPIntValue() == 2) |
| 2284 | return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(), |
| 2285 | N->getOperand(0), N->getOperand(0)); |
| 2286 | |
| 2287 | return SDValue(); |
| 2288 | } |
| 2289 | |
| 2290 | SDValue DAGCombiner::visitUMULO(SDNode *N) { |
| 2291 | // (umulo x, 2) -> (uaddo x, x) |
| 2292 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2293 | if (C2->getAPIntValue() == 2) |
| 2294 | return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(), |
| 2295 | N->getOperand(0), N->getOperand(0)); |
| 2296 | |
| 2297 | return SDValue(); |
| 2298 | } |
| 2299 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2300 | SDValue DAGCombiner::visitSDIVREM(SDNode *N) { |
| 2301 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2302 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2303 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2304 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2307 | SDValue DAGCombiner::visitUDIVREM(SDNode *N) { |
| 2308 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2309 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2310 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2311 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2314 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 2315 | /// two operands of the same opcode, try to simplify it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2316 | SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 2317 | SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2318 | EVT VT = N0.getValueType(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2319 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2320 | |
Dan Gohman | ff00a55 | 2010-01-14 03:08:49 +0000 | [diff] [blame] | 2321 | // Bail early if none of these transforms apply. |
| 2322 | if (N0.getNode()->getNumOperands() == 0) return SDValue(); |
| 2323 | |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 2324 | // For each of OP in AND/OR/XOR: |
| 2325 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 2326 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 2327 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2328 | // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free) |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2329 | // |
| 2330 | // do not sink logical op inside of a vector extend, since it may combine |
| 2331 | // into a vsetcc. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2332 | EVT Op0VT = N0.getOperand(0).getValueType(); |
| 2333 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 2334 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 2335 | // Avoid infinite looping with PromoteIntBinOp. |
| 2336 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2337 | (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2338 | (N0.getOpcode() == ISD::TRUNCATE && |
| 2339 | (!TLI.isZExtFree(VT, Op0VT) || |
| 2340 | !TLI.isTruncateFree(Op0VT, VT)) && |
| 2341 | TLI.isTypeLegal(Op0VT))) && |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2342 | !VT.isVector() && |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2343 | Op0VT == N1.getOperand(0).getValueType() && |
| 2344 | (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2345 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2346 | N0.getOperand(0).getValueType(), |
| 2347 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2348 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2349 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2350 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2351 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2352 | // For each of OP in SHL/SRL/SRA/AND... |
| 2353 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 2354 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 2355 | // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2356 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2357 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2358 | N0.getOperand(1) == N1.getOperand(1)) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2359 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2360 | N0.getOperand(0).getValueType(), |
| 2361 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2362 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2363 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 2364 | ORNode, N0.getOperand(1)); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2365 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2366 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2367 | // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B)) |
| 2368 | // Only perform this optimization after type legalization and before |
| 2369 | // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by |
| 2370 | // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and |
| 2371 | // we don't want to undo this promotion. |
| 2372 | // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper |
| 2373 | // on scalars. |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2374 | if ((N0.getOpcode() == ISD::BITCAST || |
| 2375 | N0.getOpcode() == ISD::SCALAR_TO_VECTOR) && |
| 2376 | Level == AfterLegalizeTypes) { |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2377 | SDValue In0 = N0.getOperand(0); |
| 2378 | SDValue In1 = N1.getOperand(0); |
| 2379 | EVT In0Ty = In0.getValueType(); |
| 2380 | EVT In1Ty = In1.getValueType(); |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2381 | DebugLoc DL = N->getDebugLoc(); |
| 2382 | // If both incoming values are integers, and the original types are the |
| 2383 | // same. |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2384 | if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) { |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2385 | SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1); |
| 2386 | SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2387 | AddToWorkList(Op.getNode()); |
| 2388 | return BC; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | // Xor/and/or are indifferent to the swizzle operation (shuffle of one value). |
| 2393 | // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B)) |
| 2394 | // If both shuffles use the same mask, and both shuffle within a single |
| 2395 | // vector, then it is worthwhile to move the swizzle after the operation. |
| 2396 | // The type-legalizer generates this pattern when loading illegal |
| 2397 | // vector types from memory. In many cases this allows additional shuffle |
| 2398 | // optimizations. |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2399 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 2400 | N0.getOperand(1).getOpcode() == ISD::UNDEF && |
| 2401 | N1.getOperand(1).getOpcode() == ISD::UNDEF) { |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2402 | ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0); |
| 2403 | ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1); |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2404 | |
| 2405 | assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() && |
| 2406 | "Inputs to shuffles are not the same type"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2407 | |
| 2408 | unsigned NumElts = VT.getVectorNumElements(); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Check that both shuffles use the same mask. The masks are known to be of |
| 2411 | // the same length because the result vector type is the same. |
| 2412 | bool SameMask = true; |
| 2413 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2414 | int Idx0 = SVN0->getMaskElt(i); |
| 2415 | int Idx1 = SVN1->getMaskElt(i); |
| 2416 | if (Idx0 != Idx1) { |
| 2417 | SameMask = false; |
| 2418 | break; |
| 2419 | } |
| 2420 | } |
| 2421 | |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2422 | if (SameMask) { |
| 2423 | SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT, |
| 2424 | N0.getOperand(0), N1.getOperand(0)); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2425 | AddToWorkList(Op.getNode()); |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2426 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op, |
| 2427 | DAG.getUNDEF(VT), &SVN0->getMask()[0]); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2430 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2431 | return SDValue(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2434 | SDValue DAGCombiner::visitAND(SDNode *N) { |
| 2435 | SDValue N0 = N->getOperand(0); |
| 2436 | SDValue N1 = N->getOperand(1); |
| 2437 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2438 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2439 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2440 | EVT VT = N1.getValueType(); |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2441 | unsigned BitWidth = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2442 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2443 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2444 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2445 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2446 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 2447 | |
| 2448 | // fold (and x, 0) -> 0, vector edition |
| 2449 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 2450 | return N0; |
| 2451 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 2452 | return N1; |
| 2453 | |
| 2454 | // fold (and x, -1) -> x, vector edition |
| 2455 | if (ISD::isBuildVectorAllOnes(N0.getNode())) |
| 2456 | return N1; |
| 2457 | if (ISD::isBuildVectorAllOnes(N1.getNode())) |
| 2458 | return N0; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2459 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2460 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2461 | // fold (and x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2462 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2463 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2464 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2465 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2466 | return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2467 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2468 | if (N0C && !N1C) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 2469 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2470 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2471 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2472 | return N0; |
| 2473 | // if (and x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2474 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2475 | APInt::getAllOnesValue(BitWidth))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2476 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2477 | // reassociate and |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 2478 | SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2479 | if (RAND.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2480 | return RAND; |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2481 | // fold (and (or x, C), D) -> D if (C & D) == D |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 2482 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2483 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2484 | if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2485 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2486 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 2487 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2488 | SDValue N0Op0 = N0.getOperand(0); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2489 | APInt Mask = ~N1C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2490 | Mask = Mask.trunc(N0Op0.getValueSizeInBits()); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2491 | if (DAG.MaskedValueIsZero(N0Op0, Mask)) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2492 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), |
| 2493 | N0.getValueType(), N0Op0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2494 | |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 2495 | // Replace uses of the AND with uses of the Zero extend node. |
| 2496 | CombineTo(N, Zext); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2497 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2498 | // We actually want to replace all uses of the any_extend with the |
| 2499 | // zero_extend, to avoid duplicating things. This will later cause this |
| 2500 | // AND to be folded. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2501 | CombineTo(N0.getNode(), Zext); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2502 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2503 | } |
| 2504 | } |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2505 | // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) -> |
| 2506 | // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must |
| 2507 | // already be zero by virtue of the width of the base type of the load. |
| 2508 | // |
| 2509 | // the 'X' node here can either be nothing or an extract_vector_elt to catch |
| 2510 | // more cases. |
| 2511 | if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 2512 | N0.getOperand(0).getOpcode() == ISD::LOAD) || |
| 2513 | N0.getOpcode() == ISD::LOAD) { |
| 2514 | LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ? |
| 2515 | N0 : N0.getOperand(0) ); |
| 2516 | |
| 2517 | // Get the constant (if applicable) the zero'th operand is being ANDed with. |
| 2518 | // This can be a pure constant or a vector splat, in which case we treat the |
| 2519 | // vector as a scalar and use the splat value. |
| 2520 | APInt Constant = APInt::getNullValue(1); |
| 2521 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { |
| 2522 | Constant = C->getAPIntValue(); |
| 2523 | } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) { |
| 2524 | APInt SplatValue, SplatUndef; |
| 2525 | unsigned SplatBitSize; |
| 2526 | bool HasAnyUndefs; |
| 2527 | bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef, |
| 2528 | SplatBitSize, HasAnyUndefs); |
| 2529 | if (IsSplat) { |
| 2530 | // Undef bits can contribute to a possible optimisation if set, so |
| 2531 | // set them. |
| 2532 | SplatValue |= SplatUndef; |
| 2533 | |
| 2534 | // The splat value may be something like "0x00FFFFFF", which means 0 for |
| 2535 | // the first vector value and FF for the rest, repeating. We need a mask |
| 2536 | // that will apply equally to all members of the vector, so AND all the |
| 2537 | // lanes of the constant together. |
| 2538 | EVT VT = Vector->getValueType(0); |
| 2539 | unsigned BitWidth = VT.getVectorElementType().getSizeInBits(); |
Silviu Baranga | 3d5e161 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2540 | |
| 2541 | // If the splat value has been compressed to a bitlength lower |
| 2542 | // than the size of the vector lane, we need to re-expand it to |
| 2543 | // the lane size. |
| 2544 | if (BitWidth > SplatBitSize) |
| 2545 | for (SplatValue = SplatValue.zextOrTrunc(BitWidth); |
| 2546 | SplatBitSize < BitWidth; |
| 2547 | SplatBitSize = SplatBitSize * 2) |
| 2548 | SplatValue |= SplatValue.shl(SplatBitSize); |
| 2549 | |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2550 | Constant = APInt::getAllOnesValue(BitWidth); |
Silviu Baranga | 3d5e161 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2551 | for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2552 | Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is |
| 2557 | // actually legal and isn't going to get expanded, else this is a false |
| 2558 | // optimisation. |
| 2559 | bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD, |
| 2560 | Load->getMemoryVT()); |
| 2561 | |
| 2562 | // Resize the constant to the same size as the original memory access before |
| 2563 | // extension. If it is still the AllOnesValue then this AND is completely |
| 2564 | // unneeded. |
| 2565 | Constant = |
| 2566 | Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits()); |
| 2567 | |
| 2568 | bool B; |
| 2569 | switch (Load->getExtensionType()) { |
| 2570 | default: B = false; break; |
| 2571 | case ISD::EXTLOAD: B = CanZextLoadProfitably; break; |
| 2572 | case ISD::ZEXTLOAD: |
| 2573 | case ISD::NON_EXTLOAD: B = true; break; |
| 2574 | } |
| 2575 | |
| 2576 | if (B && Constant.isAllOnesValue()) { |
| 2577 | // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to |
| 2578 | // preserve semantics once we get rid of the AND. |
| 2579 | SDValue NewLoad(Load, 0); |
| 2580 | if (Load->getExtensionType() == ISD::EXTLOAD) { |
| 2581 | NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD, |
| 2582 | Load->getValueType(0), Load->getDebugLoc(), |
| 2583 | Load->getChain(), Load->getBasePtr(), |
| 2584 | Load->getOffset(), Load->getMemoryVT(), |
| 2585 | Load->getMemOperand()); |
| 2586 | // Replace uses of the EXTLOAD with the new ZEXTLOAD. |
Hal Finkel | d65e463 | 2012-06-20 15:42:48 +0000 | [diff] [blame] | 2587 | if (Load->getNumValues() == 3) { |
| 2588 | // PRE/POST_INC loads have 3 values. |
| 2589 | SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1), |
| 2590 | NewLoad.getValue(2) }; |
| 2591 | CombineTo(Load, To, 3, true); |
| 2592 | } else { |
| 2593 | CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1)); |
| 2594 | } |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | // Fold the AND away, taking care not to fold to the old load node if we |
| 2598 | // replaced it. |
| 2599 | CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0); |
| 2600 | |
| 2601 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2602 | } |
| 2603 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2604 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 2605 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 2606 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 2607 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2608 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2609 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2610 | LL.getValueType().isInteger()) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2611 | // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2612 | if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2613 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2614 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2615 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2616 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2617 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2618 | // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1) |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2619 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2620 | SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(), |
| 2621 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2622 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2623 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2624 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2625 | // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1) |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2626 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2627 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2628 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2629 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2630 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | // canonicalize equivalent to ll == rl |
| 2634 | if (LL == RR && LR == RL) { |
| 2635 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 2636 | std::swap(RL, RR); |
| 2637 | } |
| 2638 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2639 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2640 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2641 | if (Result != ISD::SETCC_INVALID && |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 2642 | (!LegalOperations || |
Owen Anderson | 39125d9 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 2643 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
| 2644 | TLI.isOperationLegal(ISD::SETCC, |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 2645 | getSetCCResultType(N0.getSimpleValueType()))))) |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2646 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 2647 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2650 | |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2651 | // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2652 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2653 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2654 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2655 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2656 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 2657 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 2658 | // fold (and (sra)) -> (and (srl)) when possible. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2659 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2660 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2661 | return SDValue(N, 0); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2662 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2663 | // fold (zext_inreg (extload x)) -> (zextload x) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2664 | if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2665 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2666 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2667 | // If we zero all the possible extended bits, then we can turn this into |
| 2668 | // a zextload if we are running before legalize or the operation is legal. |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2669 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2670 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2671 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2672 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2673 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2674 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2675 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2676 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2677 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2678 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2679 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2680 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2681 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2682 | } |
| 2683 | } |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2684 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2685 | if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 2686 | N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2687 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2688 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2689 | // If we zero all the possible extended bits, then we can turn this into |
| 2690 | // a zextload if we are running before legalize or the operation is legal. |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2691 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2692 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2693 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2694 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2695 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2696 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2697 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2698 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 2699 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2700 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2701 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2702 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2703 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2704 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2705 | } |
| 2706 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 2708 | // fold (and (load x), 255) -> (zextload x, i8) |
| 2709 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2710 | // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) |
| 2711 | if (N1C && (N0.getOpcode() == ISD::LOAD || |
| 2712 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2713 | N0.getOperand(0).getOpcode() == ISD::LOAD))) { |
| 2714 | bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; |
| 2715 | LoadSDNode *LN0 = HasAnyExt |
| 2716 | ? cast<LoadSDNode>(N0.getOperand(0)) |
| 2717 | : cast<LoadSDNode>(N0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2718 | if (LN0->getExtensionType() != ISD::SEXTLOAD && |
Chris Lattner | bd1fccf | 2010-01-07 21:59:23 +0000 | [diff] [blame] | 2719 | LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) { |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2720 | uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits(); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2721 | if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){ |
| 2722 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); |
| 2723 | EVT LoadedVT = LN0->getMemoryVT(); |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2724 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2725 | if (ExtVT == LoadedVT && |
| 2726 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2727 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2728 | |
| 2729 | SDValue NewLoad = |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2730 | DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2731 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2732 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2733 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2734 | LN0->getAlignment()); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2735 | AddToWorkList(N); |
| 2736 | CombineTo(LN0, NewLoad, NewLoad.getValue(1)); |
| 2737 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2738 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2739 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2740 | // Do not change the width of a volatile load. |
| 2741 | // Do not generate loads of non-round integer types since these can |
| 2742 | // be expensive (and would be wrong if the type is not byte sized). |
| 2743 | if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() && |
| 2744 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
| 2745 | EVT PtrType = LN0->getOperand(1).getValueType(); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2746 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2747 | unsigned Alignment = LN0->getAlignment(); |
| 2748 | SDValue NewPtr = LN0->getBasePtr(); |
| 2749 | |
| 2750 | // For big endian targets, we need to add an offset to the pointer |
| 2751 | // to load the correct bytes. For little endian systems, we merely |
| 2752 | // need to read fewer bytes from the same pointer. |
| 2753 | if (TLI.isBigEndian()) { |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2754 | unsigned LVTStoreBytes = LoadedVT.getStoreSize(); |
| 2755 | unsigned EVTStoreBytes = ExtVT.getStoreSize(); |
| 2756 | unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2757 | NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType, |
| 2758 | NewPtr, DAG.getConstant(PtrOff, PtrType)); |
| 2759 | Alignment = MinAlign(Alignment, PtrOff); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2760 | } |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2761 | |
| 2762 | AddToWorkList(NewPtr.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2763 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2764 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
| 2765 | SDValue Load = |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2766 | DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2767 | LN0->getChain(), NewPtr, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2768 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2769 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2770 | Alignment); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2771 | AddToWorkList(N); |
| 2772 | CombineTo(LN0, Load, Load.getValue(1)); |
| 2773 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2774 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2775 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2778 | |
Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2779 | if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL && |
| 2780 | VT.getSizeInBits() <= 64) { |
| 2781 | if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 2782 | APInt ADDC = ADDI->getAPIntValue(); |
| 2783 | if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) { |
| 2784 | // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal |
| 2785 | // immediate for an add, but it is legal if its top c2 bits are set, |
| 2786 | // transform the ADD so the immediate doesn't need to be materialized |
| 2787 | // in a register. |
| 2788 | if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { |
| 2789 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
| 2790 | SRLI->getZExtValue()); |
| 2791 | if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) { |
| 2792 | ADDC |= Mask; |
| 2793 | if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) { |
| 2794 | SDValue NewAdd = |
| 2795 | DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, |
| 2796 | N0.getOperand(0), DAG.getConstant(ADDC, VT)); |
| 2797 | CombineTo(N0.getNode(), NewAdd); |
| 2798 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | } |
Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2805 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2806 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2809 | /// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16 |
| 2810 | /// |
| 2811 | SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 2812 | bool DemandHighBits) { |
| 2813 | if (!LegalOperations) |
| 2814 | return SDValue(); |
| 2815 | |
| 2816 | EVT VT = N->getValueType(0); |
| 2817 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16) |
| 2818 | return SDValue(); |
| 2819 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 2820 | return SDValue(); |
| 2821 | |
| 2822 | // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00) |
| 2823 | bool LookPassAnd0 = false; |
| 2824 | bool LookPassAnd1 = false; |
| 2825 | if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL) |
| 2826 | std::swap(N0, N1); |
| 2827 | if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL) |
| 2828 | std::swap(N0, N1); |
| 2829 | if (N0.getOpcode() == ISD::AND) { |
| 2830 | if (!N0.getNode()->hasOneUse()) |
| 2831 | return SDValue(); |
| 2832 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2833 | if (!N01C || N01C->getZExtValue() != 0xFF00) |
| 2834 | return SDValue(); |
| 2835 | N0 = N0.getOperand(0); |
| 2836 | LookPassAnd0 = true; |
| 2837 | } |
| 2838 | |
| 2839 | if (N1.getOpcode() == ISD::AND) { |
| 2840 | if (!N1.getNode()->hasOneUse()) |
| 2841 | return SDValue(); |
| 2842 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2843 | if (!N11C || N11C->getZExtValue() != 0xFF) |
| 2844 | return SDValue(); |
| 2845 | N1 = N1.getOperand(0); |
| 2846 | LookPassAnd1 = true; |
| 2847 | } |
| 2848 | |
| 2849 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) |
| 2850 | std::swap(N0, N1); |
| 2851 | if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) |
| 2852 | return SDValue(); |
| 2853 | if (!N0.getNode()->hasOneUse() || |
| 2854 | !N1.getNode()->hasOneUse()) |
| 2855 | return SDValue(); |
| 2856 | |
| 2857 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2858 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2859 | if (!N01C || !N11C) |
| 2860 | return SDValue(); |
| 2861 | if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8) |
| 2862 | return SDValue(); |
| 2863 | |
| 2864 | // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8) |
| 2865 | SDValue N00 = N0->getOperand(0); |
| 2866 | if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) { |
| 2867 | if (!N00.getNode()->hasOneUse()) |
| 2868 | return SDValue(); |
| 2869 | ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1)); |
| 2870 | if (!N001C || N001C->getZExtValue() != 0xFF) |
| 2871 | return SDValue(); |
| 2872 | N00 = N00.getOperand(0); |
| 2873 | LookPassAnd0 = true; |
| 2874 | } |
| 2875 | |
| 2876 | SDValue N10 = N1->getOperand(0); |
| 2877 | if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) { |
| 2878 | if (!N10.getNode()->hasOneUse()) |
| 2879 | return SDValue(); |
| 2880 | ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1)); |
| 2881 | if (!N101C || N101C->getZExtValue() != 0xFF00) |
| 2882 | return SDValue(); |
| 2883 | N10 = N10.getOperand(0); |
| 2884 | LookPassAnd1 = true; |
| 2885 | } |
| 2886 | |
| 2887 | if (N00 != N10) |
| 2888 | return SDValue(); |
| 2889 | |
| 2890 | // Make sure everything beyond the low halfword is zero since the SRL 16 |
| 2891 | // will clear the top bits. |
| 2892 | unsigned OpSizeInBits = VT.getSizeInBits(); |
| 2893 | if (DemandHighBits && OpSizeInBits > 16 && |
| 2894 | (!LookPassAnd0 || !LookPassAnd1) && |
| 2895 | !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16))) |
| 2896 | return SDValue(); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 2897 | |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2898 | SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00); |
| 2899 | if (OpSizeInBits > 16) |
| 2900 | Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res, |
| 2901 | DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT))); |
| 2902 | return Res; |
| 2903 | } |
| 2904 | |
| 2905 | /// isBSwapHWordElement - Return true if the specified node is an element |
| 2906 | /// that makes up a 32-bit packed halfword byteswap. i.e. |
| 2907 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| 2908 | static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) { |
| 2909 | if (!N.getNode()->hasOneUse()) |
| 2910 | return false; |
| 2911 | |
| 2912 | unsigned Opc = N.getOpcode(); |
| 2913 | if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL) |
| 2914 | return false; |
| 2915 | |
| 2916 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2917 | if (!N1C) |
| 2918 | return false; |
| 2919 | |
| 2920 | unsigned Num; |
| 2921 | switch (N1C->getZExtValue()) { |
| 2922 | default: |
| 2923 | return false; |
| 2924 | case 0xFF: Num = 0; break; |
| 2925 | case 0xFF00: Num = 1; break; |
| 2926 | case 0xFF0000: Num = 2; break; |
| 2927 | case 0xFF000000: Num = 3; break; |
| 2928 | } |
| 2929 | |
| 2930 | // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00). |
| 2931 | SDValue N0 = N.getOperand(0); |
| 2932 | if (Opc == ISD::AND) { |
| 2933 | if (Num == 0 || Num == 2) { |
| 2934 | // (x >> 8) & 0xff |
| 2935 | // (x >> 8) & 0xff0000 |
| 2936 | if (N0.getOpcode() != ISD::SRL) |
| 2937 | return false; |
| 2938 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2939 | if (!C || C->getZExtValue() != 8) |
| 2940 | return false; |
| 2941 | } else { |
| 2942 | // (x << 8) & 0xff00 |
| 2943 | // (x << 8) & 0xff000000 |
| 2944 | if (N0.getOpcode() != ISD::SHL) |
| 2945 | return false; |
| 2946 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2947 | if (!C || C->getZExtValue() != 8) |
| 2948 | return false; |
| 2949 | } |
| 2950 | } else if (Opc == ISD::SHL) { |
| 2951 | // (x & 0xff) << 8 |
| 2952 | // (x & 0xff0000) << 8 |
| 2953 | if (Num != 0 && Num != 2) |
| 2954 | return false; |
| 2955 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2956 | if (!C || C->getZExtValue() != 8) |
| 2957 | return false; |
| 2958 | } else { // Opc == ISD::SRL |
| 2959 | // (x & 0xff00) >> 8 |
| 2960 | // (x & 0xff000000) >> 8 |
| 2961 | if (Num != 1 && Num != 3) |
| 2962 | return false; |
| 2963 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2964 | if (!C || C->getZExtValue() != 8) |
| 2965 | return false; |
| 2966 | } |
| 2967 | |
| 2968 | if (Parts[Num]) |
| 2969 | return false; |
| 2970 | |
| 2971 | Parts[Num] = N0.getOperand(0).getNode(); |
| 2972 | return true; |
| 2973 | } |
| 2974 | |
| 2975 | /// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is |
| 2976 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| 2977 | /// => (rotl (bswap x), 16) |
| 2978 | SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) { |
| 2979 | if (!LegalOperations) |
| 2980 | return SDValue(); |
| 2981 | |
| 2982 | EVT VT = N->getValueType(0); |
| 2983 | if (VT != MVT::i32) |
| 2984 | return SDValue(); |
| 2985 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 2986 | return SDValue(); |
| 2987 | |
| 2988 | SmallVector<SDNode*,4> Parts(4, (SDNode*)0); |
| 2989 | // Look for either |
| 2990 | // (or (or (and), (and)), (or (and), (and))) |
| 2991 | // (or (or (or (and), (and)), (and)), (and)) |
| 2992 | if (N0.getOpcode() != ISD::OR) |
| 2993 | return SDValue(); |
| 2994 | SDValue N00 = N0.getOperand(0); |
| 2995 | SDValue N01 = N0.getOperand(1); |
| 2996 | |
Evan Cheng | 9a65a01 | 2012-12-13 01:34:32 +0000 | [diff] [blame] | 2997 | if (N1.getOpcode() == ISD::OR && |
| 2998 | N00.getNumOperands() == 2 && N01.getNumOperands() == 2) { |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2999 | // (or (or (and), (and)), (or (and), (and))) |
| 3000 | SDValue N000 = N00.getOperand(0); |
| 3001 | if (!isBSwapHWordElement(N000, Parts)) |
| 3002 | return SDValue(); |
| 3003 | |
| 3004 | SDValue N001 = N00.getOperand(1); |
| 3005 | if (!isBSwapHWordElement(N001, Parts)) |
| 3006 | return SDValue(); |
| 3007 | SDValue N010 = N01.getOperand(0); |
| 3008 | if (!isBSwapHWordElement(N010, Parts)) |
| 3009 | return SDValue(); |
| 3010 | SDValue N011 = N01.getOperand(1); |
| 3011 | if (!isBSwapHWordElement(N011, Parts)) |
| 3012 | return SDValue(); |
| 3013 | } else { |
| 3014 | // (or (or (or (and), (and)), (and)), (and)) |
| 3015 | if (!isBSwapHWordElement(N1, Parts)) |
| 3016 | return SDValue(); |
| 3017 | if (!isBSwapHWordElement(N01, Parts)) |
| 3018 | return SDValue(); |
| 3019 | if (N00.getOpcode() != ISD::OR) |
| 3020 | return SDValue(); |
| 3021 | SDValue N000 = N00.getOperand(0); |
| 3022 | if (!isBSwapHWordElement(N000, Parts)) |
| 3023 | return SDValue(); |
| 3024 | SDValue N001 = N00.getOperand(1); |
| 3025 | if (!isBSwapHWordElement(N001, Parts)) |
| 3026 | return SDValue(); |
| 3027 | } |
| 3028 | |
| 3029 | // Make sure the parts are all coming from the same node. |
| 3030 | if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3]) |
| 3031 | return SDValue(); |
| 3032 | |
| 3033 | SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, |
| 3034 | SDValue(Parts[0],0)); |
| 3035 | |
| 3036 | // Result of the bswap should be rotated by 16. If it's not legal, than |
| 3037 | // do (x << 16) | (x >> 16). |
| 3038 | SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT)); |
| 3039 | if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT)) |
| 3040 | return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3041 | if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT)) |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3042 | return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt); |
| 3043 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, |
| 3044 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt), |
| 3045 | DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt)); |
| 3046 | } |
| 3047 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3048 | SDValue DAGCombiner::visitOR(SDNode *N) { |
| 3049 | SDValue N0 = N->getOperand(0); |
| 3050 | SDValue N1 = N->getOperand(1); |
| 3051 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3052 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3053 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3054 | EVT VT = N1.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3055 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3056 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3057 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3058 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3059 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3060 | |
| 3061 | // fold (or x, 0) -> x, vector edition |
| 3062 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 3063 | return N1; |
| 3064 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 3065 | return N0; |
| 3066 | |
| 3067 | // fold (or x, -1) -> -1, vector edition |
| 3068 | if (ISD::isBuildVectorAllOnes(N0.getNode())) |
| 3069 | return N0; |
| 3070 | if (ISD::isBuildVectorAllOnes(N1.getNode())) |
| 3071 | return N1; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3072 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3073 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3074 | // fold (or x, undef) -> -1 |
Bob Wilson | 8674949 | 2010-06-28 23:40:25 +0000 | [diff] [blame] | 3075 | if (!LegalOperations && |
| 3076 | (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) { |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 3077 | EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT; |
| 3078 | return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); |
| 3079 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3080 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3081 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3082 | return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3083 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3084 | if (N0C && !N1C) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3085 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3086 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3087 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3088 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3089 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3090 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3091 | return N1; |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3092 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3093 | if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3094 | return N1; |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3095 | |
| 3096 | // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16) |
| 3097 | SDValue BSwap = MatchBSwapHWord(N, N0, N1); |
| 3098 | if (BSwap.getNode() != 0) |
| 3099 | return BSwap; |
| 3100 | BSwap = MatchBSwapHWordLow(N, N0, N1); |
| 3101 | if (BSwap.getNode() != 0) |
| 3102 | return BSwap; |
| 3103 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3104 | // reassociate or |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 3105 | SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3106 | if (ROR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3107 | return ROR; |
| 3108 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3109 | // iff (c1 & c2) == 0. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3110 | if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3111 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3112 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
Bill Wendling | 32f9eb2 | 2010-03-03 01:58:01 +0000 | [diff] [blame] | 3113 | if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 3114 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 3115 | DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 3116 | N0.getOperand(0), N1), |
| 3117 | DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3118 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3119 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 3120 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 3121 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 3122 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3123 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3124 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3125 | LL.getValueType().isInteger()) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3126 | // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) |
| 3127 | // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3128 | if (cast<ConstantSDNode>(LR)->isNullValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3129 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3130 | SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(), |
| 3131 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3132 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3133 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3134 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3135 | // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) |
| 3136 | // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3137 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3138 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3139 | SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(), |
| 3140 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3141 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3142 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | // canonicalize equivalent to ll == rl |
| 3146 | if (LL == RR && LR == RL) { |
| 3147 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 3148 | std::swap(RL, RR); |
| 3149 | } |
| 3150 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3151 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3152 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 3153 | if (Result != ISD::SETCC_INVALID && |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3154 | (!LegalOperations || |
Owen Anderson | 39125d9 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 3155 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
| 3156 | TLI.isOperationLegal(ISD::SETCC, |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 3157 | getSetCCResultType(N0.getValueType()))))) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3158 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 3159 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3160 | } |
| 3161 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3162 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3163 | // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3164 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3165 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3166 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3167 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3168 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3169 | // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible. |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3170 | if (N0.getOpcode() == ISD::AND && |
| 3171 | N1.getOpcode() == ISD::AND && |
| 3172 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 3173 | N1.getOperand(1).getOpcode() == ISD::Constant && |
| 3174 | // Don't increase # computations. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3175 | (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3176 | // We can only do this xform if we know that bits from X that are set in C2 |
| 3177 | // but not in C1 are already zero. Likewise for Y. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3178 | const APInt &LHSMask = |
| 3179 | cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 3180 | const APInt &RHSMask = |
| 3181 | cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3182 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3183 | if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
| 3184 | DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3185 | SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 3186 | N0.getOperand(0), N1.getOperand(0)); |
| 3187 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X, |
| 3188 | DAG.getConstant(LHSMask | RHSMask, VT)); |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3189 | } |
| 3190 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3191 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3192 | // See if this is some rotate idiom. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3193 | if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3194 | return SDValue(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3195 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3196 | // Simplify the operands using demanded-bits information. |
| 3197 | if (!VT.isVector() && |
| 3198 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3199 | return SDValue(N, 0); |
| 3200 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3201 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3204 | /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3205 | static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3206 | if (Op.getOpcode() == ISD::AND) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3207 | if (isa<ConstantSDNode>(Op.getOperand(1))) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3208 | Mask = Op.getOperand(1); |
| 3209 | Op = Op.getOperand(0); |
| 3210 | } else { |
| 3211 | return false; |
| 3212 | } |
| 3213 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3214 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3215 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 3216 | Shift = Op; |
| 3217 | return true; |
| 3218 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3219 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3220 | return false; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3223 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 3224 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 3225 | // a rot[lr]. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3226 | SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3227 | // Must be a legal type. Expanded 'n promoted things won't work with rotates. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3228 | EVT VT = LHS.getValueType(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3229 | if (!TLI.isTypeLegal(VT)) return 0; |
| 3230 | |
| 3231 | // The target must have at least one rotate flavor. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3232 | bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); |
| 3233 | bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3234 | if (!HasROTL && !HasROTR) return 0; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3236 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3237 | SDValue LHSShift; // The shift. |
| 3238 | SDValue LHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3239 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 3240 | return 0; // Not part of a rotate. |
| 3241 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3242 | SDValue RHSShift; // The shift. |
| 3243 | SDValue RHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3244 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 3245 | return 0; // Not part of a rotate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3246 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3247 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 3248 | return 0; // Not shifting the same value. |
| 3249 | |
| 3250 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 3251 | return 0; // Shifts must disagree. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3252 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3253 | // Canonicalize shl to left side in a shl/srl pair. |
| 3254 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 3255 | std::swap(LHS, RHS); |
| 3256 | std::swap(LHSShift, RHSShift); |
| 3257 | std::swap(LHSMask , RHSMask ); |
| 3258 | } |
| 3259 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3260 | unsigned OpSizeInBits = VT.getSizeInBits(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3261 | SDValue LHSShiftArg = LHSShift.getOperand(0); |
| 3262 | SDValue LHSShiftAmt = LHSShift.getOperand(1); |
| 3263 | SDValue RHSShiftAmt = RHSShift.getOperand(1); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3264 | |
| 3265 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 3266 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3267 | if (LHSShiftAmt.getOpcode() == ISD::Constant && |
| 3268 | RHSShiftAmt.getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3269 | uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue(); |
| 3270 | uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3271 | if ((LShVal + RShVal) != OpSizeInBits) |
| 3272 | return 0; |
| 3273 | |
Craig Topper | 32b7343 | 2012-09-29 06:54:22 +0000 | [diff] [blame] | 3274 | SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
| 3275 | LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3277 | // If there is an AND of either shifted operand, apply it to the result. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3278 | if (LHSMask.getNode() || RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3279 | APInt Mask = APInt::getAllOnesValue(OpSizeInBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3280 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3281 | if (LHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3282 | APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal); |
| 3283 | Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3284 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3285 | if (RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3286 | APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal); |
| 3287 | Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3288 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3289 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3290 | Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT)); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3291 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3292 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3293 | return Rot.getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3294 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3295 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3296 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 3297 | // that we're masking out the right stuff. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3298 | if (LHSMask.getNode() || RHSMask.getNode()) |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3299 | return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3300 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3301 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 3302 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y)) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3303 | if (RHSShiftAmt.getOpcode() == ISD::SUB && |
| 3304 | LHSShiftAmt == RHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3305 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3306 | dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3307 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Craig Topper | 32b7343 | 2012-09-29 06:54:22 +0000 | [diff] [blame] | 3308 | return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg, |
| 3309 | HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3310 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3311 | } |
| 3312 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3313 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3314 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 3315 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y)) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3316 | if (LHSShiftAmt.getOpcode() == ISD::SUB && |
| 3317 | RHSShiftAmt == LHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3318 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3319 | dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3320 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Craig Topper | 32b7343 | 2012-09-29 06:54:22 +0000 | [diff] [blame] | 3321 | return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg, |
| 3322 | HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3323 | } |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3327 | // Look for sign/zext/any-extended or truncate cases: |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3328 | if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || |
| 3329 | LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || |
| 3330 | LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || |
| 3331 | LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && |
| 3332 | (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || |
| 3333 | RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || |
| 3334 | RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || |
| 3335 | RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3336 | SDValue LExtOp0 = LHSShiftAmt.getOperand(0); |
| 3337 | SDValue RExtOp0 = RHSShiftAmt.getOperand(0); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3338 | if (RExtOp0.getOpcode() == ISD::SUB && |
| 3339 | RExtOp0.getOperand(1) == LExtOp0) { |
| 3340 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3341 | // (rotl x, y) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3342 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3343 | // (rotr x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3344 | if (ConstantSDNode *SUBC = |
| 3345 | dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3346 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3347 | return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
| 3348 | LHSShiftArg, |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3349 | HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | } else if (LExtOp0.getOpcode() == ISD::SUB && |
| 3353 | RExtOp0 == LExtOp0.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3354 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3355 | // (rotr x, y) |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 3356 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3357 | // (rotl x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3358 | if (ConstantSDNode *SUBC = |
| 3359 | dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3360 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3361 | return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, |
| 3362 | LHSShiftArg, |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 3363 | HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3364 | } |
| 3365 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3366 | } |
| 3367 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3368 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3369 | return 0; |
| 3370 | } |
| 3371 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3372 | SDValue DAGCombiner::visitXOR(SDNode *N) { |
| 3373 | SDValue N0 = N->getOperand(0); |
| 3374 | SDValue N1 = N->getOperand(1); |
| 3375 | SDValue LHS, RHS, CC; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3376 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3377 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3378 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3379 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3380 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3381 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3382 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3383 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3384 | |
| 3385 | // fold (xor x, 0) -> x, vector edition |
| 3386 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 3387 | return N1; |
| 3388 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 3389 | return N0; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3390 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3391 | |
Evan Cheng | 26471c4 | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 3392 | // fold (xor undef, undef) -> 0. This is a common idiom (misuse). |
| 3393 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 3394 | return DAG.getConstant(0, VT); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3395 | // fold (xor x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 3396 | if (N0.getOpcode() == ISD::UNDEF) |
| 3397 | return N0; |
| 3398 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3399 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3400 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3401 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3402 | return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3403 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3404 | if (N0C && !N1C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3405 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3406 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3407 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3408 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3409 | // reassociate xor |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 3410 | SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3411 | if (RXOR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3412 | return RXOR; |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3413 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3414 | // fold !(x cc y) -> (x !cc y) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3415 | if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3416 | bool isInt = LHS.getValueType().isInteger(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3417 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 3418 | isInt); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3419 | |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3420 | if (!LegalOperations || |
| 3421 | TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) { |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3422 | switch (N0.getOpcode()) { |
| 3423 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3424 | llvm_unreachable("Unhandled SetCC Equivalent!"); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3425 | case ISD::SETCC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3426 | return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3427 | case ISD::SELECT_CC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3428 | return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2), |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3429 | N0.getOperand(3), NotCC); |
| 3430 | } |
| 3431 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3432 | } |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3433 | |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3434 | // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3435 | if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3436 | N0.getNode()->hasOneUse() && |
| 3437 | isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3438 | SDValue V = N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3439 | V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V, |
Duncan Sands | 272dce0 | 2007-10-10 09:54:50 +0000 | [diff] [blame] | 3440 | DAG.getConstant(1, V.getValueType())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3441 | AddToWorkList(V.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3442 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V); |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3443 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3444 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3445 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3446 | if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3447 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3448 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3449 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 3450 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3451 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 3452 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3453 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3454 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3455 | } |
| 3456 | } |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3457 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3458 | if (N1C && N1C->isAllOnesValue() && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3459 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3460 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3461 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 3462 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3463 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 3464 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3465 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3466 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3467 | } |
| 3468 | } |
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3469 | // fold (xor (and x, y), y) -> (and (not x), y) |
| 3470 | if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
| 3471 | N0->getOperand(1) == N1) { |
| 3472 | SDValue X = N0->getOperand(0); |
Benjamin Kramer | 768ebcd | 2013-05-10 14:09:52 +0000 | [diff] [blame] | 3473 | SDValue NotX = DAG.getNOT(X.getDebugLoc(), X, VT); |
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3474 | AddToWorkList(NotX.getNode()); |
| 3475 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NotX, N1); |
| 3476 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3477 | // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3478 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 3479 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 3480 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3481 | if (N00C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3482 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1), |
| 3483 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3484 | N00C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3485 | if (N01C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3486 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3487 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3488 | N01C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3489 | } |
| 3490 | // fold (xor x, x) -> 0 |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 3491 | if (N0 == N1) |
| 3492 | return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3493 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3494 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 3495 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3496 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3497 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3498 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3499 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3500 | // Simplify the expression using non-local knowledge. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3501 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3502 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3503 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3504 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3505 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3508 | /// visitShiftByConstant - Handle transforms common to the three shifts, when |
| 3509 | /// the shift amount is a constant. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3510 | SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3511 | SDNode *LHS = N->getOperand(0).getNode(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3512 | if (!LHS->hasOneUse()) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3513 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3514 | // We want to pull some binops through shifts, so that we have (and (shift)) |
| 3515 | // instead of (shift (and)), likewise for add, or, xor, etc. This sort of |
| 3516 | // thing happens with address calculations, so it's important to canonicalize |
| 3517 | // it. |
| 3518 | bool HighBitSet = false; // Can we transform this if the high bit is set? |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3519 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3520 | switch (LHS->getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3521 | default: return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3522 | case ISD::OR: |
| 3523 | case ISD::XOR: |
| 3524 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3525 | break; |
| 3526 | case ISD::AND: |
| 3527 | HighBitSet = true; // We can only transform sra if the high bit is set. |
| 3528 | break; |
| 3529 | case ISD::ADD: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3530 | if (N->getOpcode() != ISD::SHL) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3531 | return SDValue(); // only shl(add) not sr[al](add). |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3532 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3533 | break; |
| 3534 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3535 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3536 | // We require the RHS of the binop to be a constant as well. |
| 3537 | ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3538 | if (!BinOpCst) return SDValue(); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3539 | |
| 3540 | // FIXME: disable this unless the input to the binop is a shift by a constant. |
| 3541 | // If it is not a shift, it pessimizes some common cases like: |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3542 | // |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3543 | // void foo(int *X, int i) { X[i & 1235] = 1; } |
| 3544 | // int bar(int *X, int i) { return X[i & 255]; } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3545 | SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3546 | if ((BinOpLHSVal->getOpcode() != ISD::SHL && |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3547 | BinOpLHSVal->getOpcode() != ISD::SRA && |
| 3548 | BinOpLHSVal->getOpcode() != ISD::SRL) || |
| 3549 | !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3550 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3551 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3552 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3553 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3554 | // If this is a signed shift right, and the high bit is modified by the |
| 3555 | // logical operation, do not perform the transformation. The highBitSet |
| 3556 | // boolean indicates the value of the high bit of the constant which would |
| 3557 | // cause it to be modified for this operation. |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3558 | if (N->getOpcode() == ISD::SRA) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3559 | bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); |
| 3560 | if (BinOpRHSSignSet != HighBitSet) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3561 | return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3562 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3563 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3564 | // Fold the constants, shifting the binop RHS by the shift amount. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3565 | SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(), |
| 3566 | N->getValueType(0), |
| 3567 | LHS->getOperand(1), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3568 | |
| 3569 | // Create the new shift. |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3570 | SDValue NewShift = DAG.getNode(N->getOpcode(), |
| 3571 | LHS->getOperand(0).getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3572 | VT, LHS->getOperand(0), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3573 | |
| 3574 | // Create the new binop. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3575 | return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3578 | SDValue DAGCombiner::visitSHL(SDNode *N) { |
| 3579 | SDValue N0 = N->getOperand(0); |
| 3580 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3581 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3582 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3583 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3584 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3585 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3586 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3587 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3588 | return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3589 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3590 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3591 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3592 | // fold (shl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3593 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3594 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3595 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3596 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3597 | return N0; |
Chad Rosier | 92bcd96 | 2011-06-14 22:29:10 +0000 | [diff] [blame] | 3598 | // fold (shl undef, x) -> 0 |
| 3599 | if (N0.getOpcode() == ISD::UNDEF) |
| 3600 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3601 | // if (shl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3602 | if (DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3603 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3604 | return DAG.getConstant(0, VT); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3605 | // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3606 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3607 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3608 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3609 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3610 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3611 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3612 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3613 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3614 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3615 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3616 | DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT, |
| 3617 | DAG.getNode(ISD::TRUNCATE, |
| 3618 | N->getDebugLoc(), |
| 3619 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3620 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3624 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3625 | return SDValue(N, 0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3626 | |
| 3627 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3628 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3629 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3630 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3631 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3632 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3633 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3634 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3635 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3636 | } |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3637 | |
| 3638 | // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) |
| 3639 | // For this to be valid, the second form must not preserve any of the bits |
| 3640 | // that are shifted out by the inner shift in the first form. This means |
| 3641 | // the outer shift size must be >= the number of bits added by the ext. |
| 3642 | // As a corollary, we don't care what kind of ext it is. |
| 3643 | if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 3644 | N0.getOpcode() == ISD::ANY_EXTEND || |
| 3645 | N0.getOpcode() == ISD::SIGN_EXTEND) && |
| 3646 | N0.getOperand(0).getOpcode() == ISD::SHL && |
| 3647 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3648 | uint64_t c1 = |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3649 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3650 | uint64_t c2 = N1C->getZExtValue(); |
| 3651 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3652 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
| 3653 | if (c2 >= OpSizeInBits - InnerShiftSize) { |
| 3654 | if (c1 + c2 >= OpSizeInBits) |
| 3655 | return DAG.getConstant(0, VT); |
| 3656 | return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT, |
| 3657 | DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT, |
| 3658 | N0.getOperand(0)->getOperand(0)), |
| 3659 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| 3660 | } |
| 3661 | } |
| 3662 | |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3663 | // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or |
| 3664 | // (and (srl x, (sub c1, c2), MASK) |
Chandler Carruth | 62dfc51 | 2012-01-05 11:05:55 +0000 | [diff] [blame] | 3665 | // Only fold this if the inner shift has no other uses -- if it does, folding |
| 3666 | // this will increase the total number of instructions. |
| 3667 | if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3668 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3669 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3670 | if (c1 < VT.getSizeInBits()) { |
| 3671 | uint64_t c2 = N1C->getZExtValue(); |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3672 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3673 | VT.getSizeInBits() - c1); |
| 3674 | SDValue Shift; |
| 3675 | if (c2 > c1) { |
| 3676 | Mask = Mask.shl(c2-c1); |
| 3677 | Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3678 | DAG.getConstant(c2-c1, N1.getValueType())); |
| 3679 | } else { |
| 3680 | Mask = Mask.lshr(c1-c2); |
| 3681 | Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3682 | DAG.getConstant(c1-c2, N1.getValueType())); |
| 3683 | } |
| 3684 | return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift, |
| 3685 | DAG.getConstant(Mask, VT)); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3686 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3687 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3688 | // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3689 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) { |
| 3690 | SDValue HiBitsMask = |
| 3691 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3692 | VT.getSizeInBits() - |
| 3693 | N1C->getZExtValue()), |
| 3694 | VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3695 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3696 | HiBitsMask); |
| 3697 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3698 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3699 | if (N1C) { |
| 3700 | SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3701 | if (NewSHL.getNode()) |
| 3702 | return NewSHL; |
| 3703 | } |
| 3704 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3705 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3708 | SDValue DAGCombiner::visitSRA(SDNode *N) { |
| 3709 | SDValue N0 = N->getOperand(0); |
| 3710 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3711 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3712 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3713 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3714 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3715 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3716 | // fold (sra c1, c2) -> (sra c1, c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3717 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3718 | return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3719 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3720 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3721 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3722 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3723 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3724 | return N0; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3725 | // fold (sra x, (setge c, size(x))) -> undef |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3726 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3727 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3728 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3729 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3730 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3731 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 3732 | // sext_inreg. |
| 3733 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3734 | unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3735 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); |
| 3736 | if (VT.isVector()) |
| 3737 | ExtVT = EVT::getVectorVT(*DAG.getContext(), |
| 3738 | ExtVT, VT.getVectorNumElements()); |
| 3739 | if ((!LegalOperations || |
| 3740 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3741 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3742 | N0.getOperand(0), DAG.getValueType(ExtVT)); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3743 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3744 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3745 | // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3746 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 3747 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3748 | unsigned Sum = N1C->getZExtValue() + C1->getZExtValue(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3749 | if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3750 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3751 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 3752 | } |
| 3753 | } |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3754 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3755 | // fold (sra (shl X, m), (sub result_size, n)) |
| 3756 | // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3757 | // result_size - n != m. |
| 3758 | // If truncate is free for the target sext(shl) is likely to result in better |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3759 | // code. |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3760 | if (N0.getOpcode() == ISD::SHL) { |
| 3761 | // Get the two constanst of the shifts, CN0 = m, CN = n. |
| 3762 | const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3763 | if (N01C && N1C) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3764 | // Determine what the truncate's result bitsize and type would be. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3765 | EVT TruncVT = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3766 | EVT::getIntegerVT(*DAG.getContext(), |
| 3767 | OpSizeInBits - N1C->getZExtValue()); |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3768 | // Determine the residual right-shift amount. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3769 | signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3770 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3771 | // If the shift is not a no-op (in which case this should be just a sign |
| 3772 | // extend already), the truncated to type is legal, sign_extend is legal |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 3773 | // on that type, and the truncate to that type is both legal and free, |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3774 | // perform the transform. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3775 | if ((ShiftAmt > 0) && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3776 | TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && |
| 3777 | TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 3778 | TLI.isTruncateFree(VT, TruncVT)) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3779 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3780 | SDValue Amt = DAG.getConstant(ShiftAmt, |
| 3781 | getShiftAmountTy(N0.getOperand(0).getValueType())); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3782 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, |
| 3783 | N0.getOperand(0), Amt); |
| 3784 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT, |
| 3785 | Shift); |
| 3786 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), |
| 3787 | N->getValueType(0), Trunc); |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3788 | } |
| 3789 | } |
| 3790 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3791 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3792 | // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3793 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3794 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3795 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3796 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3797 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3798 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3799 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3800 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3801 | TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3802 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3803 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3804 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3805 | DAG.getNode(ISD::TRUNCATE, |
| 3806 | N->getDebugLoc(), |
| 3807 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3808 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3812 | // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2)) |
| 3813 | // if c1 is equal to the number of bits the trunc removes |
| 3814 | if (N0.getOpcode() == ISD::TRUNCATE && |
| 3815 | (N0.getOperand(0).getOpcode() == ISD::SRL || |
| 3816 | N0.getOperand(0).getOpcode() == ISD::SRA) && |
| 3817 | N0.getOperand(0).hasOneUse() && |
| 3818 | N0.getOperand(0).getOperand(1).hasOneUse() && |
| 3819 | N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) { |
| 3820 | EVT LargeVT = N0.getOperand(0).getValueType(); |
| 3821 | ConstantSDNode *LargeShiftAmt = |
| 3822 | cast<ConstantSDNode>(N0.getOperand(0).getOperand(1)); |
| 3823 | |
| 3824 | if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits == |
| 3825 | LargeShiftAmt->getZExtValue()) { |
| 3826 | SDValue Amt = |
| 3827 | DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3828 | getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType())); |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3829 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT, |
| 3830 | N0.getOperand(0).getOperand(0), Amt); |
| 3831 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA); |
| 3832 | } |
| 3833 | } |
| 3834 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3835 | // Simplify, based on bits shifted out of the LHS. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3836 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3837 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3838 | |
| 3839 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3840 | // If the sign bit is known to be zero, switch this to a SRL. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3841 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3842 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3843 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3844 | if (N1C) { |
| 3845 | SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3846 | if (NewSRA.getNode()) |
| 3847 | return NewSRA; |
| 3848 | } |
| 3849 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3850 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3853 | SDValue DAGCombiner::visitSRL(SDNode *N) { |
| 3854 | SDValue N0 = N->getOperand(0); |
| 3855 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3856 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3857 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3858 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3859 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3860 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3861 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3862 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3863 | return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3864 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3865 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3866 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3867 | // fold (srl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3868 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3869 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3870 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3871 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3872 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3873 | // if (srl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3874 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3875 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3876 | return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3877 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3878 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3879 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3880 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3881 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3882 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3883 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3884 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3885 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3886 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3887 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3888 | |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3889 | // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2))) |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3890 | if (N1C && N0.getOpcode() == ISD::TRUNCATE && |
| 3891 | N0.getOperand(0).getOpcode() == ISD::SRL && |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3892 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3893 | uint64_t c1 = |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3894 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3895 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3896 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3897 | EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3898 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3899 | // This is only valid if the OpSizeInBits + c1 = size of inner shift. |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3900 | if (c1 + OpSizeInBits == InnerShiftSize) { |
| 3901 | if (c1 + c2 >= InnerShiftSize) |
| 3902 | return DAG.getConstant(0, VT); |
| 3903 | return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3904 | DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT, |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3905 | N0.getOperand(0)->getOperand(0), |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3906 | DAG.getConstant(c1 + c2, ShiftCountVT))); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | |
Chris Lattner | efcddc3 | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 3910 | // fold (srl (shl x, c), c) -> (and x, cst2) |
| 3911 | if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && |
| 3912 | N0.getValueSizeInBits() <= 64) { |
| 3913 | uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits(); |
| 3914 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3915 | DAG.getConstant(~0ULL >> ShAmt, VT)); |
| 3916 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3917 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3918 | |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3919 | // fold (srl (anyextend x), c) -> (anyextend (srl x, c)) |
| 3920 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 3921 | // Shifting in all undef bits? |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3922 | EVT SmallVT = N0.getOperand(0).getValueType(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3923 | if (N1C->getZExtValue() >= SmallVT.getSizeInBits()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3924 | return DAG.getUNDEF(VT); |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3925 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3926 | if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 3927 | uint64_t ShiftAmt = N1C->getZExtValue(); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3928 | SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT, |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 3929 | N0.getOperand(0), |
| 3930 | DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT))); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3931 | AddToWorkList(SmallShift.getNode()); |
| 3932 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift); |
| 3933 | } |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3934 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3935 | |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3936 | // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign |
| 3937 | // bit, which is unmodified by sra. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3938 | if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) { |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3939 | if (N0.getOpcode() == ISD::SRA) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3940 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1); |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3941 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3942 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3943 | // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3944 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3945 | N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3946 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 3947 | DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3948 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3949 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 3950 | // zeros, thus the result of the srl will always be zero. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3951 | if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3952 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3953 | // If all of the bits input the to ctlz node are known to be zero, then |
| 3954 | // the result of the ctlz is "32" and the result of the shift is one. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 3955 | APInt UnknownBits = ~KnownZero; |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3956 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3957 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3958 | // Otherwise, check to see if there is exactly one bit input to the ctlz. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3959 | if ((UnknownBits & (UnknownBits - 1)) == 0) { |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3960 | // Okay, we know that only that the single bit specified by UnknownBits |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3961 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 3962 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 3963 | // to an SRL/XOR pair, which is likely to simplify more. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3964 | unsigned ShAmt = UnknownBits.countTrailingZeros(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3965 | SDValue Op = N0.getOperand(0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3966 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3967 | if (ShAmt) { |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3968 | Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3969 | DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3970 | AddToWorkList(Op.getNode()); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3971 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3972 | |
| 3973 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 3974 | Op, DAG.getConstant(1, VT)); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3975 | } |
| 3976 | } |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3977 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3978 | // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3979 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3980 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3981 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3982 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3983 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3984 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3985 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3986 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3987 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3988 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3989 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3990 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3991 | DAG.getNode(ISD::TRUNCATE, |
| 3992 | N->getDebugLoc(), |
| 3993 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3994 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3995 | } |
| 3996 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3997 | |
Chris Lattner | 61a4c07 | 2007-04-18 03:06:49 +0000 | [diff] [blame] | 3998 | // fold operands of srl based on knowledge that the low bits are not |
| 3999 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4000 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 4001 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4002 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4003 | if (N1C) { |
| 4004 | SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 4005 | if (NewSRL.getNode()) |
| 4006 | return NewSRL; |
| 4007 | } |
| 4008 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4009 | // Attempt to convert a srl of a load into a narrower zero-extending load. |
| 4010 | SDValue NarrowLoad = ReduceLoadWidth(N); |
| 4011 | if (NarrowLoad.getNode()) |
| 4012 | return NarrowLoad; |
| 4013 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4014 | // Here is a common situation. We want to optimize: |
| 4015 | // |
| 4016 | // %a = ... |
| 4017 | // %b = and i32 %a, 2 |
| 4018 | // %c = srl i32 %b, 1 |
| 4019 | // brcond i32 %c ... |
| 4020 | // |
| 4021 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4022 | // |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4023 | // %a = ... |
| 4024 | // %b = and %a, 2 |
| 4025 | // %c = setcc eq %b, 0 |
| 4026 | // brcond %c ... |
| 4027 | // |
| 4028 | // However when after the source operand of SRL is optimized into AND, the SRL |
| 4029 | // itself may not be optimized further. Look for it and add the BRCOND into |
| 4030 | // the worklist. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 4031 | if (N->hasOneUse()) { |
| 4032 | SDNode *Use = *N->use_begin(); |
| 4033 | if (Use->getOpcode() == ISD::BRCOND) |
| 4034 | AddToWorkList(Use); |
| 4035 | else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { |
| 4036 | // Also look pass the truncate. |
| 4037 | Use = *Use->use_begin(); |
| 4038 | if (Use->getOpcode() == ISD::BRCOND) |
| 4039 | AddToWorkList(Use); |
| 4040 | } |
| 4041 | } |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4042 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4043 | return SDValue(); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4046 | SDValue DAGCombiner::visitCTLZ(SDNode *N) { |
| 4047 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4048 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4049 | |
| 4050 | // fold (ctlz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4051 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4052 | return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4053 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4056 | SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) { |
| 4057 | SDValue N0 = N->getOperand(0); |
| 4058 | EVT VT = N->getValueType(0); |
| 4059 | |
| 4060 | // fold (ctlz_zero_undef c1) -> c2 |
| 4061 | if (isa<ConstantSDNode>(N0)) |
| 4062 | return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0); |
| 4063 | return SDValue(); |
| 4064 | } |
| 4065 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4066 | SDValue DAGCombiner::visitCTTZ(SDNode *N) { |
| 4067 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4068 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4069 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4070 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4071 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4072 | return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4073 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4076 | SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) { |
| 4077 | SDValue N0 = N->getOperand(0); |
| 4078 | EVT VT = N->getValueType(0); |
| 4079 | |
| 4080 | // fold (cttz_zero_undef c1) -> c2 |
| 4081 | if (isa<ConstantSDNode>(N0)) |
| 4082 | return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0); |
| 4083 | return SDValue(); |
| 4084 | } |
| 4085 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4086 | SDValue DAGCombiner::visitCTPOP(SDNode *N) { |
| 4087 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4088 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4089 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4090 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4091 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4092 | return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4093 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4096 | SDValue DAGCombiner::visitSELECT(SDNode *N) { |
| 4097 | SDValue N0 = N->getOperand(0); |
| 4098 | SDValue N1 = N->getOperand(1); |
| 4099 | SDValue N2 = N->getOperand(2); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4100 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 4101 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 4102 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4103 | EVT VT = N->getValueType(0); |
| 4104 | EVT VT0 = N0.getValueType(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4105 | |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4106 | // fold (select C, X, X) -> X |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4107 | if (N1 == N2) |
| 4108 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4109 | // fold (select true, X, Y) -> X |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4110 | if (N0C && !N0C->isNullValue()) |
| 4111 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4112 | // fold (select false, X, Y) -> Y |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4113 | if (N0C && N0C->isNullValue()) |
| 4114 | return N2; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4115 | // fold (select C, 1, X) -> (or C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4116 | if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4117 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 4118 | // fold (select C, 0, 1) -> (xor C, 1) |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4119 | if (VT.isInteger() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4120 | (VT0 == MVT::i1 || |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4121 | (VT0.isInteger() && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 4122 | TLI.getBooleanContents(false) == |
| 4123 | TargetLowering::ZeroOrOneBooleanContent)) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4124 | N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4125 | SDValue XORNode; |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4126 | if (VT == VT0) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4127 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0, |
| 4128 | N0, DAG.getConstant(1, VT0)); |
| 4129 | XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0, |
| 4130 | N0, DAG.getConstant(1, VT0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4131 | AddToWorkList(XORNode.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4132 | if (VT.bitsGT(VT0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4133 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode); |
| 4134 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode); |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4135 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4136 | // fold (select C, 0, X) -> (and (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4137 | if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) { |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4138 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4139 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4140 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4141 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4142 | // fold (select C, X, 1) -> (or (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4143 | if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4144 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4145 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4146 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4147 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4148 | // fold (select C, X, 0) -> (and C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4149 | if (VT == MVT::i1 && N2C && N2C->isNullValue()) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4150 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
| 4151 | // fold (select X, X, Y) -> (or X, Y) |
| 4152 | // fold (select X, 1, Y) -> (or X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4153 | if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4154 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 4155 | // fold (select X, Y, X) -> (and X, Y) |
| 4156 | // fold (select X, Y, 0) -> (and X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4157 | if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4158 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4160 | // If we can fold this based on the true/false value, do so. |
| 4161 | if (SimplifySelectOps(N, N1, N2)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4162 | return SDValue(N, 0); // Don't revisit N. |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4163 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4164 | // fold selects based on a setcc into other things, such as min/max/abs |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4165 | if (N0.getOpcode() == ISD::SETCC) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4166 | // FIXME: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4167 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4168 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 4169 | // about, since there is no way to mark an opcode illegal at all value types |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4170 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) && |
Dan Gohman | 4ea4804 | 2009-08-02 16:19:38 +0000 | [diff] [blame] | 4171 | TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4172 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, |
| 4173 | N0.getOperand(0), N0.getOperand(1), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4174 | N1, N2, N0.getOperand(2)); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 4175 | return SimplifySelect(N->getDebugLoc(), N0, N1, N2); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4176 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4177 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4178 | return SDValue(); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4181 | SDValue DAGCombiner::visitVSELECT(SDNode *N) { |
| 4182 | SDValue N0 = N->getOperand(0); |
| 4183 | SDValue N1 = N->getOperand(1); |
| 4184 | SDValue N2 = N->getOperand(2); |
| 4185 | DebugLoc DL = N->getDebugLoc(); |
| 4186 | |
| 4187 | // Canonicalize integer abs. |
| 4188 | // vselect (setg[te] X, 0), X, -X -> |
| 4189 | // vselect (setgt X, -1), X, -X -> |
| 4190 | // vselect (setl[te] X, 0), -X, X -> |
| 4191 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 4192 | if (N0.getOpcode() == ISD::SETCC) { |
| 4193 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| 4194 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); |
| 4195 | bool isAbs = false; |
| 4196 | bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); |
| 4197 | |
| 4198 | if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 4199 | (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) && |
| 4200 | N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1)) |
| 4201 | isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode()); |
| 4202 | else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) && |
| 4203 | N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1)) |
| 4204 | isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode()); |
| 4205 | |
| 4206 | if (isAbs) { |
| 4207 | EVT VT = LHS.getValueType(); |
| 4208 | SDValue Shift = DAG.getNode( |
| 4209 | ISD::SRA, DL, VT, LHS, |
| 4210 | DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT)); |
| 4211 | SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift); |
| 4212 | AddToWorkList(Shift.getNode()); |
| 4213 | AddToWorkList(Add.getNode()); |
| 4214 | return DAG.getNode(ISD::XOR, DL, VT, Add, Shift); |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | return SDValue(); |
| 4219 | } |
| 4220 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4221 | SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { |
| 4222 | SDValue N0 = N->getOperand(0); |
| 4223 | SDValue N1 = N->getOperand(1); |
| 4224 | SDValue N2 = N->getOperand(2); |
| 4225 | SDValue N3 = N->getOperand(3); |
| 4226 | SDValue N4 = N->getOperand(4); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4227 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4228 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4229 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 4230 | if (N2 == N3) |
| 4231 | return N2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4232 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4233 | // Determine if the condition we're dealing with is constant |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 4234 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4235 | N0, N1, CC, N->getDebugLoc(), false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4236 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4237 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4238 | if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4239 | if (!SCCC->isNullValue()) |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4240 | return N2; // cond always true -> true val |
| 4241 | else |
| 4242 | return N3; // cond always false -> false val |
| 4243 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4244 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4245 | // Fold to a simpler select_cc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4246 | if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4247 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(), |
| 4248 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4249 | SCC.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4250 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4251 | // If we can fold this based on the true/false value, do so. |
| 4252 | if (SimplifySelectOps(N, N2, N3)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4253 | return SDValue(N, 0); // Don't revisit N. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4254 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4255 | // fold select_cc into other things, such as min/max/abs |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4256 | return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4259 | SDValue DAGCombiner::visitSETCC(SDNode *N) { |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4260 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4261 | cast<CondCodeSDNode>(N->getOperand(2))->get(), |
| 4262 | N->getDebugLoc()); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4265 | // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4266 | // "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))" |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4267 | // transformation. Returns true if extension are possible and the above |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4268 | // mentioned transformation is profitable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4269 | static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4270 | unsigned ExtOpc, |
| 4271 | SmallVector<SDNode*, 4> &ExtendNodes, |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 4272 | const TargetLowering &TLI) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4273 | bool HasCopyToRegUses = false; |
| 4274 | bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4275 | for (SDNode::use_iterator UI = N0.getNode()->use_begin(), |
| 4276 | UE = N0.getNode()->use_end(); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4277 | UI != UE; ++UI) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4278 | SDNode *User = *UI; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4279 | if (User == N) |
| 4280 | continue; |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4281 | if (UI.getUse().getResNo() != N0.getResNo()) |
| 4282 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4283 | // FIXME: Only extend SETCC N, N and SETCC N, c for now. |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4284 | if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4285 | ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); |
| 4286 | if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) |
| 4287 | // Sign bits will be lost after a zext. |
| 4288 | return false; |
| 4289 | bool Add = false; |
| 4290 | for (unsigned i = 0; i != 2; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4291 | SDValue UseOp = User->getOperand(i); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4292 | if (UseOp == N0) |
| 4293 | continue; |
| 4294 | if (!isa<ConstantSDNode>(UseOp)) |
| 4295 | return false; |
| 4296 | Add = true; |
| 4297 | } |
| 4298 | if (Add) |
| 4299 | ExtendNodes.push_back(User); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4300 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4301 | } |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4302 | // If truncates aren't free and there are users we can't |
| 4303 | // extend, it isn't worthwhile. |
| 4304 | if (!isTruncFree) |
| 4305 | return false; |
| 4306 | // Remember if this value is live-out. |
| 4307 | if (User->getOpcode() == ISD::CopyToReg) |
| 4308 | HasCopyToRegUses = true; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | if (HasCopyToRegUses) { |
| 4312 | bool BothLiveOut = false; |
| 4313 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 4314 | UI != UE; ++UI) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4315 | SDUse &Use = UI.getUse(); |
| 4316 | if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { |
| 4317 | BothLiveOut = true; |
| 4318 | break; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4319 | } |
| 4320 | } |
| 4321 | if (BothLiveOut) |
| 4322 | // Both unextended and extended values are live out. There had better be |
Bob Wilson | bebfbc5 | 2010-11-28 06:51:19 +0000 | [diff] [blame] | 4323 | // a good reason for the transformation. |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4324 | return ExtendNodes.size(); |
| 4325 | } |
| 4326 | return true; |
| 4327 | } |
| 4328 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4329 | void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs, |
| 4330 | SDValue Trunc, SDValue ExtLoad, DebugLoc DL, |
| 4331 | ISD::NodeType ExtType) { |
| 4332 | // Extend SetCC uses if necessary. |
| 4333 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 4334 | SDNode *SetCC = SetCCs[i]; |
| 4335 | SmallVector<SDValue, 4> Ops; |
| 4336 | |
| 4337 | for (unsigned j = 0; j != 2; ++j) { |
| 4338 | SDValue SOp = SetCC->getOperand(j); |
| 4339 | if (SOp == Trunc) |
| 4340 | Ops.push_back(ExtLoad); |
| 4341 | else |
| 4342 | Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp)); |
| 4343 | } |
| 4344 | |
| 4345 | Ops.push_back(SetCC->getOperand(2)); |
| 4346 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), |
| 4347 | &Ops[0], Ops.size())); |
| 4348 | } |
| 4349 | } |
| 4350 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4351 | SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
| 4352 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4353 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4354 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4355 | // fold (sext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4356 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4357 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4358 | |
Nadav Rotem | 0c8607b | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 4359 | // fold (sext (sext x)) -> (sext x) |
| 4360 | // fold (sext (aext x)) -> (sext x) |
| 4361 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
| 4362 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, |
| 4363 | N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4364 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4365 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4366 | // fold (sext (truncate (load x))) -> (sext (smaller load x)) |
| 4367 | // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4368 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4369 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4370 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4371 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4372 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4373 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4374 | AddToWorkList(oye); |
| 4375 | } |
Dan Gohman | c7b3444 | 2009-04-27 02:00:55 +0000 | [diff] [blame] | 4376 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4377 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4378 | |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4379 | // See if the value being truncated is already sign extended. If so, just |
| 4380 | // eliminate the trunc/sext pair. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4381 | SDValue Op = N0.getOperand(0); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4382 | unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits(); |
| 4383 | unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits(); |
| 4384 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4385 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4386 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4387 | if (OpBits == DestBits) { |
| 4388 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 4389 | // bits, it is already ready. |
| 4390 | if (NumSignBits > DestBits-MidBits) |
| 4391 | return Op; |
| 4392 | } else if (OpBits < DestBits) { |
| 4393 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 4394 | // bits, just sext from i32. |
| 4395 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4396 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4397 | } else { |
| 4398 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 4399 | // bits, just truncate to i32. |
| 4400 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4401 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4402 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4403 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4404 | // fold (sext (truncate x)) -> (sextinreg x). |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4405 | if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
| 4406 | N0.getValueType())) { |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4407 | if (OpBits < DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4408 | Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4409 | else if (OpBits > DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4410 | Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op); |
| 4411 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4412 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4413 | } |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4414 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4415 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4416 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4417 | // None of the supported targets knows how to perform load and sign extend |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4418 | // on vectors in one instruction. We only perform this transformation on |
| 4419 | // scalars. |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4420 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4421 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4422 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4423 | bool DoXform = true; |
| 4424 | SmallVector<SDNode*, 4> SetCCs; |
| 4425 | if (!N0.hasOneUse()) |
| 4426 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); |
| 4427 | if (DoXform) { |
| 4428 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4429 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4430 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4431 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4432 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4433 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4434 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4435 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4436 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4437 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4438 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4439 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4440 | ISD::SIGN_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4441 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4442 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 4443 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4444 | |
| 4445 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 4446 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4447 | if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 4448 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4449 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4450 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4451 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4452 | TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4453 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4454 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4455 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4456 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4457 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4458 | LN0->getAlignment()); |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4459 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4460 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4461 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4462 | N0.getValueType(), ExtLoad), |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4463 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4464 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4465 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4466 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4467 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4468 | // fold (sext (and/or/xor (load x), cst)) -> |
| 4469 | // (and/or/xor (sextload x), (sext cst)) |
| 4470 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 4471 | N0.getOpcode() == ISD::XOR) && |
| 4472 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 4473 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4474 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) && |
| 4475 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 4476 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 4477 | if (LN0->getExtensionType() != ISD::ZEXTLOAD) { |
| 4478 | bool DoXform = true; |
| 4479 | SmallVector<SDNode*, 4> SetCCs; |
| 4480 | if (!N0.hasOneUse()) |
| 4481 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND, |
| 4482 | SetCCs, TLI); |
| 4483 | if (DoXform) { |
| 4484 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT, |
| 4485 | LN0->getChain(), LN0->getBasePtr(), |
| 4486 | LN0->getPointerInfo(), |
| 4487 | LN0->getMemoryVT(), |
| 4488 | LN0->isVolatile(), |
| 4489 | LN0->isNonTemporal(), |
| 4490 | LN0->getAlignment()); |
| 4491 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 4492 | Mask = Mask.sext(VT.getSizeInBits()); |
| 4493 | SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 4494 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 4495 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| 4496 | N0.getOperand(0).getDebugLoc(), |
| 4497 | N0.getOperand(0).getValueType(), ExtLoad); |
| 4498 | CombineTo(N, And); |
| 4499 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| 4500 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4501 | ISD::SIGN_EXTEND); |
| 4502 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4503 | } |
| 4504 | } |
| 4505 | } |
| 4506 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4507 | if (N0.getOpcode() == ISD::SETCC) { |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4508 | // sext(setcc) -> sext_in_reg(vsetcc) for vectors. |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4509 | // Only do this before legalize for now. |
Owen Anderson | ed5707b | 2013-04-23 18:09:28 +0000 | [diff] [blame] | 4510 | if (VT.isVector() && !LegalOperations && |
| 4511 | TLI.getBooleanContents(true) == |
| 4512 | TargetLowering::ZeroOrNegativeOneBooleanContent) { |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4513 | EVT N0VT = N0.getOperand(0).getValueType(); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4514 | // On some architectures (such as SSE/NEON/etc) the SETCC result type is |
| 4515 | // of the same size as the compared operands. Only optimize sext(setcc()) |
| 4516 | // if this is the case. |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 4517 | EVT SVT = getSetCCResultType(N0VT); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4518 | |
| 4519 | // We know that the # elements of the results is the same as the |
| 4520 | // # elements of the compare (and the # elements of the compare result |
| 4521 | // for that matter). Check to see that they are the same size. If so, |
| 4522 | // we know that the element size of the sext'd result matches the |
| 4523 | // element size of the compare operands. |
| 4524 | if (VT.getSizeInBits() == SVT.getSizeInBits()) |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4525 | return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4526 | N0.getOperand(1), |
| 4527 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Matt Arsenault | 9aa8fdf | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4528 | |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4529 | // If the desired elements are smaller or larger than the source |
| 4530 | // elements we can use a matching integer vector type and then |
| 4531 | // truncate/sign extend |
Matt Arsenault | 9aa8fdf | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4532 | EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger(); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 4533 | if (SVT == MatchingVectorType) { |
| 4534 | SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, |
| 4535 | N0.getOperand(0), N0.getOperand(1), |
| 4536 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4537 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4538 | } |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4539 | } |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4540 | |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4541 | // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc) |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4542 | unsigned ElementWidth = VT.getScalarType().getSizeInBits(); |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4543 | SDValue NegOne = |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4544 | DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4545 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4546 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4547 | NegOne, DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4548 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4549 | if (SCC.getNode()) return SCC; |
Richard Relph | 1a5c051 | 2013-03-12 18:17:18 +0000 | [diff] [blame] | 4550 | if (!VT.isVector() && (!LegalOperations || |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 4551 | TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) |
Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 4552 | return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT, |
| 4553 | DAG.getSetCC(N->getDebugLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 4554 | getSetCCResultType(VT), |
Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 4555 | N0.getOperand(0), N0.getOperand(1), |
| 4556 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 4557 | NegOne, DAG.getConstant(0, VT)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4558 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4559 | |
Dan Gohman | 8f0ad58 | 2008-04-28 16:58:24 +0000 | [diff] [blame] | 4560 | // fold (sext x) -> (zext x) if the sign bit is known zero. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4561 | if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && |
Dan Gohman | 187db7b | 2008-04-28 18:47:17 +0000 | [diff] [blame] | 4562 | DAG.SignBitIsZero(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4563 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4564 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4565 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4568 | // isTruncateOf - If N is a truncate of some other value, return true, record |
| 4569 | // the value being truncated in Op and which of Op's bits are zero in KnownZero. |
| 4570 | // This function computes KnownZero to avoid a duplicated call to |
| 4571 | // ComputeMaskedBits in the caller. |
| 4572 | static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op, |
| 4573 | APInt &KnownZero) { |
| 4574 | APInt KnownOne; |
| 4575 | if (N->getOpcode() == ISD::TRUNCATE) { |
| 4576 | Op = N->getOperand(0); |
| 4577 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 4578 | return true; |
| 4579 | } |
| 4580 | |
| 4581 | if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 || |
| 4582 | cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE) |
| 4583 | return false; |
| 4584 | |
| 4585 | SDValue Op0 = N->getOperand(0); |
| 4586 | SDValue Op1 = N->getOperand(1); |
| 4587 | assert(Op0.getValueType() == Op1.getValueType()); |
| 4588 | |
| 4589 | ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0); |
| 4590 | ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1); |
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4591 | if (COp0 && COp0->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4592 | Op = Op1; |
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4593 | else if (COp1 && COp1->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4594 | Op = Op0; |
| 4595 | else |
| 4596 | return false; |
| 4597 | |
| 4598 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 4599 | |
| 4600 | if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue()) |
| 4601 | return false; |
| 4602 | |
| 4603 | return true; |
| 4604 | } |
| 4605 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4606 | SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
| 4607 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4608 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4609 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4610 | // fold (zext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4611 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4612 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4613 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4614 | // fold (zext (aext x)) -> (zext x) |
| 4615 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4616 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, |
| 4617 | N0.getOperand(0)); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4618 | |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4619 | // fold (zext (truncate x)) -> (zext x) or |
| 4620 | // (zext (truncate x)) -> (truncate x) |
| 4621 | // This is valid when the truncated bits of x are already zero. |
| 4622 | // FIXME: We should extend this to work for vectors too. |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4623 | SDValue Op; |
| 4624 | APInt KnownZero; |
| 4625 | if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) { |
| 4626 | APInt TruncatedBits = |
| 4627 | (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ? |
| 4628 | APInt(Op.getValueSizeInBits(), 0) : |
| 4629 | APInt::getBitsSet(Op.getValueSizeInBits(), |
| 4630 | N0.getValueSizeInBits(), |
| 4631 | std::min(Op.getValueSizeInBits(), |
| 4632 | VT.getSizeInBits())); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4633 | if (TruncatedBits == (KnownZero & TruncatedBits)) { |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4634 | if (VT.bitsGT(Op.getValueType())) |
| 4635 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op); |
| 4636 | if (VT.bitsLT(Op.getValueType())) |
| 4637 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
| 4638 | |
| 4639 | return Op; |
| 4640 | } |
| 4641 | } |
| 4642 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4643 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 4644 | // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n))) |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 4645 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4646 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4647 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4648 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4649 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4650 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4651 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4652 | AddToWorkList(oye); |
| 4653 | } |
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 4654 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4655 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4658 | // fold (zext (truncate x)) -> (and x, mask) |
| 4659 | if (N0.getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4660 | (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4661 | |
| 4662 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 4663 | // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) |
| 4664 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4665 | if (NarrowLoad.getNode()) { |
| 4666 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4667 | if (NarrowLoad.getNode() != N0.getNode()) { |
| 4668 | CombineTo(N0.getNode(), NarrowLoad); |
| 4669 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4670 | AddToWorkList(oye); |
| 4671 | } |
| 4672 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4673 | } |
| 4674 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4675 | SDValue Op = N0.getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4676 | if (Op.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4677 | Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4678 | AddToWorkList(Op.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4679 | } else if (Op.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4680 | Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4681 | AddToWorkList(Op.getNode()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4682 | } |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4683 | return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), |
| 4684 | N0.getValueType().getScalarType()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4685 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4686 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4687 | // Fold (zext (and (trunc x), cst)) -> (and x, cst), |
| 4688 | // if either of the casts is not free. |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4689 | if (N0.getOpcode() == ISD::AND && |
| 4690 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4691 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4692 | (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 4693 | N0.getValueType()) || |
| 4694 | !TLI.isZExtFree(N0.getValueType(), VT))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4695 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4696 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4697 | X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4698 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4699 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4700 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4701 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4702 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4703 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4704 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4705 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4706 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4707 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4708 | // None of the supported targets knows how to perform load and vector_zext |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4709 | // on vectors in one instruction. We only perform this transformation on |
| 4710 | // scalars. |
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4711 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4712 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4713 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4714 | bool DoXform = true; |
| 4715 | SmallVector<SDNode*, 4> SetCCs; |
| 4716 | if (!N0.hasOneUse()) |
| 4717 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); |
| 4718 | if (DoXform) { |
| 4719 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4720 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4721 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4722 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4723 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4724 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4725 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4726 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4727 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4728 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4729 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4730 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4731 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4732 | ISD::ZERO_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4733 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4734 | } |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4735 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4736 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4737 | // fold (zext (and/or/xor (load x), cst)) -> |
| 4738 | // (and/or/xor (zextload x), (zext cst)) |
| 4739 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 4740 | N0.getOpcode() == ISD::XOR) && |
| 4741 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 4742 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4743 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) && |
| 4744 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 4745 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 4746 | if (LN0->getExtensionType() != ISD::SEXTLOAD) { |
| 4747 | bool DoXform = true; |
| 4748 | SmallVector<SDNode*, 4> SetCCs; |
| 4749 | if (!N0.hasOneUse()) |
| 4750 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND, |
| 4751 | SetCCs, TLI); |
| 4752 | if (DoXform) { |
| 4753 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, |
| 4754 | LN0->getChain(), LN0->getBasePtr(), |
| 4755 | LN0->getPointerInfo(), |
| 4756 | LN0->getMemoryVT(), |
| 4757 | LN0->isVolatile(), |
| 4758 | LN0->isNonTemporal(), |
| 4759 | LN0->getAlignment()); |
| 4760 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 4761 | Mask = Mask.zext(VT.getSizeInBits()); |
| 4762 | SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 4763 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 4764 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| 4765 | N0.getOperand(0).getDebugLoc(), |
| 4766 | N0.getOperand(0).getValueType(), ExtLoad); |
| 4767 | CombineTo(N, And); |
| 4768 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| 4769 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4770 | ISD::ZERO_EXTEND); |
| 4771 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4776 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 4777 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4778 | if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 4779 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4780 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4781 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4782 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4783 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4784 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4785 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4786 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4787 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4788 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4789 | LN0->getAlignment()); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4790 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4791 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4792 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(), |
| 4793 | ExtLoad), |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4794 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4795 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4796 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4797 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4798 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4799 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4800 | if (!LegalOperations && VT.isVector()) { |
| 4801 | // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. |
| 4802 | // Only do this before legalize for now. |
| 4803 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 4804 | EVT EltVT = VT.getVectorElementType(); |
| 4805 | SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(), |
| 4806 | DAG.getConstant(1, EltVT)); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4807 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4808 | // We know that the # elements of the results is the same as the |
| 4809 | // # elements of the compare (and the # elements of the compare result |
| 4810 | // for that matter). Check to see that they are the same size. If so, |
| 4811 | // we know that the element size of the sext'd result matches the |
| 4812 | // element size of the compare operands. |
| 4813 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4814 | DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4815 | N0.getOperand(1), |
| 4816 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 4817 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 4818 | &OneOps[0], OneOps.size())); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4819 | |
| 4820 | // If the desired elements are smaller or larger than the source |
| 4821 | // elements we can use a matching integer vector type and then |
| 4822 | // truncate/sign extend |
| 4823 | EVT MatchingElementType = |
| 4824 | EVT::getIntegerVT(*DAG.getContext(), |
| 4825 | N0VT.getScalarType().getSizeInBits()); |
| 4826 | EVT MatchingVectorType = |
| 4827 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 4828 | N0VT.getVectorNumElements()); |
| 4829 | SDValue VsetCC = |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4830 | DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4831 | N0.getOperand(1), |
| 4832 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4833 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4834 | DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT), |
| 4835 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 4836 | &OneOps[0], OneOps.size())); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4840 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4841 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4842 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4843 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4844 | if (SCC.getNode()) return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4845 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4846 | |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4847 | // (zext (shl (zext x), cst)) -> (shl (zext x), cst) |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4848 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4849 | isa<ConstantSDNode>(N0.getOperand(1)) && |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4850 | N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && |
| 4851 | N0.hasOneUse()) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4852 | SDValue ShAmt = N0.getOperand(1); |
| 4853 | unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4854 | if (N0.getOpcode() == ISD::SHL) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4855 | SDValue InnerZExt = N0.getOperand(0); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4856 | // If the original shl may be shifting out bits, do not perform this |
| 4857 | // transformation. |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4858 | unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() - |
| 4859 | InnerZExt.getOperand(0).getValueType().getSizeInBits(); |
| 4860 | if (ShAmtVal > KnownZeroBits) |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4861 | return SDValue(); |
| 4862 | } |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4863 | |
| 4864 | DebugLoc DL = N->getDebugLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4865 | |
| 4866 | // Ensure that the shift amount is wide enough for the shifted value. |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4867 | if (VT.getSizeInBits() >= 256) |
| 4868 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4869 | |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4870 | return DAG.getNode(N0.getOpcode(), DL, VT, |
| 4871 | DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), |
| 4872 | ShAmt); |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4875 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4878 | SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 4879 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4880 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4881 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4882 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4883 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4884 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4885 | // fold (aext (aext x)) -> (aext x) |
| 4886 | // fold (aext (zext x)) -> (zext x) |
| 4887 | // fold (aext (sext x)) -> (sext x) |
| 4888 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 4889 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 4890 | N0.getOpcode() == ISD::SIGN_EXTEND) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4891 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4892 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4893 | // fold (aext (truncate (load x))) -> (aext (smaller load x)) |
| 4894 | // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) |
| 4895 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4896 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4897 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4898 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4899 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4900 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4901 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4902 | AddToWorkList(oye); |
| 4903 | } |
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 4904 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4905 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4908 | // fold (aext (truncate x)) |
| 4909 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4910 | SDValue TruncOp = N0.getOperand(0); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4911 | if (TruncOp.getValueType() == VT) |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 4912 | return TruncOp; // x iff x size == zext size. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4913 | if (TruncOp.getValueType().bitsGT(VT)) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4914 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp); |
| 4915 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4916 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4917 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4918 | // Fold (aext (and (trunc x), cst)) -> (and x, cst) |
| 4919 | // if the trunc is not free. |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4920 | if (N0.getOpcode() == ISD::AND && |
| 4921 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4922 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4923 | !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 4924 | N0.getValueType())) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4925 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4926 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4927 | X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4928 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4929 | X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4930 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4931 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4932 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4933 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4934 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4935 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4936 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4937 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4938 | // None of the supported targets knows how to perform load and any_ext |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4939 | // on vectors in one instruction. We only perform this transformation on |
| 4940 | // scalars. |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4941 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4942 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4943 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4944 | bool DoXform = true; |
| 4945 | SmallVector<SDNode*, 4> SetCCs; |
| 4946 | if (!N0.hasOneUse()) |
| 4947 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); |
| 4948 | if (DoXform) { |
| 4949 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4950 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4951 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4952 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4953 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4954 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4955 | LN0->getAlignment()); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4956 | CombineTo(N, ExtLoad); |
| 4957 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4958 | N0.getValueType(), ExtLoad); |
| 4959 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4960 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4961 | ISD::ANY_EXTEND); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4962 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4963 | } |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4964 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4965 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4966 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 4967 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 4968 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 4969 | if (N0.getOpcode() == ISD::LOAD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4970 | !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4971 | N0.hasOneUse()) { |
| 4972 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4973 | EVT MemVT = LN0->getMemoryVT(); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4974 | SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(), |
| 4975 | VT, LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4976 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4977 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4978 | LN0->getAlignment()); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4979 | CombineTo(N, ExtLoad); |
Evan Cheng | 4529966 | 2008-08-29 23:20:46 +0000 | [diff] [blame] | 4980 | CombineTo(N0.getNode(), |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4981 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4982 | N0.getValueType(), ExtLoad), |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4983 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4984 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4985 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4986 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4987 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4988 | // aext(setcc) -> sext_in_reg(vsetcc) for vectors. |
| 4989 | // Only do this before legalize for now. |
| 4990 | if (VT.isVector() && !LegalOperations) { |
| 4991 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 4992 | // We know that the # elements of the results is the same as the |
| 4993 | // # elements of the compare (and the # elements of the compare result |
| 4994 | // for that matter). Check to see that they are the same size. If so, |
| 4995 | // we know that the element size of the sext'd result matches the |
| 4996 | // element size of the compare operands. |
| 4997 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4998 | return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4999 | N0.getOperand(1), |
| 5000 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5001 | // If the desired elements are smaller or larger than the source |
| 5002 | // elements we can use a matching integer vector type and then |
| 5003 | // truncate/sign extend |
| 5004 | else { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5005 | EVT MatchingElementType = |
| 5006 | EVT::getIntegerVT(*DAG.getContext(), |
| 5007 | N0VT.getScalarType().getSizeInBits()); |
| 5008 | EVT MatchingVectorType = |
| 5009 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 5010 | N0VT.getVectorNumElements()); |
| 5011 | SDValue VsetCC = |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 5012 | DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5013 | N0.getOperand(1), |
| 5014 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 5015 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5020 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 5021 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 5022 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | c24bbad | 2007-04-11 16:51:53 +0000 | [diff] [blame] | 5023 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5024 | if (SCC.getNode()) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 5025 | return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5026 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5027 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 5028 | return SDValue(); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5031 | /// GetDemandedBits - See if the specified operand can be simplified with the |
| 5032 | /// knowledge that only the bits specified by Mask are used. If so, return the |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5033 | /// simpler operand, otherwise return a null SDValue. |
| 5034 | SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5035 | switch (V.getOpcode()) { |
| 5036 | default: break; |
Lang Hames | 5207bf2 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5037 | case ISD::Constant: { |
| 5038 | const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode()); |
| 5039 | assert(CV != 0 && "Const value should be ConstSDNode."); |
| 5040 | const APInt &CVal = CV->getAPIntValue(); |
| 5041 | APInt NewVal = CVal & Mask; |
| 5042 | if (NewVal != CVal) { |
| 5043 | return DAG.getConstant(NewVal, V.getValueType()); |
| 5044 | } |
| 5045 | break; |
| 5046 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5047 | case ISD::OR: |
| 5048 | case ISD::XOR: |
| 5049 | // If the LHS or RHS don't contribute bits to the or, drop them. |
| 5050 | if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) |
| 5051 | return V.getOperand(1); |
| 5052 | if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) |
| 5053 | return V.getOperand(0); |
| 5054 | break; |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5055 | case ISD::SRL: |
| 5056 | // Only look at single-use SRLs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5057 | if (!V.getNode()->hasOneUse()) |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5058 | break; |
| 5059 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) { |
| 5060 | // See if we can recursively simplify the LHS. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5061 | unsigned Amt = RHSC->getZExtValue(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5062 | |
Dan Gohman | cc91d63 | 2009-01-03 19:22:06 +0000 | [diff] [blame] | 5063 | // Watch out for shift count overflow though. |
| 5064 | if (Amt >= Mask.getBitWidth()) break; |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5065 | APInt NewMask = Mask << Amt; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5066 | SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5067 | if (SimplifyLHS.getNode()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5068 | return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(), |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5069 | SimplifyLHS, V.getOperand(1)); |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5070 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5071 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5072 | return SDValue(); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5075 | /// ReduceLoadWidth - If the result of a wider load is shifted to right of N |
| 5076 | /// bits and then truncated to a narrower type and where N is a multiple |
| 5077 | /// of number of bits of the narrower type, transform it to a narrower load |
| 5078 | /// from address + N / num of bits of new type. If the result is to be |
| 5079 | /// extended, also fold the extension to form a extending load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5080 | SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5081 | unsigned Opc = N->getOpcode(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5082 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5083 | ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5084 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5085 | EVT VT = N->getValueType(0); |
| 5086 | EVT ExtVT = VT; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5087 | |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 5088 | // This transformation isn't valid for vector loads. |
| 5089 | if (VT.isVector()) |
| 5090 | return SDValue(); |
| 5091 | |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5092 | // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then |
Evan Cheng | e177e30 | 2007-03-23 22:13:36 +0000 | [diff] [blame] | 5093 | // extended to VT. |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5094 | if (Opc == ISD::SIGN_EXTEND_INREG) { |
| 5095 | ExtType = ISD::SEXTLOAD; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5096 | ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5097 | } else if (Opc == ISD::SRL) { |
Chris Lattner | 90b0364 | 2010-12-21 18:05:22 +0000 | [diff] [blame] | 5098 | // Another special-case: SRL is basically zero-extending a narrower value. |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5099 | ExtType = ISD::ZEXTLOAD; |
| 5100 | N0 = SDValue(N, 0); |
| 5101 | ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 5102 | if (!N01) return SDValue(); |
| 5103 | ExtVT = EVT::getIntegerVT(*DAG.getContext(), |
| 5104 | VT.getSizeInBits() - N01->getZExtValue()); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5105 | } |
Richard Osborne | 4e3740e | 2011-01-31 17:41:44 +0000 | [diff] [blame] | 5106 | if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT)) |
| 5107 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5108 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5109 | unsigned EVTBits = ExtVT.getSizeInBits(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5111 | // Do not generate loads of non-round integer types since these can |
| 5112 | // be expensive (and would be wrong if the type is not byte sized). |
| 5113 | if (!ExtVT.isRound()) |
| 5114 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5115 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5116 | unsigned ShAmt = 0; |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5117 | if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5118 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5119 | ShAmt = N01->getZExtValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5120 | // Is the shift amount a multiple of size of VT? |
| 5121 | if ((ShAmt & (EVTBits-1)) == 0) { |
| 5122 | N0 = N0.getOperand(0); |
Eli Friedman | d68eea2 | 2009-08-19 08:46:10 +0000 | [diff] [blame] | 5123 | // Is the load width a multiple of size of VT? |
| 5124 | if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5125 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5126 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5127 | |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5128 | // At this point, we must have a load or else we can't do the transform. |
| 5129 | if (!isa<LoadSDNode>(N0)) return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5130 | |
Chandler Carruth | 1c49fda | 2012-12-11 00:36:57 +0000 | [diff] [blame] | 5131 | // Because a SRL must be assumed to *need* to zero-extend the high bits |
| 5132 | // (as opposed to anyext the high bits), we can't combine the zextload |
| 5133 | // lowering of SRL and an sextload. |
| 5134 | if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD) |
| 5135 | return SDValue(); |
| 5136 | |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5137 | // If the shift amount is larger than the input type then we're not |
| 5138 | // accessing any of the loaded bytes. If the load was a zextload/extload |
| 5139 | // then the result of the shift+trunc is zero/undef (handled elsewhere). |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5140 | if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5141 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5142 | } |
| 5143 | } |
| 5144 | |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5145 | // If the load is shifted left (and the result isn't shifted back right), |
| 5146 | // we can fold the truncate through the shift. |
| 5147 | unsigned ShLeftAmt = 0; |
| 5148 | if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5149 | ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5150 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 5151 | ShLeftAmt = N01->getZExtValue(); |
| 5152 | N0 = N0.getOperand(0); |
| 5153 | } |
| 5154 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5155 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5156 | // If we haven't found a load, we can't narrow it. Don't transform one with |
| 5157 | // multiple uses, this would require adding a new load. |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5158 | if (!isa<LoadSDNode>(N0) || !N0.hasOneUse()) |
| 5159 | return SDValue(); |
| 5160 | |
| 5161 | // Don't change the width of a volatile load. |
| 5162 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| 5163 | if (LN0->isVolatile()) |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5164 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5165 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5166 | // Verify that we are actually reducing a load width here. |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5167 | if (LN0->getMemoryVT().getSizeInBits() < EVTBits) |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5168 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5169 | |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5170 | // For the transform to be legal, the load must produce only two values |
| 5171 | // (the value loaded and the chain). Don't transform a pre-increment |
| 5172 | // load, for example, which produces an extra value. Otherwise the |
| 5173 | // transformation is not equivalent, and the downstream logic to replace |
| 5174 | // uses gets things wrong. |
| 5175 | if (LN0->getNumValues() > 2) |
| 5176 | return SDValue(); |
| 5177 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5178 | EVT PtrType = N0.getOperand(1).getValueType(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5179 | |
Evan Cheng | 16436df | 2012-06-26 01:19:33 +0000 | [diff] [blame] | 5180 | if (PtrType == MVT::Untyped || PtrType.isExtended()) |
| 5181 | // It's not possible to generate a constant of extended or untyped type. |
| 5182 | return SDValue(); |
| 5183 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5184 | // For big endian targets, we need to adjust the offset to the pointer to |
| 5185 | // load the correct bytes. |
| 5186 | if (TLI.isBigEndian()) { |
| 5187 | unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); |
| 5188 | unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); |
| 5189 | ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5192 | uint64_t PtrOff = ShAmt / 8; |
| 5193 | unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); |
| 5194 | SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), |
| 5195 | PtrType, LN0->getBasePtr(), |
| 5196 | DAG.getConstant(PtrOff, PtrType)); |
| 5197 | AddToWorkList(NewPtr.getNode()); |
| 5198 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5199 | SDValue Load; |
| 5200 | if (ExtType == ISD::NON_EXTLOAD) |
| 5201 | Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr, |
| 5202 | LN0->getPointerInfo().getWithOffset(PtrOff), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5203 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5204 | LN0->isInvariant(), NewAlign); |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5205 | else |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5206 | Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr, |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5207 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 5208 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 5209 | NewAlign); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5210 | |
| 5211 | // Replace the old load's chain with the new load's chain. |
| 5212 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5213 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5214 | |
| 5215 | // Shift the result left, if we've swallowed a left shift. |
| 5216 | SDValue Result = Load; |
| 5217 | if (ShLeftAmt != 0) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5218 | EVT ShImmTy = getShiftAmountTy(Result.getValueType()); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5219 | if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) |
| 5220 | ShImmTy = VT; |
Paul Redmond | 5c97450 | 2013-02-12 15:21:21 +0000 | [diff] [blame] | 5221 | // If the shift amount is as large as the result size (but, presumably, |
| 5222 | // no larger than the source) then the useful bits of the result are |
| 5223 | // zero; we can't simply return the shortened shift, because the result |
| 5224 | // of that operation is undefined. |
| 5225 | if (ShLeftAmt >= VT.getSizeInBits()) |
| 5226 | Result = DAG.getConstant(0, VT); |
| 5227 | else |
| 5228 | Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, |
| 5229 | Result, DAG.getConstant(ShLeftAmt, ShImmTy)); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | // Return the new loaded value. |
| 5233 | return Result; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5236 | SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
| 5237 | SDValue N0 = N->getOperand(0); |
| 5238 | SDValue N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5239 | EVT VT = N->getValueType(0); |
| 5240 | EVT EVT = cast<VTSDNode>(N1)->getVT(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5241 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5242 | unsigned EVTBits = EVT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5243 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5244 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 5245 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5246 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5247 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 5248 | // If the input is already sign extended, just drop the extension. |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5249 | if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 5250 | return N0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5251 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5252 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 5253 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5254 | EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) { |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5255 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
| 5256 | N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5257 | } |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5258 | |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5259 | // fold (sext_in_reg (sext x)) -> (sext x) |
| 5260 | // fold (sext_in_reg (aext x)) -> (sext x) |
| 5261 | // if x is small enough. |
| 5262 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { |
| 5263 | SDValue N00 = N0.getOperand(0); |
Evan Cheng | 003d7c4 | 2010-04-16 22:26:19 +0000 | [diff] [blame] | 5264 | if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits && |
| 5265 | (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5266 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1); |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5269 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5270 | if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5271 | return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5272 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5273 | // fold operands of sext_in_reg based on knowledge that the top bits are not |
| 5274 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5275 | if (SimplifyDemandedBits(SDValue(N, 0))) |
| 5276 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5277 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5278 | // fold (sext_in_reg (load x)) -> (smaller sextload x) |
| 5279 | // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5280 | SDValue NarrowLoad = ReduceLoadWidth(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5281 | if (NarrowLoad.getNode()) |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5282 | return NarrowLoad; |
| 5283 | |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5284 | // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5285 | // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible. |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5286 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 5287 | if (N0.getOpcode() == ISD::SRL) { |
| 5288 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5289 | if (ShAmt->getZExtValue()+EVTBits <= VTBits) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5290 | // We can turn this into an SRA iff the input to the SRL is already sign |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5291 | // extended enough. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5292 | unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5293 | if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5294 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, |
| 5295 | N0.getOperand(0), N0.getOperand(1)); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5296 | } |
| 5297 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5298 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5299 | // fold (sext_inreg (extload x)) -> (sextload x) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5300 | if (ISD::isEXTLoad(N0.getNode()) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5301 | ISD::isUNINDEXEDLoad(N0.getNode()) && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5302 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5303 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5304 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5305 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5306 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5307 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5308 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 5309 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5310 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5311 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5312 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5313 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 5314 | AddToWorkList(ExtLoad.getNode()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5315 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5316 | } |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5317 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5318 | if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5319 | N0.hasOneUse() && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5320 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5321 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5322 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5323 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5324 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5325 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5326 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 5327 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5328 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5329 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5330 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5331 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5332 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5333 | } |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5334 | |
| 5335 | // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16)) |
| 5336 | if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) { |
| 5337 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), |
| 5338 | N0.getOperand(1), false); |
| 5339 | if (BSwap.getNode() != 0) |
| 5340 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
| 5341 | BSwap, N1); |
| 5342 | } |
| 5343 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5344 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5347 | SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { |
| 5348 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5349 | EVT VT = N->getValueType(0); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5350 | bool isLE = TLI.isLittleEndian(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5351 | |
| 5352 | // noop truncate |
| 5353 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5354 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5355 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5356 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5357 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5358 | // fold (truncate (truncate x)) -> (truncate x) |
| 5359 | if (N0.getOpcode() == ISD::TRUNCATE) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5360 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5361 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | 7f893c0 | 2010-04-07 18:13:33 +0000 | [diff] [blame] | 5362 | if (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 5363 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 5364 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5365 | if (N0.getOperand(0).getValueType().bitsLT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5366 | // if the source is smaller than the dest, we still need an extend |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5367 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 5368 | N0.getOperand(0)); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5369 | if (N0.getOperand(0).getValueType().bitsGT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5370 | // if the source is larger than the dest, than we just need the truncate |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5371 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5372 | // if the source and dest are the same type, we can drop both the extend |
| 5373 | // and the truncate. |
| 5374 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5375 | } |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5376 | |
Nadav Rotem | cc870a8 | 2012-02-05 11:39:23 +0000 | [diff] [blame] | 5377 | // Fold extract-and-trunc into a narrow extract. For example: |
| 5378 | // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1) |
| 5379 | // i32 y = TRUNCATE(i64 x) |
| 5380 | // -- becomes -- |
| 5381 | // v16i8 b = BITCAST (v2i64 val) |
| 5382 | // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8) |
| 5383 | // |
| 5384 | // Note: We only run this optimization after type legalization (which often |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5385 | // creates this pattern) and before operation legalization after which |
| 5386 | // we need to be more careful about the vector instructions that we generate. |
| 5387 | if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 5388 | LegalTypes && !LegalOperations && N0->hasOneUse()) { |
| 5389 | |
| 5390 | EVT VecTy = N0.getOperand(0).getValueType(); |
| 5391 | EVT ExTy = N0.getValueType(); |
| 5392 | EVT TrTy = N->getValueType(0); |
| 5393 | |
| 5394 | unsigned NumElem = VecTy.getVectorNumElements(); |
| 5395 | unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits(); |
| 5396 | |
| 5397 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem); |
| 5398 | assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size"); |
| 5399 | |
| 5400 | SDValue EltNo = N0->getOperand(1); |
| 5401 | if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) { |
| 5402 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5403 | EVT IndexTy = N0->getOperand(1).getValueType(); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5404 | int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1)); |
| 5405 | |
| 5406 | SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
| 5407 | NVT, N0.getOperand(0)); |
| 5408 | |
| 5409 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 5410 | N->getDebugLoc(), TrTy, V, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5411 | DAG.getConstant(Index, IndexTy)); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5412 | } |
| 5413 | } |
| 5414 | |
Arnold Schwaighofer | c46e2df | 2013-02-20 21:33:32 +0000 | [diff] [blame] | 5415 | // Fold a series of buildvector, bitcast, and truncate if possible. |
| 5416 | // For example fold |
| 5417 | // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to |
| 5418 | // (2xi32 (buildvector x, y)). |
| 5419 | if (Level == AfterLegalizeVectorOps && VT.isVector() && |
| 5420 | N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() && |
| 5421 | N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && |
| 5422 | N0.getOperand(0).hasOneUse()) { |
| 5423 | |
| 5424 | SDValue BuildVect = N0.getOperand(0); |
| 5425 | EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType(); |
| 5426 | EVT TruncVecEltTy = VT.getVectorElementType(); |
| 5427 | |
| 5428 | // Check that the element types match. |
| 5429 | if (BuildVectEltTy == TruncVecEltTy) { |
| 5430 | // Now we only need to compute the offset of the truncated elements. |
| 5431 | unsigned BuildVecNumElts = BuildVect.getNumOperands(); |
| 5432 | unsigned TruncVecNumElts = VT.getVectorNumElements(); |
| 5433 | unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts; |
| 5434 | |
| 5435 | assert((BuildVecNumElts % TruncVecNumElts) == 0 && |
| 5436 | "Invalid number of elements"); |
| 5437 | |
| 5438 | SmallVector<SDValue, 8> Opnds; |
| 5439 | for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset) |
| 5440 | Opnds.push_back(BuildVect.getOperand(i)); |
| 5441 | |
| 5442 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, &Opnds[0], |
| 5443 | Opnds.size()); |
| 5444 | } |
| 5445 | } |
| 5446 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5447 | // See if we can simplify the input to this truncate through knowledge that |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5448 | // only the low bits are being used. |
| 5449 | // For example "trunc (or (shl x, 8), y)" // -> trunc y |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5450 | // Currently we only perform this optimization on scalars because vectors |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5451 | // may have different active low bits. |
| 5452 | if (!VT.isVector()) { |
| 5453 | SDValue Shorter = |
| 5454 | GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), |
| 5455 | VT.getSizeInBits())); |
| 5456 | if (Shorter.getNode()) |
| 5457 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter); |
| 5458 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 5459 | // fold (truncate (load x)) -> (smaller load x) |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5460 | // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5461 | if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { |
| 5462 | SDValue Reduced = ReduceLoadWidth(N); |
| 5463 | if (Reduced.getNode()) |
| 5464 | return Reduced; |
| 5465 | } |
Michael Liao | 07edaf3 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5466 | // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)), |
| 5467 | // where ... are all 'undef'. |
| 5468 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) { |
| 5469 | SmallVector<EVT, 8> VTs; |
| 5470 | SDValue V; |
| 5471 | unsigned Idx = 0; |
| 5472 | unsigned NumDefs = 0; |
| 5473 | |
| 5474 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { |
| 5475 | SDValue X = N0.getOperand(i); |
| 5476 | if (X.getOpcode() != ISD::UNDEF) { |
| 5477 | V = X; |
| 5478 | Idx = i; |
| 5479 | NumDefs++; |
| 5480 | } |
| 5481 | // Stop if more than one members are non-undef. |
| 5482 | if (NumDefs > 1) |
| 5483 | break; |
| 5484 | VTs.push_back(EVT::getVectorVT(*DAG.getContext(), |
| 5485 | VT.getVectorElementType(), |
| 5486 | X.getValueType().getVectorNumElements())); |
| 5487 | } |
| 5488 | |
| 5489 | if (NumDefs == 0) |
| 5490 | return DAG.getUNDEF(VT); |
| 5491 | |
| 5492 | if (NumDefs == 1) { |
| 5493 | assert(V.getNode() && "The single defined operand is empty!"); |
| 5494 | SmallVector<SDValue, 8> Opnds; |
| 5495 | for (unsigned i = 0, e = VTs.size(); i != e; ++i) { |
| 5496 | if (i != Idx) { |
| 5497 | Opnds.push_back(DAG.getUNDEF(VTs[i])); |
| 5498 | continue; |
| 5499 | } |
| 5500 | SDValue NV = DAG.getNode(ISD::TRUNCATE, V.getDebugLoc(), VTs[i], V); |
| 5501 | AddToWorkList(NV.getNode()); |
| 5502 | Opnds.push_back(NV); |
| 5503 | } |
| 5504 | return DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, |
| 5505 | &Opnds[0], Opnds.size()); |
| 5506 | } |
| 5507 | } |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5508 | |
| 5509 | // Simplify the operands using demanded-bits information. |
| 5510 | if (!VT.isVector() && |
| 5511 | SimplifyDemandedBits(SDValue(N, 0))) |
| 5512 | return SDValue(N, 0); |
| 5513 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 5514 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5517 | static SDNode *getBuildPairElt(SDNode *N, unsigned i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5518 | SDValue Elt = N->getOperand(i); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5519 | if (Elt.getOpcode() != ISD::MERGE_VALUES) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5520 | return Elt.getNode(); |
| 5521 | return Elt.getOperand(Elt.getResNo()).getNode(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | /// CombineConsecutiveLoads - build_pair (load, load) -> load |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5525 | /// if load locations are consecutive. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5526 | SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5527 | assert(N->getOpcode() == ISD::BUILD_PAIR); |
| 5528 | |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5529 | LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); |
| 5530 | LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5531 | if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || |
| 5532 | LD1->getPointerInfo().getAddrSpace() != |
| 5533 | LD2->getPointerInfo().getAddrSpace()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5534 | return SDValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5535 | EVT LD1VT = LD1->getValueType(0); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5536 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5537 | if (ISD::isNON_EXTLoad(LD2) && |
| 5538 | LD2->hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5539 | // If both are volatile this would reduce the number of volatile loads. |
| 5540 | // If one is volatile it might be ok, but play conservative and bail out. |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5541 | !LD1->isVolatile() && |
| 5542 | !LD2->isVolatile() && |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 5543 | DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) { |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5544 | unsigned Align = LD1->getAlignment(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5545 | unsigned NewAlign = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5546 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5547 | |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5548 | if (NewAlign <= Align && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5549 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5550 | return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5551 | LD1->getBasePtr(), LD1->getPointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5552 | false, false, false, Align); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5553 | } |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5554 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5555 | return SDValue(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5558 | SDValue DAGCombiner::visitBITCAST(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5559 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5560 | EVT VT = N->getValueType(0); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5561 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5562 | // If the input is a BUILD_VECTOR with all constant elements, fold this now. |
| 5563 | // Only do this before legalize, since afterward the target may be depending |
| 5564 | // on the bitconvert. |
| 5565 | // First check to see if this is all constant. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5566 | if (!LegalTypes && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5567 | N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5568 | VT.isVector()) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5569 | bool isSimple = true; |
| 5570 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) |
| 5571 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && |
| 5572 | N0.getOperand(i).getOpcode() != ISD::Constant && |
| 5573 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5574 | isSimple = false; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5575 | break; |
| 5576 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5577 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5578 | EVT DestEltVT = N->getValueType(0).getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5579 | assert(!DestEltVT.isVector() && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5580 | "Element type of vector ValueType must not be vector!"); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5581 | if (isSimple) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5582 | return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5583 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5584 | |
Dan Gohman | 3dd168d | 2008-09-05 01:58:21 +0000 | [diff] [blame] | 5585 | // If the input is a constant, let getNode fold it. |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5586 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5587 | SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0); |
Dan Gohman | a407ca1 | 2009-08-10 23:15:10 +0000 | [diff] [blame] | 5588 | if (Res.getNode() != N) { |
| 5589 | if (!LegalOperations || |
| 5590 | TLI.isOperationLegal(Res.getNode()->getOpcode(), VT)) |
| 5591 | return Res; |
| 5592 | |
| 5593 | // Folding it resulted in an illegal node, and it's too late to |
| 5594 | // do that. Clean up the old node and forego the transformation. |
| 5595 | // Ideally this won't happen very often, because instcombine |
| 5596 | // and the earlier dagcombine runs (where illegal nodes are |
| 5597 | // permitted) should have folded most of them already. |
| 5598 | DAG.DeleteNode(Res.getNode()); |
| 5599 | } |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5600 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5601 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5602 | // (conv (conv x, t1), t2) -> (conv x, t2) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5603 | if (N0.getOpcode() == ISD::BITCAST) |
| 5604 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5605 | N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5606 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5607 | // fold (conv (load x)) -> (load (conv*)x) |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 5608 | // If the resultant load doesn't need a higher alignment than the original! |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5609 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5610 | // Do not change the width of a volatile load. |
| 5611 | !cast<LoadSDNode>(N0)->isVolatile() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5612 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5613 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5614 | unsigned Align = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5615 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5616 | unsigned OrigAlign = LN0->getAlignment(); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5617 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5618 | if (Align <= OrigAlign) { |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5619 | SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5620 | LN0->getBasePtr(), LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5621 | LN0->isVolatile(), LN0->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5622 | LN0->isInvariant(), OrigAlign); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5623 | AddToWorkList(N); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 5624 | CombineTo(N0.getNode(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5625 | DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5626 | N0.getValueType(), Load), |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5627 | Load.getValue(1)); |
| 5628 | return Load; |
| 5629 | } |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5630 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5631 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5632 | // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) |
| 5633 | // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5634 | // This often reduces constant pool loads. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 5635 | if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) || |
| 5636 | (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) && |
Nadav Rotem | 91a7e01 | 2012-09-13 14:54:28 +0000 | [diff] [blame] | 5637 | N0.getNode()->hasOneUse() && VT.isInteger() && |
| 5638 | !VT.isVector() && !N0.getValueType().isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5639 | SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5640 | N0.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5641 | AddToWorkList(NewConv.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5642 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5643 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5644 | if (N0.getOpcode() == ISD::FNEG) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5645 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 5646 | NewConv, DAG.getConstant(SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5647 | assert(N0.getOpcode() == ISD::FABS); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5648 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 5649 | NewConv, DAG.getConstant(~SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5650 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5651 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5652 | // fold (bitconvert (fcopysign cst, x)) -> |
| 5653 | // (or (and (bitconvert x), sign), (and cst, (not sign))) |
| 5654 | // Note that we don't handle (copysign x, cst) because this can always be |
| 5655 | // folded to an fneg or fabs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5656 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 5657 | isa<ConstantFPSDNode>(N0.getOperand(0)) && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5658 | VT.isInteger() && !VT.isVector()) { |
| 5659 | unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5660 | EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5661 | if (isTypeLegal(IntXVT)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5662 | SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5663 | IntXVT, N0.getOperand(1)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5664 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5665 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5666 | // If X has a different width than the result/lhs, sext it or truncate it. |
| 5667 | unsigned VTWidth = VT.getSizeInBits(); |
| 5668 | if (OrigXWidth < VTWidth) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5669 | X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5670 | AddToWorkList(X.getNode()); |
| 5671 | } else if (OrigXWidth > VTWidth) { |
| 5672 | // To get the sign bit in the right place, we have to shift it right |
| 5673 | // before truncating. |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5674 | X = DAG.getNode(ISD::SRL, X.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5675 | X.getValueType(), X, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5676 | DAG.getConstant(OrigXWidth-VTWidth, X.getValueType())); |
| 5677 | AddToWorkList(X.getNode()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5678 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5679 | AddToWorkList(X.getNode()); |
| 5680 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5681 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5682 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5683 | X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5684 | X, DAG.getConstant(SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5685 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5686 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5687 | SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5688 | VT, N0.getOperand(0)); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5689 | Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5690 | Cst, DAG.getConstant(~SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5691 | AddToWorkList(Cst.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5692 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5693 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5694 | } |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5695 | } |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5696 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5697 | // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5698 | if (N0.getOpcode() == ISD::BUILD_PAIR) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5699 | SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT); |
| 5700 | if (CombineLD.getNode()) |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5701 | return CombineLD; |
| 5702 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5703 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5704 | return SDValue(); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5707 | SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5708 | EVT VT = N->getValueType(0); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5709 | return CombineConsecutiveLoads(N, VT); |
| 5710 | } |
| 5711 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5712 | /// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5713 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5714 | /// destination element value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5715 | SDValue DAGCombiner:: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5716 | ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5717 | EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5718 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5719 | // If this is already the right type, we're done. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5720 | if (SrcEltVT == DstEltVT) return SDValue(BV, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5721 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5722 | unsigned SrcBitSize = SrcEltVT.getSizeInBits(); |
| 5723 | unsigned DstBitSize = DstEltVT.getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5724 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5725 | // If this is a conversion of N elements of one type to N elements of another |
| 5726 | // type, convert each element. This handles FP<->INT cases. |
| 5727 | if (SrcBitSize == DstBitSize) { |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5728 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 5729 | BV->getValueType(0).getVectorNumElements()); |
| 5730 | |
| 5731 | // Due to the FP element handling below calling this routine recursively, |
| 5732 | // we can end up with a scalar-to-vector node here. |
| 5733 | if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5734 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 5735 | DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5736 | DstEltVT, BV->getOperand(0))); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5737 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5738 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5739 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5740 | SDValue Op = BV->getOperand(i); |
| 5741 | // If the vector element type is not legal, the BUILD_VECTOR operands |
| 5742 | // are promoted and implicitly truncated. Make that explicit here. |
Bob Wilson | c885165 | 2009-04-20 17:27:09 +0000 | [diff] [blame] | 5743 | if (Op.getValueType() != SrcEltVT) |
| 5744 | Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5745 | Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5746 | DstEltVT, Op)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5747 | AddToWorkList(Ops.back().getNode()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 5748 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5749 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5750 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5751 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5752 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5753 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 5754 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 5755 | // int first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5756 | if (SrcEltVT.isFloatingPoint()) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5757 | // Convert the input float vector to a int vector where the elements are the |
| 5758 | // same sizes. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5759 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5760 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5761 | BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5762 | SrcEltVT = IntVT; |
| 5763 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5764 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5765 | // Now we know the input is an integer vector. If the output is a FP type, |
| 5766 | // convert to integer first, then to FP of the right size. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5767 | if (DstEltVT.isFloatingPoint()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5768 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5769 | EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5770 | SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5771 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5772 | // Next, convert to FP elements of the same size. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5773 | return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5774 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5775 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5776 | // Okay, we know the src/dst types are both integers of differing types. |
| 5777 | // Handling growing first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5778 | assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5779 | if (SrcBitSize < DstBitSize) { |
| 5780 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5781 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5782 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5783 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5784 | i += NumInputsPerOutput) { |
| 5785 | bool isLE = TLI.isLittleEndian(); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5786 | APInt NewBits = APInt(DstBitSize, 0); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5787 | bool EltIsUndef = true; |
| 5788 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 5789 | // Shift the previously computed bits over. |
| 5790 | NewBits <<= SrcBitSize; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5791 | SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5792 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 5793 | EltIsUndef = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5794 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5795 | NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). |
Dan Gohman | 58c2587 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 5796 | zextOrTrunc(SrcBitSize).zext(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5797 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5798 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5799 | if (EltIsUndef) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5800 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5801 | else |
| 5802 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 5803 | } |
| 5804 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5805 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5806 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5807 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5808 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5809 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5810 | // Finally, this must be the case where we are shrinking elements: each input |
| 5811 | // turns into multiple outputs. |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5812 | bool isS2V = ISD::isScalarToVector(BV); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5813 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5814 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 5815 | NumOutputsPerInput*BV->getNumOperands()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5816 | SmallVector<SDValue, 8> Ops; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5817 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5818 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5819 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 5820 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5821 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5822 | continue; |
| 5823 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5824 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5825 | APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))-> |
| 5826 | getAPIntValue().zextOrTrunc(SrcBitSize); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5827 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5828 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5829 | APInt ThisVal = OpVal.trunc(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5830 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5831 | if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal) |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5832 | // Simply turn this into a SCALAR_TO_VECTOR of the new type. |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5833 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 5834 | Ops[0]); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5835 | OpVal = OpVal.lshr(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | // For big endian targets, swap the order of the pieces of each element. |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 5839 | if (TLI.isBigEndian()) |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5840 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 5841 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5842 | |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5843 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5844 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5847 | SDValue DAGCombiner::visitFADD(SDNode *N) { |
| 5848 | SDValue N0 = N->getOperand(0); |
| 5849 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5850 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5851 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5852 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5853 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5854 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5855 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5856 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5857 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 5858 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5859 | |
Lang Hames | 0180694 | 2012-06-14 20:37:15 +0000 | [diff] [blame] | 5860 | // fold (fadd c1, c2) -> c1 + c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 5861 | if (N0CFP && N1CFP) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5862 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5863 | // canonicalize constant to RHS |
| 5864 | if (N0CFP && !N1CFP) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5865 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0); |
| 5866 | // fold (fadd A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5867 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 5868 | N1CFP->getValueAPF().isZero()) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5869 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5870 | // fold (fadd A, (fneg B)) -> (fsub A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5871 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 5872 | isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5873 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5874 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5875 | // fold (fadd (fneg A), B) -> (fsub B, A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5876 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 5877 | isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5878 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5879 | GetNegatedExpression(N0, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5880 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 5881 | // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2)) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5882 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 5883 | N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() && |
| 5884 | isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5885 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0), |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5886 | DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5887 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5888 | |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 5889 | // No FP constant should be created after legalization as Instruction |
| 5890 | // Selection pass has hard time in dealing with FP constant. |
| 5891 | // |
| 5892 | // We don't need test this condition for transformation like following, as |
| 5893 | // the DAG being transformed implies it is legal to take FP constant as |
| 5894 | // operand. |
| 5895 | // |
| 5896 | // (fadd (fmul c, x), x) -> (fmul c+1, x) |
| 5897 | // |
| 5898 | bool AllowNewFpConst = (Level < AfterLegalizeDAG); |
| 5899 | |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 5900 | // If allow, fold (fadd (fneg x), x) -> 0.0 |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 5901 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 5902 | N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) { |
| 5903 | return DAG.getConstantFP(0.0, VT); |
| 5904 | } |
| 5905 | |
| 5906 | // If allow, fold (fadd x, (fneg x)) -> 0.0 |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 5907 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 5908 | N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) { |
| 5909 | return DAG.getConstantFP(0.0, VT); |
| 5910 | } |
| 5911 | |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 5912 | // In unsafe math mode, we can fold chains of FADD's of the same value |
| 5913 | // into multiplications. This transform is not safe in general because |
| 5914 | // we are reducing the number of rounding steps. |
| 5915 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5916 | TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && |
| 5917 | !N0CFP && !N1CFP) { |
| 5918 | if (N0.getOpcode() == ISD::FMUL) { |
| 5919 | ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); |
| 5920 | ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); |
| 5921 | |
| 5922 | // (fadd (fmul c, x), x) -> (fmul c+1, x) |
| 5923 | if (CFP00 && !CFP01 && N0.getOperand(1) == N1) { |
| 5924 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5925 | SDValue(CFP00, 0), |
| 5926 | DAG.getConstantFP(1.0, VT)); |
| 5927 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5928 | N1, NewCFP); |
| 5929 | } |
| 5930 | |
| 5931 | // (fadd (fmul x, c), x) -> (fmul c+1, x) |
| 5932 | if (CFP01 && !CFP00 && N0.getOperand(0) == N1) { |
| 5933 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5934 | SDValue(CFP01, 0), |
| 5935 | DAG.getConstantFP(1.0, VT)); |
| 5936 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5937 | N1, NewCFP); |
| 5938 | } |
| 5939 | |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 5940 | // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x) |
| 5941 | if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD && |
| 5942 | N1.getOperand(0) == N1.getOperand(1) && |
| 5943 | N0.getOperand(1) == N1.getOperand(0)) { |
| 5944 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5945 | SDValue(CFP00, 0), |
| 5946 | DAG.getConstantFP(2.0, VT)); |
| 5947 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5948 | N0.getOperand(1), NewCFP); |
| 5949 | } |
| 5950 | |
| 5951 | // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x) |
| 5952 | if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD && |
| 5953 | N1.getOperand(0) == N1.getOperand(1) && |
| 5954 | N0.getOperand(0) == N1.getOperand(0)) { |
| 5955 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5956 | SDValue(CFP01, 0), |
| 5957 | DAG.getConstantFP(2.0, VT)); |
| 5958 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5959 | N0.getOperand(0), NewCFP); |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | if (N1.getOpcode() == ISD::FMUL) { |
| 5964 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); |
| 5965 | ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1)); |
| 5966 | |
| 5967 | // (fadd x, (fmul c, x)) -> (fmul c+1, x) |
| 5968 | if (CFP10 && !CFP11 && N1.getOperand(1) == N0) { |
| 5969 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5970 | SDValue(CFP10, 0), |
| 5971 | DAG.getConstantFP(1.0, VT)); |
| 5972 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5973 | N0, NewCFP); |
| 5974 | } |
| 5975 | |
| 5976 | // (fadd x, (fmul x, c)) -> (fmul c+1, x) |
| 5977 | if (CFP11 && !CFP10 && N1.getOperand(0) == N0) { |
| 5978 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5979 | SDValue(CFP11, 0), |
| 5980 | DAG.getConstantFP(1.0, VT)); |
| 5981 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5982 | N0, NewCFP); |
| 5983 | } |
| 5984 | |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 5985 | |
| 5986 | // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x) |
| 5987 | if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD && |
| 5988 | N1.getOperand(0) == N1.getOperand(1) && |
| 5989 | N0.getOperand(1) == N1.getOperand(0)) { |
| 5990 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5991 | SDValue(CFP10, 0), |
| 5992 | DAG.getConstantFP(2.0, VT)); |
| 5993 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 5994 | N0.getOperand(1), NewCFP); |
| 5995 | } |
| 5996 | |
| 5997 | // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x) |
| 5998 | if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD && |
| 5999 | N1.getOperand(0) == N1.getOperand(1) && |
| 6000 | N0.getOperand(0) == N1.getOperand(0)) { |
| 6001 | SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 6002 | SDValue(CFP11, 0), |
| 6003 | DAG.getConstantFP(2.0, VT)); |
| 6004 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 6005 | N0.getOperand(0), NewCFP); |
| 6006 | } |
| 6007 | } |
| 6008 | |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6009 | if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) { |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6010 | ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); |
| 6011 | // (fadd (fadd x, x), x) -> (fmul 3.0, x) |
| 6012 | if (!CFP && N0.getOperand(0) == N0.getOperand(1) && |
| 6013 | (N0.getOperand(0) == N1)) { |
| 6014 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 6015 | N1, DAG.getConstantFP(3.0, VT)); |
| 6016 | } |
| 6017 | } |
| 6018 | |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6019 | if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) { |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6020 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); |
| 6021 | // (fadd x, (fadd x, x)) -> (fmul 3.0, x) |
| 6022 | if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) && |
| 6023 | N1.getOperand(0) == N0) { |
| 6024 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 6025 | N0, DAG.getConstantFP(3.0, VT)); |
| 6026 | } |
| 6027 | } |
| 6028 | |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6029 | // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x) |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6030 | if (AllowNewFpConst && |
| 6031 | N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD && |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6032 | N0.getOperand(0) == N0.getOperand(1) && |
| 6033 | N1.getOperand(0) == N1.getOperand(1) && |
| 6034 | N0.getOperand(0) == N1.getOperand(0)) { |
| 6035 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 6036 | N0.getOperand(0), |
| 6037 | DAG.getConstantFP(4.0, VT)); |
| 6038 | } |
| 6039 | } |
| 6040 | |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6041 | // FADD -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6042 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6043 | DAG.getTarget().Options.UnsafeFPMath) && |
| 6044 | DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) && |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6045 | TLI.isOperationLegalOrCustom(ISD::FMA, VT)) { |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6046 | |
| 6047 | // fold (fadd (fmul x, y), z) -> (fma x, y, z) |
| 6048 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) { |
| 6049 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 6050 | N0.getOperand(0), N0.getOperand(1), N1); |
| 6051 | } |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6052 | |
Michael Liao | b79bff5 | 2012-09-01 04:09:16 +0000 | [diff] [blame] | 6053 | // fold (fadd x, (fmul y, z)) -> (fma y, z, x) |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6054 | // Note: Commutes FADD operands. |
| 6055 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) { |
| 6056 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 6057 | N1.getOperand(0), N1.getOperand(1), N0); |
| 6058 | } |
| 6059 | } |
| 6060 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6061 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6064 | SDValue DAGCombiner::visitFSUB(SDNode *N) { |
| 6065 | SDValue N0 = N->getOperand(0); |
| 6066 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6067 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6068 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6069 | EVT VT = N->getValueType(0); |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6070 | DebugLoc dl = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6071 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6072 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6073 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6074 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6075 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6076 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6077 | |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6078 | // fold (fsub c1, c2) -> c1-c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6079 | if (N0CFP && N1CFP) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 6080 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6081 | // fold (fsub A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6082 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6083 | N1CFP && N1CFP->getValueAPF().isZero()) |
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 6084 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6085 | // fold (fsub 0, B) -> -B |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6086 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6087 | N0CFP && N0CFP->getValueAPF().isZero()) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6088 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6089 | return GetNegatedExpression(N1, DAG, LegalOperations); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6090 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6091 | return DAG.getNode(ISD::FNEG, dl, VT, N1); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6092 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6093 | // fold (fsub A, (fneg B)) -> (fadd A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6094 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6095 | return DAG.getNode(ISD::FADD, dl, VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6096 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6097 | |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6098 | // If 'unsafe math' is enabled, fold |
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6099 | // (fsub x, x) -> 0.0 & |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6100 | // (fsub x, (fadd x, y)) -> (fneg y) & |
| 6101 | // (fsub x, (fadd y, x)) -> (fneg y) |
| 6102 | if (DAG.getTarget().Options.UnsafeFPMath) { |
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6103 | if (N0 == N1) |
| 6104 | return DAG.getConstantFP(0.0f, VT); |
| 6105 | |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6106 | if (N1.getOpcode() == ISD::FADD) { |
| 6107 | SDValue N10 = N1->getOperand(0); |
| 6108 | SDValue N11 = N1->getOperand(1); |
| 6109 | |
| 6110 | if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, |
| 6111 | &DAG.getTarget().Options)) |
| 6112 | return GetNegatedExpression(N11, DAG, LegalOperations); |
| 6113 | else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, |
| 6114 | &DAG.getTarget().Options)) |
| 6115 | return GetNegatedExpression(N10, DAG, LegalOperations); |
| 6116 | } |
| 6117 | } |
| 6118 | |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6119 | // FSUB -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6120 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6121 | DAG.getTarget().Options.UnsafeFPMath) && |
| 6122 | DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) && |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6123 | TLI.isOperationLegalOrCustom(ISD::FMA, VT)) { |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6124 | |
| 6125 | // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z)) |
| 6126 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) { |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6127 | return DAG.getNode(ISD::FMA, dl, VT, |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6128 | N0.getOperand(0), N0.getOperand(1), |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6129 | DAG.getNode(ISD::FNEG, dl, VT, N1)); |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x) |
| 6133 | // Note: Commutes FSUB operands. |
| 6134 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) { |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6135 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6136 | DAG.getNode(ISD::FNEG, dl, VT, |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6137 | N1.getOperand(0)), |
| 6138 | N1.getOperand(1), N0); |
| 6139 | } |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6140 | |
| 6141 | // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z)) |
| 6142 | if (N0.getOpcode() == ISD::FNEG && |
| 6143 | N0.getOperand(0).getOpcode() == ISD::FMUL && |
| 6144 | N0->hasOneUse() && N0.getOperand(0).hasOneUse()) { |
| 6145 | SDValue N00 = N0.getOperand(0).getOperand(0); |
| 6146 | SDValue N01 = N0.getOperand(0).getOperand(1); |
| 6147 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6148 | DAG.getNode(ISD::FNEG, dl, VT, N00), N01, |
| 6149 | DAG.getNode(ISD::FNEG, dl, VT, N1)); |
| 6150 | } |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6153 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6156 | SDValue DAGCombiner::visitFMUL(SDNode *N) { |
| 6157 | SDValue N0 = N->getOperand(0); |
| 6158 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6159 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6160 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6161 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6162 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6163 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6164 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6165 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6166 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6167 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6168 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6169 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6170 | // fold (fmul c1, c2) -> c1*c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6171 | if (N0CFP && N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6172 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6173 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6174 | if (N0CFP && !N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6175 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0); |
| 6176 | // fold (fmul A, 0) -> 0 |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6177 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6178 | N1CFP && N1CFP->getValueAPF().isZero()) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6179 | return N1; |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6180 | // fold (fmul A, 0) -> 0, vector edition. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6181 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6182 | ISD::isBuildVectorAllZeros(N1.getNode())) |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6183 | return N1; |
Owen Anderson | 363e4b9 | 2012-05-02 21:32:35 +0000 | [diff] [blame] | 6184 | // fold (fmul A, 1.0) -> A |
| 6185 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| 6186 | return N0; |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6187 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 6188 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6189 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0); |
Dan Gohman | eb1fedc | 2009-08-10 16:50:32 +0000 | [diff] [blame] | 6190 | // fold (fmul X, -1.0) -> (fneg X) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6191 | if (N1CFP && N1CFP->isExactlyValue(-1.0)) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6192 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6193 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6194 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6195 | // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6196 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6197 | &DAG.getTarget().Options)) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6198 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6199 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6200 | // Both can be negated for free, check to see if at least one is cheaper |
| 6201 | // negated. |
| 6202 | if (LHSNeg == 2 || RHSNeg == 2) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6203 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6204 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 6205 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6206 | } |
| 6207 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6208 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 6209 | // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6210 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6211 | N1CFP && N0.getOpcode() == ISD::FMUL && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6212 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6213 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6214 | DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6215 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6216 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6217 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6220 | SDValue DAGCombiner::visitFMA(SDNode *N) { |
| 6221 | SDValue N0 = N->getOperand(0); |
| 6222 | SDValue N1 = N->getOperand(1); |
| 6223 | SDValue N2 = N->getOperand(2); |
| 6224 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6225 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 6226 | EVT VT = N->getValueType(0); |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6227 | DebugLoc dl = N->getDebugLoc(); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6228 | |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6229 | if (DAG.getTarget().Options.UnsafeFPMath) { |
| 6230 | if (N0CFP && N0CFP->isZero()) |
| 6231 | return N2; |
| 6232 | if (N1CFP && N1CFP->isZero()) |
| 6233 | return N2; |
| 6234 | } |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6235 | if (N0CFP && N0CFP->isExactlyValue(1.0)) |
| 6236 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2); |
| 6237 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| 6238 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2); |
| 6239 | |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6240 | // Canonicalize (fma c, x, y) -> (fma x, c, y) |
Owen Anderson | f917d20 | 2012-05-30 18:54:50 +0000 | [diff] [blame] | 6241 | if (N0CFP && !N1CFP) |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6242 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2); |
| 6243 | |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6244 | // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) |
| 6245 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 6246 | N2.getOpcode() == ISD::FMUL && |
| 6247 | N0 == N2.getOperand(0) && |
| 6248 | N2.getOperand(1).getOpcode() == ISD::ConstantFP) { |
| 6249 | return DAG.getNode(ISD::FMUL, dl, VT, N0, |
| 6250 | DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1))); |
| 6251 | } |
| 6252 | |
| 6253 | |
| 6254 | // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y) |
| 6255 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6256 | N0.getOpcode() == ISD::FMUL && N1CFP && |
| 6257 | N0.getOperand(1).getOpcode() == ISD::ConstantFP) { |
| 6258 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6259 | N0.getOperand(0), |
| 6260 | DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)), |
| 6261 | N2); |
| 6262 | } |
| 6263 | |
| 6264 | // (fma x, 1, y) -> (fadd x, y) |
| 6265 | // (fma x, -1, y) -> (fadd (fneg x), y) |
| 6266 | if (N1CFP) { |
| 6267 | if (N1CFP->isExactlyValue(1.0)) |
| 6268 | return DAG.getNode(ISD::FADD, dl, VT, N0, N2); |
| 6269 | |
| 6270 | if (N1CFP->isExactlyValue(-1.0) && |
| 6271 | (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) { |
| 6272 | SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0); |
| 6273 | AddToWorkList(RHSNeg.getNode()); |
| 6274 | return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg); |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | // (fma x, c, x) -> (fmul x, (c+1)) |
| 6279 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) { |
| 6280 | return DAG.getNode(ISD::FMUL, dl, VT, |
| 6281 | N0, |
| 6282 | DAG.getNode(ISD::FADD, dl, VT, |
| 6283 | N1, DAG.getConstantFP(1.0, VT))); |
| 6284 | } |
| 6285 | |
| 6286 | // (fma x, c, (fneg x)) -> (fmul x, (c-1)) |
| 6287 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 6288 | N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) { |
| 6289 | return DAG.getNode(ISD::FMUL, dl, VT, |
| 6290 | N0, |
| 6291 | DAG.getNode(ISD::FADD, dl, VT, |
| 6292 | N1, DAG.getConstantFP(-1.0, VT))); |
| 6293 | } |
| 6294 | |
| 6295 | |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6296 | return SDValue(); |
| 6297 | } |
| 6298 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6299 | SDValue DAGCombiner::visitFDIV(SDNode *N) { |
| 6300 | SDValue N0 = N->getOperand(0); |
| 6301 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6302 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6303 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6304 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6305 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6306 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6307 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6308 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6309 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6310 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6311 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6312 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6313 | // fold (fdiv c1, c2) -> c1/c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6314 | if (N0CFP && N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6315 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6316 | |
Duncan Sands | 3ef3fcf | 2012-04-08 18:08:12 +0000 | [diff] [blame] | 6317 | // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable. |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6318 | if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) { |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6319 | // Compute the reciprocal 1.0 / c2. |
| 6320 | APFloat N1APF = N1CFP->getValueAPF(); |
| 6321 | APFloat Recip(N1APF.getSemantics(), 1); // 1.0 |
| 6322 | APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven); |
Duncan Sands | 507bb7a | 2012-04-10 20:35:27 +0000 | [diff] [blame] | 6323 | // Only do the transform if the reciprocal is a legal fp immediate that |
| 6324 | // isn't too nasty (eg NaN, denormal, ...). |
| 6325 | if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty |
Anton Korobeynikov | 999821c | 2012-04-10 13:22:49 +0000 | [diff] [blame] | 6326 | (!LegalOperations || |
| 6327 | // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM |
| 6328 | // backend)... we should handle this gracefully after Legalize. |
| 6329 | // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) || |
| 6330 | TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) || |
| 6331 | TLI.isFPImmLegal(Recip, VT))) |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6332 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, |
| 6333 | DAG.getConstantFP(Recip, VT)); |
| 6334 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6335 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6336 | // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6337 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6338 | &DAG.getTarget().Options)) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6339 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6340 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6341 | // Both can be negated for free, check to see if at least one is cheaper |
| 6342 | // negated. |
| 6343 | if (LHSNeg == 2 || RHSNeg == 2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6344 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6345 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 6346 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6347 | } |
| 6348 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6349 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6350 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6353 | SDValue DAGCombiner::visitFREM(SDNode *N) { |
| 6354 | SDValue N0 = N->getOperand(0); |
| 6355 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6356 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6357 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6358 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6359 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6360 | // fold (frem c1, c2) -> fmod(c1,c2) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6361 | if (N0CFP && N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6362 | return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6363 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6364 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6367 | SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 6368 | SDValue N0 = N->getOperand(0); |
| 6369 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6370 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6371 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6372 | EVT VT = N->getValueType(0); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6373 | |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6374 | if (N0CFP && N1CFP) // Constant fold |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 6375 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6376 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6377 | if (N1CFP) { |
Dale Johannesen | e6c1742 | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 6378 | const APFloat& V = N1CFP->getValueAPF(); |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 6379 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 6380 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6381 | if (!V.isNegative()) { |
| 6382 | if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6383 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6384 | } else { |
| 6385 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6386 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 6387 | DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0)); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6388 | } |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6389 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6390 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6391 | // copysign(fabs(x), y) -> copysign(x, y) |
| 6392 | // copysign(fneg(x), y) -> copysign(x, y) |
| 6393 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 6394 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 6395 | N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6396 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 6397 | N0.getOperand(0), N1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6398 | |
| 6399 | // copysign(x, abs(y)) -> abs(x) |
| 6400 | if (N1.getOpcode() == ISD::FABS) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6401 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6402 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6403 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 6404 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6405 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 6406 | N0, N1.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6407 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6408 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 6409 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 6410 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6411 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 6412 | N0, N1.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6413 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6414 | return SDValue(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6417 | SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
| 6418 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6419 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6420 | EVT VT = N->getValueType(0); |
| 6421 | EVT OpVT = N0.getValueType(); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6422 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6423 | // fold (sint_to_fp c1) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6424 | if (N0C && |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6425 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6426 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6427 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6428 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6429 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6430 | // If the input is a legal type, and SINT_TO_FP is not legal on this target, |
| 6431 | // but UINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6432 | if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && |
| 6433 | TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6434 | // If the sign bit is known to be zero, we can change this to UINT_TO_FP. |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6435 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6436 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6437 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6438 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6439 | // The next optimizations are desireable only if SELECT_CC can be lowered. |
| 6440 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 6441 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 6442 | // about, since there is no way to mark an opcode illegal at all value types |
| 6443 | // (See also visitSELECT) |
| 6444 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { |
| 6445 | // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
| 6446 | if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 && |
| 6447 | !VT.isVector() && |
| 6448 | (!LegalOperations || |
| 6449 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6450 | SDValue Ops[] = |
| 6451 | { N0.getOperand(0), N0.getOperand(1), |
| 6452 | DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 6453 | N0.getOperand(2) }; |
| 6454 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 6455 | } |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6456 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6457 | // fold (sint_to_fp (zext (setcc x, y, cc))) -> |
| 6458 | // (select_cc x, y, 1.0, 0.0,, cc) |
| 6459 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 6460 | N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() && |
| 6461 | (!LegalOperations || |
| 6462 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6463 | SDValue Ops[] = |
| 6464 | { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1), |
| 6465 | DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 6466 | N0.getOperand(0).getOperand(2) }; |
| 6467 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 6468 | } |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6471 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6474 | SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
| 6475 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6476 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6477 | EVT VT = N->getValueType(0); |
| 6478 | EVT OpVT = N0.getValueType(); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6479 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6480 | // fold (uint_to_fp c1) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6481 | if (N0C && |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6482 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6483 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6484 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6485 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6486 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6487 | // If the input is a legal type, and UINT_TO_FP is not legal on this target, |
| 6488 | // but SINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6489 | if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && |
| 6490 | TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6491 | // If the sign bit is known to be zero, we can change this to SINT_TO_FP. |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6492 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6493 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6494 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6495 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6496 | // The next optimizations are desireable only if SELECT_CC can be lowered. |
| 6497 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 6498 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 6499 | // about, since there is no way to mark an opcode illegal at all value types |
| 6500 | // (See also visitSELECT) |
| 6501 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { |
| 6502 | // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6503 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6504 | if (N0.getOpcode() == ISD::SETCC && !VT.isVector() && |
| 6505 | (!LegalOperations || |
| 6506 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6507 | SDValue Ops[] = |
| 6508 | { N0.getOperand(0), N0.getOperand(1), |
| 6509 | DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT), |
| 6510 | N0.getOperand(2) }; |
| 6511 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 6512 | } |
| 6513 | } |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6514 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6515 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6518 | SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
| 6519 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6520 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6521 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6522 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6523 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6524 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6525 | return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0); |
| 6526 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6527 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6530 | SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
| 6531 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6532 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6533 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6534 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6535 | // fold (fp_to_uint c1fp) -> c1 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6536 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6537 | return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0); |
| 6538 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6539 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6542 | SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { |
| 6543 | SDValue N0 = N->getOperand(0); |
| 6544 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6545 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6546 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6547 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6548 | // fold (fp_round c1fp) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6549 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6550 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6551 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6552 | // fold (fp_round (fp_extend x)) -> x |
| 6553 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 6554 | return N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6555 | |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6556 | // fold (fp_round (fp_round x)) -> (fp_round x) |
| 6557 | if (N0.getOpcode() == ISD::FP_ROUND) { |
| 6558 | // This is a value preserving truncation if both round's are. |
| 6559 | bool IsTrunc = N->getConstantOperandVal(1) == 1 && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6560 | N0.getNode()->getConstantOperandVal(1) == 1; |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6561 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6562 | DAG.getIntPtrConstant(IsTrunc)); |
| 6563 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6564 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6565 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6566 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6567 | SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT, |
| 6568 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6569 | AddToWorkList(Tmp.getNode()); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6570 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 6571 | Tmp, N0.getOperand(1)); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6572 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6573 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6574 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6577 | SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
| 6578 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6579 | EVT VT = N->getValueType(0); |
| 6580 | EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6581 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6582 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6583 | // fold (fp_round_inreg c1fp) -> c1fp |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6584 | if (N0CFP && isTypeLegal(EVT)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 6585 | SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6586 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6587 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6588 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6589 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6590 | } |
| 6591 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6592 | SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { |
| 6593 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6594 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6595 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6596 | |
Chris Lattner | 5938bef | 2007-12-29 06:55:23 +0000 | [diff] [blame] | 6597 | // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6598 | if (N->hasOneUse() && |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6599 | N->use_begin()->getOpcode() == ISD::FP_ROUND) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6600 | return SDValue(); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6601 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6602 | // fold (fp_extend c1fp) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6603 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6604 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6605 | |
| 6606 | // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the |
| 6607 | // value of X. |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 6608 | if (N0.getOpcode() == ISD::FP_ROUND |
| 6609 | && N0.getNode()->getConstantOperandVal(1) == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6610 | SDValue In = N0.getOperand(0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6611 | if (In.getValueType() == VT) return In; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 6612 | if (VT.bitsLT(In.getValueType())) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6613 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, |
| 6614 | In, N0.getOperand(1)); |
| 6615 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6616 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6617 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6618 | // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6619 | if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6620 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 6621 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 6622 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 6623 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6624 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 6625 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6626 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6627 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 6628 | LN0->getAlignment()); |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6629 | CombineTo(N, ExtLoad); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6630 | CombineTo(N0.getNode(), |
| 6631 | DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), |
| 6632 | N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)), |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6633 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6634 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6635 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6636 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6637 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6640 | SDValue DAGCombiner::visitFNEG(SDNode *N) { |
| 6641 | SDValue N0 = N->getOperand(0); |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6642 | EVT VT = N->getValueType(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6643 | |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 6644 | if (VT.isVector()) { |
| 6645 | SDValue FoldedVOp = SimplifyVUnaryOp(N); |
| 6646 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 956342b | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6649 | if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(), |
| 6650 | &DAG.getTarget().Options)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6651 | return GetNegatedExpression(N0, DAG, LegalOperations); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6652 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6653 | // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading |
| 6654 | // constant pool values. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6655 | if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST && |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6656 | !VT.isVector() && |
| 6657 | N0.getNode()->hasOneUse() && |
| 6658 | N0.getOperand(0).getValueType().isInteger()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6659 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6660 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6661 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6662 | Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int, |
| 6663 | DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6664 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6665 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6666 | VT, Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6667 | } |
| 6668 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6669 | |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6670 | // (fneg (fmul c, x)) -> (fmul -c, x) |
| 6671 | if (N0.getOpcode() == ISD::FMUL) { |
| 6672 | ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); |
| 6673 | if (CFP1) { |
| 6674 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
| 6675 | N0.getOperand(0), |
| 6676 | DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, |
| 6677 | N0.getOperand(1))); |
| 6678 | } |
| 6679 | } |
| 6680 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6681 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6684 | SDValue DAGCombiner::visitFCEIL(SDNode *N) { |
| 6685 | SDValue N0 = N->getOperand(0); |
| 6686 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6687 | EVT VT = N->getValueType(0); |
| 6688 | |
| 6689 | // fold (fceil c1) -> fceil(c1) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6690 | if (N0CFP) |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6691 | return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0); |
| 6692 | |
| 6693 | return SDValue(); |
| 6694 | } |
| 6695 | |
| 6696 | SDValue DAGCombiner::visitFTRUNC(SDNode *N) { |
| 6697 | SDValue N0 = N->getOperand(0); |
| 6698 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6699 | EVT VT = N->getValueType(0); |
| 6700 | |
| 6701 | // fold (ftrunc c1) -> ftrunc(c1) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6702 | if (N0CFP) |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6703 | return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0); |
| 6704 | |
| 6705 | return SDValue(); |
| 6706 | } |
| 6707 | |
| 6708 | SDValue DAGCombiner::visitFFLOOR(SDNode *N) { |
| 6709 | SDValue N0 = N->getOperand(0); |
| 6710 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6711 | EVT VT = N->getValueType(0); |
| 6712 | |
| 6713 | // fold (ffloor c1) -> ffloor(c1) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6714 | if (N0CFP) |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6715 | return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0); |
| 6716 | |
| 6717 | return SDValue(); |
| 6718 | } |
| 6719 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6720 | SDValue DAGCombiner::visitFABS(SDNode *N) { |
| 6721 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6722 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6723 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6724 | |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 6725 | if (VT.isVector()) { |
| 6726 | SDValue FoldedVOp = SimplifyVUnaryOp(N); |
| 6727 | if (FoldedVOp.getNode()) return FoldedVOp; |
| 6728 | } |
| 6729 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6730 | // fold (fabs c1) -> fabs(c1) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6731 | if (N0CFP) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6732 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6733 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6734 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 6735 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6736 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6737 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 6738 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6739 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6740 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6741 | // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading |
| 6742 | // constant pool values. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6743 | if (!TLI.isFAbsFree(VT) && |
| 6744 | N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6745 | N0.getOperand(0).getValueType().isInteger() && |
| 6746 | !N0.getOperand(0).getValueType().isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6747 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6748 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6749 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6750 | Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int, |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6751 | DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6752 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6753 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6754 | N->getValueType(0), Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6755 | } |
| 6756 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6757 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6758 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6761 | SDValue DAGCombiner::visitBRCOND(SDNode *N) { |
| 6762 | SDValue Chain = N->getOperand(0); |
| 6763 | SDValue N1 = N->getOperand(1); |
| 6764 | SDValue N2 = N->getOperand(2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6765 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 6766 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 6767 | // branch. However that doesn't happen very often in normal code, because |
| 6768 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 6769 | // If we did this folding here, it would be necessary to update the |
| 6770 | // MachineBasicBlock CFG, which is awkward. |
| 6771 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6772 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 6773 | // on the target. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6774 | if (N1.getOpcode() == ISD::SETCC && |
Tom Stellard | 3ef5383 | 2013-03-08 15:36:57 +0000 | [diff] [blame] | 6775 | TLI.isOperationLegalOrCustom(ISD::BR_CC, |
| 6776 | N1.getOperand(0).getValueType())) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6777 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6778 | Chain, N1.getOperand(2), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6779 | N1.getOperand(0), N1.getOperand(1), N2); |
| 6780 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6781 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6782 | if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || |
| 6783 | ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && |
| 6784 | (N1.getOperand(0).hasOneUse() && |
| 6785 | N1.getOperand(0).getOpcode() == ISD::SRL))) { |
| 6786 | SDNode *Trunc = 0; |
| 6787 | if (N1.getOpcode() == ISD::TRUNCATE) { |
| 6788 | // Look pass the truncate. |
| 6789 | Trunc = N1.getNode(); |
| 6790 | N1 = N1.getOperand(0); |
| 6791 | } |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6792 | |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6793 | // Match this pattern so that we can generate simpler code: |
| 6794 | // |
| 6795 | // %a = ... |
| 6796 | // %b = and i32 %a, 2 |
| 6797 | // %c = srl i32 %b, 1 |
| 6798 | // brcond i32 %c ... |
| 6799 | // |
| 6800 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6801 | // |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6802 | // %a = ... |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6803 | // %b = and i32 %a, 2 |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6804 | // %c = setcc eq %b, 0 |
| 6805 | // brcond %c ... |
| 6806 | // |
| 6807 | // This applies only when the AND constant value has one bit set and the |
| 6808 | // SRL constant is equal to the log2 of the AND constant. The back-end is |
| 6809 | // smart enough to convert the result into a TEST/JMP sequence. |
| 6810 | SDValue Op0 = N1.getOperand(0); |
| 6811 | SDValue Op1 = N1.getOperand(1); |
| 6812 | |
| 6813 | if (Op0.getOpcode() == ISD::AND && |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6814 | Op1.getOpcode() == ISD::Constant) { |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6815 | SDValue AndOp1 = Op0.getOperand(1); |
| 6816 | |
| 6817 | if (AndOp1.getOpcode() == ISD::Constant) { |
| 6818 | const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); |
| 6819 | |
| 6820 | if (AndConst.isPowerOf2() && |
| 6821 | cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { |
| 6822 | SDValue SetCC = |
| 6823 | DAG.getSetCC(N->getDebugLoc(), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 6824 | getSetCCResultType(Op0.getValueType()), |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6825 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 6826 | ISD::SETNE); |
| 6827 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6828 | SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6829 | MVT::Other, Chain, SetCC, N2); |
| 6830 | // Don't add the new BRCond into the worklist or else SimplifySelectCC |
| 6831 | // will convert it back to (X & C1) >> C2. |
| 6832 | CombineTo(N, NewBRCond, false); |
| 6833 | // Truncate is dead. |
| 6834 | if (Trunc) { |
| 6835 | removeFromWorkList(Trunc); |
| 6836 | DAG.DeleteNode(Trunc); |
| 6837 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6838 | // Replace the uses of SRL with SETCC |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6839 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6840 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6841 | removeFromWorkList(N1.getNode()); |
| 6842 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6843 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6844 | } |
| 6845 | } |
| 6846 | } |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6847 | |
| 6848 | if (Trunc) |
| 6849 | // Restore N1 if the above transformation doesn't match. |
| 6850 | N1 = N->getOperand(1); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6851 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6852 | |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6853 | // Transform br(xor(x, y)) -> br(x != y) |
| 6854 | // Transform br(xor(xor(x,y), 1)) -> br (x == y) |
| 6855 | if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { |
| 6856 | SDNode *TheXor = N1.getNode(); |
| 6857 | SDValue Op0 = TheXor->getOperand(0); |
| 6858 | SDValue Op1 = TheXor->getOperand(1); |
| 6859 | if (Op0.getOpcode() == Op1.getOpcode()) { |
| 6860 | // Avoid missing important xor optimizations. |
| 6861 | SDValue Tmp = visitXOR(TheXor); |
Evan Cheng | 78ec025 | 2013-01-09 20:56:40 +0000 | [diff] [blame] | 6862 | if (Tmp.getNode()) { |
| 6863 | if (Tmp.getNode() != TheXor) { |
| 6864 | DEBUG(dbgs() << "\nReplacing.8 "; |
| 6865 | TheXor->dump(&DAG); |
| 6866 | dbgs() << "\nWith: "; |
| 6867 | Tmp.getNode()->dump(&DAG); |
| 6868 | dbgs() << '\n'); |
| 6869 | WorkListRemover DeadNodes(*this); |
| 6870 | DAG.ReplaceAllUsesOfValueWith(N1, Tmp); |
| 6871 | removeFromWorkList(TheXor); |
| 6872 | DAG.DeleteNode(TheXor); |
| 6873 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6874 | MVT::Other, Chain, Tmp, N2); |
| 6875 | } |
| 6876 | |
Benjamin Kramer | 0b68b75 | 2013-03-30 21:28:18 +0000 | [diff] [blame] | 6877 | // visitXOR has changed XOR's operands or replaced the XOR completely, |
| 6878 | // bail out. |
| 6879 | return SDValue(N, 0); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { |
| 6884 | bool Equal = false; |
| 6885 | if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0)) |
| 6886 | if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() && |
| 6887 | Op0.getOpcode() == ISD::XOR) { |
| 6888 | TheXor = Op0.getNode(); |
| 6889 | Equal = true; |
| 6890 | } |
| 6891 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6892 | EVT SetCCVT = N1.getValueType(); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6893 | if (LegalTypes) |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 6894 | SetCCVT = getSetCCResultType(SetCCVT); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6895 | SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(), |
| 6896 | SetCCVT, |
| 6897 | Op0, Op1, |
| 6898 | Equal ? ISD::SETEQ : ISD::SETNE); |
| 6899 | // Replace the uses of XOR with SETCC |
| 6900 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6901 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6902 | removeFromWorkList(N1.getNode()); |
| 6903 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6904 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6905 | MVT::Other, Chain, SetCC, N2); |
| 6906 | } |
| 6907 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6908 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6909 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 6912 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 6913 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6914 | SDValue DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 6915 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6916 | SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6917 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 6918 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 6919 | // branch. However that doesn't happen very often in normal code, because |
| 6920 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 6921 | // If we did this folding here, it would be necessary to update the |
| 6922 | // MachineBasicBlock CFG, which is awkward. |
| 6923 | |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 6924 | // Use SimplifySetCC to simplify SETCC's. |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 6925 | SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 6926 | CondLHS, CondRHS, CC->get(), N->getDebugLoc(), |
| 6927 | false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6928 | if (Simp.getNode()) AddToWorkList(Simp.getNode()); |
Chris Lattner | 30f73e7 | 2006-10-14 03:52:46 +0000 | [diff] [blame] | 6929 | |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 6930 | // fold to a simpler setcc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6931 | if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6932 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6933 | N->getOperand(0), Simp.getOperand(2), |
| 6934 | Simp.getOperand(0), Simp.getOperand(1), |
| 6935 | N->getOperand(4)); |
| 6936 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6937 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 6938 | } |
| 6939 | |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6940 | /// canFoldInAddressingMode - Return true if 'Use' is a load or a store that |
| 6941 | /// uses N as its base pointer and that N may be folded in the load / store |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6942 | /// addressing mode. |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6943 | static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, |
| 6944 | SelectionDAG &DAG, |
| 6945 | const TargetLowering &TLI) { |
| 6946 | EVT VT; |
| 6947 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) { |
| 6948 | if (LD->isIndexed() || LD->getBasePtr().getNode() != N) |
| 6949 | return false; |
| 6950 | VT = Use->getValueType(0); |
| 6951 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) { |
| 6952 | if (ST->isIndexed() || ST->getBasePtr().getNode() != N) |
| 6953 | return false; |
| 6954 | VT = ST->getValue().getValueType(); |
| 6955 | } else |
| 6956 | return false; |
| 6957 | |
Chandler Carruth | 56d433d | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 6958 | TargetLowering::AddrMode AM; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6959 | if (N->getOpcode() == ISD::ADD) { |
| 6960 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 6961 | if (Offset) |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6962 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6963 | AM.BaseOffs = Offset->getSExtValue(); |
| 6964 | else |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6965 | // [reg +/- reg] |
| 6966 | AM.Scale = 1; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6967 | } else if (N->getOpcode() == ISD::SUB) { |
| 6968 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 6969 | if (Offset) |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6970 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6971 | AM.BaseOffs = -Offset->getSExtValue(); |
| 6972 | else |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6973 | // [reg +/- reg] |
| 6974 | AM.Scale = 1; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6975 | } else |
| 6976 | return false; |
| 6977 | |
| 6978 | return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext())); |
| 6979 | } |
| 6980 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 6981 | /// CombineToPreIndexedLoadStore - Try turning a load / store into a |
| 6982 | /// pre-indexed load / store when the base pointer is an add or subtract |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6983 | /// and it has other uses besides the load / store. After the |
| 6984 | /// transformation, the new indexed load / store has effectively folded |
| 6985 | /// the add / subtract in and all of its other uses are redirected to the |
| 6986 | /// new load / store. |
| 6987 | bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6988 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6989 | return false; |
| 6990 | |
| 6991 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6992 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6993 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6994 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6995 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 6996 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6997 | VT = LD->getMemoryVT(); |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 6998 | if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6999 | !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) |
| 7000 | return false; |
| 7001 | Ptr = LD->getBasePtr(); |
| 7002 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7003 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7004 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7005 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7006 | if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && |
| 7007 | !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) |
| 7008 | return false; |
| 7009 | Ptr = ST->getBasePtr(); |
| 7010 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7011 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7012 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7013 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7014 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7015 | // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail |
| 7016 | // out. There is no reason to make this a preinc/predec. |
| 7017 | if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7018 | Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7019 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7020 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7021 | // Ask the target to do addressing mode selection. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7022 | SDValue BasePtr; |
| 7023 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7024 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 7025 | if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) |
| 7026 | return false; |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7027 | |
| 7028 | // Backends without true r+i pre-indexed forms may need to pass a |
| 7029 | // constant base with a variable offset so that constant coercion |
| 7030 | // will work with the patterns in canonical form. |
| 7031 | bool Swapped = false; |
| 7032 | if (isa<ConstantSDNode>(BasePtr)) { |
| 7033 | std::swap(BasePtr, Offset); |
| 7034 | Swapped = true; |
| 7035 | } |
| 7036 | |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7037 | // Don't create a indexed load / store with zero offset. |
| 7038 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7039 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7040 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7041 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7042 | // Try turning it into a pre-indexed load / store except when: |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7043 | // 1) The new base ptr is a frame index. |
| 7044 | // 2) If N is a store and the new base ptr is either the same as or is a |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7045 | // predecessor of the value being stored. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7046 | // 3) Another use of old base ptr is a predecessor of N. If ptr is folded |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7047 | // that would create a cycle. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7048 | // 4) All uses are load / store ops that use it as old base ptr. |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7049 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7050 | // Check #1. Preinc'ing a frame index would require copying the stack pointer |
| 7051 | // (plus the implicit offset) to a register to preinc anyway. |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7052 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7053 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7054 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7055 | // Check #2. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7056 | if (!isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7057 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7058 | if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7059 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7060 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7061 | |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7062 | // If the offset is a constant, there may be other adds of constants that |
| 7063 | // can be folded with this one. We should do this to avoid having to keep |
| 7064 | // a copy of the original base pointer. |
| 7065 | SmallVector<SDNode *, 16> OtherUses; |
| 7066 | if (isa<ConstantSDNode>(Offset)) |
| 7067 | for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(), |
| 7068 | E = BasePtr.getNode()->use_end(); I != E; ++I) { |
| 7069 | SDNode *Use = *I; |
| 7070 | if (Use == Ptr.getNode()) |
| 7071 | continue; |
| 7072 | |
| 7073 | if (Use->isPredecessorOf(N)) |
| 7074 | continue; |
| 7075 | |
| 7076 | if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) { |
| 7077 | OtherUses.clear(); |
| 7078 | break; |
| 7079 | } |
| 7080 | |
| 7081 | SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1); |
| 7082 | if (Op1.getNode() == BasePtr.getNode()) |
| 7083 | std::swap(Op0, Op1); |
| 7084 | assert(Op0.getNode() == BasePtr.getNode() && |
| 7085 | "Use of ADD/SUB but not an operand"); |
| 7086 | |
| 7087 | if (!isa<ConstantSDNode>(Op1)) { |
| 7088 | OtherUses.clear(); |
| 7089 | break; |
| 7090 | } |
| 7091 | |
| 7092 | // FIXME: In some cases, we can be smarter about this. |
| 7093 | if (Op1.getValueType() != Offset.getValueType()) { |
| 7094 | OtherUses.clear(); |
| 7095 | break; |
| 7096 | } |
| 7097 | |
| 7098 | OtherUses.push_back(Use); |
| 7099 | } |
| 7100 | |
| 7101 | if (Swapped) |
| 7102 | std::swap(BasePtr, Offset); |
| 7103 | |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7104 | // Now check for #3 and #4. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7105 | bool RealUse = false; |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7106 | |
| 7107 | // Caches for hasPredecessorHelper |
| 7108 | SmallPtrSet<const SDNode *, 32> Visited; |
| 7109 | SmallVector<const SDNode *, 16> Worklist; |
| 7110 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7111 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 7112 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7113 | SDNode *Use = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7114 | if (Use == N) |
| 7115 | continue; |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7116 | if (N->hasPredecessorHelper(Use, Visited, Worklist)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7117 | return false; |
| 7118 | |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7119 | // If Ptr may be folded in addressing mode of other use, then it's |
| 7120 | // not profitable to do this transformation. |
| 7121 | if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7122 | RealUse = true; |
| 7123 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7124 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7125 | if (!RealUse) |
| 7126 | return false; |
| 7127 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7128 | SDValue Result; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7129 | if (isLoad) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7130 | Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 7131 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7132 | else |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7133 | Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 7134 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7135 | ++PreIndexedNodes; |
| 7136 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7137 | DEBUG(dbgs() << "\nReplacing.4 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7138 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7139 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7140 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7141 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7142 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7143 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7144 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 7145 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7146 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7147 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7150 | // Finally, since the node is now dead, remove it from the graph. |
| 7151 | DAG.DeleteNode(N); |
| 7152 | |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7153 | if (Swapped) |
| 7154 | std::swap(BasePtr, Offset); |
| 7155 | |
| 7156 | // Replace other uses of BasePtr that can be updated to use Ptr |
| 7157 | for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) { |
| 7158 | unsigned OffsetIdx = 1; |
| 7159 | if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode()) |
| 7160 | OffsetIdx = 0; |
| 7161 | assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() == |
| 7162 | BasePtr.getNode() && "Expected BasePtr operand"); |
| 7163 | |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7164 | // We need to replace ptr0 in the following expression: |
| 7165 | // x0 * offset0 + y0 * ptr0 = t0 |
| 7166 | // knowing that |
| 7167 | // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store) |
| 7168 | // |
| 7169 | // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the |
| 7170 | // indexed load/store and the expresion that needs to be re-written. |
| 7171 | // |
| 7172 | // Therefore, we have: |
| 7173 | // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1 |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7174 | |
| 7175 | ConstantSDNode *CN = |
| 7176 | cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx)); |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7177 | int X0, X1, Y0, Y1; |
| 7178 | APInt Offset0 = CN->getAPIntValue(); |
| 7179 | APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue(); |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7180 | |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7181 | X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1; |
| 7182 | Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1; |
| 7183 | X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1; |
| 7184 | Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1; |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7185 | |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7186 | unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD; |
| 7187 | |
| 7188 | APInt CNV = Offset0; |
| 7189 | if (X0 < 0) CNV = -CNV; |
| 7190 | if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1; |
| 7191 | else CNV = CNV - Offset1; |
| 7192 | |
| 7193 | // We can now generate the new expression. |
| 7194 | SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0)); |
| 7195 | SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0); |
| 7196 | |
| 7197 | SDValue NewUse = DAG.getNode(Opcode, |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7198 | OtherUses[i]->getDebugLoc(), |
| 7199 | OtherUses[i]->getValueType(0), NewOp1, NewOp2); |
| 7200 | DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse); |
| 7201 | removeFromWorkList(OtherUses[i]); |
| 7202 | DAG.DeleteNode(OtherUses[i]); |
| 7203 | } |
| 7204 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7205 | // Replace the uses of Ptr with uses of the updated base value. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7206 | DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7207 | removeFromWorkList(Ptr.getNode()); |
| 7208 | DAG.DeleteNode(Ptr.getNode()); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7209 | |
| 7210 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7211 | } |
| 7212 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 7213 | /// CombineToPostIndexedLoadStore - Try to combine a load / store with a |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7214 | /// add / sub of the base pointer node into a post-indexed load / store. |
| 7215 | /// The transformation folded the add / subtract into the new indexed |
| 7216 | /// load / store effectively and all of its uses are redirected to the |
| 7217 | /// new load / store. |
| 7218 | bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 7219 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7220 | return false; |
| 7221 | |
| 7222 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7223 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7224 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7225 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7226 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7227 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7228 | VT = LD->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7229 | if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && |
| 7230 | !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) |
| 7231 | return false; |
| 7232 | Ptr = LD->getBasePtr(); |
| 7233 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7234 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7235 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7236 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7237 | if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && |
| 7238 | !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) |
| 7239 | return false; |
| 7240 | Ptr = ST->getBasePtr(); |
| 7241 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7242 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7243 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7244 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7245 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7246 | if (Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7247 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7248 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7249 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 7250 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7251 | SDNode *Op = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7252 | if (Op == N || |
| 7253 | (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) |
| 7254 | continue; |
| 7255 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7256 | SDValue BasePtr; |
| 7257 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7258 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 7259 | if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7260 | // Don't create a indexed load / store with zero offset. |
| 7261 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7262 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7263 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7264 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7265 | // Try turning it into a post-indexed load / store except when |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7266 | // 1) All uses are load / store ops that use it as base ptr (and |
| 7267 | // it may be folded as addressing mmode). |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7268 | // 2) Op must be independent of N, i.e. Op is neither a predecessor |
| 7269 | // nor a successor of N. Otherwise, if Op is folded that would |
| 7270 | // create a cycle. |
| 7271 | |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7272 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
| 7273 | continue; |
| 7274 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7275 | // Check for #1. |
| 7276 | bool TryNext = false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7277 | for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(), |
| 7278 | EE = BasePtr.getNode()->use_end(); II != EE; ++II) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7279 | SDNode *Use = *II; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7280 | if (Use == Ptr.getNode()) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7281 | continue; |
| 7282 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7283 | // If all the uses are load / store addresses, then don't do the |
| 7284 | // transformation. |
| 7285 | if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ |
| 7286 | bool RealUse = false; |
| 7287 | for (SDNode::use_iterator III = Use->use_begin(), |
| 7288 | EEE = Use->use_end(); III != EEE; ++III) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7289 | SDNode *UseUse = *III; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7290 | if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7291 | RealUse = true; |
| 7292 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7293 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7294 | if (!RealUse) { |
| 7295 | TryNext = true; |
| 7296 | break; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7297 | } |
| 7298 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7299 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7300 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7301 | if (TryNext) |
| 7302 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7303 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7304 | // Check for #2 |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 7305 | if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7306 | SDValue Result = isLoad |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7307 | ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 7308 | BasePtr, Offset, AM) |
| 7309 | : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 7310 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7311 | ++PostIndexedNodes; |
| 7312 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7313 | DEBUG(dbgs() << "\nReplacing.5 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7314 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7315 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7316 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7317 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7318 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7319 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7320 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 7321 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7322 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7323 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7324 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7325 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7326 | // Finally, since the node is now dead, remove it from the graph. |
| 7327 | DAG.DeleteNode(N); |
| 7328 | |
| 7329 | // Replace the uses of Use with uses of the updated base value. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7330 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7331 | Result.getValue(isLoad ? 1 : 0)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7332 | removeFromWorkList(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7333 | DAG.DeleteNode(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7334 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7335 | } |
| 7336 | } |
| 7337 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7338 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7339 | return false; |
| 7340 | } |
| 7341 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7342 | SDValue DAGCombiner::visitLOAD(SDNode *N) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7343 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7344 | SDValue Chain = LD->getChain(); |
| 7345 | SDValue Ptr = LD->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7346 | |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7347 | // If load is not volatile and there are no uses of the loaded value (and |
| 7348 | // the updated indexed value in case of indexed loads), change uses of the |
| 7349 | // chain value into uses of the chain input (i.e. delete the dead load). |
| 7350 | if (!LD->isVolatile()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7351 | if (N->getValueType(1) == MVT::Other) { |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7352 | // Unindexed loads. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7353 | if (!N->hasAnyUseOfValue(0)) { |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7354 | // It's not safe to use the two value CombineTo variant here. e.g. |
| 7355 | // v1, chain2 = load chain1, loc |
| 7356 | // v2, chain3 = load chain2, loc |
| 7357 | // v3 = add v2, c |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7358 | // Now we replace use of chain2 with chain1. This makes the second load |
| 7359 | // isomorphic to the one we are deleting, and thus makes this load live. |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7360 | DEBUG(dbgs() << "\nReplacing.6 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7361 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7362 | dbgs() << "\nWith chain: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7363 | Chain.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7364 | dbgs() << "\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7365 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7366 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7367 | |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7368 | if (N->use_empty()) { |
| 7369 | removeFromWorkList(N); |
| 7370 | DAG.DeleteNode(N); |
| 7371 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7372 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7373 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7374 | } |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7375 | } else { |
| 7376 | // Indexed loads. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7377 | assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7378 | if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 7379 | SDValue Undef = DAG.getUNDEF(N->getValueType(0)); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7380 | DEBUG(dbgs() << "\nReplacing.7 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7381 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7382 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7383 | Undef.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7384 | dbgs() << " and 2 other values\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7385 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7386 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7387 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7388 | DAG.getUNDEF(N->getValueType(1))); |
| 7389 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7390 | removeFromWorkList(N); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7391 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7392 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7393 | } |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7394 | } |
| 7395 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7396 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7397 | // If this load is directly stored, replace the load value with the stored |
| 7398 | // value. |
| 7399 | // TODO: Handle store large -> read small portion. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7400 | // TODO: Handle TRUNCSTORE/LOADEXT |
Evan Cheng | 9ef82ce | 2011-03-11 00:48:56 +0000 | [diff] [blame] | 7401 | if (ISD::isNormalLoad(N) && !LD->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7402 | if (ISD::isNON_TRUNCStore(Chain.getNode())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7403 | StoreSDNode *PrevST = cast<StoreSDNode>(Chain); |
| 7404 | if (PrevST->getBasePtr() == Ptr && |
| 7405 | PrevST->getValue().getValueType() == N->getValueType(0)) |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7406 | return CombineTo(N, Chain.getOperand(1), Chain); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7407 | } |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7408 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7409 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7410 | // Try to infer better alignment information than the load already has. |
| 7411 | if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7412 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
Owen Anderson | b48783b | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7413 | if (Align > LD->getMemOperand()->getBaseAlignment()) { |
| 7414 | SDValue NewLoad = |
| 7415 | DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(), |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7416 | LD->getValueType(0), |
| 7417 | Chain, Ptr, LD->getPointerInfo(), |
| 7418 | LD->getMemoryVT(), |
| 7419 | LD->isVolatile(), LD->isNonTemporal(), Align); |
Owen Anderson | b48783b | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7420 | return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true); |
| 7421 | } |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7422 | } |
| 7423 | } |
| 7424 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7425 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7426 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7427 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7428 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7429 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7430 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7431 | SDValue ReplLoad; |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7432 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7433 | // Replace the chain to void dependency. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7434 | if (LD->getExtensionType() == ISD::NON_EXTLOAD) { |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7435 | ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7436 | BetterChain, Ptr, LD->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7437 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7438 | LD->isInvariant(), LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7439 | } else { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 7440 | ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(), |
| 7441 | LD->getValueType(0), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7442 | BetterChain, Ptr, LD->getPointerInfo(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7443 | LD->getMemoryVT(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7444 | LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7445 | LD->isNonTemporal(), |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 7446 | LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7447 | } |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7448 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7449 | // Create token factor to keep old chain connected. |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7450 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7451 | MVT::Other, Chain, ReplLoad.getValue(1)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7452 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7453 | // Make sure the new and old chains are cleaned up. |
| 7454 | AddToWorkList(Token.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7455 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 7456 | // Replace uses with load result and token factor. Don't add users |
| 7457 | // to work list. |
| 7458 | return CombineTo(N, ReplLoad.getValue(0), Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7462 | // Try transforming N to an indexed load. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 7463 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7464 | return SDValue(N, 0); |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7465 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7466 | return SDValue(); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7469 | /// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the |
| 7470 | /// load is having specific bytes cleared out. If so, return the byte size |
| 7471 | /// being masked out and the shift amount. |
| 7472 | static std::pair<unsigned, unsigned> |
| 7473 | CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { |
| 7474 | std::pair<unsigned, unsigned> Result(0, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7475 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7476 | // Check for the structure we're looking for. |
| 7477 | if (V->getOpcode() != ISD::AND || |
| 7478 | !isa<ConstantSDNode>(V->getOperand(1)) || |
| 7479 | !ISD::isNormalLoad(V->getOperand(0).getNode())) |
| 7480 | return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7481 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 7482 | // Check the chain and pointer. |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7483 | LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 7484 | if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7485 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 7486 | // The store should be chained directly to the load or be an operand of a |
| 7487 | // tokenfactor. |
| 7488 | if (LD == Chain.getNode()) |
| 7489 | ; // ok. |
| 7490 | else if (Chain->getOpcode() != ISD::TokenFactor) |
| 7491 | return Result; // Fail. |
| 7492 | else { |
| 7493 | bool isOk = false; |
| 7494 | for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i) |
| 7495 | if (Chain->getOperand(i).getNode() == LD) { |
| 7496 | isOk = true; |
| 7497 | break; |
| 7498 | } |
| 7499 | if (!isOk) return Result; |
| 7500 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7501 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7502 | // This only handles simple types. |
| 7503 | if (V.getValueType() != MVT::i16 && |
| 7504 | V.getValueType() != MVT::i32 && |
| 7505 | V.getValueType() != MVT::i64) |
| 7506 | return Result; |
| 7507 | |
| 7508 | // Check the constant mask. Invert it so that the bits being masked out are |
| 7509 | // 0 and the bits being kept are 1. Use getSExtValue so that leading bits |
| 7510 | // follow the sign bit for uniformity. |
| 7511 | uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); |
| 7512 | unsigned NotMaskLZ = CountLeadingZeros_64(NotMask); |
| 7513 | if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. |
| 7514 | unsigned NotMaskTZ = CountTrailingZeros_64(NotMask); |
| 7515 | if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. |
| 7516 | if (NotMaskLZ == 64) return Result; // All zero mask. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7517 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7518 | // See if we have a continuous run of bits. If so, we have 0*1+0* |
| 7519 | if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64) |
| 7520 | return Result; |
| 7521 | |
| 7522 | // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. |
| 7523 | if (V.getValueType() != MVT::i64 && NotMaskLZ) |
| 7524 | NotMaskLZ -= 64-V.getValueSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7525 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7526 | unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; |
| 7527 | switch (MaskedBytes) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7528 | case 1: |
| 7529 | case 2: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7530 | case 4: break; |
| 7531 | default: return Result; // All one mask, or 5-byte mask. |
| 7532 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7533 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7534 | // Verify that the first bit starts at a multiple of mask so that the access |
| 7535 | // is aligned the same as the access width. |
| 7536 | if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7537 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7538 | Result.first = MaskedBytes; |
| 7539 | Result.second = NotMaskTZ/8; |
| 7540 | return Result; |
| 7541 | } |
| 7542 | |
| 7543 | |
| 7544 | /// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that |
| 7545 | /// provides a value as specified by MaskInfo. If so, replace the specified |
| 7546 | /// store with a narrower store of truncated IVal. |
| 7547 | static SDNode * |
| 7548 | ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, |
| 7549 | SDValue IVal, StoreSDNode *St, |
| 7550 | DAGCombiner *DC) { |
| 7551 | unsigned NumBytes = MaskInfo.first; |
| 7552 | unsigned ByteShift = MaskInfo.second; |
| 7553 | SelectionDAG &DAG = DC->getDAG(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7554 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7555 | // Check to see if IVal is all zeros in the part being masked in by the 'or' |
| 7556 | // that uses this. If not, this is not a replacement. |
| 7557 | APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), |
| 7558 | ByteShift*8, (ByteShift+NumBytes)*8); |
| 7559 | if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7560 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7561 | // Check that it is legal on the target to do this. It is legal if the new |
| 7562 | // VT we're shrinking to (i8/i16/i32) is legal or we're still before type |
| 7563 | // legalization. |
| 7564 | MVT VT = MVT::getIntegerVT(NumBytes*8); |
| 7565 | if (!DC->isTypeLegal(VT)) |
| 7566 | return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7567 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7568 | // Okay, we can do this! Replace the 'St' store with a store of IVal that is |
| 7569 | // shifted by ByteShift and truncated down to NumBytes. |
| 7570 | if (ByteShift) |
| 7571 | IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 7572 | DAG.getConstant(ByteShift*8, |
| 7573 | DC->getShiftAmountTy(IVal.getValueType()))); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7574 | |
| 7575 | // Figure out the offset for the store and the alignment of the access. |
| 7576 | unsigned StOffset; |
| 7577 | unsigned NewAlign = St->getAlignment(); |
| 7578 | |
| 7579 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 7580 | StOffset = ByteShift; |
| 7581 | else |
| 7582 | StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7583 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7584 | SDValue Ptr = St->getBasePtr(); |
| 7585 | if (StOffset) { |
| 7586 | Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(), |
| 7587 | Ptr, DAG.getConstant(StOffset, Ptr.getValueType())); |
| 7588 | NewAlign = MinAlign(NewAlign, StOffset); |
| 7589 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7590 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7591 | // Truncate down to the new size. |
| 7592 | IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7593 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7594 | ++OpsNarrowed; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7595 | return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7596 | St->getPointerInfo().getWithOffset(StOffset), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7597 | false, false, NewAlign).getNode(); |
| 7598 | } |
| 7599 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7600 | |
| 7601 | /// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is |
| 7602 | /// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some |
| 7603 | /// of the loaded bits, try narrowing the load and store if it would end up |
| 7604 | /// being a win for performance or code size. |
| 7605 | SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { |
| 7606 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7607 | if (ST->isVolatile()) |
| 7608 | return SDValue(); |
| 7609 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7610 | SDValue Chain = ST->getChain(); |
| 7611 | SDValue Value = ST->getValue(); |
| 7612 | SDValue Ptr = ST->getBasePtr(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7613 | EVT VT = Value.getValueType(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7614 | |
| 7615 | if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7616 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7617 | |
| 7618 | unsigned Opc = Value.getOpcode(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7619 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7620 | // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst |
| 7621 | // is a byte mask indicating a consecutive number of bytes, check to see if |
| 7622 | // Y is known to provide just those bytes. If so, we try to replace the |
| 7623 | // load + replace + store sequence with a single (narrower) store, which makes |
| 7624 | // the load dead. |
| 7625 | if (Opc == ISD::OR) { |
| 7626 | std::pair<unsigned, unsigned> MaskedLoad; |
| 7627 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); |
| 7628 | if (MaskedLoad.first) |
| 7629 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 7630 | Value.getOperand(1), ST,this)) |
| 7631 | return SDValue(NewST, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7632 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7633 | // Or is commutative, so try swapping X and Y. |
| 7634 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); |
| 7635 | if (MaskedLoad.first) |
| 7636 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 7637 | Value.getOperand(0), ST,this)) |
| 7638 | return SDValue(NewST, 0); |
| 7639 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7640 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7641 | if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || |
| 7642 | Value.getOperand(1).getOpcode() != ISD::Constant) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7643 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7644 | |
| 7645 | SDValue N0 = Value.getOperand(0); |
Dan Gohman | 24bde5b | 2010-09-02 21:18:42 +0000 | [diff] [blame] | 7646 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| 7647 | Chain == SDValue(N0.getNode(), 1)) { |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7648 | LoadSDNode *LD = cast<LoadSDNode>(N0); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7649 | if (LD->getBasePtr() != Ptr || |
| 7650 | LD->getPointerInfo().getAddrSpace() != |
| 7651 | ST->getPointerInfo().getAddrSpace()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7652 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7653 | |
| 7654 | // Find the type to narrow it the load / op / store to. |
| 7655 | SDValue N1 = Value.getOperand(1); |
| 7656 | unsigned BitWidth = N1.getValueSizeInBits(); |
| 7657 | APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); |
| 7658 | if (Opc == ISD::AND) |
| 7659 | Imm ^= APInt::getAllOnesValue(BitWidth); |
Evan Cheng | d3c76bb | 2009-05-28 23:52:18 +0000 | [diff] [blame] | 7660 | if (Imm == 0 || Imm.isAllOnesValue()) |
| 7661 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7662 | unsigned ShAmt = Imm.countTrailingZeros(); |
| 7663 | unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; |
| 7664 | unsigned NewBW = NextPowerOf2(MSB - ShAmt); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 7665 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7666 | while (NewBW < BitWidth && |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7667 | !(TLI.isOperationLegalOrCustom(Opc, NewVT) && |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7668 | TLI.isNarrowingProfitable(VT, NewVT))) { |
| 7669 | NewBW = NextPowerOf2(NewBW); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 7670 | NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7671 | } |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7672 | if (NewBW >= BitWidth) |
| 7673 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7674 | |
| 7675 | // If the lsb changed does not start at the type bitwidth boundary, |
| 7676 | // start at the previous one. |
| 7677 | if (ShAmt % NewBW) |
| 7678 | ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; |
Manman Ren | 981b963 | 2012-12-12 01:13:50 +0000 | [diff] [blame] | 7679 | APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, |
| 7680 | std::min(BitWidth, ShAmt + NewBW)); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7681 | if ((Imm & Mask) == Imm) { |
| 7682 | APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); |
| 7683 | if (Opc == ISD::AND) |
| 7684 | NewImm ^= APInt::getAllOnesValue(NewBW); |
| 7685 | uint64_t PtrOff = ShAmt / 8; |
| 7686 | // For big endian targets, we need to adjust the offset to the pointer to |
| 7687 | // load the correct bytes. |
| 7688 | if (TLI.isBigEndian()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7689 | PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7690 | |
| 7691 | unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7692 | Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 7693 | if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy)) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7694 | return SDValue(); |
| 7695 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7696 | SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(), |
| 7697 | Ptr.getValueType(), Ptr, |
| 7698 | DAG.getConstant(PtrOff, Ptr.getValueType())); |
| 7699 | SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(), |
| 7700 | LD->getChain(), NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7701 | LD->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7702 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7703 | LD->isInvariant(), NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7704 | SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD, |
| 7705 | DAG.getConstant(NewImm, NewVT)); |
| 7706 | SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(), |
| 7707 | NewVal, NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7708 | ST->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7709 | false, false, NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7710 | |
| 7711 | AddToWorkList(NewPtr.getNode()); |
| 7712 | AddToWorkList(NewLD.getNode()); |
| 7713 | AddToWorkList(NewVal.getNode()); |
| 7714 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7715 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7716 | ++OpsNarrowed; |
| 7717 | return NewST; |
| 7718 | } |
| 7719 | } |
| 7720 | |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7721 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7722 | } |
| 7723 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7724 | /// TransformFPLoadStorePair - For a given floating point load / store pair, |
| 7725 | /// if the load value isn't used by any other operations, then consider |
| 7726 | /// transforming the pair to integer load / store operations if the target |
| 7727 | /// deems the transformation profitable. |
| 7728 | SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { |
| 7729 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 7730 | SDValue Chain = ST->getChain(); |
| 7731 | SDValue Value = ST->getValue(); |
| 7732 | if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && |
| 7733 | Value.hasOneUse() && |
| 7734 | Chain == SDValue(Value.getNode(), 1)) { |
| 7735 | LoadSDNode *LD = cast<LoadSDNode>(Value); |
| 7736 | EVT VT = LD->getMemoryVT(); |
| 7737 | if (!VT.isFloatingPoint() || |
| 7738 | VT != ST->getMemoryVT() || |
| 7739 | LD->isNonTemporal() || |
| 7740 | ST->isNonTemporal() || |
| 7741 | LD->getPointerInfo().getAddrSpace() != 0 || |
| 7742 | ST->getPointerInfo().getAddrSpace() != 0) |
| 7743 | return SDValue(); |
| 7744 | |
| 7745 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); |
| 7746 | if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || |
| 7747 | !TLI.isOperationLegal(ISD::STORE, IntVT) || |
| 7748 | !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || |
| 7749 | !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) |
| 7750 | return SDValue(); |
| 7751 | |
| 7752 | unsigned LDAlign = LD->getAlignment(); |
| 7753 | unsigned STAlign = ST->getAlignment(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7754 | Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 7755 | unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7756 | if (LDAlign < ABIAlign || STAlign < ABIAlign) |
| 7757 | return SDValue(); |
| 7758 | |
| 7759 | SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(), |
| 7760 | LD->getChain(), LD->getBasePtr(), |
| 7761 | LD->getPointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7762 | false, false, false, LDAlign); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7763 | |
| 7764 | SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(), |
| 7765 | NewLD, ST->getBasePtr(), |
| 7766 | ST->getPointerInfo(), |
| 7767 | false, false, STAlign); |
| 7768 | |
| 7769 | AddToWorkList(NewLD.getNode()); |
| 7770 | AddToWorkList(NewST.getNode()); |
| 7771 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7772 | DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7773 | ++LdStFP2Int; |
| 7774 | return NewST; |
| 7775 | } |
| 7776 | |
| 7777 | return SDValue(); |
| 7778 | } |
| 7779 | |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7780 | /// Helper struct to parse and store a memory address as base + index + offset. |
| 7781 | /// We ignore sign extensions when it is safe to do so. |
| 7782 | /// The following two expressions are not equivalent. To differentiate we need |
| 7783 | /// to store whether there was a sign extension involved in the index |
| 7784 | /// computation. |
| 7785 | /// (load (i64 add (i64 copyfromreg %c) |
| 7786 | /// (i64 signextend (add (i8 load %index) |
| 7787 | /// (i8 1)))) |
| 7788 | /// vs |
| 7789 | /// |
| 7790 | /// (load (i64 add (i64 copyfromreg %c) |
| 7791 | /// (i64 signextend (i32 add (i32 signextend (i8 load %index)) |
| 7792 | /// (i32 1))))) |
| 7793 | struct BaseIndexOffset { |
| 7794 | SDValue Base; |
| 7795 | SDValue Index; |
| 7796 | int64_t Offset; |
| 7797 | bool IsIndexSignExt; |
| 7798 | |
| 7799 | BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {} |
| 7800 | |
| 7801 | BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset, |
| 7802 | bool IsIndexSignExt) : |
| 7803 | Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {} |
| 7804 | |
| 7805 | bool equalBaseIndex(const BaseIndexOffset &Other) { |
| 7806 | return Other.Base == Base && Other.Index == Index && |
| 7807 | Other.IsIndexSignExt == IsIndexSignExt; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7810 | /// Parses tree in Ptr for base, index, offset addresses. |
| 7811 | static BaseIndexOffset match(SDValue Ptr) { |
| 7812 | bool IsIndexSignExt = false; |
| 7813 | |
| 7814 | // Just Base or possibly anything else. |
| 7815 | if (Ptr->getOpcode() != ISD::ADD) |
| 7816 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
| 7817 | |
| 7818 | // Base + offset. |
| 7819 | if (isa<ConstantSDNode>(Ptr->getOperand(1))) { |
| 7820 | int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue(); |
| 7821 | return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset, |
| 7822 | IsIndexSignExt); |
| 7823 | } |
| 7824 | |
| 7825 | // Look at Base + Index + Offset cases. |
| 7826 | SDValue Base = Ptr->getOperand(0); |
| 7827 | SDValue IndexOffset = Ptr->getOperand(1); |
| 7828 | |
| 7829 | // Skip signextends. |
| 7830 | if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) { |
| 7831 | IndexOffset = IndexOffset->getOperand(0); |
| 7832 | IsIndexSignExt = true; |
| 7833 | } |
| 7834 | |
| 7835 | // Either the case of Base + Index (no offset) or something else. |
| 7836 | if (IndexOffset->getOpcode() != ISD::ADD) |
| 7837 | return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt); |
| 7838 | |
| 7839 | // Now we have the case of Base + Index + offset. |
| 7840 | SDValue Index = IndexOffset->getOperand(0); |
| 7841 | SDValue Offset = IndexOffset->getOperand(1); |
| 7842 | |
| 7843 | if (!isa<ConstantSDNode>(Offset)) |
| 7844 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
| 7845 | |
| 7846 | // Ignore signextends. |
| 7847 | if (Index->getOpcode() == ISD::SIGN_EXTEND) { |
| 7848 | Index = Index->getOperand(0); |
| 7849 | IsIndexSignExt = true; |
| 7850 | } else IsIndexSignExt = false; |
| 7851 | |
| 7852 | int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue(); |
| 7853 | return BaseIndexOffset(Base, Index, Off, IsIndexSignExt); |
| 7854 | } |
| 7855 | }; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7856 | |
| 7857 | /// Holds a pointer to an LSBaseSDNode as well as information on where it |
| 7858 | /// is located in a sequence of memory operations connected by a chain. |
| 7859 | struct MemOpLink { |
| 7860 | MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq): |
| 7861 | MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { } |
| 7862 | // Ptr to the mem node. |
| 7863 | LSBaseSDNode *MemNode; |
| 7864 | // Offset from the base ptr. |
| 7865 | int64_t OffsetFromBase; |
| 7866 | // What is the sequence number of this mem node. |
| 7867 | // Lowest mem operand in the DAG starts at zero. |
| 7868 | unsigned SequenceNum; |
| 7869 | }; |
| 7870 | |
| 7871 | /// Sorts store nodes in a link according to their offset from a shared |
| 7872 | // base ptr. |
| 7873 | struct ConsecutiveMemoryChainSorter { |
| 7874 | bool operator()(MemOpLink LHS, MemOpLink RHS) { |
| 7875 | return LHS.OffsetFromBase < RHS.OffsetFromBase; |
| 7876 | } |
| 7877 | }; |
| 7878 | |
| 7879 | bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) { |
| 7880 | EVT MemVT = St->getMemoryVT(); |
| 7881 | int64_t ElementSizeBytes = MemVT.getSizeInBits()/8; |
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 7882 | bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes(). |
| 7883 | hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7884 | |
| 7885 | // Don't merge vectors into wider inputs. |
| 7886 | if (MemVT.isVector() || !MemVT.isSimple()) |
| 7887 | return false; |
| 7888 | |
| 7889 | // Perform an early exit check. Do not bother looking at stored values that |
| 7890 | // are not constants or loads. |
| 7891 | SDValue StoredVal = St->getValue(); |
| 7892 | bool IsLoadSrc = isa<LoadSDNode>(StoredVal); |
| 7893 | if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) && |
| 7894 | !IsLoadSrc) |
| 7895 | return false; |
| 7896 | |
| 7897 | // Only look at ends of store sequences. |
| 7898 | SDValue Chain = SDValue(St, 1); |
| 7899 | if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE) |
| 7900 | return false; |
| 7901 | |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7902 | // This holds the base pointer, index, and the offset in bytes from the base |
| 7903 | // pointer. |
| 7904 | BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7905 | |
| 7906 | // We must have a base and an offset. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7907 | if (!BasePtr.Base.getNode()) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7908 | return false; |
| 7909 | |
| 7910 | // Do not handle stores to undef base pointers. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7911 | if (BasePtr.Base.getOpcode() == ISD::UNDEF) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7912 | return false; |
| 7913 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 7914 | // Save the LoadSDNodes that we find in the chain. |
| 7915 | // We need to make sure that these nodes do not interfere with |
| 7916 | // any of the store nodes. |
| 7917 | SmallVector<LSBaseSDNode*, 8> AliasLoadNodes; |
| 7918 | |
| 7919 | // Save the StoreSDNodes that we find in the chain. |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7920 | SmallVector<MemOpLink, 8> StoreNodes; |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 7921 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7922 | // Walk up the chain and look for nodes with offsets from the same |
| 7923 | // base pointer. Stop when reaching an instruction with a different kind |
| 7924 | // or instruction which has a different base pointer. |
| 7925 | unsigned Seq = 0; |
| 7926 | StoreSDNode *Index = St; |
| 7927 | while (Index) { |
| 7928 | // If the chain has more than one use, then we can't reorder the mem ops. |
| 7929 | if (Index != St && !SDValue(Index, 1)->hasOneUse()) |
| 7930 | break; |
| 7931 | |
| 7932 | // Find the base pointer and offset for this memory node. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7933 | BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7934 | |
| 7935 | // Check that the base pointer is the same as the original one. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7936 | if (!Ptr.equalBaseIndex(BasePtr)) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7937 | break; |
| 7938 | |
| 7939 | // Check that the alignment is the same. |
| 7940 | if (Index->getAlignment() != St->getAlignment()) |
| 7941 | break; |
| 7942 | |
| 7943 | // The memory operands must not be volatile. |
| 7944 | if (Index->isVolatile() || Index->isIndexed()) |
| 7945 | break; |
| 7946 | |
| 7947 | // No truncation. |
| 7948 | if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index)) |
| 7949 | if (St->isTruncatingStore()) |
| 7950 | break; |
| 7951 | |
| 7952 | // The stored memory type must be the same. |
| 7953 | if (Index->getMemoryVT() != MemVT) |
| 7954 | break; |
| 7955 | |
| 7956 | // We do not allow unaligned stores because we want to prevent overriding |
| 7957 | // stores. |
| 7958 | if (Index->getAlignment()*8 != MemVT.getSizeInBits()) |
| 7959 | break; |
| 7960 | |
| 7961 | // We found a potential memory operand to merge. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 7962 | StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++)); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7963 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 7964 | // Find the next memory operand in the chain. If the next operand in the |
| 7965 | // chain is a store then move up and continue the scan with the next |
| 7966 | // memory operand. If the next operand is a load save it and use alias |
| 7967 | // information to check if it interferes with anything. |
| 7968 | SDNode *NextInChain = Index->getChain().getNode(); |
| 7969 | while (1) { |
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 7970 | if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) { |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 7971 | // We found a store node. Use it for the next iteration. |
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 7972 | Index = STn; |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 7973 | break; |
| 7974 | } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) { |
| 7975 | // Save the load node for later. Continue the scan. |
| 7976 | AliasLoadNodes.push_back(Ldn); |
| 7977 | NextInChain = Ldn->getChain().getNode(); |
| 7978 | continue; |
| 7979 | } else { |
| 7980 | Index = NULL; |
| 7981 | break; |
| 7982 | } |
| 7983 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | // Check if there is anything to merge. |
| 7987 | if (StoreNodes.size() < 2) |
| 7988 | return false; |
| 7989 | |
| 7990 | // Sort the memory operands according to their distance from the base pointer. |
| 7991 | std::sort(StoreNodes.begin(), StoreNodes.end(), |
| 7992 | ConsecutiveMemoryChainSorter()); |
| 7993 | |
| 7994 | // Scan the memory operations on the chain and find the first non-consecutive |
| 7995 | // store memory address. |
| 7996 | unsigned LastConsecutiveStore = 0; |
| 7997 | int64_t StartAddress = StoreNodes[0].OffsetFromBase; |
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 7998 | for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) { |
| 7999 | |
| 8000 | // Check that the addresses are consecutive starting from the second |
| 8001 | // element in the list of stores. |
| 8002 | if (i > 0) { |
| 8003 | int64_t CurrAddress = StoreNodes[i].OffsetFromBase; |
| 8004 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) |
| 8005 | break; |
| 8006 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8007 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8008 | bool Alias = false; |
| 8009 | // Check if this store interferes with any of the loads that we found. |
| 8010 | for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld) |
| 8011 | if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) { |
| 8012 | Alias = true; |
| 8013 | break; |
| 8014 | } |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8015 | // We found a load that alias with this store. Stop the sequence. |
| 8016 | if (Alias) |
| 8017 | break; |
| 8018 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8019 | // Mark this node as useful. |
| 8020 | LastConsecutiveStore = i; |
| 8021 | } |
| 8022 | |
| 8023 | // The node with the lowest store address. |
| 8024 | LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; |
| 8025 | |
| 8026 | // Store the constants into memory as one consecutive store. |
| 8027 | if (!IsLoadSrc) { |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8028 | unsigned LastLegalType = 0; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8029 | unsigned LastLegalVectorType = 0; |
| 8030 | bool NonZero = false; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8031 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
| 8032 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 8033 | SDValue StoredVal = St->getValue(); |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8034 | |
| 8035 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) { |
Benjamin Kramer | ebd7eab | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 8036 | NonZero |= !C->isNullValue(); |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8037 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) { |
Benjamin Kramer | ebd7eab | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 8038 | NonZero |= !C->getConstantFPValue()->isNullValue(); |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8039 | } else { |
| 8040 | // Non constant. |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8041 | break; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8042 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8043 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8044 | // Find a legal type for the constant store. |
| 8045 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; |
| 8046 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 8047 | if (TLI.isTypeLegal(StoreTy)) |
| 8048 | LastLegalType = i+1; |
Arnold Schwaighofer | e737018 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 8049 | // Or check whether a truncstore is legal. |
| 8050 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == |
| 8051 | TargetLowering::TypePromoteInteger) { |
| 8052 | EVT LegalizedStoredValueTy = |
| 8053 | TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType()); |
| 8054 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy)) |
| 8055 | LastLegalType = i+1; |
| 8056 | } |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8057 | |
| 8058 | // Find a legal type for the vector store. |
| 8059 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); |
| 8060 | if (TLI.isTypeLegal(Ty)) |
| 8061 | LastLegalVectorType = i + 1; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8062 | } |
| 8063 | |
Bob Wilson | 99d8e76 | 2012-12-20 01:36:20 +0000 | [diff] [blame] | 8064 | // We only use vectors if the constant is known to be zero and the |
| 8065 | // function is not marked with the noimplicitfloat attribute. |
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8066 | if (NonZero || NoVectors) |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8067 | LastLegalVectorType = 0; |
| 8068 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8069 | // Check if we found a legal integer type to store. |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8070 | if (LastLegalType == 0 && LastLegalVectorType == 0) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8071 | return false; |
| 8072 | |
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8073 | bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8074 | unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType; |
| 8075 | |
| 8076 | // Make sure we have something to merge. |
| 8077 | if (NumElem < 2) |
| 8078 | return false; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8079 | |
| 8080 | unsigned EarliestNodeUsed = 0; |
| 8081 | for (unsigned i=0; i < NumElem; ++i) { |
| 8082 | // Find a chain for the new wide-store operand. Notice that some |
| 8083 | // of the store nodes that we found may not be selected for inclusion |
| 8084 | // in the wide store. The chain we use needs to be the chain of the |
| 8085 | // earliest store node which is *used* and replaced by the wide store. |
| 8086 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) |
| 8087 | EarliestNodeUsed = i; |
| 8088 | } |
| 8089 | |
| 8090 | // The earliest Node in the DAG. |
| 8091 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8092 | DebugLoc DL = StoreNodes[0].MemNode->getDebugLoc(); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8093 | |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8094 | SDValue StoredVal; |
| 8095 | if (UseVector) { |
| 8096 | // Find a legal type for the vector store. |
| 8097 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); |
| 8098 | assert(TLI.isTypeLegal(Ty) && "Illegal vector store"); |
| 8099 | StoredVal = DAG.getConstant(0, Ty); |
| 8100 | } else { |
| 8101 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; |
| 8102 | APInt StoreInt(StoreBW, 0); |
| 8103 | |
| 8104 | // Construct a single integer constant which is made of the smaller |
| 8105 | // constant inputs. |
| 8106 | bool IsLE = TLI.isLittleEndian(); |
| 8107 | for (unsigned i = 0; i < NumElem ; ++i) { |
| 8108 | unsigned Idx = IsLE ?(NumElem - 1 - i) : i; |
| 8109 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode); |
| 8110 | SDValue Val = St->getValue(); |
| 8111 | StoreInt<<=ElementSizeBytes*8; |
| 8112 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) { |
| 8113 | StoreInt|=C->getAPIntValue().zext(StoreBW); |
| 8114 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) { |
| 8115 | StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW); |
| 8116 | } else { |
| 8117 | assert(false && "Invalid constant element type"); |
| 8118 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8119 | } |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8120 | |
| 8121 | // Create the new Load and Store operations. |
| 8122 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 8123 | StoredVal = DAG.getConstant(StoreInt, StoreTy); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8124 | } |
| 8125 | |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8126 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal, |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8127 | FirstInChain->getBasePtr(), |
| 8128 | FirstInChain->getPointerInfo(), |
| 8129 | false, false, |
| 8130 | FirstInChain->getAlignment()); |
| 8131 | |
| 8132 | // Replace the first store with the new store |
| 8133 | CombineTo(EarliestOp, NewStore); |
| 8134 | // Erase all other stores. |
| 8135 | for (unsigned i = 0; i < NumElem ; ++i) { |
| 8136 | if (StoreNodes[i].MemNode == EarliestOp) |
| 8137 | continue; |
| 8138 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
Rafael Espindola | 8e2b8ae | 2012-11-14 05:08:56 +0000 | [diff] [blame] | 8139 | // ReplaceAllUsesWith will replace all uses that existed when it was |
| 8140 | // called, but graph optimizations may cause new ones to appear. For |
| 8141 | // example, the case in pr14333 looks like |
| 8142 | // |
| 8143 | // St's chain -> St -> another store -> X |
| 8144 | // |
| 8145 | // And the only difference from St to the other store is the chain. |
| 8146 | // When we change it's chain to be St's chain they become identical, |
| 8147 | // get CSEed and the net result is that X is now a use of St. |
| 8148 | // Since we know that St is redundant, just iterate. |
| 8149 | while (!St->use_empty()) |
| 8150 | DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8151 | removeFromWorkList(St); |
| 8152 | DAG.DeleteNode(St); |
| 8153 | } |
| 8154 | |
| 8155 | return true; |
| 8156 | } |
| 8157 | |
| 8158 | // Below we handle the case of multiple consecutive stores that |
| 8159 | // come from multiple consecutive loads. We merge them into a single |
| 8160 | // wide load and a single wide store. |
| 8161 | |
| 8162 | // Look for load nodes which are used by the stored values. |
| 8163 | SmallVector<MemOpLink, 8> LoadNodes; |
| 8164 | |
| 8165 | // Find acceptable loads. Loads need to have the same chain (token factor), |
| 8166 | // must not be zext, volatile, indexed, and they must be consecutive. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8167 | BaseIndexOffset LdBasePtr; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8168 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
| 8169 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 8170 | LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue()); |
| 8171 | if (!Ld) break; |
| 8172 | |
| 8173 | // Loads must only have one use. |
| 8174 | if (!Ld->hasNUsesOfValue(1, 0)) |
| 8175 | break; |
| 8176 | |
| 8177 | // Check that the alignment is the same as the stores. |
| 8178 | if (Ld->getAlignment() != St->getAlignment()) |
| 8179 | break; |
| 8180 | |
| 8181 | // The memory operands must not be volatile. |
| 8182 | if (Ld->isVolatile() || Ld->isIndexed()) |
| 8183 | break; |
| 8184 | |
| 8185 | // We do not accept ext loads. |
| 8186 | if (Ld->getExtensionType() != ISD::NON_EXTLOAD) |
| 8187 | break; |
| 8188 | |
| 8189 | // The stored memory type must be the same. |
| 8190 | if (Ld->getMemoryVT() != MemVT) |
| 8191 | break; |
| 8192 | |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8193 | BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8194 | // If this is not the first ptr that we check. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8195 | if (LdBasePtr.Base.getNode()) { |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8196 | // The base ptr must be the same. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8197 | if (!LdPtr.equalBaseIndex(LdBasePtr)) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8198 | break; |
| 8199 | } else { |
| 8200 | // Check that all other base pointers are the same as this one. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8201 | LdBasePtr = LdPtr; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8202 | } |
| 8203 | |
| 8204 | // We found a potential memory operand to merge. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8205 | LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0)); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
| 8208 | if (LoadNodes.size() < 2) |
| 8209 | return false; |
| 8210 | |
| 8211 | // Scan the memory operations on the chain and find the first non-consecutive |
| 8212 | // load memory address. These variables hold the index in the store node |
| 8213 | // array. |
| 8214 | unsigned LastConsecutiveLoad = 0; |
| 8215 | // This variable refers to the size and not index in the array. |
| 8216 | unsigned LastLegalVectorType = 0; |
| 8217 | unsigned LastLegalIntegerType = 0; |
| 8218 | StartAddress = LoadNodes[0].OffsetFromBase; |
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 8219 | SDValue FirstChain = LoadNodes[0].MemNode->getChain(); |
| 8220 | for (unsigned i = 1; i < LoadNodes.size(); ++i) { |
| 8221 | // All loads much share the same chain. |
| 8222 | if (LoadNodes[i].MemNode->getChain() != FirstChain) |
| 8223 | break; |
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8224 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8225 | int64_t CurrAddress = LoadNodes[i].OffsetFromBase; |
| 8226 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) |
| 8227 | break; |
| 8228 | LastConsecutiveLoad = i; |
| 8229 | |
| 8230 | // Find a legal type for the vector store. |
| 8231 | EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); |
| 8232 | if (TLI.isTypeLegal(StoreTy)) |
| 8233 | LastLegalVectorType = i + 1; |
| 8234 | |
| 8235 | // Find a legal type for the integer store. |
| 8236 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; |
| 8237 | StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 8238 | if (TLI.isTypeLegal(StoreTy)) |
| 8239 | LastLegalIntegerType = i + 1; |
Arnold Schwaighofer | e737018 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 8240 | // Or check whether a truncstore and extload is legal. |
| 8241 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == |
| 8242 | TargetLowering::TypePromoteInteger) { |
| 8243 | EVT LegalizedStoredValueTy = |
| 8244 | TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy); |
| 8245 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) && |
| 8246 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) && |
| 8247 | TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) && |
| 8248 | TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy)) |
| 8249 | LastLegalIntegerType = i+1; |
| 8250 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8251 | } |
| 8252 | |
| 8253 | // Only use vector types if the vector type is larger than the integer type. |
| 8254 | // If they are the same, use integers. |
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8255 | bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8256 | unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType); |
| 8257 | |
| 8258 | // We add +1 here because the LastXXX variables refer to location while |
| 8259 | // the NumElem refers to array/index size. |
| 8260 | unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1; |
| 8261 | NumElem = std::min(LastLegalType, NumElem); |
| 8262 | |
| 8263 | if (NumElem < 2) |
| 8264 | return false; |
| 8265 | |
| 8266 | // The earliest Node in the DAG. |
| 8267 | unsigned EarliestNodeUsed = 0; |
| 8268 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; |
| 8269 | for (unsigned i=1; i<NumElem; ++i) { |
| 8270 | // Find a chain for the new wide-store operand. Notice that some |
| 8271 | // of the store nodes that we found may not be selected for inclusion |
| 8272 | // in the wide store. The chain we use needs to be the chain of the |
| 8273 | // earliest store node which is *used* and replaced by the wide store. |
| 8274 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) |
| 8275 | EarliestNodeUsed = i; |
| 8276 | } |
| 8277 | |
| 8278 | // Find if it is better to use vectors or integers to load and store |
| 8279 | // to memory. |
| 8280 | EVT JointMemOpVT; |
| 8281 | if (UseVectorTy) { |
| 8282 | JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); |
| 8283 | } else { |
| 8284 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; |
| 8285 | JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 8286 | } |
| 8287 | |
| 8288 | DebugLoc LoadDL = LoadNodes[0].MemNode->getDebugLoc(); |
| 8289 | DebugLoc StoreDL = StoreNodes[0].MemNode->getDebugLoc(); |
| 8290 | |
| 8291 | LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode); |
| 8292 | SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL, |
| 8293 | FirstLoad->getChain(), |
| 8294 | FirstLoad->getBasePtr(), |
| 8295 | FirstLoad->getPointerInfo(), |
| 8296 | false, false, false, |
| 8297 | FirstLoad->getAlignment()); |
| 8298 | |
| 8299 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad, |
| 8300 | FirstInChain->getBasePtr(), |
| 8301 | FirstInChain->getPointerInfo(), false, false, |
| 8302 | FirstInChain->getAlignment()); |
| 8303 | |
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 8304 | // Replace one of the loads with the new load. |
| 8305 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode); |
| 8306 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), |
| 8307 | SDValue(NewLoad.getNode(), 1)); |
| 8308 | |
| 8309 | // Remove the rest of the load chains. |
| 8310 | for (unsigned i = 1; i < NumElem ; ++i) { |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8311 | // Replace all chain users of the old load nodes with the chain of the new |
| 8312 | // load node. |
| 8313 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode); |
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 8314 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain()); |
| 8315 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8316 | |
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 8317 | // Replace the first store with the new store. |
| 8318 | CombineTo(EarliestOp, NewStore); |
| 8319 | // Erase all other stores. |
| 8320 | for (unsigned i = 0; i < NumElem ; ++i) { |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8321 | // Remove all Store nodes. |
| 8322 | if (StoreNodes[i].MemNode == EarliestOp) |
| 8323 | continue; |
| 8324 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 8325 | DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain()); |
| 8326 | removeFromWorkList(St); |
| 8327 | DAG.DeleteNode(St); |
| 8328 | } |
| 8329 | |
| 8330 | return true; |
| 8331 | } |
| 8332 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8333 | SDValue DAGCombiner::visitSTORE(SDNode *N) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 8334 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8335 | SDValue Chain = ST->getChain(); |
| 8336 | SDValue Value = ST->getValue(); |
| 8337 | SDValue Ptr = ST->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8338 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 8339 | // If this is a store of a bit convert, store the input value if the |
Evan Cheng | 2c4f943 | 2007-05-09 21:49:47 +0000 | [diff] [blame] | 8340 | // resultant store does not need a higher alignment than the original. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8341 | if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8342 | ST->isUnindexed()) { |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 8343 | unsigned OrigAlign = ST->getAlignment(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8344 | EVT SVT = Value.getOperand(0).getValueType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8345 | unsigned Align = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 8346 | getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext())); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8347 | if (Align <= OrigAlign && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8348 | ((!LegalOperations && !ST->isVolatile()) || |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 8349 | TLI.isOperationLegalOrCustom(ISD::STORE, SVT))) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8350 | return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8351 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8352 | ST->isNonTemporal(), OrigAlign); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8353 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 8354 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 8355 | // Turn 'store undef, Ptr' -> nothing. |
| 8356 | if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed()) |
| 8357 | return Chain; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8358 | |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 8359 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 8360 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8361 | // NOTE: If the original store is volatile, this transform must not increase |
| 8362 | // the number of stores. For example, on x86-32 an f64 can be stored in one |
| 8363 | // processor operation but an i64 (which is not legal) requires two. So the |
| 8364 | // transform should not be done in this case. |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 8365 | if (Value.getOpcode() != ISD::TargetConstantFP) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8366 | SDValue Tmp; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8367 | switch (CFP->getValueType(0).getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8368 | default: llvm_unreachable("Unknown FP type"); |
Pete Cooper | 438c040 | 2012-06-21 18:00:39 +0000 | [diff] [blame] | 8369 | case MVT::f16: // We don't do this for these yet. |
| 8370 | case MVT::f80: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8371 | case MVT::f128: |
| 8372 | case MVT::ppcf128: |
Dale Johannesen | c7b21d5 | 2007-09-18 18:36:59 +0000 | [diff] [blame] | 8373 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8374 | case MVT::f32: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8375 | if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8376 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 8377 | Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8378 | bitcastToAPInt().getZExtValue(), MVT::i32); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8379 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8380 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8381 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8382 | } |
| 8383 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8384 | case MVT::f64: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8385 | if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 8386 | !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8387 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 8388 | Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8389 | getZExtValue(), MVT::i64); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8390 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8391 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8392 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 8393 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 8394 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 8395 | if (!ST->isVolatile() && |
| 8396 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 8397 | // Many FP stores are not made apparent until after legalize, e.g. for |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8398 | // argument passing. Since this is so common, custom legalize the |
| 8399 | // 64-bit integer store into two 32-bit stores. |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 8400 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8401 | SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); |
| 8402 | SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 8403 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8404 | |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 8405 | unsigned Alignment = ST->getAlignment(); |
| 8406 | bool isVolatile = ST->isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8407 | bool isNonTemporal = ST->isNonTemporal(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 8408 | |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8409 | SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8410 | Ptr, ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8411 | isVolatile, isNonTemporal, |
| 8412 | ST->getAlignment()); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8413 | Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr, |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8414 | DAG.getConstant(4, Ptr.getValueType())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 8415 | Alignment = MinAlign(Alignment, 4U); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8416 | SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8417 | Ptr, ST->getPointerInfo().getWithOffset(4), |
| 8418 | isVolatile, isNonTemporal, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8419 | Alignment); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8420 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8421 | St0, St1); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8422 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8423 | |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 8424 | break; |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 8425 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 8426 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 8427 | } |
| 8428 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 8429 | // Try to infer better alignment information than the store already has. |
| 8430 | if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 8431 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 8432 | if (Align > ST->getAlignment()) |
| 8433 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value, |
| 8434 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
| 8435 | ST->isVolatile(), ST->isNonTemporal(), Align); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 8436 | } |
| 8437 | } |
| 8438 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8439 | // Try transforming a pair floating point load / store ops to integer |
| 8440 | // load / store ops. |
| 8441 | SDValue NewST = TransformFPLoadStorePair(N); |
| 8442 | if (NewST.getNode()) |
| 8443 | return NewST; |
| 8444 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8445 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8446 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8447 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8448 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8449 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8450 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8451 | SDValue ReplStore; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8452 | |
| 8453 | // Replace the chain to avoid dependency. |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 8454 | if (ST->isTruncatingStore()) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8455 | ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 8456 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8457 | ST->getMemoryVT(), ST->isVolatile(), |
| 8458 | ST->isNonTemporal(), ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 8459 | } else { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8460 | ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8461 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8462 | ST->isVolatile(), ST->isNonTemporal(), |
| 8463 | ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 8464 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8465 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8466 | // Create token to keep both nodes around. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8467 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8468 | MVT::Other, Chain, ReplStore); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8469 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8470 | // Make sure the new and old chains are cleaned up. |
| 8471 | AddToWorkList(Token.getNode()); |
| 8472 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 8473 | // Don't add users to work list. |
| 8474 | return CombineTo(N, Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8475 | } |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 8476 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8477 | |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 8478 | // Try transforming N to an indexed store. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 8479 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8480 | return SDValue(N, 0); |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 8481 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 8482 | // FIXME: is there such a thing as a truncating indexed store? |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8483 | if (ST->isTruncatingStore() && ST->isUnindexed() && |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 8484 | Value.getValueType().isInteger()) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 8485 | // See if we can simplify the input to this truncstore with knowledge that |
| 8486 | // only the low bits are being used. For example: |
| 8487 | // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8488 | SDValue Shorter = |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 8489 | GetDemandedBits(Value, |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 8490 | APInt::getLowBitsSet( |
| 8491 | Value.getValueType().getScalarType().getSizeInBits(), |
| 8492 | ST->getMemoryVT().getScalarType().getSizeInBits())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8493 | AddToWorkList(Value.getNode()); |
| 8494 | if (Shorter.getNode()) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8495 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 8496 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8497 | ST->isVolatile(), ST->isNonTemporal(), |
| 8498 | ST->getAlignment()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8499 | |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 8500 | // Otherwise, see if we can simplify the operation with |
| 8501 | // SimplifyDemandedBits, which only works if the value has a single use. |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 8502 | if (SimplifyDemandedBits(Value, |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 8503 | APInt::getLowBitsSet( |
| 8504 | Value.getValueType().getScalarType().getSizeInBits(), |
| 8505 | ST->getMemoryVT().getScalarType().getSizeInBits()))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8506 | return SDValue(N, 0); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 8507 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8508 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 8509 | // If this is a load followed by a store to the same location, then the store |
| 8510 | // is dead/noop. |
| 8511 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 8512 | if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8513 | ST->isUnindexed() && !ST->isVolatile() && |
Chris Lattner | 07649d9 | 2008-01-08 23:08:06 +0000 | [diff] [blame] | 8514 | // There can't be any side effects between the load and store, such as |
| 8515 | // a call or store. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8516 | Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 8517 | // The store is dead, remove it. |
| 8518 | return Chain; |
| 8519 | } |
| 8520 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8521 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8522 | // If this is an FP_ROUND or TRUNC followed by a store, fold this into a |
| 8523 | // truncating store. We can do this even if this is already a truncstore. |
| 8524 | if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8525 | && Value.getNode()->hasOneUse() && ST->isUnindexed() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8526 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 8527 | ST->getMemoryVT())) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8528 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 8529 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8530 | ST->isVolatile(), ST->isNonTemporal(), |
| 8531 | ST->getAlignment()); |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 8532 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8533 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8534 | // Only perform this optimization before the types are legal, because we |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8535 | // don't want to perform this optimization on every DAGCombine invocation. |
Nadav Rotem | a569a80 | 2012-12-02 17:14:09 +0000 | [diff] [blame] | 8536 | if (!LegalTypes) { |
| 8537 | bool EverChanged = false; |
| 8538 | |
| 8539 | do { |
| 8540 | // There can be multiple store sequences on the same chain. |
| 8541 | // Keep trying to merge store sequences until we are unable to do so |
| 8542 | // or until we merge the last store on the chain. |
| 8543 | bool Changed = MergeConsecutiveStores(ST); |
| 8544 | EverChanged |= Changed; |
| 8545 | if (!Changed) break; |
| 8546 | } while (ST->getOpcode() != ISD::DELETED_NODE); |
| 8547 | |
| 8548 | if (EverChanged) |
| 8549 | return SDValue(N, 0); |
| 8550 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8551 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8552 | return ReduceLoadOpStoreWidth(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8555 | SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 8556 | SDValue InVec = N->getOperand(0); |
| 8557 | SDValue InVal = N->getOperand(1); |
| 8558 | SDValue EltNo = N->getOperand(2); |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 8559 | DebugLoc dl = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8560 | |
Bob Wilson | 492fd45 | 2010-05-19 23:42:58 +0000 | [diff] [blame] | 8561 | // If the inserted element is an UNDEF, just use the input vector. |
| 8562 | if (InVal.getOpcode() == ISD::UNDEF) |
| 8563 | return InVec; |
| 8564 | |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 8565 | EVT VT = InVec.getValueType(); |
| 8566 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8567 | // If we can't generate a legal BUILD_VECTOR, exit |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 8568 | if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
| 8569 | return SDValue(); |
| 8570 | |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 8571 | // Check that we know which element is being inserted |
| 8572 | if (!isa<ConstantSDNode>(EltNo)) |
| 8573 | return SDValue(); |
| 8574 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8575 | |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 8576 | // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially |
| 8577 | // be converted to a BUILD_VECTOR). Fill in the Ops vector with the |
| 8578 | // vector elements. |
| 8579 | SmallVector<SDValue, 8> Ops; |
| 8580 | if (InVec.getOpcode() == ISD::BUILD_VECTOR) { |
| 8581 | Ops.append(InVec.getNode()->op_begin(), |
| 8582 | InVec.getNode()->op_end()); |
| 8583 | } else if (InVec.getOpcode() == ISD::UNDEF) { |
| 8584 | unsigned NElts = VT.getVectorNumElements(); |
| 8585 | Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); |
| 8586 | } else { |
| 8587 | return SDValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8588 | } |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 8589 | |
| 8590 | // Insert the element |
| 8591 | if (Elt < Ops.size()) { |
| 8592 | // All the operands of BUILD_VECTOR must have the same type; |
| 8593 | // we enforce that here. |
| 8594 | EVT OpVT = Ops[0].getValueType(); |
| 8595 | if (InVal.getValueType() != OpVT) |
| 8596 | InVal = OpVT.bitsGT(InVal.getValueType()) ? |
| 8597 | DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) : |
| 8598 | DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal); |
| 8599 | Ops[Elt] = InVal; |
| 8600 | } |
| 8601 | |
| 8602 | // Return the new vector |
| 8603 | return DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 8604 | VT, &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 8605 | } |
| 8606 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8607 | SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 8608 | // (vextract (scalar_to_vector val, 0) -> val |
| 8609 | SDValue InVec = N->getOperand(0); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8610 | EVT VT = InVec.getValueType(); |
| 8611 | EVT NVT = N->getValueType(0); |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 8612 | |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 8613 | if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 8614 | // Check if the result type doesn't match the inserted element type. A |
| 8615 | // SCALAR_TO_VECTOR may truncate the inserted element and the |
| 8616 | // EXTRACT_VECTOR_ELT may widen the extracted vector. |
| 8617 | SDValue InOp = InVec.getOperand(0); |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 8618 | if (InOp.getValueType() != NVT) { |
| 8619 | assert(InOp.getValueType().isInteger() && NVT.isInteger()); |
| 8620 | return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT); |
| 8621 | } |
| 8622 | return InOp; |
| 8623 | } |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8624 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8625 | SDValue EltNo = N->getOperand(1); |
| 8626 | bool ConstEltNo = isa<ConstantSDNode>(EltNo); |
| 8627 | |
| 8628 | // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT. |
| 8629 | // We only perform this optimization before the op legalization phase because |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 8630 | // we may introduce new vector instructions which are not backed by TD |
| 8631 | // patterns. For example on AVX, extracting elements from a wide vector |
| 8632 | // without using extract_subvector. |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8633 | if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE |
| 8634 | && ConstEltNo && !LegalOperations) { |
| 8635 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| 8636 | int NumElem = VT.getVectorNumElements(); |
| 8637 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec); |
| 8638 | // Find the new index to extract from. |
| 8639 | int OrigElt = SVOp->getMaskElt(Elt); |
| 8640 | |
| 8641 | // Extracting an undef index is undef. |
| 8642 | if (OrigElt == -1) |
| 8643 | return DAG.getUNDEF(NVT); |
| 8644 | |
| 8645 | // Select the right vector half to extract from. |
| 8646 | if (OrigElt < NumElem) { |
| 8647 | InVec = InVec->getOperand(0); |
| 8648 | } else { |
| 8649 | InVec = InVec->getOperand(1); |
| 8650 | OrigElt -= NumElem; |
| 8651 | } |
| 8652 | |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 8653 | EVT IndexTy = N->getOperand(1).getValueType(); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8654 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 8655 | InVec, DAG.getConstant(OrigElt, IndexTy)); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8658 | // Perform only after legalization to ensure build_vector / vector_shuffle |
| 8659 | // optimizations have already been done. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8660 | if (!LegalOperations) return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8661 | |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 8662 | // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) |
| 8663 | // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) |
| 8664 | // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr) |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8665 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 8666 | if (ConstEltNo) { |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 8667 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8668 | bool NewLoad = false; |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 8669 | bool BCNumEltsChanged = false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8670 | EVT ExtVT = VT.getVectorElementType(); |
| 8671 | EVT LVT = ExtVT; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8672 | |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 8673 | // If the result of load has to be truncated, then it's not necessarily |
| 8674 | // profitable. |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 8675 | if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT)) |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 8676 | return SDValue(); |
| 8677 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8678 | if (InVec.getOpcode() == ISD::BITCAST) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8679 | // Don't duplicate a load with other uses. |
| 8680 | if (!InVec.hasOneUse()) |
| 8681 | return SDValue(); |
| 8682 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8683 | EVT BCVT = InVec.getOperand(0).getValueType(); |
| 8684 | if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8685 | return SDValue(); |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 8686 | if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) |
| 8687 | BCNumEltsChanged = true; |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8688 | InVec = InVec.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8689 | ExtVT = BCVT.getVectorElementType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8690 | NewLoad = true; |
| 8691 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8692 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8693 | LoadSDNode *LN0 = NULL; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 8694 | const ShuffleVectorSDNode *SVN = NULL; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8695 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8696 | LN0 = cast<LoadSDNode>(InVec); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8697 | } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8698 | InVec.getOperand(0).getValueType() == ExtVT && |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8699 | ISD::isNormalLoad(InVec.getOperand(0).getNode())) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8700 | // Don't duplicate a load with other uses. |
| 8701 | if (!InVec.hasOneUse()) |
| 8702 | return SDValue(); |
| 8703 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8704 | LN0 = cast<LoadSDNode>(InVec.getOperand(0)); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 8705 | } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8706 | // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) |
| 8707 | // => |
| 8708 | // (load $addr+1*size) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8709 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8710 | // Don't duplicate a load with other uses. |
| 8711 | if (!InVec.hasOneUse()) |
| 8712 | return SDValue(); |
| 8713 | |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 8714 | // If the bit convert changed the number of elements, it is unsafe |
| 8715 | // to examine the mask. |
| 8716 | if (BCNumEltsChanged) |
| 8717 | return SDValue(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 8718 | |
| 8719 | // Select the input vector, guarding against out of range extract vector. |
| 8720 | unsigned NumElems = VT.getVectorNumElements(); |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 8721 | int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 8722 | InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); |
| 8723 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8724 | if (InVec.getOpcode() == ISD::BITCAST) { |
| 8725 | // Don't duplicate a load with other uses. |
| 8726 | if (!InVec.hasOneUse()) |
| 8727 | return SDValue(); |
| 8728 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8729 | InVec = InVec.getOperand(0); |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8730 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8731 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8732 | LN0 = cast<LoadSDNode>(InVec); |
Ted Kremenek | d0e88f3 | 2010-04-08 18:49:30 +0000 | [diff] [blame] | 8733 | Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8734 | } |
| 8735 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8736 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8737 | // Make sure we found a non-volatile load and the extractelement is |
| 8738 | // the only use. |
Nadav Rotem | 42febc6 | 2011-05-11 14:40:50 +0000 | [diff] [blame] | 8739 | if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8740 | return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8741 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 8742 | // If Idx was -1 above, Elt is going to be -1, so just return undef. |
| 8743 | if (Elt == -1) |
Eli Friedman | ed4b427 | 2011-07-25 22:25:42 +0000 | [diff] [blame] | 8744 | return DAG.getUNDEF(LVT); |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 8745 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8746 | unsigned Align = LN0->getAlignment(); |
| 8747 | if (NewLoad) { |
| 8748 | // Check the resultant load doesn't need a higher alignment than the |
| 8749 | // original load. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8750 | unsigned NewAlign = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8751 | TLI.getDataLayout() |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 8752 | ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8753 | |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 8754 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8755 | return SDValue(); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8756 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8757 | Align = NewAlign; |
| 8758 | } |
| 8759 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8760 | SDValue NewPtr = LN0->getBasePtr(); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8761 | unsigned PtrOff = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8762 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 8763 | if (Elt) { |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8764 | PtrOff = LVT.getSizeInBits() * Elt / 8; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8765 | EVT PtrType = NewPtr.getValueType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8766 | if (TLI.isBigEndian()) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8767 | PtrOff = VT.getSizeInBits() / 8 - PtrOff; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8768 | NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr, |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 8769 | DAG.getConstant(PtrOff, PtrType)); |
| 8770 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8771 | |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 8772 | // The replacement we need to do here is a little tricky: we need to |
| 8773 | // replace an extractelement of a load with a load. |
| 8774 | // Use ReplaceAllUsesOfValuesWith to do the replacement. |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 8775 | // Note that this replacement assumes that the extractvalue is the only |
| 8776 | // use of the load; that's okay because we don't want to perform this |
| 8777 | // transformation in other cases anyway. |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 8778 | SDValue Load; |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 8779 | SDValue Chain; |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 8780 | if (NVT.bitsGT(LVT)) { |
| 8781 | // If the result type of vextract is wider than the load, then issue an |
| 8782 | // extending load instead. |
| 8783 | ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT) |
| 8784 | ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| 8785 | Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(), |
| 8786 | NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff), |
| 8787 | LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 8788 | Chain = Load.getValue(1); |
| 8789 | } else { |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 8790 | Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr, |
| 8791 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 8792 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 8793 | LN0->isInvariant(), Align); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 8794 | Chain = Load.getValue(1); |
| 8795 | if (NVT.bitsLT(LVT)) |
| 8796 | Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load); |
| 8797 | else |
| 8798 | Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load); |
| 8799 | } |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 8800 | WorkListRemover DeadNodes(*this); |
| 8801 | SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) }; |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 8802 | SDValue To[] = { Load, Chain }; |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 8803 | DAG.ReplaceAllUsesOfValuesWith(From, To, 2); |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 8804 | // Since we're explcitly calling ReplaceAllUses, add the new node to the |
| 8805 | // worklist explicitly as well. |
| 8806 | AddToWorkList(Load.getNode()); |
Craig Topper | 0c9da21 | 2012-03-20 05:28:39 +0000 | [diff] [blame] | 8807 | AddUsersToWorkList(Load.getNode()); // Add users too |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 8808 | // Make sure to revisit this node to clean it up; it will usually be dead. |
| 8809 | AddToWorkList(N); |
| 8810 | return SDValue(N, 0); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8811 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 8812 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8813 | return SDValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8814 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 8815 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8816 | // Simplify (build_vec (ext )) to (bitcast (build_vec )) |
| 8817 | SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) { |
| 8818 | // We perform this optimization post type-legalization because |
| 8819 | // the type-legalizer often scalarizes integer-promoted vectors. |
| 8820 | // Performing this optimization before may create bit-casts which |
| 8821 | // will be type-legalized to complex code sequences. |
| 8822 | // We perform this optimization only before the operation legalizer because we |
| 8823 | // may introduce illegal operations. |
| 8824 | if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes) |
| 8825 | return SDValue(); |
| 8826 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8827 | unsigned NumInScalars = N->getNumOperands(); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8828 | DebugLoc dl = N->getDebugLoc(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8829 | EVT VT = N->getValueType(0); |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 8830 | |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8831 | // Check to see if this is a BUILD_VECTOR of a bunch of values |
| 8832 | // which come from any_extend or zero_extend nodes. If so, we can create |
| 8833 | // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8834 | // optimizations. We do not handle sign-extend because we can't fill the sign |
| 8835 | // using shuffles. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8836 | EVT SourceType = MVT::Other; |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 8837 | bool AllAnyExt = true; |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 8838 | |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 8839 | for (unsigned i = 0; i != NumInScalars; ++i) { |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8840 | SDValue In = N->getOperand(i); |
| 8841 | // Ignore undef inputs. |
| 8842 | if (In.getOpcode() == ISD::UNDEF) continue; |
| 8843 | |
| 8844 | bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND; |
| 8845 | bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND; |
| 8846 | |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8847 | // Abort if the element is not an extension. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8848 | if (!ZeroExt && !AnyExt) { |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8849 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8850 | break; |
| 8851 | } |
| 8852 | |
| 8853 | // The input is a ZeroExt or AnyExt. Check the original type. |
| 8854 | EVT InTy = In.getOperand(0).getValueType(); |
| 8855 | |
| 8856 | // Check that all of the widened source types are the same. |
| 8857 | if (SourceType == MVT::Other) |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8858 | // First time. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8859 | SourceType = InTy; |
| 8860 | else if (InTy != SourceType) { |
| 8861 | // Multiple income types. Abort. |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8862 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8863 | break; |
| 8864 | } |
| 8865 | |
| 8866 | // Check if all of the extends are ANY_EXTENDs. |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 8867 | AllAnyExt &= AnyExt; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8868 | } |
| 8869 | |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8870 | // In order to have valid types, all of the inputs must be extended from the |
| 8871 | // same source type and all of the inputs must be any or zero extend. |
| 8872 | // Scalar sizes must be a power of two. |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8873 | EVT OutScalarTy = VT.getScalarType(); |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 8874 | bool ValidTypes = SourceType != MVT::Other && |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 8875 | isPowerOf2_32(OutScalarTy.getSizeInBits()) && |
| 8876 | isPowerOf2_32(SourceType.getSizeInBits()); |
| 8877 | |
Nadav Rotem | 6431ff9 | 2012-03-15 08:49:06 +0000 | [diff] [blame] | 8878 | // Create a new simpler BUILD_VECTOR sequence which other optimizations can |
| 8879 | // turn into a single shuffle instruction. |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8880 | if (!ValidTypes) |
| 8881 | return SDValue(); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8882 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8883 | bool isLE = TLI.isLittleEndian(); |
| 8884 | unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits(); |
| 8885 | assert(ElemRatio > 1 && "Invalid element size ratio"); |
| 8886 | SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType): |
| 8887 | DAG.getConstant(0, SourceType); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8888 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8889 | unsigned NewBVElems = ElemRatio * VT.getVectorNumElements(); |
| 8890 | SmallVector<SDValue, 8> Ops(NewBVElems, Filler); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8891 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8892 | // Populate the new build_vector |
Jakub Staszak | adf3891 | 2012-10-24 00:38:25 +0000 | [diff] [blame] | 8893 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8894 | SDValue Cast = N->getOperand(i); |
| 8895 | assert((Cast.getOpcode() == ISD::ANY_EXTEND || |
| 8896 | Cast.getOpcode() == ISD::ZERO_EXTEND || |
| 8897 | Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode"); |
| 8898 | SDValue In; |
| 8899 | if (Cast.getOpcode() == ISD::UNDEF) |
| 8900 | In = DAG.getUNDEF(SourceType); |
| 8901 | else |
| 8902 | In = Cast->getOperand(0); |
| 8903 | unsigned Index = isLE ? (i * ElemRatio) : |
| 8904 | (i * ElemRatio + (ElemRatio - 1)); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8905 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8906 | assert(Index < Ops.size() && "Invalid index"); |
| 8907 | Ops[Index] = In; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 8908 | } |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 8909 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8910 | // The type of the new BUILD_VECTOR node. |
| 8911 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems); |
| 8912 | assert(VecVT.getSizeInBits() == VT.getSizeInBits() && |
| 8913 | "Invalid vector size"); |
| 8914 | // Check if the new vector type is legal. |
| 8915 | if (!isTypeLegal(VecVT)) return SDValue(); |
| 8916 | |
| 8917 | // Make the new BUILD_VECTOR. |
| 8918 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size()); |
| 8919 | |
| 8920 | // The new BUILD_VECTOR node has the potential to be further optimized. |
| 8921 | AddToWorkList(BV.getNode()); |
| 8922 | // Bitcast to the desired type. |
| 8923 | return DAG.getNode(ISD::BITCAST, dl, VT, BV); |
| 8924 | } |
| 8925 | |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 8926 | SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) { |
| 8927 | EVT VT = N->getValueType(0); |
| 8928 | |
| 8929 | unsigned NumInScalars = N->getNumOperands(); |
| 8930 | DebugLoc dl = N->getDebugLoc(); |
| 8931 | |
| 8932 | EVT SrcVT = MVT::Other; |
| 8933 | unsigned Opcode = ISD::DELETED_NODE; |
| 8934 | unsigned NumDefs = 0; |
| 8935 | |
| 8936 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 8937 | SDValue In = N->getOperand(i); |
| 8938 | unsigned Opc = In.getOpcode(); |
| 8939 | |
| 8940 | if (Opc == ISD::UNDEF) |
| 8941 | continue; |
| 8942 | |
| 8943 | // If all scalar values are floats and converted from integers. |
| 8944 | if (Opcode == ISD::DELETED_NODE && |
| 8945 | (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) { |
| 8946 | Opcode = Opc; |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 8947 | } |
Tom Stellard | d40758b | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 8948 | |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 8949 | if (Opc != Opcode) |
| 8950 | return SDValue(); |
| 8951 | |
| 8952 | EVT InVT = In.getOperand(0).getValueType(); |
| 8953 | |
| 8954 | // If all scalar values are typed differently, bail out. It's chosen to |
| 8955 | // simplify BUILD_VECTOR of integer types. |
| 8956 | if (SrcVT == MVT::Other) |
| 8957 | SrcVT = InVT; |
| 8958 | if (SrcVT != InVT) |
| 8959 | return SDValue(); |
| 8960 | NumDefs++; |
| 8961 | } |
| 8962 | |
| 8963 | // If the vector has just one element defined, it's not worth to fold it into |
| 8964 | // a vectorized one. |
| 8965 | if (NumDefs < 2) |
| 8966 | return SDValue(); |
| 8967 | |
| 8968 | assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP) |
| 8969 | && "Should only handle conversion from integer to float."); |
| 8970 | assert(SrcVT != MVT::Other && "Cannot determine source type!"); |
| 8971 | |
| 8972 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars); |
Tom Stellard | d40758b | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 8973 | |
| 8974 | if (!TLI.isOperationLegalOrCustom(Opcode, NVT)) |
| 8975 | return SDValue(); |
| 8976 | |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 8977 | SmallVector<SDValue, 8> Opnds; |
| 8978 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 8979 | SDValue In = N->getOperand(i); |
| 8980 | |
| 8981 | if (In.getOpcode() == ISD::UNDEF) |
| 8982 | Opnds.push_back(DAG.getUNDEF(SrcVT)); |
| 8983 | else |
| 8984 | Opnds.push_back(In.getOperand(0)); |
| 8985 | } |
| 8986 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, |
| 8987 | &Opnds[0], Opnds.size()); |
| 8988 | AddToWorkList(BV.getNode()); |
| 8989 | |
| 8990 | return DAG.getNode(Opcode, dl, VT, BV); |
| 8991 | } |
| 8992 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 8993 | SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { |
| 8994 | unsigned NumInScalars = N->getNumOperands(); |
| 8995 | DebugLoc dl = N->getDebugLoc(); |
| 8996 | EVT VT = N->getValueType(0); |
| 8997 | |
| 8998 | // A vector built entirely of undefs is undef. |
| 8999 | if (ISD::allOperandsUndef(N)) |
| 9000 | return DAG.getUNDEF(VT); |
| 9001 | |
| 9002 | SDValue V = reduceBuildVecExtToExtBuildVec(N); |
| 9003 | if (V.getNode()) |
| 9004 | return V; |
| 9005 | |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9006 | V = reduceBuildVecConvertToConvertBuildVec(N); |
| 9007 | if (V.getNode()) |
| 9008 | return V; |
| 9009 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9010 | // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT |
| 9011 | // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from |
| 9012 | // at most two distinct vectors, turn this into a shuffle node. |
Duncan Sands | 00294ca | 2012-03-19 15:35:44 +0000 | [diff] [blame] | 9013 | |
| 9014 | // May only combine to shuffle after legalize if shuffle is legal. |
| 9015 | if (LegalOperations && |
| 9016 | !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT)) |
| 9017 | return SDValue(); |
| 9018 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9019 | SDValue VecIn1, VecIn2; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9020 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 9021 | // Ignore undef inputs. |
| 9022 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9023 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9024 | // If this input is something other than a EXTRACT_VECTOR_ELT with a |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9025 | // constant index, bail out. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9026 | if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9027 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9028 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9029 | break; |
| 9030 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9031 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9032 | // We allow up to two distinct input vectors. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9033 | SDValue ExtractedFromVec = N->getOperand(i).getOperand(0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9034 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 9035 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9036 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9037 | if (VecIn1.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9038 | VecIn1 = ExtractedFromVec; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9039 | } else if (VecIn2.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9040 | VecIn2 = ExtractedFromVec; |
| 9041 | } else { |
| 9042 | // Too many inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9043 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9044 | break; |
| 9045 | } |
| 9046 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9047 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9048 | // If everything is good, we can make a shuffle operation. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9049 | if (VecIn1.getNode()) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9050 | SmallVector<int, 8> Mask; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9051 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 9052 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9053 | Mask.push_back(-1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9054 | continue; |
| 9055 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9056 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 9057 | // If extracting from the first vector, just use the index directly. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9058 | SDValue Extract = N->getOperand(i); |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 9059 | SDValue ExtVal = Extract.getOperand(1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9060 | if (Extract.getOperand(0) == VecIn1) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9061 | unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
| 9062 | if (ExtIndex > VT.getVectorNumElements()) |
| 9063 | return SDValue(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9064 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9065 | Mask.push_back(ExtIndex); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9066 | continue; |
| 9067 | } |
| 9068 | |
| 9069 | // Otherwise, use InIdx + VecSize |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 9070 | unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9071 | Mask.push_back(Idx+NumInScalars); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9072 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9073 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9074 | // We can't generate a shuffle node with mismatched input and output types. |
| 9075 | // Attempt to transform a single input vector to the correct type. |
| 9076 | if ((VT != VecIn1.getValueType())) { |
| 9077 | // We don't support shuffeling between TWO values of different types. |
| 9078 | if (VecIn2.getNode() != 0) |
| 9079 | return SDValue(); |
| 9080 | |
| 9081 | // We only support widening of vectors which are half the size of the |
| 9082 | // output registers. For example XMM->YMM widening on X86 with AVX. |
| 9083 | if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits()) |
| 9084 | return SDValue(); |
| 9085 | |
James Molloy | 8cd08bf | 2012-09-10 14:01:21 +0000 | [diff] [blame] | 9086 | // If the input vector type has a different base type to the output |
| 9087 | // vector type, bail out. |
| 9088 | if (VecIn1.getValueType().getVectorElementType() != |
| 9089 | VT.getVectorElementType()) |
| 9090 | return SDValue(); |
| 9091 | |
Stepan Dyatkovskiy | fdeb9fe | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 9092 | // Widen the input vector by adding undef values. |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9093 | VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, |
Stepan Dyatkovskiy | fdeb9fe | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 9094 | VecIn1, DAG.getUNDEF(VecIn1.getValueType())); |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9095 | } |
| 9096 | |
| 9097 | // If VecIn2 is unused then change it to undef. |
| 9098 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); |
| 9099 | |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 9100 | // Check that we were able to transform all incoming values to the same |
| 9101 | // type. |
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 9102 | if (VecIn2.getValueType() != VecIn1.getValueType() || |
| 9103 | VecIn1.getValueType() != VT) |
| 9104 | return SDValue(); |
| 9105 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9106 | // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes. |
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 9107 | if (!isTypeLegal(VT)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9108 | return SDValue(); |
| 9109 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9110 | // Return the new VECTOR_SHUFFLE node. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9111 | SDValue Ops[2]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 9112 | Ops[0] = VecIn1; |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9113 | Ops[1] = VecIn2; |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9114 | return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9115 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9116 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9117 | return SDValue(); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9118 | } |
| 9119 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9120 | SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9121 | // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of |
| 9122 | // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector |
| 9123 | // inputs come from at most two distinct vectors, turn this into a shuffle |
| 9124 | // node. |
| 9125 | |
| 9126 | // If we only have one input vector, we don't need to do any concatenation. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9127 | if (N->getNumOperands() == 1) |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9128 | return N->getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9129 | |
Nadav Rotem | b7e230d | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 9130 | // Check if all of the operands are undefs. |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9131 | if (ISD::allOperandsUndef(N)) |
Nadav Rotem | b7e230d | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 9132 | return DAG.getUNDEF(N->getValueType(0)); |
| 9133 | |
Nadav Rotem | b2ed5fa | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 9134 | // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR |
| 9135 | // nodes often generate nop CONCAT_VECTOR nodes. |
| 9136 | // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that |
| 9137 | // place the incoming vectors at the exact same location. |
| 9138 | SDValue SingleSource = SDValue(); |
| 9139 | unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements(); |
| 9140 | |
| 9141 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 9142 | SDValue Op = N->getOperand(i); |
| 9143 | |
| 9144 | if (Op.getOpcode() == ISD::UNDEF) |
| 9145 | continue; |
| 9146 | |
| 9147 | // Check if this is the identity extract: |
| 9148 | if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR) |
| 9149 | return SDValue(); |
| 9150 | |
| 9151 | // Find the single incoming vector for the extract_subvector. |
| 9152 | if (SingleSource.getNode()) { |
| 9153 | if (Op.getOperand(0) != SingleSource) |
| 9154 | return SDValue(); |
| 9155 | } else { |
| 9156 | SingleSource = Op.getOperand(0); |
Michael Kuperstein | 2720248 | 2013-05-06 08:06:13 +0000 | [diff] [blame] | 9157 | |
| 9158 | // Check the source type is the same as the type of the result. |
| 9159 | // If not, this concat may extend the vector, so we can not |
| 9160 | // optimize it away. |
| 9161 | if (SingleSource.getValueType() != N->getValueType(0)) |
| 9162 | return SDValue(); |
Nadav Rotem | b2ed5fa | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 9163 | } |
| 9164 | |
| 9165 | unsigned IdentityIndex = i * PartNumElem; |
| 9166 | ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 9167 | // The extract index must be constant. |
| 9168 | if (!CS) |
| 9169 | return SDValue(); |
| 9170 | |
| 9171 | // Check that we are reading from the identity index. |
| 9172 | if (CS->getZExtValue() != IdentityIndex) |
| 9173 | return SDValue(); |
| 9174 | } |
| 9175 | |
| 9176 | if (SingleSource.getNode()) |
| 9177 | return SingleSource; |
| 9178 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9179 | return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9180 | } |
| 9181 | |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 9182 | SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) { |
| 9183 | EVT NVT = N->getValueType(0); |
| 9184 | SDValue V = N->getOperand(0); |
| 9185 | |
Michael Liao | 13429e2 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 9186 | if (V->getOpcode() == ISD::CONCAT_VECTORS) { |
| 9187 | // Combine: |
| 9188 | // (extract_subvec (concat V1, V2, ...), i) |
| 9189 | // Into: |
| 9190 | // Vi if possible |
Michael Liao | 9aecdb5 | 2012-10-19 03:17:00 +0000 | [diff] [blame] | 9191 | // Only operand 0 is checked as 'concat' assumes all inputs of the same type. |
| 9192 | if (V->getOperand(0).getValueType() != NVT) |
| 9193 | return SDValue(); |
Michael Liao | 13429e2 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 9194 | unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
| 9195 | unsigned NumElems = NVT.getVectorNumElements(); |
| 9196 | assert((Idx % NumElems) == 0 && |
| 9197 | "IDX in concat is not a multiple of the result vector length."); |
| 9198 | return V->getOperand(Idx / NumElems); |
| 9199 | } |
| 9200 | |
Michael Liao | b4f98ea | 2013-03-25 23:47:35 +0000 | [diff] [blame] | 9201 | // Skip bitcasting |
| 9202 | if (V->getOpcode() == ISD::BITCAST) |
| 9203 | V = V.getOperand(0); |
| 9204 | |
| 9205 | if (V->getOpcode() == ISD::INSERT_SUBVECTOR) { |
| 9206 | DebugLoc dl = N->getDebugLoc(); |
| 9207 | // Handle only simple case where vector being inserted and vector |
| 9208 | // being extracted are of same type, and are half size of larger vectors. |
| 9209 | EVT BigVT = V->getOperand(0).getValueType(); |
| 9210 | EVT SmallVT = V->getOperand(1).getValueType(); |
| 9211 | if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits()) |
| 9212 | return SDValue(); |
| 9213 | |
| 9214 | // Only handle cases where both indexes are constants with the same type. |
| 9215 | ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 9216 | ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2)); |
| 9217 | |
| 9218 | if (InsIdx && ExtIdx && |
| 9219 | InsIdx->getValueType(0).getSizeInBits() <= 64 && |
| 9220 | ExtIdx->getValueType(0).getSizeInBits() <= 64) { |
| 9221 | // Combine: |
| 9222 | // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx) |
| 9223 | // Into: |
| 9224 | // indices are equal or bit offsets are equal => V1 |
| 9225 | // otherwise => (extract_subvec V1, ExtIdx) |
| 9226 | if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() == |
| 9227 | ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits()) |
| 9228 | return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1)); |
| 9229 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, |
| 9230 | DAG.getNode(ISD::BITCAST, dl, |
| 9231 | N->getOperand(0).getValueType(), |
| 9232 | V->getOperand(0)), N->getOperand(1)); |
| 9233 | } |
| 9234 | } |
| 9235 | |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 9236 | return SDValue(); |
| 9237 | } |
| 9238 | |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9239 | // Tries to turn a shuffle of two CONCAT_VECTORS into a single concat. |
| 9240 | static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) { |
| 9241 | EVT VT = N->getValueType(0); |
| 9242 | unsigned NumElts = VT.getVectorNumElements(); |
| 9243 | |
| 9244 | SDValue N0 = N->getOperand(0); |
| 9245 | SDValue N1 = N->getOperand(1); |
| 9246 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 9247 | |
| 9248 | SmallVector<SDValue, 4> Ops; |
| 9249 | EVT ConcatVT = N0.getOperand(0).getValueType(); |
| 9250 | unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements(); |
| 9251 | unsigned NumConcats = NumElts / NumElemsPerConcat; |
| 9252 | |
| 9253 | // Look at every vector that's inserted. We're looking for exact |
| 9254 | // subvector-sized copies from a concatenated vector |
| 9255 | for (unsigned I = 0; I != NumConcats; ++I) { |
| 9256 | // Make sure we're dealing with a copy. |
| 9257 | unsigned Begin = I * NumElemsPerConcat; |
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9258 | bool AllUndef = true, NoUndef = true; |
| 9259 | for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) { |
| 9260 | if (SVN->getMaskElt(J) >= 0) |
| 9261 | AllUndef = false; |
| 9262 | else |
| 9263 | NoUndef = false; |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9266 | if (NoUndef) { |
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9267 | if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0) |
| 9268 | return SDValue(); |
| 9269 | |
| 9270 | for (unsigned J = 1; J != NumElemsPerConcat; ++J) |
| 9271 | if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J)) |
| 9272 | return SDValue(); |
| 9273 | |
| 9274 | unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat; |
| 9275 | if (FirstElt < N0.getNumOperands()) |
| 9276 | Ops.push_back(N0.getOperand(FirstElt)); |
| 9277 | else |
| 9278 | Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands())); |
| 9279 | |
| 9280 | } else if (AllUndef) { |
| 9281 | Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType())); |
| 9282 | } else { // Mixed with general masks and undefs, can't do optimization. |
| 9283 | return SDValue(); |
| 9284 | } |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
| 9287 | return DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, Ops.data(), |
| 9288 | Ops.size()); |
| 9289 | } |
| 9290 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9291 | SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9292 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9293 | unsigned NumElts = VT.getVectorNumElements(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 9294 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 9295 | SDValue N0 = N->getOperand(0); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 9296 | SDValue N1 = N->getOperand(1); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 9297 | |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 9298 | assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG"); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 9299 | |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 9300 | // Canonicalize shuffle undef, undef -> undef |
| 9301 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 9302 | return DAG.getUNDEF(VT); |
| 9303 | |
| 9304 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 9305 | |
| 9306 | // Canonicalize shuffle v, v -> v, undef |
| 9307 | if (N0 == N1) { |
| 9308 | SmallVector<int, 8> NewMask; |
| 9309 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9310 | int Idx = SVN->getMaskElt(i); |
| 9311 | if (Idx >= (int)NumElts) Idx -= NumElts; |
| 9312 | NewMask.push_back(Idx); |
| 9313 | } |
| 9314 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT), |
| 9315 | &NewMask[0]); |
| 9316 | } |
| 9317 | |
| 9318 | // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. |
| 9319 | if (N0.getOpcode() == ISD::UNDEF) { |
| 9320 | SmallVector<int, 8> NewMask; |
| 9321 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9322 | int Idx = SVN->getMaskElt(i); |
Craig Topper | 4b206bd | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 9323 | if (Idx >= 0) { |
| 9324 | if (Idx < (int)NumElts) |
| 9325 | Idx += NumElts; |
| 9326 | else |
| 9327 | Idx -= NumElts; |
| 9328 | } |
| 9329 | NewMask.push_back(Idx); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 9330 | } |
| 9331 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT), |
| 9332 | &NewMask[0]); |
| 9333 | } |
| 9334 | |
| 9335 | // Remove references to rhs if it is undef |
| 9336 | if (N1.getOpcode() == ISD::UNDEF) { |
| 9337 | bool Changed = false; |
| 9338 | SmallVector<int, 8> NewMask; |
| 9339 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9340 | int Idx = SVN->getMaskElt(i); |
| 9341 | if (Idx >= (int)NumElts) { |
| 9342 | Idx = -1; |
| 9343 | Changed = true; |
| 9344 | } |
| 9345 | NewMask.push_back(Idx); |
| 9346 | } |
| 9347 | if (Changed) |
| 9348 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]); |
| 9349 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 9350 | |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 9351 | // If it is a splat, check if the argument vector is another splat or a |
| 9352 | // build_vector with all scalar elements the same. |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 9353 | if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9354 | SDNode *V = N0.getNode(); |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 9355 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9356 | // If this is a bit convert that changes the element type of the vector but |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 9357 | // not the number of vector elements, look through it. Be careful not to |
| 9358 | // look though conversions that change things like v4f32 to v2f64. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9359 | if (V->getOpcode() == ISD::BITCAST) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9360 | SDValue ConvInput = V->getOperand(0); |
Evan Cheng | 2925786 | 2008-07-22 20:42:56 +0000 | [diff] [blame] | 9361 | if (ConvInput.getValueType().isVector() && |
| 9362 | ConvInput.getValueType().getVectorNumElements() == NumElts) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9363 | V = ConvInput.getNode(); |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9366 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 9367 | assert(V->getNumOperands() == NumElts && |
| 9368 | "BUILD_VECTOR has wrong number of operands"); |
| 9369 | SDValue Base; |
| 9370 | bool AllSame = true; |
| 9371 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9372 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 9373 | Base = V->getOperand(i); |
| 9374 | break; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 9375 | } |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 9376 | } |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 9377 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 9378 | if (!Base.getNode()) |
| 9379 | return N0; |
| 9380 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9381 | if (V->getOperand(i) != Base) { |
| 9382 | AllSame = false; |
| 9383 | break; |
| 9384 | } |
| 9385 | } |
| 9386 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 9387 | if (AllSame) |
| 9388 | return N0; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 9389 | } |
| 9390 | } |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9391 | |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9392 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && |
| 9393 | Level < AfterLegalizeVectorOps && |
| 9394 | (N1.getOpcode() == ISD::UNDEF || |
| 9395 | (N1.getOpcode() == ISD::CONCAT_VECTORS && |
| 9396 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) { |
| 9397 | SDValue V = partitionShuffleOfConcats(N, DAG); |
| 9398 | |
| 9399 | if (V.getNode()) |
| 9400 | return V; |
| 9401 | } |
| 9402 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9403 | // If this shuffle node is simply a swizzle of another shuffle node, |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 9404 | // and it reverses the swizzle of the previous shuffle then we can |
| 9405 | // optimize shuffle(shuffle(x, undef), undef) -> x. |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9406 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 9407 | N1.getOpcode() == ISD::UNDEF) { |
| 9408 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9409 | ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0); |
| 9410 | |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 9411 | // Shuffle nodes can only reverse shuffles with a single non-undef value. |
| 9412 | if (N0.getOperand(1).getOpcode() != ISD::UNDEF) |
| 9413 | return SDValue(); |
| 9414 | |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 9415 | // The incoming shuffle must be of the same type as the result of the |
| 9416 | // current shuffle. |
| 9417 | assert(OtherSV->getOperand(0).getValueType() == VT && |
| 9418 | "Shuffle types don't match"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9419 | |
| 9420 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9421 | int Idx = SVN->getMaskElt(i); |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 9422 | assert(Idx < (int)NumElts && "Index references undef operand"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9423 | // Next, this index comes from the first value, which is the incoming |
| 9424 | // shuffle. Adopt the incoming index. |
| 9425 | if (Idx >= 0) |
| 9426 | Idx = OtherSV->getMaskElt(Idx); |
| 9427 | |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 9428 | // The combined shuffle must map each index to itself. |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 9429 | if (Idx >= 0 && (unsigned)Idx != i) |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 9430 | return SDValue(); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9431 | } |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 9432 | |
| 9433 | return OtherSV->getOperand(0); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9436 | return SDValue(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9439 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9440 | /// an AND to a vector_shuffle with the destination vector and a zero vector. |
| 9441 | /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9442 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9443 | SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9444 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9445 | DebugLoc dl = N->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9446 | SDValue LHS = N->getOperand(0); |
| 9447 | SDValue RHS = N->getOperand(1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9448 | if (N->getOpcode() == ISD::AND) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9449 | if (RHS.getOpcode() == ISD::BITCAST) |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9450 | RHS = RHS.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9451 | if (RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9452 | SmallVector<int, 8> Indices; |
| 9453 | unsigned NumElts = RHS.getNumOperands(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9454 | for (unsigned i = 0; i != NumElts; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9455 | SDValue Elt = RHS.getOperand(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9456 | if (!isa<ConstantSDNode>(Elt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9457 | return SDValue(); |
Craig Topper | b7135e5 | 2012-04-09 05:59:53 +0000 | [diff] [blame] | 9458 | |
| 9459 | if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9460 | Indices.push_back(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9461 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9462 | Indices.push_back(NumElts); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9463 | else |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9464 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9465 | } |
| 9466 | |
| 9467 | // Let's see if the target supports this vector_shuffle. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9468 | EVT RVT = RHS.getValueType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9469 | if (!TLI.isVectorClearMaskLegal(Indices, RVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9470 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9471 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9472 | // Return the new VECTOR_SHUFFLE node. |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 9473 | EVT EltVT = RVT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9474 | SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(), |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 9475 | DAG.getConstant(0, EltVT)); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9476 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 9477 | RVT, &ZeroOps[0], ZeroOps.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9478 | LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9479 | SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9480 | return DAG.getNode(ISD::BITCAST, dl, VT, Shuf); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9481 | } |
| 9482 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9483 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9484 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9485 | } |
| 9486 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9487 | /// SimplifyVBinOp - Visit a binary vector operation, like ADD. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9488 | SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 9489 | assert(N->getValueType(0).isVector() && |
| 9490 | "SimplifyVBinOp only works on vectors!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9491 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9492 | SDValue LHS = N->getOperand(0); |
| 9493 | SDValue RHS = N->getOperand(1); |
| 9494 | SDValue Shuffle = XformToShuffleWithZero(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9495 | if (Shuffle.getNode()) return Shuffle; |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 9496 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9497 | // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 9498 | // this operation. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9499 | if (LHS.getOpcode() == ISD::BUILD_VECTOR && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9500 | RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9501 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9502 | for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9503 | SDValue LHSOp = LHS.getOperand(i); |
| 9504 | SDValue RHSOp = RHS.getOperand(i); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 9505 | // If these two elements can't be folded, bail out. |
| 9506 | if ((LHSOp.getOpcode() != ISD::UNDEF && |
| 9507 | LHSOp.getOpcode() != ISD::Constant && |
| 9508 | LHSOp.getOpcode() != ISD::ConstantFP) || |
| 9509 | (RHSOp.getOpcode() != ISD::UNDEF && |
| 9510 | RHSOp.getOpcode() != ISD::Constant && |
| 9511 | RHSOp.getOpcode() != ISD::ConstantFP)) |
| 9512 | break; |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9513 | |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 9514 | // Can't fold divide by zero. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9515 | if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV || |
| 9516 | N->getOpcode() == ISD::FDIV) { |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 9517 | if ((RHSOp.getOpcode() == ISD::Constant && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9518 | cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) || |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 9519 | (RHSOp.getOpcode() == ISD::ConstantFP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9520 | cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero())) |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 9521 | break; |
| 9522 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9523 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 9524 | EVT VT = LHSOp.getValueType(); |
Bob Wilson | db2b18f | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 9525 | EVT RVT = RHSOp.getValueType(); |
| 9526 | if (RVT != VT) { |
| 9527 | // Integer BUILD_VECTOR operands may have types larger than the element |
| 9528 | // size (e.g., when the element type is not legal). Prior to type |
| 9529 | // legalization, the types may not match between the two BUILD_VECTORS. |
| 9530 | // Truncate one of the operands to make them match. |
| 9531 | if (RVT.getSizeInBits() > VT.getSizeInBits()) { |
| 9532 | RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp); |
| 9533 | } else { |
| 9534 | LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp); |
| 9535 | VT = RVT; |
| 9536 | } |
| 9537 | } |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 9538 | SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT, |
Evan Cheng | a083988 | 2010-05-18 00:03:40 +0000 | [diff] [blame] | 9539 | LHSOp, RHSOp); |
| 9540 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 9541 | FoldOp.getOpcode() != ISD::Constant && |
| 9542 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 9543 | break; |
| 9544 | Ops.push_back(FoldOp); |
| 9545 | AddToWorkList(FoldOp.getNode()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 9546 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9547 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 9548 | if (Ops.size() == LHS.getNumOperands()) |
| 9549 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 9550 | LHS.getValueType(), &Ops[0], Ops.size()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 9551 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9552 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9553 | return SDValue(); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 9554 | } |
| 9555 | |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 9556 | /// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG. |
| 9557 | SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) { |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 9558 | assert(N->getValueType(0).isVector() && |
| 9559 | "SimplifyVUnaryOp only works on vectors!"); |
| 9560 | |
| 9561 | SDValue N0 = N->getOperand(0); |
| 9562 | |
| 9563 | if (N0.getOpcode() != ISD::BUILD_VECTOR) |
| 9564 | return SDValue(); |
| 9565 | |
| 9566 | // Operand is a BUILD_VECTOR node, see if we can constant fold it. |
| 9567 | SmallVector<SDValue, 8> Ops; |
| 9568 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { |
| 9569 | SDValue Op = N0.getOperand(i); |
| 9570 | if (Op.getOpcode() != ISD::UNDEF && |
| 9571 | Op.getOpcode() != ISD::ConstantFP) |
| 9572 | break; |
| 9573 | EVT EltVT = Op.getValueType(); |
| 9574 | SDValue FoldOp = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), EltVT, Op); |
| 9575 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 9576 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 9577 | break; |
| 9578 | Ops.push_back(FoldOp); |
| 9579 | AddToWorkList(FoldOp.getNode()); |
| 9580 | } |
| 9581 | |
| 9582 | if (Ops.size() != N0.getNumOperands()) |
| 9583 | return SDValue(); |
| 9584 | |
| 9585 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 9586 | N0.getValueType(), &Ops[0], Ops.size()); |
| 9587 | } |
| 9588 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9589 | SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0, |
| 9590 | SDValue N1, SDValue N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9591 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9592 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9593 | SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9594 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9595 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9596 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 9597 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 9598 | // the SELECT node, since we were called with a SELECT node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9599 | if (SCC.getNode()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9600 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 9601 | // Otherwise, just return whatever node we got back, like fabs. |
| 9602 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9603 | SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(), |
| 9604 | N0.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9605 | SCC.getOperand(0), SCC.getOperand(1), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9606 | SCC.getOperand(4)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9607 | AddToWorkList(SETCC.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9608 | return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(), |
| 9609 | SCC.getOperand(2), SCC.getOperand(3), SETCC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9610 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9611 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9612 | return SCC; |
| 9613 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9614 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 9615 | } |
| 9616 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 9617 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 9618 | /// are the two values being selected between, see if we can simplify the |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 9619 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 9620 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 9621 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 9622 | /// looked at. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9623 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9624 | SDValue RHS) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9625 | |
Nadav Rotem | f94fdb6 | 2011-02-11 19:57:47 +0000 | [diff] [blame] | 9626 | // Cannot simplify select with vector condition |
| 9627 | if (TheSelect->getOperand(0).getValueType().isVector()) return false; |
| 9628 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 9629 | // If this is a select from two identical things, try to pull the operation |
| 9630 | // through the select. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9631 | if (LHS.getOpcode() != RHS.getOpcode() || |
| 9632 | !LHS.hasOneUse() || !RHS.hasOneUse()) |
| 9633 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9634 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9635 | // If this is a load and the token chain is identical, replace the select |
| 9636 | // of two loads with a load through a select of the address to load from. |
| 9637 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 9638 | // constants have been dropped into the constant pool. |
| 9639 | if (LHS.getOpcode() == ISD::LOAD) { |
| 9640 | LoadSDNode *LLD = cast<LoadSDNode>(LHS); |
| 9641 | LoadSDNode *RLD = cast<LoadSDNode>(RHS); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9642 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9643 | // Token chains must be identical. |
| 9644 | if (LHS.getOperand(0) != RHS.getOperand(0) || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9645 | // Do not let this transformation reduce the number of volatile loads. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9646 | LLD->isVolatile() || RLD->isVolatile() || |
| 9647 | // If this is an EXTLOAD, the VT's must match. |
| 9648 | LLD->getMemoryVT() != RLD->getMemoryVT() || |
Duncan Sands | dcfd3a7 | 2010-11-18 20:05:18 +0000 | [diff] [blame] | 9649 | // If this is an EXTLOAD, the kind of extension must match. |
| 9650 | (LLD->getExtensionType() != RLD->getExtensionType() && |
| 9651 | // The only exception is if one of the extensions is anyext. |
| 9652 | LLD->getExtensionType() != ISD::EXTLOAD && |
| 9653 | RLD->getExtensionType() != ISD::EXTLOAD) || |
Dan Gohman | 75832d7 | 2009-10-31 14:14:04 +0000 | [diff] [blame] | 9654 | // FIXME: this discards src value information. This is |
| 9655 | // over-conservative. It would be beneficial to be able to remember |
Mon P Wang | fe240b1 | 2010-01-11 20:12:49 +0000 | [diff] [blame] | 9656 | // both potential memory locations. Since we are discarding |
| 9657 | // src value info, don't do the transformation if the memory |
| 9658 | // locations are not in the default address space. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9659 | LLD->getPointerInfo().getAddrSpace() != 0 || |
Pete Cooper | b0fde6d | 2013-02-12 03:14:50 +0000 | [diff] [blame] | 9660 | RLD->getPointerInfo().getAddrSpace() != 0 || |
| 9661 | !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(), |
| 9662 | LLD->getBasePtr().getValueType())) |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9663 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9664 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9665 | // Check that the select condition doesn't reach either load. If so, |
| 9666 | // folding this will induce a cycle into the DAG. If not, this is safe to |
| 9667 | // xform, so create a select of the addresses. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9668 | SDValue Addr; |
| 9669 | if (TheSelect->getOpcode() == ISD::SELECT) { |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9670 | SDNode *CondNode = TheSelect->getOperand(0).getNode(); |
| 9671 | if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || |
| 9672 | (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) |
| 9673 | return false; |
Nadav Rotem | 1c5bf3f | 2012-10-18 18:06:48 +0000 | [diff] [blame] | 9674 | // The loads must not depend on one another. |
| 9675 | if (LLD->isPredecessorOf(RLD) || |
| 9676 | RLD->isPredecessorOf(LLD)) |
| 9677 | return false; |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9678 | Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(), |
| 9679 | LLD->getBasePtr().getValueType(), |
| 9680 | TheSelect->getOperand(0), LLD->getBasePtr(), |
| 9681 | RLD->getBasePtr()); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9682 | } else { // Otherwise SELECT_CC |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9683 | SDNode *CondLHS = TheSelect->getOperand(0).getNode(); |
| 9684 | SDNode *CondRHS = TheSelect->getOperand(1).getNode(); |
| 9685 | |
| 9686 | if ((LLD->hasAnyUseOfValue(1) && |
| 9687 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || |
Chris Lattner | 77d9521 | 2012-03-27 16:27:21 +0000 | [diff] [blame] | 9688 | (RLD->hasAnyUseOfValue(1) && |
| 9689 | (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS)))) |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9690 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9691 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9692 | Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(), |
| 9693 | LLD->getBasePtr().getValueType(), |
| 9694 | TheSelect->getOperand(0), |
| 9695 | TheSelect->getOperand(1), |
| 9696 | LLD->getBasePtr(), RLD->getBasePtr(), |
| 9697 | TheSelect->getOperand(4)); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 9698 | } |
| 9699 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9700 | SDValue Load; |
| 9701 | if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { |
| 9702 | Load = DAG.getLoad(TheSelect->getValueType(0), |
| 9703 | TheSelect->getDebugLoc(), |
| 9704 | // FIXME: Discards pointer info. |
| 9705 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 9706 | LLD->isVolatile(), LLD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 9707 | LLD->isInvariant(), LLD->getAlignment()); |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9708 | } else { |
Duncan Sands | b9064bb | 2010-11-18 21:16:28 +0000 | [diff] [blame] | 9709 | Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ? |
| 9710 | RLD->getExtensionType() : LLD->getExtensionType(), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9711 | TheSelect->getDebugLoc(), |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 9712 | TheSelect->getValueType(0), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9713 | // FIXME: Discards pointer info. |
| 9714 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 9715 | LLD->getMemoryVT(), LLD->isVolatile(), |
| 9716 | LLD->isNonTemporal(), LLD->getAlignment()); |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 9717 | } |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 9718 | |
| 9719 | // Users of the select now use the result of the load. |
| 9720 | CombineTo(TheSelect, Load); |
| 9721 | |
| 9722 | // Users of the old loads now use the new load's chain. We know the |
| 9723 | // old-load value is dead now. |
| 9724 | CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 9725 | CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 9726 | return true; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 9727 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9728 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 9729 | return false; |
| 9730 | } |
| 9731 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9732 | /// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3 |
| 9733 | /// where 'cond' is the comparison specified by CC. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9734 | SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9735 | SDValue N2, SDValue N3, |
| 9736 | ISD::CondCode CC, bool NotExtCompare) { |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9737 | // (x ? y : y) -> y. |
| 9738 | if (N2 == N3) return N2; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9739 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9740 | EVT VT = N2.getValueType(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9741 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| 9742 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); |
| 9743 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9744 | |
| 9745 | // Determine if the condition we're dealing with is constant |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 9746 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 9747 | N0, N1, CC, DL, false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9748 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
| 9749 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9750 | |
| 9751 | // fold select_cc true, x, y -> x |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9752 | if (SCCC && !SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9753 | return N2; |
| 9754 | // fold select_cc false, x, y -> y |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9755 | if (SCCC && SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9756 | return N3; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9757 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9758 | // Check to see if we can simplify the select into an fabs node |
| 9759 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 9760 | // Allow either -0.0 or 0.0 |
Dale Johannesen | 87503a6 | 2007-08-25 22:10:57 +0000 | [diff] [blame] | 9761 | if (CFP->getValueAPF().isZero()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9762 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 9763 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 9764 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 9765 | N2 == N3.getOperand(0)) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9766 | return DAG.getNode(ISD::FABS, DL, VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9767 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9768 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 9769 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 9770 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 9771 | N2.getOperand(0) == N3) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9772 | return DAG.getNode(ISD::FABS, DL, VT, N3); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9773 | } |
| 9774 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9775 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9776 | // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" |
| 9777 | // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 |
| 9778 | // in it. This is a win when the constant is not otherwise available because |
| 9779 | // it replaces two constant pool loads with one. We only do this if the FP |
| 9780 | // type is known to be legal, because if it isn't, then we are before legalize |
| 9781 | // types an we want the other legalization to happen first (e.g. to avoid |
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 9782 | // messing with soft float) and if the ConstantFP is not legal, because if |
| 9783 | // it is legal, we may not need to store the FP constant in a constant pool. |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9784 | if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) |
| 9785 | if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { |
| 9786 | if (TLI.isTypeLegal(N2.getValueType()) && |
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 9787 | (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != |
| 9788 | TargetLowering::Legal) && |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9789 | // If both constants have multiple uses, then we won't need to do an |
| 9790 | // extra load, they are likely around in registers for other users. |
| 9791 | (TV->hasOneUse() || FV->hasOneUse())) { |
| 9792 | Constant *Elts[] = { |
| 9793 | const_cast<ConstantFP*>(FV->getConstantFPValue()), |
| 9794 | const_cast<ConstantFP*>(TV->getConstantFPValue()) |
| 9795 | }; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 9796 | Type *FPTy = Elts[0]->getType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 9797 | const DataLayout &TD = *TLI.getDataLayout(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9798 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9799 | // Create a ConstantArray of the two constants. |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 9800 | Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9801 | SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(), |
| 9802 | TD.getPrefTypeAlignment(FPTy)); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 9803 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9804 | |
| 9805 | // Get the offsets to the 0 and 1 element of the array so that we can |
| 9806 | // select between them. |
| 9807 | SDValue Zero = DAG.getIntPtrConstant(0); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 9808 | unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9809 | SDValue One = DAG.getIntPtrConstant(EltSize); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9810 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9811 | SDValue Cond = DAG.getSetCC(DL, |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 9812 | getSetCCResultType(N0.getValueType()), |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9813 | N0, N1, CC); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 9814 | AddToWorkList(Cond.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9815 | SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(), |
| 9816 | Cond, One, Zero); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 9817 | AddToWorkList(CstOffset.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9818 | CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx, |
| 9819 | CstOffset); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 9820 | AddToWorkList(CPIdx.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9821 | return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 9822 | MachinePointerInfo::getConstantPool(), false, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 9823 | false, false, Alignment); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 9824 | |
| 9825 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9826 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9827 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9828 | // Check to see if we can perform the "gzip trick", transforming |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9829 | // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A) |
Chris Lattner | e3152e5 | 2006-09-20 06:41:35 +0000 | [diff] [blame] | 9830 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9831 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
| 9832 | (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9833 | EVT XType = N0.getValueType(); |
| 9834 | EVT AType = N2.getValueType(); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 9835 | if (XType.bitsGE(AType)) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 9836 | // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 9837 | // single-bit constant. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9838 | if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) { |
| 9839 | unsigned ShCtV = N2C->getAPIntValue().logBase2(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9840 | ShCtV = XType.getSizeInBits()-ShCtV-1; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9841 | SDValue ShCt = DAG.getConstant(ShCtV, |
| 9842 | getShiftAmountTy(N0.getValueType())); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9843 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9844 | XType, N0, ShCt); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9845 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9846 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 9847 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9848 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9849 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9850 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9851 | |
| 9852 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9853 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9854 | |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9855 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9856 | XType, N0, |
| 9857 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9858 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9859 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9860 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 9861 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9862 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9863 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9864 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9865 | |
| 9866 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9867 | } |
| 9868 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9869 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9870 | // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) |
| 9871 | // where y is has a single bit set. |
| 9872 | // A plaintext description would be, we can turn the SELECT_CC into an AND |
| 9873 | // when the condition can be materialized as an all-ones register. Any |
| 9874 | // single bit-test can be materialized as an all-ones register with |
| 9875 | // shift-left and shift-right-arith. |
| 9876 | if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && |
| 9877 | N0->getValueType(0) == VT && |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9878 | N1C && N1C->isNullValue() && |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9879 | N2C && N2C->isNullValue()) { |
| 9880 | SDValue AndLHS = N0->getOperand(0); |
| 9881 | ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); |
| 9882 | if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { |
| 9883 | // Shift the tested bit over the sign bit. |
| 9884 | APInt AndMask = ConstAndRHS->getAPIntValue(); |
| 9885 | SDValue ShlAmt = |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9886 | DAG.getConstant(AndMask.countLeadingZeros(), |
| 9887 | getShiftAmountTy(AndLHS.getValueType())); |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9888 | SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9889 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9890 | // Now arithmetic right shift it all the way over, so the result is either |
| 9891 | // all-ones, or zero. |
| 9892 | SDValue ShrAmt = |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9893 | DAG.getConstant(AndMask.getBitWidth()-1, |
| 9894 | getShiftAmountTy(Shl.getValueType())); |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9895 | SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9896 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 9897 | return DAG.getNode(ISD::AND, DL, VT, Shr, N3); |
| 9898 | } |
| 9899 | } |
| 9900 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 9901 | // fold select C, 16, 0 -> shl C, 4 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9902 | if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() && |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 9903 | TLI.getBooleanContents(N0.getValueType().isVector()) == |
| 9904 | TargetLowering::ZeroOrOneBooleanContent) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9905 | |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 9906 | // If the caller doesn't want us to simplify this into a zext of a compare, |
| 9907 | // don't do it. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9908 | if (NotExtCompare && N2C->getAPIntValue() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9909 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9910 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 9911 | // Get a SetCC of the condition |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 9912 | // NOTE: Don't create a SETCC if it's not legal on this target. |
| 9913 | if (!LegalOperations || |
| 9914 | TLI.isOperationLegal(ISD::SETCC, |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 9915 | LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) { |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 9916 | SDValue Temp, SCC; |
| 9917 | // cast from setcc result type to select result type |
| 9918 | if (LegalTypes) { |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 9919 | SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()), |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 9920 | N0, N1, CC); |
| 9921 | if (N2.getValueType().bitsLT(SCC.getValueType())) |
| 9922 | Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), |
| 9923 | N2.getValueType()); |
| 9924 | else |
| 9925 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
| 9926 | N2.getValueType(), SCC); |
| 9927 | } else { |
| 9928 | SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9929 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9930 | N2.getValueType(), SCC); |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 9931 | } |
| 9932 | |
| 9933 | AddToWorkList(SCC.getNode()); |
| 9934 | AddToWorkList(Temp.getNode()); |
| 9935 | |
| 9936 | if (N2C->getAPIntValue() == 1) |
| 9937 | return Temp; |
| 9938 | |
| 9939 | // shl setcc result by log2 n2c |
| 9940 | return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp, |
| 9941 | DAG.getConstant(N2C->getAPIntValue().logBase2(), |
| 9942 | getShiftAmountTy(Temp.getValueType()))); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 9943 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 9944 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9945 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9946 | // Check to see if this is the equivalent of setcc |
| 9947 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 9948 | // otherwise, go ahead with the folds. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 9949 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9950 | EVT XType = N0.getValueType(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9951 | if (!LegalOperations || |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame^] | 9952 | TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) { |
| 9953 | SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9954 | if (Res.getValueType() != VT) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9955 | Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9956 | return Res; |
| 9957 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9958 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9959 | // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X)))) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9960 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9961 | (!LegalOperations || |
Duncan Sands | 184a876 | 2008-06-14 17:48:34 +0000 | [diff] [blame] | 9962 | TLI.isOperationLegal(ISD::CTLZ, XType))) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9963 | SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9964 | return DAG.getNode(ISD::SRL, DL, XType, Ctlz, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9965 | DAG.getConstant(Log2_32(XType.getSizeInBits()), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9966 | getShiftAmountTy(Ctlz.getValueType()))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9967 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9968 | // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9969 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9970 | SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(), |
| 9971 | XType, DAG.getConstant(0, XType), N0); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 9972 | SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9973 | return DAG.getNode(ISD::SRL, DL, XType, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 9974 | DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0), |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9975 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9976 | getShiftAmountTy(XType))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9977 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9978 | // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1)) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9979 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 9980 | SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9981 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9982 | getShiftAmountTy(N0.getValueType()))); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 9983 | return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType)); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9984 | } |
| 9985 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9986 | |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 9987 | // Check to see if this is an integer abs. |
| 9988 | // select_cc setg[te] X, 0, X, -X -> |
| 9989 | // select_cc setgt X, -1, X, -X -> |
| 9990 | // select_cc setl[te] X, 0, -X, X -> |
| 9991 | // select_cc setlt X, 1, -X, X -> |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 9992 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 9993 | if (N1C) { |
| 9994 | ConstantSDNode *SubC = NULL; |
| 9995 | if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 9996 | (N1C->isAllOnesValue() && CC == ISD::SETGT)) && |
| 9997 | N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) |
| 9998 | SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); |
| 9999 | else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || |
| 10000 | (N1C->isOne() && CC == ISD::SETLT)) && |
| 10001 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) |
| 10002 | SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); |
| 10003 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10004 | EVT XType = N0.getValueType(); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10005 | if (SubC && SubC->isNullValue() && XType.isInteger()) { |
| 10006 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, |
| 10007 | N0, |
| 10008 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10009 | getShiftAmountTy(N0.getValueType()))); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10010 | SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), |
| 10011 | XType, N0, Shift); |
| 10012 | AddToWorkList(Shift.getNode()); |
| 10013 | AddToWorkList(Add.getNode()); |
| 10014 | return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10015 | } |
| 10016 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10017 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10018 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 10019 | } |
| 10020 | |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 10021 | /// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10022 | SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10023 | SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 10024 | DebugLoc DL, bool foldBooleans) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10025 | TargetLowering::DAGCombinerInfo |
Nadav Rotem | 444b4bf | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 10026 | DagCombineInfo(DAG, Level, false, this); |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 10027 | return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 10028 | } |
| 10029 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10030 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 10031 | /// return a DAG expression to select that will generate the same value by |
| 10032 | /// multiplying by a magic number. See: |
| 10033 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10034 | SDValue DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10035 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 10036 | SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10037 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10038 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10039 | ii != ee; ++ii) |
| 10040 | AddToWorkList(*ii); |
| 10041 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10042 | } |
| 10043 | |
| 10044 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 10045 | /// return a DAG expression to select that will generate the same value by |
| 10046 | /// multiplying by a magic number. See: |
| 10047 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10048 | SDValue DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10049 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 10050 | SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10051 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10052 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10053 | ii != ee; ++ii) |
| 10054 | AddToWorkList(*ii); |
| 10055 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10056 | } |
| 10057 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10058 | /// FindBaseOffset - Return true if base is a frame index, which is known not |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 10059 | // to alias with anything but itself. Provides base object and offset as |
| 10060 | // results. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10061 | static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, |
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10062 | const GlobalValue *&GV, const void *&CV) { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10063 | // Assume it is a primitive operation. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10064 | Base = Ptr; Offset = 0; GV = 0; CV = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10065 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10066 | // If it's an adding a simple constant then integrate the offset. |
| 10067 | if (Base.getOpcode() == ISD::ADD) { |
| 10068 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { |
| 10069 | Base = Base.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 10070 | Offset += C->getZExtValue(); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10071 | } |
| 10072 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10073 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10074 | // Return the underlying GlobalValue, and update the Offset. Return false |
| 10075 | // for GlobalAddressSDNode since the same GlobalAddress may be represented |
| 10076 | // by multiple nodes with different offsets. |
| 10077 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { |
| 10078 | GV = G->getGlobal(); |
| 10079 | Offset += G->getOffset(); |
| 10080 | return false; |
| 10081 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10082 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10083 | // Return the underlying Constant value, and update the Offset. Return false |
| 10084 | // for ConstantSDNodes since the same constant pool entry may be represented |
| 10085 | // by multiple nodes with different offsets. |
| 10086 | if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { |
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10087 | CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal() |
| 10088 | : (const void *)C->getConstVal(); |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10089 | Offset += C->getOffset(); |
| 10090 | return false; |
| 10091 | } |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10092 | // If it's any of the following then it can't alias with anything but itself. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10093 | return isa<FrameIndexSDNode>(Base); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10094 | } |
| 10095 | |
| 10096 | /// isAlias - Return true if there is any possibility that the two addresses |
| 10097 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10098 | bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10099 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10100 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10101 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10102 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10103 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10104 | unsigned SrcValueAlign2, |
| 10105 | const MDNode *TBAAInfo2) const { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10106 | // If they are the same then they must be aliases. |
| 10107 | if (Ptr1 == Ptr2) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10108 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10109 | // Gather base node and offset information. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10110 | SDValue Base1, Base2; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10111 | int64_t Offset1, Offset2; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 10112 | const GlobalValue *GV1, *GV2; |
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10113 | const void *CV1, *CV2; |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10114 | bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1); |
| 10115 | bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10116 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10117 | // If they have a same base address then check to see if they overlap. |
| 10118 | if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10119 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10120 | |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 10121 | // It is possible for different frame indices to alias each other, mostly |
| 10122 | // when tail call optimization reuses return address slots for arguments. |
| 10123 | // To catch this case, look up the actual index of frame indices to compute |
| 10124 | // the real alias relationship. |
| 10125 | if (isFrameIndex1 && isFrameIndex2) { |
| 10126 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 10127 | Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); |
| 10128 | Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); |
| 10129 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
| 10130 | } |
| 10131 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10132 | // Otherwise, if we know what the bases are, and they aren't identical, then |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 10133 | // we know they cannot alias. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10134 | if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) |
| 10135 | return false; |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10136 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10137 | // If we know required SrcValue1 and SrcValue2 have relatively large alignment |
| 10138 | // compared to the size and offset of the access, we may be able to prove they |
| 10139 | // do not alias. This check is conservative for now to catch cases created by |
| 10140 | // splitting vector types. |
| 10141 | if ((SrcValueAlign1 == SrcValueAlign2) && |
| 10142 | (SrcValueOffset1 != SrcValueOffset2) && |
| 10143 | (Size1 == Size2) && (SrcValueAlign1 > Size1)) { |
| 10144 | int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1; |
| 10145 | int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10146 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10147 | // There is no overlap between these relatively aligned accesses of similar |
| 10148 | // size, return no alias. |
| 10149 | if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1) |
| 10150 | return false; |
| 10151 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10152 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 10153 | if (CombinerGlobalAA) { |
| 10154 | // Use alias analysis information. |
Dan Gohman | e9c8fa0 | 2007-08-27 16:32:11 +0000 | [diff] [blame] | 10155 | int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2); |
| 10156 | int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset; |
| 10157 | int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10158 | AliasAnalysis::AliasResult AAResult = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10159 | AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1), |
| 10160 | AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2)); |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 10161 | if (AAResult == AliasAnalysis::NoAlias) |
| 10162 | return false; |
| 10163 | } |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10164 | |
| 10165 | // Otherwise we have to assume they alias. |
| 10166 | return true; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10167 | } |
| 10168 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 10169 | bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) { |
| 10170 | SDValue Ptr0, Ptr1; |
| 10171 | int64_t Size0, Size1; |
| 10172 | const Value *SrcValue0, *SrcValue1; |
| 10173 | int SrcValueOffset0, SrcValueOffset1; |
| 10174 | unsigned SrcValueAlign0, SrcValueAlign1; |
| 10175 | const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1; |
| 10176 | FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0, |
| 10177 | SrcValueAlign0, SrcTBAAInfo0); |
| 10178 | FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1, |
| 10179 | SrcValueAlign1, SrcTBAAInfo1); |
| 10180 | return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0, |
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 10181 | SrcValueAlign0, SrcTBAAInfo0, |
| 10182 | Ptr1, Size1, SrcValue1, SrcValueOffset1, |
| 10183 | SrcValueAlign1, SrcTBAAInfo1); |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 10184 | } |
| 10185 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10186 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 10187 | /// node. Returns true if the operand was a load. |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 10188 | bool DAGCombiner::FindAliasInfo(SDNode *N, |
Benjamin Kramer | ae4746b | 2012-01-15 11:50:43 +0000 | [diff] [blame] | 10189 | SDValue &Ptr, int64_t &Size, |
| 10190 | const Value *&SrcValue, |
| 10191 | int &SrcValueOffset, |
| 10192 | unsigned &SrcValueAlign, |
| 10193 | const MDNode *&TBAAInfo) const { |
| 10194 | LSBaseSDNode *LS = cast<LSBaseSDNode>(N); |
| 10195 | |
| 10196 | Ptr = LS->getBasePtr(); |
| 10197 | Size = LS->getMemoryVT().getSizeInBits() >> 3; |
| 10198 | SrcValue = LS->getSrcValue(); |
| 10199 | SrcValueOffset = LS->getSrcValueOffset(); |
| 10200 | SrcValueAlign = LS->getOriginalAlignment(); |
| 10201 | TBAAInfo = LS->getTBAAInfo(); |
| 10202 | return isa<LoadSDNode>(LS); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10203 | } |
| 10204 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10205 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 10206 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10207 | void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 10208 | SmallVector<SDValue, 8> &Aliases) { |
| 10209 | SmallVector<SDValue, 8> Chains; // List of chains to visit. |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10210 | SmallPtrSet<SDNode *, 16> Visited; // Visited node set. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10211 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10212 | // Get alias information for node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10213 | SDValue Ptr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10214 | int64_t Size; |
| 10215 | const Value *SrcValue; |
| 10216 | int SrcValueOffset; |
| 10217 | unsigned SrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10218 | const MDNode *SrcTBAAInfo; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10219 | bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10220 | SrcValueAlign, SrcTBAAInfo); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10221 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10222 | // Starting off. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10223 | Chains.push_back(OriginalChain); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10224 | unsigned Depth = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10225 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10226 | // Look at each chain and determine if it is an alias. If so, add it to the |
| 10227 | // aliases list. If not, then continue up the chain looking for the next |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10228 | // candidate. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10229 | while (!Chains.empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10230 | SDValue Chain = Chains.back(); |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10231 | Chains.pop_back(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10232 | |
| 10233 | // For TokenFactor nodes, look at each operand and only continue up the |
| 10234 | // chain until we find two aliases. If we've seen two aliases, assume we'll |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10235 | // find more and revert to original chain since the xform is unlikely to be |
| 10236 | // profitable. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10237 | // |
| 10238 | // FIXME: The depth check could be made to return the last non-aliasing |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10239 | // chain we found before we hit a tokenfactor rather than the original |
| 10240 | // chain. |
| 10241 | if (Depth > 6 || Aliases.size() == 2) { |
| 10242 | Aliases.clear(); |
| 10243 | Aliases.push_back(OriginalChain); |
| 10244 | break; |
| 10245 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10246 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10247 | // Don't bother if we've been before. |
| 10248 | if (!Visited.insert(Chain.getNode())) |
| 10249 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10250 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10251 | switch (Chain.getOpcode()) { |
| 10252 | case ISD::EntryToken: |
| 10253 | // Entry token is ideal chain operand, but handled in FindBetterChain. |
| 10254 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10255 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10256 | case ISD::LOAD: |
| 10257 | case ISD::STORE: { |
| 10258 | // Get alias information for Chain. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10259 | SDValue OpPtr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10260 | int64_t OpSize; |
| 10261 | const Value *OpSrcValue; |
| 10262 | int OpSrcValueOffset; |
| 10263 | unsigned OpSrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10264 | const MDNode *OpSrcTBAAInfo; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10265 | bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10266 | OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10267 | OpSrcValueAlign, |
| 10268 | OpSrcTBAAInfo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10269 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10270 | // If chain is alias then stop here. |
| 10271 | if (!(IsLoad && IsOpLoad) && |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10272 | isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10273 | SrcTBAAInfo, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10274 | OpPtr, OpSize, OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10275 | OpSrcValueAlign, OpSrcTBAAInfo)) { |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10276 | Aliases.push_back(Chain); |
| 10277 | } else { |
| 10278 | // Look further up the chain. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10279 | Chains.push_back(Chain.getOperand(0)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10280 | ++Depth; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10281 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10282 | break; |
| 10283 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10284 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10285 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10286 | // We have to check each of the operands of the token factor for "small" |
| 10287 | // token factors, so we queue them up. Adding the operands to the queue |
| 10288 | // (stack) in reverse order maintains the original order and increases the |
| 10289 | // likelihood that getNode will find a matching token factor (CSE.) |
| 10290 | if (Chain.getNumOperands() > 16) { |
| 10291 | Aliases.push_back(Chain); |
| 10292 | break; |
| 10293 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10294 | for (unsigned n = Chain.getNumOperands(); n;) |
| 10295 | Chains.push_back(Chain.getOperand(--n)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10296 | ++Depth; |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10297 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10298 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10299 | default: |
| 10300 | // For all other instructions we will just have to take what we can get. |
| 10301 | Aliases.push_back(Chain); |
| 10302 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10303 | } |
| 10304 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10305 | } |
| 10306 | |
| 10307 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking |
| 10308 | /// for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10309 | SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { |
| 10310 | SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10311 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10312 | // Accumulate all the aliases to this node. |
| 10313 | GatherAllAliases(N, OldChain, Aliases); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10314 | |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 10315 | // If no operands then chain to entry token. |
| 10316 | if (Aliases.size() == 0) |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10317 | return DAG.getEntryNode(); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 10318 | |
| 10319 | // If a single operand then chain to it. We don't need to revisit it. |
| 10320 | if (Aliases.size() == 1) |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10321 | return Aliases[0]; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10322 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10323 | // Construct a custom tailored token factor. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10324 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10325 | &Aliases[0], Aliases.size()); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10326 | } |
| 10327 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 10328 | // SelectionDAG::Combine - This is the entry point for the file. |
| 10329 | // |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 10330 | void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 10331 | CodeGenOpt::Level OptLevel) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 10332 | /// run - This is the main entry point to this class. |
| 10333 | /// |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 10334 | DAGCombiner(*this, AA, OptLevel).Run(Level); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 10335 | } |