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 | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Chris Lattner | 00161a6 | 2008-01-25 07:20:16 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetLowering.h" |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 40 | STATISTIC(NodesCombined , "Number of dag nodes combined"); |
| 41 | STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created"); |
| 42 | STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created"); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 43 | STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 44 | STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 45 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 46 | namespace { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
Owen Anderson | 0dcc814 | 2010-09-19 21:01:26 +0000 | [diff] [blame] | 48 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 49 | cl::desc("Turn on alias analysis during testing")); |
Jim Laskey | 3ad175b | 2006-10-12 15:22:24 +0000 | [diff] [blame] | 50 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
| 52 | CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, |
| 53 | cl::desc("Include global information in alias analysis")); |
| 54 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 55 | //------------------------------ DAGCombiner ---------------------------------// |
| 56 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 57 | class DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 58 | SelectionDAG &DAG; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 59 | const TargetLowering &TLI; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 60 | CombineLevel Level; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 61 | CodeGenOpt::Level OptLevel; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 62 | bool LegalOperations; |
| 63 | bool LegalTypes; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 64 | |
| 65 | // Worklist of all of the nodes that need to be simplified. |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 66 | // |
| 67 | // This has the semantics that when adding to the worklist, |
| 68 | // the item added must be next to be processed. It should |
| 69 | // also only appear once. The naive approach to this takes |
| 70 | // linear time. |
| 71 | // |
| 72 | // To reduce the insert/remove time to logarithmic, we use |
| 73 | // a set and a vector to maintain our worklist. |
| 74 | // |
| 75 | // The set contains the items on the worklist, but does not |
| 76 | // maintain the order they should be visited. |
| 77 | // |
| 78 | // The vector maintains the order nodes should be visited, but may |
| 79 | // contain duplicate or removed nodes. When choosing a node to |
| 80 | // visit, we pop off the order stack until we find an item that is |
| 81 | // also in the contents set. All operations are O(log N). |
| 82 | SmallPtrSet<SDNode*, 64> WorkListContents; |
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 83 | SmallVector<SDNode*, 64> WorkListOrder; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 84 | |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 85 | // AA - Used for DAG load/store alias analysis. |
| 86 | AliasAnalysis &AA; |
| 87 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 88 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 89 | /// the instruction to the work lists because they might get more simplified |
| 90 | /// now. |
| 91 | /// |
| 92 | void AddUsersToWorkList(SDNode *N) { |
| 93 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 94 | UI != UE; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 95 | AddToWorkList(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 98 | /// visit - call the node-specific routine that knows how to fold each |
| 99 | /// particular type of node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 100 | SDValue visit(SDNode *N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 102 | public: |
James Molloy | 6afa3f7 | 2012-02-16 09:48:07 +0000 | [diff] [blame] | 103 | /// 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] | 104 | /// back (next to be processed.) |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 105 | void AddToWorkList(SDNode *N) { |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 106 | WorkListContents.insert(N); |
| 107 | WorkListOrder.push_back(N); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 108 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 109 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 110 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 111 | /// |
| 112 | void removeFromWorkList(SDNode *N) { |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 113 | WorkListContents.erase(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 114 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 115 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 116 | SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 117 | bool AddTo = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 118 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 119 | SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 120 | return CombineTo(N, &Res, 1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 121 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 122 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 123 | SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 124 | bool AddTo = true) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 125 | SDValue To[] = { Res0, Res1 }; |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 126 | return CombineTo(N, To, 2, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 127 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 128 | |
| 129 | void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 133 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 134 | /// 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] | 135 | /// propagation. If so, return true. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 136 | bool SimplifyDemandedBits(SDValue Op) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 137 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
| 138 | APInt Demanded = APInt::getAllOnesValue(BitWidth); |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 139 | return SimplifyDemandedBits(Op, Demanded); |
| 140 | } |
| 141 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 142 | bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 144 | bool CombineToPreIndexedLoadStore(SDNode *N); |
| 145 | bool CombineToPostIndexedLoadStore(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 146 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 147 | void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad); |
| 148 | SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace); |
| 149 | SDValue SExtPromoteOperand(SDValue Op, EVT PVT); |
| 150 | SDValue ZExtPromoteOperand(SDValue Op, EVT PVT); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 151 | SDValue PromoteIntBinOp(SDValue Op); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 152 | SDValue PromoteIntShiftOp(SDValue Op); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 153 | SDValue PromoteExtend(SDValue Op); |
| 154 | bool PromoteLoad(SDValue Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 155 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 156 | void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs, |
| 157 | SDValue Trunc, SDValue ExtLoad, DebugLoc DL, |
| 158 | ISD::NodeType ExtType); |
| 159 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 160 | /// combine - call the node-specific routine that knows how to fold each |
| 161 | /// particular type of node. If that doesn't do anything, try the |
| 162 | /// target-specific DAG combines. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 163 | SDValue combine(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 164 | |
| 165 | // Visitation implementation - Implement dag node combining for different |
| 166 | // node types. The semantics are as follows: |
| 167 | // Return Value: |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 168 | // SDValue.getNode() == 0 - No change was made |
| 169 | // SDValue.getNode() == N - N was replaced, is dead and has been handled. |
| 170 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 171 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 172 | SDValue visitTokenFactor(SDNode *N); |
| 173 | SDValue visitMERGE_VALUES(SDNode *N); |
| 174 | SDValue visitADD(SDNode *N); |
| 175 | SDValue visitSUB(SDNode *N); |
| 176 | SDValue visitADDC(SDNode *N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 177 | SDValue visitSUBC(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 178 | SDValue visitADDE(SDNode *N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 179 | SDValue visitSUBE(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 180 | SDValue visitMUL(SDNode *N); |
| 181 | SDValue visitSDIV(SDNode *N); |
| 182 | SDValue visitUDIV(SDNode *N); |
| 183 | SDValue visitSREM(SDNode *N); |
| 184 | SDValue visitUREM(SDNode *N); |
| 185 | SDValue visitMULHU(SDNode *N); |
| 186 | SDValue visitMULHS(SDNode *N); |
| 187 | SDValue visitSMUL_LOHI(SDNode *N); |
| 188 | SDValue visitUMUL_LOHI(SDNode *N); |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 189 | SDValue visitSMULO(SDNode *N); |
| 190 | SDValue visitUMULO(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 191 | SDValue visitSDIVREM(SDNode *N); |
| 192 | SDValue visitUDIVREM(SDNode *N); |
| 193 | SDValue visitAND(SDNode *N); |
| 194 | SDValue visitOR(SDNode *N); |
| 195 | SDValue visitXOR(SDNode *N); |
| 196 | SDValue SimplifyVBinOp(SDNode *N); |
| 197 | SDValue visitSHL(SDNode *N); |
| 198 | SDValue visitSRA(SDNode *N); |
| 199 | SDValue visitSRL(SDNode *N); |
| 200 | SDValue visitCTLZ(SDNode *N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 201 | SDValue visitCTLZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 202 | SDValue visitCTTZ(SDNode *N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 203 | SDValue visitCTTZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 204 | SDValue visitCTPOP(SDNode *N); |
| 205 | SDValue visitSELECT(SDNode *N); |
| 206 | SDValue visitSELECT_CC(SDNode *N); |
| 207 | SDValue visitSETCC(SDNode *N); |
| 208 | SDValue visitSIGN_EXTEND(SDNode *N); |
| 209 | SDValue visitZERO_EXTEND(SDNode *N); |
| 210 | SDValue visitANY_EXTEND(SDNode *N); |
| 211 | SDValue visitSIGN_EXTEND_INREG(SDNode *N); |
| 212 | SDValue visitTRUNCATE(SDNode *N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 213 | SDValue visitBITCAST(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 214 | SDValue visitBUILD_PAIR(SDNode *N); |
| 215 | SDValue visitFADD(SDNode *N); |
| 216 | SDValue visitFSUB(SDNode *N); |
| 217 | SDValue visitFMUL(SDNode *N); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 218 | SDValue visitFMA(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 219 | SDValue visitFDIV(SDNode *N); |
| 220 | SDValue visitFREM(SDNode *N); |
| 221 | SDValue visitFCOPYSIGN(SDNode *N); |
| 222 | SDValue visitSINT_TO_FP(SDNode *N); |
| 223 | SDValue visitUINT_TO_FP(SDNode *N); |
| 224 | SDValue visitFP_TO_SINT(SDNode *N); |
| 225 | SDValue visitFP_TO_UINT(SDNode *N); |
| 226 | SDValue visitFP_ROUND(SDNode *N); |
| 227 | SDValue visitFP_ROUND_INREG(SDNode *N); |
| 228 | SDValue visitFP_EXTEND(SDNode *N); |
| 229 | SDValue visitFNEG(SDNode *N); |
| 230 | SDValue visitFABS(SDNode *N); |
| 231 | SDValue visitBRCOND(SDNode *N); |
| 232 | SDValue visitBR_CC(SDNode *N); |
| 233 | SDValue visitLOAD(SDNode *N); |
| 234 | SDValue visitSTORE(SDNode *N); |
| 235 | SDValue visitINSERT_VECTOR_ELT(SDNode *N); |
| 236 | SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); |
| 237 | SDValue visitBUILD_VECTOR(SDNode *N); |
| 238 | SDValue visitCONCAT_VECTORS(SDNode *N); |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 239 | SDValue visitEXTRACT_SUBVECTOR(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 240 | SDValue visitVECTOR_SHUFFLE(SDNode *N); |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 241 | SDValue visitMEMBARRIER(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 242 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 243 | SDValue XformToShuffleWithZero(SDNode *N); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 244 | SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 245 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 246 | SDValue visitShiftByConstant(SDNode *N, unsigned Amt); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 247 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 248 | bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); |
| 249 | SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 250 | SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 251 | SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2, |
| 252 | SDValue N3, ISD::CondCode CC, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 253 | bool NotExtCompare = false); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 254 | SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 255 | DebugLoc DL, bool foldBooleans = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 256 | SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 257 | unsigned HiOp); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 258 | SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 259 | SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 260 | SDValue BuildSDIV(SDNode *N); |
| 261 | SDValue BuildUDIV(SDNode *N); |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 262 | SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 263 | bool DemandHighBits = true); |
| 264 | SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 265 | SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 266 | SDValue ReduceLoadWidth(SDNode *N); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 267 | SDValue ReduceLoadOpStoreWidth(SDNode *N); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 268 | SDValue TransformFPLoadStorePair(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 269 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 270 | SDValue GetDemandedBits(SDValue V, const APInt &Mask); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 271 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 272 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 273 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 274 | void GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 275 | SmallVector<SDValue, 8> &Aliases); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 276 | |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 277 | /// isAlias - Return true if there is any possibility that the two addresses |
| 278 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 279 | bool isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 280 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 281 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 282 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 283 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 284 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 285 | unsigned SrcValueAlign2, |
| 286 | const MDNode *TBAAInfo2) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 287 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 288 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 289 | /// node. Returns true if the operand was a load. |
| 290 | bool FindAliasInfo(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 291 | SDValue &Ptr, int64_t &Size, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 292 | const Value *&SrcValue, int &SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 293 | unsigned &SrcValueAlignment, |
| 294 | const MDNode *&TBAAInfo) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 295 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 296 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 297 | /// looking for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 298 | SDValue FindBetterChain(SDNode *N, SDValue Chain); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 300 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 301 | DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 302 | : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 303 | OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 304 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 305 | /// Run - runs the dag combiner on all nodes in the work list |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 306 | void Run(CombineLevel AtLevel); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 308 | SelectionDAG &getDAG() const { return DAG; } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 310 | /// getShiftAmountTy - Returns a type large enough to hold any valid |
| 311 | /// shift amount - before type legalization these can be huge. |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 312 | EVT getShiftAmountTy(EVT LHSTy) { |
| 313 | return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy(); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 314 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 316 | /// isTypeLegal - This method returns true if we are running before type |
| 317 | /// legalization or if the specified VT is legal. |
| 318 | bool isTypeLegal(const EVT &VT) { |
| 319 | if (!LegalTypes) return true; |
| 320 | return TLI.isTypeLegal(VT); |
| 321 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 322 | }; |
| 323 | } |
| 324 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 325 | |
| 326 | namespace { |
| 327 | /// WorkListRemover - This class is a DAGUpdateListener that removes any deleted |
| 328 | /// nodes from the worklist. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 329 | class WorkListRemover : public SelectionDAG::DAGUpdateListener { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 330 | DAGCombiner &DC; |
| 331 | public: |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 332 | explicit WorkListRemover(DAGCombiner &dc) |
| 333 | : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 334 | |
Duncan Sands | edfcf59 | 2008-06-11 11:42:12 +0000 | [diff] [blame] | 335 | virtual void NodeDeleted(SDNode *N, SDNode *E) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 336 | DC.removeFromWorkList(N); |
| 337 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 338 | }; |
| 339 | } |
| 340 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 341 | //===----------------------------------------------------------------------===// |
| 342 | // TargetLowering::DAGCombinerInfo implementation |
| 343 | //===----------------------------------------------------------------------===// |
| 344 | |
| 345 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 346 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 347 | } |
| 348 | |
Cameron Zwarich | ed3caf9 | 2011-04-02 02:40:26 +0000 | [diff] [blame] | 349 | void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) { |
| 350 | ((DAGCombiner*)DC)->removeFromWorkList(N); |
| 351 | } |
| 352 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 353 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 354 | CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) { |
| 355 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 358 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 359 | CombineTo(SDNode *N, SDValue Res, bool AddTo) { |
| 360 | return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 364 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 365 | CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { |
| 366 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 369 | void TargetLowering::DAGCombinerInfo:: |
| 370 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
| 371 | return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); |
| 372 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 375 | // Helper Functions |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | /// isNegatibleForFree - Return 1 if we can compute the negated form of the |
| 379 | /// specified expression for the same cost as the expression itself, or 2 if we |
| 380 | /// can compute the negated form more cheaply than the expression itself. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 381 | static char isNegatibleForFree(SDValue Op, bool LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 382 | const TargetLowering &TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 383 | const TargetOptions *Options, |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 384 | unsigned Depth = 0) { |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 385 | // No compile time optimizations on this type. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 386 | if (Op.getValueType() == MVT::ppcf128) |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 387 | return 0; |
| 388 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 389 | // fneg is removable even if it has multiple uses. |
| 390 | if (Op.getOpcode() == ISD::FNEG) return 2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 392 | // Don't allow anything with multiple uses. |
| 393 | if (!Op.hasOneUse()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 395 | // Don't recurse exponentially. |
| 396 | if (Depth > 6) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 398 | switch (Op.getOpcode()) { |
| 399 | default: return false; |
| 400 | case ISD::ConstantFP: |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 401 | // Don't invert constant FP values after legalize. The negated constant |
| 402 | // isn't necessarily legal. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 403 | return LegalOperations ? 0 : 1; |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 404 | case ISD::FADD: |
| 405 | // FIXME: determine better conditions for this xform. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 406 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 407 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 408 | // After operation legalization, it might not be legal to create new FSUBs. |
| 409 | if (LegalOperations && |
| 410 | !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) |
| 411 | return 0; |
| 412 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 413 | // fold (fsub (fadd A, B)) -> (fsub (fneg A), B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 414 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
| 415 | Options, Depth + 1)) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 416 | return V; |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 417 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 418 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 419 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 420 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 421 | // 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] | 422 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 423 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 424 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 425 | return 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 427 | case ISD::FMUL: |
| 428 | case ISD::FDIV: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 429 | if (Options->HonorSignDependentRoundingFPMath()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 430 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 431 | // 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] | 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; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 435 | |
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); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 439 | case ISD::FP_EXTEND: |
| 440 | case ISD::FP_ROUND: |
| 441 | case ISD::FSIN: |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 442 | return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 443 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | /// GetNegatedExpression - If isNegatibleForFree returns true, this function |
| 448 | /// returns the newly negated expression. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 449 | static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 450 | bool LegalOperations, unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 451 | // fneg is removable even if it has multiple uses. |
| 452 | if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 454 | // Don't allow anything with multiple uses. |
| 455 | assert(Op.hasOneUse() && "Unknown reuse!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 457 | assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 458 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 459 | default: llvm_unreachable("Unknown code"); |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 460 | case ISD::ConstantFP: { |
| 461 | APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); |
| 462 | V.changeSign(); |
| 463 | return DAG.getConstantFP(V, Op.getValueType()); |
| 464 | } |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 465 | case ISD::FADD: |
| 466 | // FIXME: determine better conditions for this xform. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 467 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 468 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 469 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 470 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 471 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 472 | &DAG.getTarget().Options, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 473 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 474 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 475 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 476 | Op.getOperand(1)); |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 477 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 478 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 479 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 480 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 481 | Op.getOperand(0)); |
| 482 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 483 | // 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] | 484 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 485 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 486 | // fold (fneg (fsub 0, B)) -> B |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 487 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 488 | if (N0CFP->getValueAPF().isZero()) |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 489 | return Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 490 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 491 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 492 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
| 493 | Op.getOperand(1), Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 495 | case ISD::FMUL: |
| 496 | case ISD::FDIV: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 497 | assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 498 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 499 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 500 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 501 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 502 | &DAG.getTarget().Options, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 503 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 504 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 505 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 506 | Op.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 507 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 508 | // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 509 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 510 | Op.getOperand(0), |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 511 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 512 | LegalOperations, Depth+1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 514 | case ISD::FP_EXTEND: |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 515 | case ISD::FSIN: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 516 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 517 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 518 | LegalOperations, Depth+1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 519 | case ISD::FP_ROUND: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 520 | return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 521 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 522 | LegalOperations, Depth+1), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 523 | Op.getOperand(1)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 526 | |
| 527 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 528 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 529 | // 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] | 530 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 531 | // nodes based on the type of node we are checking. This simplifies life a |
| 532 | // bit for the callers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 533 | static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, |
| 534 | SDValue &CC) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 535 | if (N.getOpcode() == ISD::SETCC) { |
| 536 | LHS = N.getOperand(0); |
| 537 | RHS = N.getOperand(1); |
| 538 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 539 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 540 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 541 | if (N.getOpcode() == ISD::SELECT_CC && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 542 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 543 | N.getOperand(3).getOpcode() == ISD::Constant && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 544 | cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 545 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 546 | LHS = N.getOperand(0); |
| 547 | RHS = N.getOperand(1); |
| 548 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 549 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 550 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 551 | return false; |
| 552 | } |
| 553 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 554 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 555 | // one use. If this is true, it allows the users to invert the operation for |
| 556 | // free when it is profitable to do so. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 557 | static bool isOneUseSetCC(SDValue N) { |
| 558 | SDValue N0, N1, N2; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 559 | if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 560 | return true; |
| 561 | return false; |
| 562 | } |
| 563 | |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 564 | SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL, |
| 565 | SDValue N0, SDValue N1) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 566 | EVT VT = N0.getValueType(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 567 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
| 568 | if (isa<ConstantSDNode>(N1)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 569 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 570 | SDValue OpNode = |
| 571 | DAG.FoldConstantArithmetic(Opc, VT, |
| 572 | cast<ConstantSDNode>(N0.getOperand(1)), |
| 573 | cast<ConstantSDNode>(N1)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 574 | return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 575 | } |
| 576 | if (N0.hasOneUse()) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 577 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
| 578 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
| 579 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 580 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 581 | return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 584 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 585 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
| 586 | if (isa<ConstantSDNode>(N0)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 587 | // reassoc. (op c2, (op x, c1)) -> (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>(N1.getOperand(1)), |
| 591 | cast<ConstantSDNode>(N0)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 592 | return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 593 | } |
| 594 | if (N1.hasOneUse()) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 595 | // 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] | 596 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 597 | N1.getOperand(0), N0); |
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, N1.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 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 603 | return SDValue(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 606 | SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
| 607 | bool AddTo) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 608 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
| 609 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 610 | DEBUG(dbgs() << "\nReplacing.1 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 611 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 612 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 613 | To[0].getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 614 | dbgs() << " and " << NumTo-1 << " other values\n"; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 615 | for (unsigned i = 0, e = NumTo; i != e; ++i) |
Jakob Stoklund Olesen | 9f0d4e6 | 2009-12-03 05:15:35 +0000 | [diff] [blame] | 616 | assert((!To[i].getNode() || |
| 617 | N->getValueType(i) == To[i].getValueType()) && |
Dan Gohman | 764fd0c | 2009-01-21 15:17:51 +0000 | [diff] [blame] | 618 | "Cannot combine value to value of different type!")); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 619 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 620 | DAG.ReplaceAllUsesWith(N, To); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 621 | if (AddTo) { |
| 622 | // Push the new nodes and any users onto the worklist |
| 623 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 624 | if (To[i].getNode()) { |
| 625 | AddToWorkList(To[i].getNode()); |
| 626 | AddUsersToWorkList(To[i].getNode()); |
| 627 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 630 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 631 | // Finally, if the node is now dead, remove it from the graph. The node |
| 632 | // may not be dead if the replacement process recursively simplified to |
| 633 | // something else needing this node. |
| 634 | if (N->use_empty()) { |
| 635 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 636 | // process a node that has been replaced. |
| 637 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 638 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 639 | // Finally, since the node is now dead, remove it from the graph. |
| 640 | DAG.DeleteNode(N); |
| 641 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 642 | return SDValue(N, 0); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 645 | void DAGCombiner:: |
| 646 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 647 | // Replace all uses. If any nodes become isomorphic to other nodes and |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 648 | // are deleted, make sure to remove them from our worklist. |
| 649 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 650 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 651 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 652 | // Push the new node and any (possibly new) users onto the worklist. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 653 | AddToWorkList(TLO.New.getNode()); |
| 654 | AddUsersToWorkList(TLO.New.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 655 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 656 | // Finally, if the node is now dead, remove it from the graph. The node |
| 657 | // may not be dead if the replacement process recursively simplified to |
| 658 | // something else needing this node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 659 | if (TLO.Old.getNode()->use_empty()) { |
| 660 | removeFromWorkList(TLO.Old.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 661 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 662 | // If the operands of this node are only used by the node, they will now |
| 663 | // 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] | 664 | for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i) |
| 665 | if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse()) |
| 666 | AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 667 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 668 | DAG.DeleteNode(TLO.Old.getNode()); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 669 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
| 673 | /// it can be simplified or if things it uses can be simplified by bit |
| 674 | /// propagation. If so, return true. |
| 675 | bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 676 | TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 677 | APInt KnownZero, KnownOne; |
| 678 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 679 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 680 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 681 | // Revisit the node. |
| 682 | AddToWorkList(Op.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 683 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 684 | // Replace the old value with the new one. |
| 685 | ++NodesCombined; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 686 | DEBUG(dbgs() << "\nReplacing.2 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 687 | TLO.Old.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 688 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 689 | TLO.New.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 690 | dbgs() << '\n'); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 691 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 692 | CommitTargetLoweringOpt(TLO); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 693 | return true; |
| 694 | } |
| 695 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 696 | void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { |
| 697 | DebugLoc dl = Load->getDebugLoc(); |
| 698 | EVT VT = Load->getValueType(0); |
| 699 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 700 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 701 | DEBUG(dbgs() << "\nReplacing.9 "; |
| 702 | Load->dump(&DAG); |
| 703 | dbgs() << "\nWith: "; |
| 704 | Trunc.getNode()->dump(&DAG); |
| 705 | dbgs() << '\n'); |
| 706 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 707 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc); |
| 708 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1)); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 709 | removeFromWorkList(Load); |
| 710 | DAG.DeleteNode(Load); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 711 | AddToWorkList(Trunc.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { |
| 715 | Replace = false; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 716 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 717 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 718 | EVT MemVT = LD->getMemoryVT(); |
| 719 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 720 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 721 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 722 | : LD->getExtensionType(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 723 | Replace = true; |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 724 | return DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 725 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 726 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 727 | MemVT, LD->isVolatile(), |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 728 | LD->isNonTemporal(), LD->getAlignment()); |
| 729 | } |
| 730 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 731 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 732 | switch (Opc) { |
| 733 | default: break; |
| 734 | case ISD::AssertSext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 735 | return DAG.getNode(ISD::AssertSext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 736 | SExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 737 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 738 | case ISD::AssertZext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 739 | return DAG.getNode(ISD::AssertZext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 740 | ZExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 741 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 742 | case ISD::Constant: { |
| 743 | unsigned ExtOpc = |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 744 | Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 745 | return DAG.getNode(ExtOpc, dl, PVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 746 | } |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 750 | return SDValue(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 751 | return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 754 | SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 755 | if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) |
| 756 | return SDValue(); |
| 757 | EVT OldVT = Op.getValueType(); |
| 758 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 759 | bool Replace = false; |
| 760 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 761 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 762 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 763 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 764 | |
| 765 | if (Replace) |
| 766 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 767 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 768 | DAG.getValueType(OldVT)); |
| 769 | } |
| 770 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 771 | SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 772 | EVT OldVT = Op.getValueType(); |
| 773 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 774 | bool Replace = false; |
| 775 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 776 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 777 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 778 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 779 | |
| 780 | if (Replace) |
| 781 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 782 | return DAG.getZeroExtendInReg(NewOp, dl, OldVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 785 | /// PromoteIntBinOp - Promote the specified integer binary operation if the |
| 786 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 787 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 788 | SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { |
| 789 | if (!LegalOperations) |
| 790 | return SDValue(); |
| 791 | |
| 792 | EVT VT = Op.getValueType(); |
| 793 | if (VT.isVector() || !VT.isInteger()) |
| 794 | return SDValue(); |
| 795 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 796 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 797 | // promoting it. |
| 798 | unsigned Opc = Op.getOpcode(); |
| 799 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 800 | return SDValue(); |
| 801 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 802 | EVT PVT = VT; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 803 | // Consult target whether it is a good idea to promote this operation and |
| 804 | // what's the right type to promote it to. |
| 805 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 806 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 807 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 808 | bool Replace0 = false; |
| 809 | SDValue N0 = Op.getOperand(0); |
| 810 | SDValue NN0 = PromoteOperand(N0, PVT, Replace0); |
| 811 | if (NN0.getNode() == 0) |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 812 | return SDValue(); |
| 813 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 814 | bool Replace1 = false; |
| 815 | SDValue N1 = Op.getOperand(1); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 816 | SDValue NN1; |
| 817 | if (N0 == N1) |
| 818 | NN1 = NN0; |
| 819 | else { |
| 820 | NN1 = PromoteOperand(N1, PVT, Replace1); |
| 821 | if (NN1.getNode() == 0) |
| 822 | return SDValue(); |
| 823 | } |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 824 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 825 | AddToWorkList(NN0.getNode()); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 826 | if (NN1.getNode()) |
| 827 | AddToWorkList(NN1.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 828 | |
| 829 | if (Replace0) |
| 830 | ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); |
| 831 | if (Replace1) |
| 832 | ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 833 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 834 | DEBUG(dbgs() << "\nPromoting "; |
| 835 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 836 | DebugLoc dl = Op.getDebugLoc(); |
| 837 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 838 | DAG.getNode(Opc, dl, PVT, NN0, NN1)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 839 | } |
| 840 | return SDValue(); |
| 841 | } |
| 842 | |
| 843 | /// PromoteIntShiftOp - Promote the specified integer shift operation if the |
| 844 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 845 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 846 | SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { |
| 847 | if (!LegalOperations) |
| 848 | return SDValue(); |
| 849 | |
| 850 | EVT VT = Op.getValueType(); |
| 851 | if (VT.isVector() || !VT.isInteger()) |
| 852 | return SDValue(); |
| 853 | |
| 854 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 855 | // promoting it. |
| 856 | unsigned Opc = Op.getOpcode(); |
| 857 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 858 | return SDValue(); |
| 859 | |
| 860 | EVT PVT = VT; |
| 861 | // Consult target whether it is a good idea to promote this operation and |
| 862 | // what's the right type to promote it to. |
| 863 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 864 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 865 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 866 | bool Replace = false; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 867 | SDValue N0 = Op.getOperand(0); |
| 868 | if (Opc == ISD::SRA) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 869 | N0 = SExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 870 | else if (Opc == ISD::SRL) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 871 | N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 872 | else |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 873 | N0 = PromoteOperand(N0, PVT, Replace); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 874 | if (N0.getNode() == 0) |
| 875 | return SDValue(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 876 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 877 | AddToWorkList(N0.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 878 | if (Replace) |
| 879 | ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 880 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 881 | DEBUG(dbgs() << "\nPromoting "; |
| 882 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 883 | DebugLoc dl = Op.getDebugLoc(); |
| 884 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 885 | DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1))); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 886 | } |
| 887 | return SDValue(); |
| 888 | } |
| 889 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 890 | SDValue DAGCombiner::PromoteExtend(SDValue Op) { |
| 891 | if (!LegalOperations) |
| 892 | return SDValue(); |
| 893 | |
| 894 | EVT VT = Op.getValueType(); |
| 895 | if (VT.isVector() || !VT.isInteger()) |
| 896 | return SDValue(); |
| 897 | |
| 898 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 899 | // promoting it. |
| 900 | unsigned Opc = Op.getOpcode(); |
| 901 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 902 | return SDValue(); |
| 903 | |
| 904 | EVT PVT = VT; |
| 905 | // Consult target whether it is a good idea to promote this operation and |
| 906 | // what's the right type to promote it to. |
| 907 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 908 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 909 | // fold (aext (aext x)) -> (aext x) |
| 910 | // fold (aext (zext x)) -> (zext x) |
| 911 | // fold (aext (sext x)) -> (sext x) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 912 | DEBUG(dbgs() << "\nPromoting "; |
| 913 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 914 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0)); |
| 915 | } |
| 916 | return SDValue(); |
| 917 | } |
| 918 | |
| 919 | bool DAGCombiner::PromoteLoad(SDValue Op) { |
| 920 | if (!LegalOperations) |
| 921 | return false; |
| 922 | |
| 923 | EVT VT = Op.getValueType(); |
| 924 | if (VT.isVector() || !VT.isInteger()) |
| 925 | return false; |
| 926 | |
| 927 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 928 | // promoting it. |
| 929 | unsigned Opc = Op.getOpcode(); |
| 930 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 931 | return false; |
| 932 | |
| 933 | EVT PVT = VT; |
| 934 | // Consult target whether it is a good idea to promote this operation and |
| 935 | // what's the right type to promote it to. |
| 936 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 937 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 938 | |
| 939 | DebugLoc dl = Op.getDebugLoc(); |
| 940 | SDNode *N = Op.getNode(); |
| 941 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 942 | EVT MemVT = LD->getMemoryVT(); |
| 943 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 944 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 945 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 946 | : LD->getExtensionType(); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 947 | SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 948 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 949 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 950 | MemVT, LD->isVolatile(), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 951 | LD->isNonTemporal(), LD->getAlignment()); |
| 952 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD); |
| 953 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 954 | DEBUG(dbgs() << "\nPromoting "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 955 | N->dump(&DAG); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 956 | dbgs() << "\nTo: "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 957 | Result.getNode()->dump(&DAG); |
| 958 | dbgs() << '\n'); |
| 959 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 960 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
| 961 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 962 | removeFromWorkList(N); |
| 963 | DAG.DeleteNode(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 964 | AddToWorkList(Result.getNode()); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 965 | return true; |
| 966 | } |
| 967 | return false; |
| 968 | } |
| 969 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 971 | //===----------------------------------------------------------------------===// |
| 972 | // Main DAG Combiner implementation |
| 973 | //===----------------------------------------------------------------------===// |
| 974 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 975 | void DAGCombiner::Run(CombineLevel AtLevel) { |
| 976 | // set the instance variables, so that the various visit routines may use it. |
| 977 | Level = AtLevel; |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 978 | LegalOperations = Level >= AfterLegalizeVectorOps; |
| 979 | LegalTypes = Level >= AfterLegalizeTypes; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 980 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 981 | // Add all the dag nodes to the worklist. |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 982 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 983 | E = DAG.allnodes_end(); I != E; ++I) |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 984 | AddToWorkList(I); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 985 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 986 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 987 | // to the root node, preventing it from being deleted, and tracking any |
| 988 | // changes of the root. |
| 989 | HandleSDNode Dummy(DAG.getRoot()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 990 | |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 991 | // The root of the dag may dangle to deleted nodes until the dag combiner is |
| 992 | // done. Set it to null to avoid confusion. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 993 | DAG.setRoot(SDValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 994 | |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 995 | // while the worklist isn't empty, find a node and |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 996 | // try and combine it. |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 997 | while (!WorkListContents.empty()) { |
| 998 | SDNode *N; |
| 999 | // The WorkListOrder holds the SDNodes in order, but it may contain duplicates. |
| 1000 | // In order to avoid a linear scan, we use a set (O(log N)) to hold what the |
| 1001 | // worklist *should* contain, and check the node we want to visit is should |
| 1002 | // actually be visited. |
| 1003 | do { |
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 1004 | N = WorkListOrder.pop_back_val(); |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1005 | } while (!WorkListContents.erase(N)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1006 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1007 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
| 1008 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 1009 | // reduced number of uses, allowing other xforms. |
| 1010 | if (N->use_empty() && N != &Dummy) { |
| 1011 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1012 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1013 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1014 | DAG.DeleteNode(N); |
| 1015 | continue; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1016 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1017 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1018 | SDValue RV = combine(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1019 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1020 | if (RV.getNode() == 0) |
| 1021 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1022 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1023 | ++NodesCombined; |
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 we get back the same node we passed in, rather than a new node or |
| 1026 | // zero, we know that the node must have defined multiple values and |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1027 | // CombineTo was used. Since CombineTo takes care of the worklist |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1028 | // mechanics for us, we have no work to do in this case. |
| 1029 | if (RV.getNode() == N) |
| 1030 | continue; |
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 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1033 | RV.getNode()->getOpcode() != ISD::DELETED_NODE && |
| 1034 | "Node was deleted but visit returned new node!"); |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1035 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1036 | DEBUG(dbgs() << "\nReplacing.3 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1037 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1038 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1039 | RV.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1040 | dbgs() << '\n'); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1041 | |
Devang Patel | 9728ea2 | 2011-05-23 22:04:42 +0000 | [diff] [blame] | 1042 | // Transfer debug value. |
| 1043 | DAG.TransferDbgValues(SDValue(N, 0), RV); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1044 | WorkListRemover DeadNodes(*this); |
| 1045 | if (N->getNumValues() == RV.getNode()->getNumValues()) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1046 | DAG.ReplaceAllUsesWith(N, RV.getNode()); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1047 | else { |
| 1048 | assert(N->getValueType(0) == RV.getValueType() && |
| 1049 | N->getNumValues() == 1 && "Type mismatch"); |
| 1050 | SDValue OpV = RV; |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1051 | DAG.ReplaceAllUsesWith(N, &OpV); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1052 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1053 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1054 | // Push the new node and any users onto the worklist |
| 1055 | AddToWorkList(RV.getNode()); |
| 1056 | AddUsersToWorkList(RV.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1057 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1058 | // Add any uses of the old node to the worklist in case this node is the |
| 1059 | // last one that uses them. They may become dead after this node is |
| 1060 | // deleted. |
| 1061 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1062 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1063 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1064 | // Finally, if the node is now dead, remove it from the graph. The node |
| 1065 | // may not be dead if the replacement process recursively simplified to |
| 1066 | // something else needing this node. |
| 1067 | if (N->use_empty()) { |
| 1068 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 1069 | // process a node that has been replaced. |
| 1070 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1071 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1072 | // Finally, since the node is now dead, remove it from the graph. |
| 1073 | DAG.DeleteNode(N); |
| 1074 | } |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1075 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1076 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 1077 | // If the root changed (e.g. it was a dead load, update the root). |
| 1078 | DAG.setRoot(Dummy.getValue()); |
Hal Finkel | 31490ba | 2012-04-16 03:33:22 +0000 | [diff] [blame] | 1079 | DAG.RemoveDeadNodes(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1082 | SDValue DAGCombiner::visit(SDNode *N) { |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1083 | switch (N->getOpcode()) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1084 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 1085 | case ISD::TokenFactor: return visitTokenFactor(N); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1086 | case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1087 | case ISD::ADD: return visitADD(N); |
| 1088 | case ISD::SUB: return visitSUB(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1089 | case ISD::ADDC: return visitADDC(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1090 | case ISD::SUBC: return visitSUBC(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1091 | case ISD::ADDE: return visitADDE(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1092 | case ISD::SUBE: return visitSUBE(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1093 | case ISD::MUL: return visitMUL(N); |
| 1094 | case ISD::SDIV: return visitSDIV(N); |
| 1095 | case ISD::UDIV: return visitUDIV(N); |
| 1096 | case ISD::SREM: return visitSREM(N); |
| 1097 | case ISD::UREM: return visitUREM(N); |
| 1098 | case ISD::MULHU: return visitMULHU(N); |
| 1099 | case ISD::MULHS: return visitMULHS(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1100 | case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); |
| 1101 | case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 1102 | case ISD::SMULO: return visitSMULO(N); |
| 1103 | case ISD::UMULO: return visitUMULO(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1104 | case ISD::SDIVREM: return visitSDIVREM(N); |
| 1105 | case ISD::UDIVREM: return visitUDIVREM(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1106 | case ISD::AND: return visitAND(N); |
| 1107 | case ISD::OR: return visitOR(N); |
| 1108 | case ISD::XOR: return visitXOR(N); |
| 1109 | case ISD::SHL: return visitSHL(N); |
| 1110 | case ISD::SRA: return visitSRA(N); |
| 1111 | case ISD::SRL: return visitSRL(N); |
| 1112 | case ISD::CTLZ: return visitCTLZ(N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1113 | case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1114 | case ISD::CTTZ: return visitCTTZ(N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1115 | case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1116 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1117 | case ISD::SELECT: return visitSELECT(N); |
| 1118 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 1119 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1120 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 1121 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1122 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1123 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 1124 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1125 | case ISD::BITCAST: return visitBITCAST(N); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 1126 | case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1127 | case ISD::FADD: return visitFADD(N); |
| 1128 | case ISD::FSUB: return visitFSUB(N); |
| 1129 | case ISD::FMUL: return visitFMUL(N); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 1130 | case ISD::FMA: return visitFMA(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1131 | case ISD::FDIV: return visitFDIV(N); |
| 1132 | case ISD::FREM: return visitFREM(N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 1133 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1134 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 1135 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 1136 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 1137 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 1138 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 1139 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 1140 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 1141 | case ISD::FNEG: return visitFNEG(N); |
| 1142 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1143 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1144 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 1145 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 1146 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 1147 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 1148 | case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1149 | case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); |
| 1150 | case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 1151 | case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 1152 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 1153 | case ISD::MEMBARRIER: return visitMEMBARRIER(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1154 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1155 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1158 | SDValue DAGCombiner::combine(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1159 | SDValue RV = visit(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1160 | |
| 1161 | // If nothing happened, try a target-specific DAG combine. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1162 | if (RV.getNode() == 0) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1163 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1164 | "Node was deleted but visit returned NULL!"); |
| 1165 | |
| 1166 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 1167 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { |
| 1168 | |
| 1169 | // Expose the DAG combiner to the target combiner impls. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1170 | TargetLowering::DAGCombinerInfo |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 1171 | DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1172 | |
| 1173 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1177 | // If nothing happened still, try promoting the operation. |
| 1178 | if (RV.getNode() == 0) { |
| 1179 | switch (N->getOpcode()) { |
| 1180 | default: break; |
| 1181 | case ISD::ADD: |
| 1182 | case ISD::SUB: |
| 1183 | case ISD::MUL: |
| 1184 | case ISD::AND: |
| 1185 | case ISD::OR: |
| 1186 | case ISD::XOR: |
| 1187 | RV = PromoteIntBinOp(SDValue(N, 0)); |
| 1188 | break; |
| 1189 | case ISD::SHL: |
| 1190 | case ISD::SRA: |
| 1191 | case ISD::SRL: |
| 1192 | RV = PromoteIntShiftOp(SDValue(N, 0)); |
| 1193 | break; |
| 1194 | case ISD::SIGN_EXTEND: |
| 1195 | case ISD::ZERO_EXTEND: |
| 1196 | case ISD::ANY_EXTEND: |
| 1197 | RV = PromoteExtend(SDValue(N, 0)); |
| 1198 | break; |
| 1199 | case ISD::LOAD: |
| 1200 | if (PromoteLoad(SDValue(N, 0))) |
| 1201 | RV = SDValue(N, 0); |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1206 | // 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] | 1207 | // sdisel CSE. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1208 | if (RV.getNode() == 0 && |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1209 | SelectionDAG::isCommutativeBinOp(N->getOpcode()) && |
| 1210 | N->getNumValues() == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1211 | SDValue N0 = N->getOperand(0); |
| 1212 | SDValue N1 = N->getOperand(1); |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1213 | |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1214 | // Constant operands are canonicalized to RHS. |
| 1215 | if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1216 | SDValue Ops[] = { N1, N0 }; |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1217 | SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), |
| 1218 | Ops, 2); |
Evan Cheng | ea10046 | 2008-03-24 23:55:16 +0000 | [diff] [blame] | 1219 | if (CSENode) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1220 | return SDValue(CSENode, 0); |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1224 | return RV; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1225 | } |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1227 | /// getInputChainForNode - Given a node, return its input chain if it has one, |
| 1228 | /// otherwise return a null sd operand. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1229 | static SDValue getInputChainForNode(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1230 | if (unsigned NumOps = N->getNumOperands()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1231 | if (N->getOperand(0).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1232 | return N->getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1233 | else if (N->getOperand(NumOps-1).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1234 | return N->getOperand(NumOps-1); |
| 1235 | for (unsigned i = 1; i < NumOps-1; ++i) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1236 | if (N->getOperand(i).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1237 | return N->getOperand(i); |
| 1238 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1239 | return SDValue(); |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1242 | SDValue DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1243 | // If N has two operands, where one has an input chain equal to the other, |
| 1244 | // the 'other' chain is redundant. |
| 1245 | if (N->getNumOperands() == 2) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1246 | if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1247 | return N->getOperand(0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1248 | if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1249 | return N->getOperand(1); |
| 1250 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1252 | SmallVector<SDNode *, 8> TFs; // List of token factors to visit. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1253 | SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1254 | SmallPtrSet<SDNode*, 16> SeenOps; |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1255 | bool Changed = false; // If we should replace this token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1256 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1257 | // Start out with this token factor. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1258 | TFs.push_back(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1259 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 1260 | // Iterate through token factors. The TFs grows when new token factors are |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1261 | // encountered. |
| 1262 | for (unsigned i = 0; i < TFs.size(); ++i) { |
| 1263 | SDNode *TF = TFs[i]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1264 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1265 | // Check each of the operands. |
| 1266 | for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1267 | SDValue Op = TF->getOperand(i); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1268 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1269 | switch (Op.getOpcode()) { |
| 1270 | case ISD::EntryToken: |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1271 | // Entry tokens don't need to be added to the list. They are |
| 1272 | // rededundant. |
| 1273 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1274 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1275 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1276 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 1277 | if (Op.hasOneUse() && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1278 | std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1279 | // Queue up for processing. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1280 | TFs.push_back(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1281 | // Clean up in case the token factor is removed. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1282 | AddToWorkList(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1283 | Changed = true; |
| 1284 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1285 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1286 | // Fall thru |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1287 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1288 | default: |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1289 | // Only add if it isn't already in the list. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1290 | if (SeenOps.insert(Op.getNode())) |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1291 | Ops.push_back(Op); |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1292 | else |
| 1293 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1294 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1297 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1298 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1299 | SDValue Result; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1300 | |
| 1301 | // If we've change things around then replace token factor. |
| 1302 | if (Changed) { |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1303 | if (Ops.empty()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1304 | // The entry token is the only possible outcome. |
| 1305 | Result = DAG.getEntryNode(); |
| 1306 | } else { |
| 1307 | // New and improved token factor. |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1308 | Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1309 | MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1310 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1311 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 1312 | // Don't add users to work list. |
| 1313 | return CombineTo(N, Result, false); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1314 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1315 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1316 | return Result; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1319 | /// MERGE_VALUES can always be eliminated. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1320 | SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1321 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1322 | // Replacing results may cause a different MERGE_VALUES to suddenly |
| 1323 | // be CSE'd with N, and carry its uses with it. Iterate until no |
| 1324 | // uses remain, to ensure that the node can be safely deleted. |
Pete Cooper | 3affd9e | 2012-06-20 19:35:43 +0000 | [diff] [blame] | 1325 | // First add the users of this node to the work list so that they |
| 1326 | // can be tried again once they have new operands. |
| 1327 | AddUsersToWorkList(N); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1328 | do { |
| 1329 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1330 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i)); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1331 | } while (!N->use_empty()); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1332 | removeFromWorkList(N); |
| 1333 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1334 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1337 | static |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1338 | SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1, |
| 1339 | SelectionDAG &DAG) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1340 | EVT VT = N0.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1341 | SDValue N00 = N0.getOperand(0); |
| 1342 | SDValue N01 = N0.getOperand(1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1343 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1344 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1345 | if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() && |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1346 | isa<ConstantSDNode>(N00.getOperand(1))) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1347 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
| 1348 | N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, |
| 1349 | DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT, |
| 1350 | N00.getOperand(0), N01), |
| 1351 | DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT, |
| 1352 | N00.getOperand(1), N01)); |
| 1353 | return DAG.getNode(ISD::ADD, DL, VT, N0, N1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1354 | } |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1355 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1356 | return SDValue(); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1359 | SDValue DAGCombiner::visitADD(SDNode *N) { |
| 1360 | SDValue N0 = N->getOperand(0); |
| 1361 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1362 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1363 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1364 | EVT VT = N0.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1365 | |
| 1366 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1367 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1368 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1369 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1370 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1371 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1372 | // fold (add x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1373 | if (N0.getOpcode() == ISD::UNDEF) |
| 1374 | return N0; |
| 1375 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1376 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1377 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1378 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1379 | return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1380 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1381 | if (N0C && !N1C) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1382 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1383 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1384 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1385 | return N0; |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1386 | // fold (add Sym, c) -> Sym+c |
| 1387 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1388 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C && |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1389 | GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1390 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1391 | GA->getOffset() + |
| 1392 | (uint64_t)N1C->getSExtValue()); |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1393 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 1394 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 1395 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1396 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1397 | DAG.getConstant(N1C->getAPIntValue()+ |
| 1398 | N0C->getAPIntValue(), VT), |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1399 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1400 | // reassociate add |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1401 | SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1402 | if (RADD.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1403 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1404 | // fold ((0-A) + B) -> B-A |
| 1405 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 1406 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1407 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1408 | // fold (A + (0-B)) -> A-B |
| 1409 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 1410 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1411 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1412 | // fold (A+(B-A)) -> B |
| 1413 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1414 | return N1.getOperand(0); |
Dale Johannesen | 56eca91 | 2008-11-27 00:43:21 +0000 | [diff] [blame] | 1415 | // fold ((B-A)+A) -> B |
| 1416 | if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) |
| 1417 | return N0.getOperand(0); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1418 | // fold (A+(B-(A+C))) to (B-C) |
| 1419 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1420 | N0 == N1.getOperand(1).getOperand(0)) |
| 1421 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1422 | N1.getOperand(1).getOperand(1)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1423 | // fold (A+(B-(C+A))) to (B-C) |
| 1424 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1425 | N0 == N1.getOperand(1).getOperand(1)) |
| 1426 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1427 | N1.getOperand(1).getOperand(0)); |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1428 | // fold (A+((B-A)+or-C)) to (B+or-C) |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1429 | if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && |
| 1430 | N1.getOperand(0).getOpcode() == ISD::SUB && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1431 | N0 == N1.getOperand(0).getOperand(1)) |
| 1432 | return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT, |
| 1433 | N1.getOperand(0).getOperand(0), N1.getOperand(1)); |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1434 | |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1435 | // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant |
| 1436 | if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { |
| 1437 | SDValue N00 = N0.getOperand(0); |
| 1438 | SDValue N01 = N0.getOperand(1); |
| 1439 | SDValue N10 = N1.getOperand(0); |
| 1440 | SDValue N11 = N1.getOperand(1); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) |
| 1443 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1444 | DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10), |
| 1445 | DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1446 | } |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1447 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1448 | if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) |
| 1449 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1451 | // fold (a+b) -> (a|b) iff a and b share no bits. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1452 | if (VT.isInteger() && !VT.isVector()) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1453 | APInt LHSZero, LHSOne; |
| 1454 | APInt RHSZero, RHSOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1455 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1456 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1457 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1458 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1460 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1461 | // 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] | 1462 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1463 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
Evan Cheng | 3ef554d | 2006-11-06 08:14:30 +0000 | [diff] [blame] | 1466 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1467 | // 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] | 1468 | if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1469 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1470 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1471 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1472 | if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1473 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1474 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1477 | // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) |
| 1478 | if (N1.getOpcode() == ISD::SHL && |
| 1479 | N1.getOperand(0).getOpcode() == ISD::SUB) |
| 1480 | if (ConstantSDNode *C = |
| 1481 | dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0))) |
| 1482 | if (C->getAPIntValue() == 0) |
| 1483 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, |
| 1484 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1485 | N1.getOperand(0).getOperand(1), |
| 1486 | N1.getOperand(1))); |
| 1487 | if (N0.getOpcode() == ISD::SHL && |
| 1488 | N0.getOperand(0).getOpcode() == ISD::SUB) |
| 1489 | if (ConstantSDNode *C = |
| 1490 | dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0))) |
| 1491 | if (C->getAPIntValue() == 0) |
| 1492 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, |
| 1493 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1494 | N0.getOperand(0).getOperand(1), |
| 1495 | N0.getOperand(1))); |
| 1496 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1497 | if (N1.getOpcode() == ISD::AND) { |
| 1498 | SDValue AndOp0 = N1.getOperand(0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1499 | ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1)); |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1500 | unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); |
| 1501 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1502 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1503 | // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) |
| 1504 | // and similar xforms where the inner op is either ~0 or 0. |
| 1505 | if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) { |
| 1506 | DebugLoc DL = N->getDebugLoc(); |
| 1507 | return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0); |
| 1508 | } |
| 1509 | } |
| 1510 | |
Benjamin Kramer | f50125e | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1511 | // add (sext i1), X -> sub X, (zext i1) |
| 1512 | if (N0.getOpcode() == ISD::SIGN_EXTEND && |
| 1513 | N0.getOperand(0).getValueType() == MVT::i1 && |
| 1514 | !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { |
| 1515 | DebugLoc DL = N->getDebugLoc(); |
| 1516 | SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); |
| 1517 | return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); |
| 1518 | } |
| 1519 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1520 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1523 | SDValue DAGCombiner::visitADDC(SDNode *N) { |
| 1524 | SDValue N0 = N->getOperand(0); |
| 1525 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1526 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1527 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1528 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1530 | // If the flag result is dead, turn this into an ADD. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1531 | if (!N->hasAnyUseOfValue(1)) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1532 | return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1533 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1534 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1536 | // canonicalize constant to RHS. |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1537 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1538 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1540 | // fold (addc x, 0) -> x + no carry out |
| 1541 | if (N1C && N1C->isNullValue()) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1542 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1543 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1544 | |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1545 | // 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] | 1546 | APInt LHSZero, LHSOne; |
| 1547 | APInt RHSZero, RHSOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1548 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1549 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1550 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1551 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1553 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1554 | // 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] | 1555 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1556 | return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1557 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1558 | N->getDebugLoc(), MVT::Glue)); |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1559 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1560 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1561 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1564 | SDValue DAGCombiner::visitADDE(SDNode *N) { |
| 1565 | SDValue N0 = N->getOperand(0); |
| 1566 | SDValue N1 = N->getOperand(1); |
| 1567 | SDValue CarryIn = N->getOperand(2); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1568 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1569 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1571 | // canonicalize constant to RHS |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1572 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1573 | return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(), |
| 1574 | N1, N0, CarryIn); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1576 | // fold (adde x, y, false) -> (addc x, y) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1577 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1578 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1579 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1580 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1583 | // Since it may not be valid to emit a fold to zero for vector initializers |
| 1584 | // check if we can before folding. |
| 1585 | static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1586 | SelectionDAG &DAG, bool LegalOperations) { |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1587 | if (!VT.isVector()) { |
| 1588 | return DAG.getConstant(0, VT); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 1589 | } |
| 1590 | if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) { |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1591 | // Produce a vector of zeros. |
| 1592 | SDValue El = DAG.getConstant(0, VT.getVectorElementType()); |
| 1593 | std::vector<SDValue> Ops(VT.getVectorNumElements(), El); |
| 1594 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, |
| 1595 | &Ops[0], Ops.size()); |
| 1596 | } |
| 1597 | return SDValue(); |
| 1598 | } |
| 1599 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1600 | SDValue DAGCombiner::visitSUB(SDNode *N) { |
| 1601 | SDValue N0 = N->getOperand(0); |
| 1602 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1603 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1604 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1605 | ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 : |
| 1606 | dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1607 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1608 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1609 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1610 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1611 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1612 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1613 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 1615 | // fold (sub x, x) -> 0 |
Eric Christopher | 169e155 | 2011-02-16 01:10:03 +0000 | [diff] [blame] | 1616 | // FIXME: Refactor this and xor and other similar operations together. |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1617 | if (N0 == N1) |
| 1618 | return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1619 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1620 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1621 | return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 1622 | // fold (sub x, c) -> (add x, -c) |
| 1623 | if (N1C) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1624 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1625 | DAG.getConstant(-N1C->getAPIntValue(), VT)); |
Evan Cheng | 1ad0e8b | 2010-01-18 21:38:44 +0000 | [diff] [blame] | 1626 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) |
| 1627 | if (N0C && N0C->isAllOnesValue()) |
| 1628 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Benjamin Kramer | 2c94b42 | 2011-01-29 12:34:05 +0000 | [diff] [blame] | 1629 | // fold A-(A-B) -> B |
| 1630 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) |
| 1631 | return N1.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1632 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1633 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1634 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1635 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1636 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1637 | return N0.getOperand(0); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1638 | // fold C2-(A+C1) -> (C2-C1)-A |
| 1639 | if (N1.getOpcode() == ISD::ADD && N0C && N1C1) { |
| 1640 | SDValue NewC = DAG.getConstant((N0C->getAPIntValue() - N1C1->getAPIntValue()), VT); |
| 1641 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC, |
| 1642 | N1.getOperand(0)); |
| 1643 | } |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1644 | // fold ((A+(B+or-C))-B) -> A+or-C |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1645 | if (N0.getOpcode() == ISD::ADD && |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1646 | (N0.getOperand(1).getOpcode() == ISD::SUB || |
| 1647 | N0.getOperand(1).getOpcode() == ISD::ADD) && |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1648 | N0.getOperand(1).getOperand(0) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1649 | return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT, |
| 1650 | N0.getOperand(0), N0.getOperand(1).getOperand(1)); |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1651 | // fold ((A+(C+B))-B) -> A+C |
| 1652 | if (N0.getOpcode() == ISD::ADD && |
| 1653 | N0.getOperand(1).getOpcode() == ISD::ADD && |
| 1654 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1655 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1656 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Dale Johannesen | 58e39b0 | 2008-12-23 01:59:54 +0000 | [diff] [blame] | 1657 | // fold ((A-(B-C))-C) -> A-B |
| 1658 | if (N0.getOpcode() == ISD::SUB && |
| 1659 | N0.getOperand(1).getOpcode() == ISD::SUB && |
| 1660 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1661 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1662 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1663 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1664 | // If either operand of a sub is undef, the result is undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1665 | if (N0.getOpcode() == ISD::UNDEF) |
| 1666 | return N0; |
| 1667 | if (N1.getOpcode() == ISD::UNDEF) |
| 1668 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1669 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1670 | // If the relocation model supports it, consider symbol offsets. |
| 1671 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1672 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1673 | // fold (sub Sym, c) -> Sym-c |
| 1674 | if (N1C && GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1675 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1676 | GA->getOffset() - |
| 1677 | (uint64_t)N1C->getSExtValue()); |
| 1678 | // fold (sub Sym+c1, Sym+c2) -> c1-c2 |
| 1679 | if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) |
| 1680 | if (GA->getGlobal() == GB->getGlobal()) |
| 1681 | return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), |
| 1682 | VT); |
| 1683 | } |
| 1684 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1685 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1688 | SDValue DAGCombiner::visitSUBC(SDNode *N) { |
| 1689 | SDValue N0 = N->getOperand(0); |
| 1690 | SDValue N1 = N->getOperand(1); |
| 1691 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1692 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1693 | EVT VT = N0.getValueType(); |
| 1694 | |
| 1695 | // If the flag result is dead, turn this into an SUB. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1696 | if (!N->hasAnyUseOfValue(1)) |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1697 | return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1), |
| 1698 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1699 | MVT::Glue)); |
| 1700 | |
| 1701 | // fold (subc x, x) -> 0 + no borrow |
| 1702 | if (N0 == N1) |
| 1703 | return CombineTo(N, DAG.getConstant(0, VT), |
| 1704 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1705 | MVT::Glue)); |
| 1706 | |
| 1707 | // fold (subc x, 0) -> x + no borrow |
| 1708 | if (N1C && N1C->isNullValue()) |
| 1709 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1710 | MVT::Glue)); |
| 1711 | |
| 1712 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow |
| 1713 | if (N0C && N0C->isAllOnesValue()) |
| 1714 | return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0), |
| 1715 | DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(), |
| 1716 | MVT::Glue)); |
| 1717 | |
| 1718 | return SDValue(); |
| 1719 | } |
| 1720 | |
| 1721 | SDValue DAGCombiner::visitSUBE(SDNode *N) { |
| 1722 | SDValue N0 = N->getOperand(0); |
| 1723 | SDValue N1 = N->getOperand(1); |
| 1724 | SDValue CarryIn = N->getOperand(2); |
| 1725 | |
| 1726 | // fold (sube x, y, false) -> (subc x, y) |
| 1727 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
| 1728 | return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1); |
| 1729 | |
| 1730 | return SDValue(); |
| 1731 | } |
| 1732 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1733 | SDValue DAGCombiner::visitMUL(SDNode *N) { |
| 1734 | SDValue N0 = N->getOperand(0); |
| 1735 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1736 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1737 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1738 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1739 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1740 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1741 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1742 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1743 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1744 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1745 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1746 | // fold (mul x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1747 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1748 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1749 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1750 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1751 | return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1752 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1753 | if (N0C && !N1C) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1754 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1755 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1756 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1757 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1758 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1759 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1760 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1761 | DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1762 | // fold (mul x, (1 << c)) -> x << c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1763 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1764 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1765 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1766 | getShiftAmountTy(N0.getValueType()))); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1767 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1768 | if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) { |
| 1769 | unsigned Log2Val = (-N1C->getAPIntValue()).logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1770 | // 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] | 1771 | // single-use add), we should put the negate there. |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1772 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1773 | DAG.getConstant(0, VT), |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1774 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1775 | DAG.getConstant(Log2Val, |
| 1776 | getShiftAmountTy(N0.getValueType())))); |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1777 | } |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1778 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1779 | if (N1C && N0.getOpcode() == ISD::SHL && |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1780 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1781 | SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1782 | N1, N0.getOperand(1)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1783 | AddToWorkList(C3.getNode()); |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1784 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1785 | N0.getOperand(0), C3); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1786 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1787 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1788 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 1789 | // use. |
| 1790 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1791 | SDValue Sh(0,0), Y(0,0); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1792 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| 1793 | if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1794 | N0.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1795 | Sh = N0; Y = N1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1796 | } else if (N1.getOpcode() == ISD::SHL && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 1797 | isa<ConstantSDNode>(N1.getOperand(1)) && |
| 1798 | N1.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1799 | Sh = N1; Y = N0; |
| 1800 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1801 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1802 | if (Sh.getNode()) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1803 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1804 | Sh.getOperand(0), Y); |
| 1805 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1806 | Mul, Sh.getOperand(1)); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1807 | } |
| 1808 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 1810 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1811 | if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1812 | isa<ConstantSDNode>(N0.getOperand(1))) |
| 1813 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1814 | DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT, |
| 1815 | N0.getOperand(0), N1), |
| 1816 | DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT, |
| 1817 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1818 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1819 | // reassociate mul |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1820 | SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1821 | if (RMUL.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1822 | return RMUL; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1823 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1824 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1827 | SDValue DAGCombiner::visitSDIV(SDNode *N) { |
| 1828 | SDValue N0 = N->getOperand(0); |
| 1829 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1830 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1831 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1832 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1833 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1834 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1835 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1836 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1837 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1838 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1839 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1840 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1841 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1842 | return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1843 | // fold (sdiv X, 1) -> X |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1844 | if (N1C && N1C->getAPIntValue() == 1LL) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1845 | return N0; |
| 1846 | // fold (sdiv X, -1) -> 0-X |
| 1847 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1848 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1849 | DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 1850 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1851 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1852 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1853 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1854 | return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(), |
| 1855 | N0, N1); |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 1856 | } |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 1857 | // fold (sdiv X, pow2) -> simple ops after legalize |
Eli Friedman | 1c663fe | 2011-12-07 03:55:52 +0000 | [diff] [blame] | 1858 | if (N1C && !N1C->isNullValue() && |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1859 | (N1C->getAPIntValue().isPowerOf2() || |
| 1860 | (-N1C->getAPIntValue()).isPowerOf2())) { |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1861 | // If dividing by powers of two is cheap, then don't perform the following |
| 1862 | // fold. |
| 1863 | if (TLI.isPow2DivCheap()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1864 | return SDValue(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1865 | |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1866 | unsigned lg2 = N1C->getAPIntValue().countTrailingZeros(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1868 | // Splat the sign bit into the register |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1869 | SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
| 1870 | DAG.getConstant(VT.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1871 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1872 | AddToWorkList(SGN.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1874 | // Add (N0 < 0) ? abs2 - 1 : 0; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1875 | SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN, |
| 1876 | DAG.getConstant(VT.getSizeInBits() - lg2, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1877 | getShiftAmountTy(SGN.getValueType()))); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1878 | SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1879 | AddToWorkList(SRL.getNode()); |
| 1880 | AddToWorkList(ADD.getNode()); // Divide by pow2 |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1881 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1882 | DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType()))); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1883 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1884 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 1885 | // negate the result. |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1886 | if (N1C->getAPIntValue().isNonNegative()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1887 | return SRA; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1888 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1889 | AddToWorkList(SRA.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1890 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1891 | DAG.getConstant(0, VT), SRA); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1892 | } |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1893 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1894 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 1895 | // alternate sequence. |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1896 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1897 | SDValue Op = BuildSDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1898 | if (Op.getNode()) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1899 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1900 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1901 | // undef / X -> 0 |
| 1902 | if (N0.getOpcode() == ISD::UNDEF) |
| 1903 | return DAG.getConstant(0, VT); |
| 1904 | // X / undef -> undef |
| 1905 | if (N1.getOpcode() == ISD::UNDEF) |
| 1906 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1907 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1908 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1911 | SDValue DAGCombiner::visitUDIV(SDNode *N) { |
| 1912 | SDValue N0 = N->getOperand(0); |
| 1913 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1914 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1915 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1916 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1917 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1918 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1919 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1920 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1921 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1922 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1923 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1924 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1925 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1926 | return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1927 | // fold (udiv x, (1 << c)) -> x >>u c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1928 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1929 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1930 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1931 | getShiftAmountTy(N0.getValueType()))); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1932 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
| 1933 | if (N1.getOpcode() == ISD::SHL) { |
| 1934 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1935 | if (SHC->getAPIntValue().isPowerOf2()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1936 | EVT ADDVT = N1.getOperand(1).getValueType(); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1937 | SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT, |
| 1938 | N1.getOperand(1), |
| 1939 | DAG.getConstant(SHC->getAPIntValue() |
| 1940 | .logBase2(), |
| 1941 | ADDVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1942 | AddToWorkList(Add.getNode()); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1943 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1947 | // fold (udiv x, c) -> alternate |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1948 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1949 | SDValue Op = BuildUDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1950 | if (Op.getNode()) return Op; |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 1951 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1952 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1953 | // undef / X -> 0 |
| 1954 | if (N0.getOpcode() == ISD::UNDEF) |
| 1955 | return DAG.getConstant(0, VT); |
| 1956 | // X / undef -> undef |
| 1957 | if (N1.getOpcode() == ISD::UNDEF) |
| 1958 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1959 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1960 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1963 | SDValue DAGCombiner::visitSREM(SDNode *N) { |
| 1964 | SDValue N0 = N->getOperand(0); |
| 1965 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1966 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1967 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1968 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1969 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1970 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1971 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1972 | return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1973 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1974 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1975 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1976 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1977 | return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | ee339f4 | 2008-01-27 23:21:58 +0000 | [diff] [blame] | 1978 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1979 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1980 | // If X/C can be simplified by the division-by-constant logic, lower |
| 1981 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1982 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1983 | SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1984 | AddToWorkList(Div.getNode()); |
| 1985 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 1986 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1987 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1988 | OptimizedDiv, N1); |
| 1989 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1990 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1991 | return Sub; |
| 1992 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1993 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1994 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1995 | // undef % X -> 0 |
| 1996 | if (N0.getOpcode() == ISD::UNDEF) |
| 1997 | return DAG.getConstant(0, VT); |
| 1998 | // X % undef -> undef |
| 1999 | if (N1.getOpcode() == ISD::UNDEF) |
| 2000 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2001 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2002 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2005 | SDValue DAGCombiner::visitUREM(SDNode *N) { |
| 2006 | SDValue N0 = N->getOperand(0); |
| 2007 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2008 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2009 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2010 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2011 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2012 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2013 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2014 | return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2015 | // fold (urem x, pow2) -> (and x, pow2-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2016 | if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2017 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2018 | DAG.getConstant(N1C->getAPIntValue()-1,VT)); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2019 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 2020 | if (N1.getOpcode() == ISD::SHL) { |
| 2021 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2022 | if (SHC->getAPIntValue().isPowerOf2()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2023 | SDValue Add = |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2024 | DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2025 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2026 | VT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2027 | AddToWorkList(Add.getNode()); |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2028 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2032 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2033 | // If X/C can be simplified by the division-by-constant logic, lower |
| 2034 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2035 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2036 | SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 942ca7f | 2008-09-08 16:59:01 +0000 | [diff] [blame] | 2037 | AddToWorkList(Div.getNode()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2038 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 2039 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2040 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 2041 | OptimizedDiv, N1); |
| 2042 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2043 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2044 | return Sub; |
| 2045 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2046 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2047 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2048 | // undef % X -> 0 |
| 2049 | if (N0.getOpcode() == ISD::UNDEF) |
| 2050 | return DAG.getConstant(0, VT); |
| 2051 | // X % undef -> undef |
| 2052 | if (N1.getOpcode() == ISD::UNDEF) |
| 2053 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2054 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2055 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2058 | SDValue DAGCombiner::visitMULHS(SDNode *N) { |
| 2059 | SDValue N0 = N->getOperand(0); |
| 2060 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2061 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2062 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2063 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2064 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2065 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2066 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2067 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2068 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2069 | if (N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 326411d | 2009-01-30 03:00:18 +0000 | [diff] [blame] | 2070 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0, |
| 2071 | DAG.getConstant(N0.getValueType().getSizeInBits() - 1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2072 | getShiftAmountTy(N0.getValueType()))); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2073 | // fold (mulhs x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2074 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2075 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2076 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2077 | // If the type twice as wide is legal, transform the mulhs to a wider multiply |
| 2078 | // plus a shift. |
| 2079 | if (VT.isSimple() && !VT.isVector()) { |
| 2080 | MVT Simple = VT.getSimpleVT(); |
| 2081 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2082 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2083 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2084 | N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); |
| 2085 | N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); |
| 2086 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
Chris Lattner | 1a0fbe2 | 2010-12-15 05:51:39 +0000 | [diff] [blame] | 2087 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2088 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2089 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2090 | } |
| 2091 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2092 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2093 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2096 | SDValue DAGCombiner::visitMULHU(SDNode *N) { |
| 2097 | SDValue N0 = N->getOperand(0); |
| 2098 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2099 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2100 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2101 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2102 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2103 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2104 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2105 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2106 | // fold (mulhu x, 1) -> 0 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2107 | if (N1C && N1C->getAPIntValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2108 | return DAG.getConstant(0, N0.getValueType()); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2109 | // fold (mulhu x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2110 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2111 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2113 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2114 | // plus a shift. |
| 2115 | if (VT.isSimple() && !VT.isVector()) { |
| 2116 | MVT Simple = VT.getSimpleVT(); |
| 2117 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2118 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2119 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2120 | N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); |
| 2121 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); |
| 2122 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
| 2123 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2124 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2125 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2126 | } |
| 2127 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2128 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2129 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2132 | /// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that |
| 2133 | /// compute two values. LoOp and HiOp give the opcodes for the two computations |
| 2134 | /// that are being performed. Return true if a simplification was made. |
| 2135 | /// |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2136 | SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2137 | unsigned HiOp) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2138 | // If the high half is not needed, just compute the low half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2139 | bool HiExists = N->hasAnyUseOfValue(1); |
| 2140 | if (!HiExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2141 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2142 | TLI.isOperationLegal(LoOp, N->getValueType(0)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2143 | SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2144 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2145 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | // If the low half is not needed, just compute the high half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2149 | bool LoExists = N->hasAnyUseOfValue(0); |
| 2150 | if (!LoExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2151 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2152 | TLI.isOperationLegal(HiOp, N->getValueType(1)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2153 | SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
| 2154 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2155 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2158 | // If both halves are used, return as it is. |
| 2159 | if (LoExists && HiExists) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2160 | return SDValue(); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2161 | |
| 2162 | // If the two computed results can be simplified separately, separate them. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2163 | if (LoExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2164 | SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2165 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2166 | AddToWorkList(Lo.getNode()); |
| 2167 | SDValue LoOpt = combine(Lo.getNode()); |
| 2168 | if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2169 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2170 | TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2171 | return CombineTo(N, LoOpt, LoOpt); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2174 | if (HiExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2175 | SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2176 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2177 | AddToWorkList(Hi.getNode()); |
| 2178 | SDValue HiOpt = combine(Hi.getNode()); |
| 2179 | if (HiOpt.getNode() && HiOpt != Hi && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2180 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2181 | TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2182 | return CombineTo(N, HiOpt, HiOpt); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2183 | } |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2184 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2185 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2188 | SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { |
| 2189 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2190 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2192 | EVT VT = N->getValueType(0); |
| 2193 | DebugLoc DL = N->getDebugLoc(); |
| 2194 | |
| 2195 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2196 | // plus a shift. |
| 2197 | if (VT.isSimple() && !VT.isVector()) { |
| 2198 | MVT Simple = VT.getSimpleVT(); |
| 2199 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2200 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2201 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2202 | SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2203 | SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2204 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2205 | // Compute the high part as N1. |
| 2206 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2207 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2208 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2209 | // Compute the low part as N0. |
| 2210 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2211 | return CombineTo(N, Lo, Hi); |
| 2212 | } |
| 2213 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2214 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2215 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2218 | SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { |
| 2219 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2220 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2221 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2222 | EVT VT = N->getValueType(0); |
| 2223 | DebugLoc DL = N->getDebugLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2224 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2225 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2226 | // plus a shift. |
| 2227 | if (VT.isSimple() && !VT.isVector()) { |
| 2228 | MVT Simple = VT.getSimpleVT(); |
| 2229 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2230 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2231 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2232 | SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2233 | SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2234 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2235 | // Compute the high part as N1. |
| 2236 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2237 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2238 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2239 | // Compute the low part as N0. |
| 2240 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2241 | return CombineTo(N, Lo, Hi); |
| 2242 | } |
| 2243 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2244 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2245 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2248 | SDValue DAGCombiner::visitSMULO(SDNode *N) { |
| 2249 | // (smulo x, 2) -> (saddo x, x) |
| 2250 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2251 | if (C2->getAPIntValue() == 2) |
| 2252 | return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(), |
| 2253 | N->getOperand(0), N->getOperand(0)); |
| 2254 | |
| 2255 | return SDValue(); |
| 2256 | } |
| 2257 | |
| 2258 | SDValue DAGCombiner::visitUMULO(SDNode *N) { |
| 2259 | // (umulo x, 2) -> (uaddo x, x) |
| 2260 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2261 | if (C2->getAPIntValue() == 2) |
| 2262 | return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(), |
| 2263 | N->getOperand(0), N->getOperand(0)); |
| 2264 | |
| 2265 | return SDValue(); |
| 2266 | } |
| 2267 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2268 | SDValue DAGCombiner::visitSDIVREM(SDNode *N) { |
| 2269 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2270 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2271 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2272 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2275 | SDValue DAGCombiner::visitUDIVREM(SDNode *N) { |
| 2276 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2277 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2278 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2279 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2282 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 2283 | /// two operands of the same opcode, try to simplify it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2284 | SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 2285 | SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2286 | EVT VT = N0.getValueType(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2287 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2288 | |
Dan Gohman | ff00a55 | 2010-01-14 03:08:49 +0000 | [diff] [blame] | 2289 | // Bail early if none of these transforms apply. |
| 2290 | if (N0.getNode()->getNumOperands() == 0) return SDValue(); |
| 2291 | |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 2292 | // For each of OP in AND/OR/XOR: |
| 2293 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 2294 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 2295 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2296 | // 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] | 2297 | // |
| 2298 | // do not sink logical op inside of a vector extend, since it may combine |
| 2299 | // into a vsetcc. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2300 | EVT Op0VT = N0.getOperand(0).getValueType(); |
| 2301 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 2302 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 2303 | // Avoid infinite looping with PromoteIntBinOp. |
| 2304 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2305 | (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2306 | (N0.getOpcode() == ISD::TRUNCATE && |
| 2307 | (!TLI.isZExtFree(VT, Op0VT) || |
| 2308 | !TLI.isTruncateFree(Op0VT, VT)) && |
| 2309 | TLI.isTypeLegal(Op0VT))) && |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2310 | !VT.isVector() && |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2311 | Op0VT == N1.getOperand(0).getValueType() && |
| 2312 | (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2313 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2314 | N0.getOperand(0).getValueType(), |
| 2315 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2316 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2317 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2318 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2319 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2320 | // For each of OP in SHL/SRL/SRA/AND... |
| 2321 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 2322 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 2323 | // 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] | 2324 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2325 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2326 | N0.getOperand(1) == N1.getOperand(1)) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2327 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2328 | N0.getOperand(0).getValueType(), |
| 2329 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2330 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2331 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 2332 | ORNode, N0.getOperand(1)); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2333 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2334 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2335 | // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B)) |
| 2336 | // Only perform this optimization after type legalization and before |
| 2337 | // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by |
| 2338 | // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and |
| 2339 | // we don't want to undo this promotion. |
| 2340 | // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper |
| 2341 | // on scalars. |
| 2342 | if ((N0.getOpcode() == ISD::BITCAST || N0.getOpcode() == ISD::SCALAR_TO_VECTOR) |
Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 2343 | && Level == AfterLegalizeTypes) { |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2344 | SDValue In0 = N0.getOperand(0); |
| 2345 | SDValue In1 = N1.getOperand(0); |
| 2346 | EVT In0Ty = In0.getValueType(); |
| 2347 | EVT In1Ty = In1.getValueType(); |
| 2348 | // If both incoming values are integers, and the original types are the same. |
| 2349 | if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) { |
| 2350 | SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), In0Ty, In0, In1); |
| 2351 | SDValue BC = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, Op); |
| 2352 | AddToWorkList(Op.getNode()); |
| 2353 | return BC; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | // Xor/and/or are indifferent to the swizzle operation (shuffle of one value). |
| 2358 | // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B)) |
| 2359 | // If both shuffles use the same mask, and both shuffle within a single |
| 2360 | // vector, then it is worthwhile to move the swizzle after the operation. |
| 2361 | // The type-legalizer generates this pattern when loading illegal |
| 2362 | // vector types from memory. In many cases this allows additional shuffle |
| 2363 | // optimizations. |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2364 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 2365 | N0.getOperand(1).getOpcode() == ISD::UNDEF && |
| 2366 | N1.getOperand(1).getOpcode() == ISD::UNDEF) { |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2367 | ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0); |
| 2368 | ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1); |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2369 | |
| 2370 | assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() && |
| 2371 | "Inputs to shuffles are not the same type"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2372 | |
| 2373 | unsigned NumElts = VT.getVectorNumElements(); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Check that both shuffles use the same mask. The masks are known to be of |
| 2376 | // the same length because the result vector type is the same. |
| 2377 | bool SameMask = true; |
| 2378 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2379 | int Idx0 = SVN0->getMaskElt(i); |
| 2380 | int Idx1 = SVN1->getMaskElt(i); |
| 2381 | if (Idx0 != Idx1) { |
| 2382 | SameMask = false; |
| 2383 | break; |
| 2384 | } |
| 2385 | } |
| 2386 | |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2387 | if (SameMask) { |
| 2388 | SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT, |
| 2389 | N0.getOperand(0), N1.getOperand(0)); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2390 | AddToWorkList(Op.getNode()); |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2391 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op, |
| 2392 | DAG.getUNDEF(VT), &SVN0->getMask()[0]); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2393 | } |
| 2394 | } |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2395 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2396 | return SDValue(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2399 | SDValue DAGCombiner::visitAND(SDNode *N) { |
| 2400 | SDValue N0 = N->getOperand(0); |
| 2401 | SDValue N1 = N->getOperand(1); |
| 2402 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2403 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2404 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2405 | EVT VT = N1.getValueType(); |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2406 | unsigned BitWidth = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2407 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2408 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2409 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2410 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2411 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2412 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2413 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2414 | // fold (and x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2415 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2416 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2417 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2418 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2419 | return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2420 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2421 | if (N0C && !N1C) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 2422 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2423 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2424 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2425 | return N0; |
| 2426 | // if (and x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2427 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2428 | APInt::getAllOnesValue(BitWidth))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2429 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2430 | // reassociate and |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 2431 | SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2432 | if (RAND.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2433 | return RAND; |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2434 | // fold (and (or x, C), D) -> D if (C & D) == D |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 2435 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2436 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2437 | if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2438 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2439 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 2440 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2441 | SDValue N0Op0 = N0.getOperand(0); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2442 | APInt Mask = ~N1C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2443 | Mask = Mask.trunc(N0Op0.getValueSizeInBits()); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2444 | if (DAG.MaskedValueIsZero(N0Op0, Mask)) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2445 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), |
| 2446 | N0.getValueType(), N0Op0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 2448 | // Replace uses of the AND with uses of the Zero extend node. |
| 2449 | CombineTo(N, Zext); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2451 | // We actually want to replace all uses of the any_extend with the |
| 2452 | // zero_extend, to avoid duplicating things. This will later cause this |
| 2453 | // AND to be folded. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2454 | CombineTo(N0.getNode(), Zext); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2455 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2456 | } |
| 2457 | } |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2458 | // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) -> |
| 2459 | // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must |
| 2460 | // already be zero by virtue of the width of the base type of the load. |
| 2461 | // |
| 2462 | // the 'X' node here can either be nothing or an extract_vector_elt to catch |
| 2463 | // more cases. |
| 2464 | if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 2465 | N0.getOperand(0).getOpcode() == ISD::LOAD) || |
| 2466 | N0.getOpcode() == ISD::LOAD) { |
| 2467 | LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ? |
| 2468 | N0 : N0.getOperand(0) ); |
| 2469 | |
| 2470 | // Get the constant (if applicable) the zero'th operand is being ANDed with. |
| 2471 | // This can be a pure constant or a vector splat, in which case we treat the |
| 2472 | // vector as a scalar and use the splat value. |
| 2473 | APInt Constant = APInt::getNullValue(1); |
| 2474 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { |
| 2475 | Constant = C->getAPIntValue(); |
| 2476 | } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) { |
| 2477 | APInt SplatValue, SplatUndef; |
| 2478 | unsigned SplatBitSize; |
| 2479 | bool HasAnyUndefs; |
| 2480 | bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef, |
| 2481 | SplatBitSize, HasAnyUndefs); |
| 2482 | if (IsSplat) { |
| 2483 | // Undef bits can contribute to a possible optimisation if set, so |
| 2484 | // set them. |
| 2485 | SplatValue |= SplatUndef; |
| 2486 | |
| 2487 | // The splat value may be something like "0x00FFFFFF", which means 0 for |
| 2488 | // the first vector value and FF for the rest, repeating. We need a mask |
| 2489 | // that will apply equally to all members of the vector, so AND all the |
| 2490 | // lanes of the constant together. |
| 2491 | EVT VT = Vector->getValueType(0); |
| 2492 | unsigned BitWidth = VT.getVectorElementType().getSizeInBits(); |
| 2493 | Constant = APInt::getAllOnesValue(BitWidth); |
| 2494 | for (unsigned i = 0, n = VT.getVectorNumElements(); i < n; ++i) |
| 2495 | Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is |
| 2500 | // actually legal and isn't going to get expanded, else this is a false |
| 2501 | // optimisation. |
| 2502 | bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD, |
| 2503 | Load->getMemoryVT()); |
| 2504 | |
| 2505 | // Resize the constant to the same size as the original memory access before |
| 2506 | // extension. If it is still the AllOnesValue then this AND is completely |
| 2507 | // unneeded. |
| 2508 | Constant = |
| 2509 | Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits()); |
| 2510 | |
| 2511 | bool B; |
| 2512 | switch (Load->getExtensionType()) { |
| 2513 | default: B = false; break; |
| 2514 | case ISD::EXTLOAD: B = CanZextLoadProfitably; break; |
| 2515 | case ISD::ZEXTLOAD: |
| 2516 | case ISD::NON_EXTLOAD: B = true; break; |
| 2517 | } |
| 2518 | |
| 2519 | if (B && Constant.isAllOnesValue()) { |
| 2520 | // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to |
| 2521 | // preserve semantics once we get rid of the AND. |
| 2522 | SDValue NewLoad(Load, 0); |
| 2523 | if (Load->getExtensionType() == ISD::EXTLOAD) { |
| 2524 | NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD, |
| 2525 | Load->getValueType(0), Load->getDebugLoc(), |
| 2526 | Load->getChain(), Load->getBasePtr(), |
| 2527 | Load->getOffset(), Load->getMemoryVT(), |
| 2528 | Load->getMemOperand()); |
| 2529 | // Replace uses of the EXTLOAD with the new ZEXTLOAD. |
Hal Finkel | d65e463 | 2012-06-20 15:42:48 +0000 | [diff] [blame] | 2530 | if (Load->getNumValues() == 3) { |
| 2531 | // PRE/POST_INC loads have 3 values. |
| 2532 | SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1), |
| 2533 | NewLoad.getValue(2) }; |
| 2534 | CombineTo(Load, To, 3, true); |
| 2535 | } else { |
| 2536 | CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1)); |
| 2537 | } |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | // Fold the AND away, taking care not to fold to the old load node if we |
| 2541 | // replaced it. |
| 2542 | CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0); |
| 2543 | |
| 2544 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2545 | } |
| 2546 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2547 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 2548 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 2549 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 2550 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2551 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2552 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2553 | LL.getValueType().isInteger()) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2554 | // 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] | 2555 | if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2556 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2557 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2558 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2559 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2560 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2561 | // 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] | 2562 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2563 | SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(), |
| 2564 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2565 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2566 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2567 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2568 | // 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] | 2569 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2570 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2571 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2572 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2573 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2574 | } |
| 2575 | } |
| 2576 | // canonicalize equivalent to ll == rl |
| 2577 | if (LL == RR && LR == RL) { |
| 2578 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 2579 | std::swap(RL, RR); |
| 2580 | } |
| 2581 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2582 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2583 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2584 | if (Result != ISD::SETCC_INVALID && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2585 | (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType()))) |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2586 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 2587 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2590 | |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2591 | // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2592 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2593 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2594 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2595 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2596 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 2597 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 2598 | // fold (and (sra)) -> (and (srl)) when possible. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2599 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2600 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2601 | return SDValue(N, 0); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2602 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2603 | // fold (zext_inreg (extload x)) -> (zextload x) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2604 | if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2605 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2606 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2607 | // If we zero all the possible extended bits, then we can turn this into |
| 2608 | // 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] | 2609 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2610 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2611 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2612 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2613 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2614 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2615 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2616 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2617 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2618 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2619 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2620 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2621 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2625 | if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 2626 | N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2627 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2628 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2629 | // If we zero all the possible extended bits, then we can turn this into |
| 2630 | // 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] | 2631 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2632 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2633 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2634 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2635 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2636 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2637 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2638 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 2639 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2640 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2641 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2642 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2643 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2644 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2645 | } |
| 2646 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 2648 | // fold (and (load x), 255) -> (zextload x, i8) |
| 2649 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2650 | // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) |
| 2651 | if (N1C && (N0.getOpcode() == ISD::LOAD || |
| 2652 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2653 | N0.getOperand(0).getOpcode() == ISD::LOAD))) { |
| 2654 | bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; |
| 2655 | LoadSDNode *LN0 = HasAnyExt |
| 2656 | ? cast<LoadSDNode>(N0.getOperand(0)) |
| 2657 | : cast<LoadSDNode>(N0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2658 | if (LN0->getExtensionType() != ISD::SEXTLOAD && |
Chris Lattner | bd1fccf | 2010-01-07 21:59:23 +0000 | [diff] [blame] | 2659 | LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) { |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2660 | uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits(); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2661 | if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){ |
| 2662 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); |
| 2663 | EVT LoadedVT = LN0->getMemoryVT(); |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2664 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2665 | if (ExtVT == LoadedVT && |
| 2666 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2667 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2668 | |
| 2669 | SDValue NewLoad = |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2670 | DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2671 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2672 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2673 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2674 | LN0->getAlignment()); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2675 | AddToWorkList(N); |
| 2676 | CombineTo(LN0, NewLoad, NewLoad.getValue(1)); |
| 2677 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2678 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2680 | // Do not change the width of a volatile load. |
| 2681 | // Do not generate loads of non-round integer types since these can |
| 2682 | // be expensive (and would be wrong if the type is not byte sized). |
| 2683 | if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() && |
| 2684 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
| 2685 | EVT PtrType = LN0->getOperand(1).getValueType(); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2686 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2687 | unsigned Alignment = LN0->getAlignment(); |
| 2688 | SDValue NewPtr = LN0->getBasePtr(); |
| 2689 | |
| 2690 | // For big endian targets, we need to add an offset to the pointer |
| 2691 | // to load the correct bytes. For little endian systems, we merely |
| 2692 | // need to read fewer bytes from the same pointer. |
| 2693 | if (TLI.isBigEndian()) { |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2694 | unsigned LVTStoreBytes = LoadedVT.getStoreSize(); |
| 2695 | unsigned EVTStoreBytes = ExtVT.getStoreSize(); |
| 2696 | unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2697 | NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType, |
| 2698 | NewPtr, DAG.getConstant(PtrOff, PtrType)); |
| 2699 | Alignment = MinAlign(Alignment, PtrOff); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2700 | } |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2701 | |
| 2702 | AddToWorkList(NewPtr.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2703 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2704 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
| 2705 | SDValue Load = |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2706 | DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2707 | LN0->getChain(), NewPtr, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2708 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2709 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2710 | Alignment); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2711 | AddToWorkList(N); |
| 2712 | CombineTo(LN0, Load, Load.getValue(1)); |
| 2713 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2714 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2715 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 2716 | } |
| 2717 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2718 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2719 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2722 | /// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16 |
| 2723 | /// |
| 2724 | SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 2725 | bool DemandHighBits) { |
| 2726 | if (!LegalOperations) |
| 2727 | return SDValue(); |
| 2728 | |
| 2729 | EVT VT = N->getValueType(0); |
| 2730 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16) |
| 2731 | return SDValue(); |
| 2732 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 2733 | return SDValue(); |
| 2734 | |
| 2735 | // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00) |
| 2736 | bool LookPassAnd0 = false; |
| 2737 | bool LookPassAnd1 = false; |
| 2738 | if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL) |
| 2739 | std::swap(N0, N1); |
| 2740 | if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL) |
| 2741 | std::swap(N0, N1); |
| 2742 | if (N0.getOpcode() == ISD::AND) { |
| 2743 | if (!N0.getNode()->hasOneUse()) |
| 2744 | return SDValue(); |
| 2745 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2746 | if (!N01C || N01C->getZExtValue() != 0xFF00) |
| 2747 | return SDValue(); |
| 2748 | N0 = N0.getOperand(0); |
| 2749 | LookPassAnd0 = true; |
| 2750 | } |
| 2751 | |
| 2752 | if (N1.getOpcode() == ISD::AND) { |
| 2753 | if (!N1.getNode()->hasOneUse()) |
| 2754 | return SDValue(); |
| 2755 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2756 | if (!N11C || N11C->getZExtValue() != 0xFF) |
| 2757 | return SDValue(); |
| 2758 | N1 = N1.getOperand(0); |
| 2759 | LookPassAnd1 = true; |
| 2760 | } |
| 2761 | |
| 2762 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) |
| 2763 | std::swap(N0, N1); |
| 2764 | if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) |
| 2765 | return SDValue(); |
| 2766 | if (!N0.getNode()->hasOneUse() || |
| 2767 | !N1.getNode()->hasOneUse()) |
| 2768 | return SDValue(); |
| 2769 | |
| 2770 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2771 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2772 | if (!N01C || !N11C) |
| 2773 | return SDValue(); |
| 2774 | if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8) |
| 2775 | return SDValue(); |
| 2776 | |
| 2777 | // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8) |
| 2778 | SDValue N00 = N0->getOperand(0); |
| 2779 | if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) { |
| 2780 | if (!N00.getNode()->hasOneUse()) |
| 2781 | return SDValue(); |
| 2782 | ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1)); |
| 2783 | if (!N001C || N001C->getZExtValue() != 0xFF) |
| 2784 | return SDValue(); |
| 2785 | N00 = N00.getOperand(0); |
| 2786 | LookPassAnd0 = true; |
| 2787 | } |
| 2788 | |
| 2789 | SDValue N10 = N1->getOperand(0); |
| 2790 | if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) { |
| 2791 | if (!N10.getNode()->hasOneUse()) |
| 2792 | return SDValue(); |
| 2793 | ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1)); |
| 2794 | if (!N101C || N101C->getZExtValue() != 0xFF00) |
| 2795 | return SDValue(); |
| 2796 | N10 = N10.getOperand(0); |
| 2797 | LookPassAnd1 = true; |
| 2798 | } |
| 2799 | |
| 2800 | if (N00 != N10) |
| 2801 | return SDValue(); |
| 2802 | |
| 2803 | // Make sure everything beyond the low halfword is zero since the SRL 16 |
| 2804 | // will clear the top bits. |
| 2805 | unsigned OpSizeInBits = VT.getSizeInBits(); |
| 2806 | if (DemandHighBits && OpSizeInBits > 16 && |
| 2807 | (!LookPassAnd0 || !LookPassAnd1) && |
| 2808 | !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16))) |
| 2809 | return SDValue(); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 2810 | |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2811 | SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00); |
| 2812 | if (OpSizeInBits > 16) |
| 2813 | Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res, |
| 2814 | DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT))); |
| 2815 | return Res; |
| 2816 | } |
| 2817 | |
| 2818 | /// isBSwapHWordElement - Return true if the specified node is an element |
| 2819 | /// that makes up a 32-bit packed halfword byteswap. i.e. |
| 2820 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| 2821 | static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) { |
| 2822 | if (!N.getNode()->hasOneUse()) |
| 2823 | return false; |
| 2824 | |
| 2825 | unsigned Opc = N.getOpcode(); |
| 2826 | if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL) |
| 2827 | return false; |
| 2828 | |
| 2829 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2830 | if (!N1C) |
| 2831 | return false; |
| 2832 | |
| 2833 | unsigned Num; |
| 2834 | switch (N1C->getZExtValue()) { |
| 2835 | default: |
| 2836 | return false; |
| 2837 | case 0xFF: Num = 0; break; |
| 2838 | case 0xFF00: Num = 1; break; |
| 2839 | case 0xFF0000: Num = 2; break; |
| 2840 | case 0xFF000000: Num = 3; break; |
| 2841 | } |
| 2842 | |
| 2843 | // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00). |
| 2844 | SDValue N0 = N.getOperand(0); |
| 2845 | if (Opc == ISD::AND) { |
| 2846 | if (Num == 0 || Num == 2) { |
| 2847 | // (x >> 8) & 0xff |
| 2848 | // (x >> 8) & 0xff0000 |
| 2849 | if (N0.getOpcode() != ISD::SRL) |
| 2850 | return false; |
| 2851 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2852 | if (!C || C->getZExtValue() != 8) |
| 2853 | return false; |
| 2854 | } else { |
| 2855 | // (x << 8) & 0xff00 |
| 2856 | // (x << 8) & 0xff000000 |
| 2857 | if (N0.getOpcode() != ISD::SHL) |
| 2858 | return false; |
| 2859 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2860 | if (!C || C->getZExtValue() != 8) |
| 2861 | return false; |
| 2862 | } |
| 2863 | } else if (Opc == ISD::SHL) { |
| 2864 | // (x & 0xff) << 8 |
| 2865 | // (x & 0xff0000) << 8 |
| 2866 | if (Num != 0 && Num != 2) |
| 2867 | return false; |
| 2868 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2869 | if (!C || C->getZExtValue() != 8) |
| 2870 | return false; |
| 2871 | } else { // Opc == ISD::SRL |
| 2872 | // (x & 0xff00) >> 8 |
| 2873 | // (x & 0xff000000) >> 8 |
| 2874 | if (Num != 1 && Num != 3) |
| 2875 | return false; |
| 2876 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 2877 | if (!C || C->getZExtValue() != 8) |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
| 2881 | if (Parts[Num]) |
| 2882 | return false; |
| 2883 | |
| 2884 | Parts[Num] = N0.getOperand(0).getNode(); |
| 2885 | return true; |
| 2886 | } |
| 2887 | |
| 2888 | /// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is |
| 2889 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| 2890 | /// => (rotl (bswap x), 16) |
| 2891 | SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) { |
| 2892 | if (!LegalOperations) |
| 2893 | return SDValue(); |
| 2894 | |
| 2895 | EVT VT = N->getValueType(0); |
| 2896 | if (VT != MVT::i32) |
| 2897 | return SDValue(); |
| 2898 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 2899 | return SDValue(); |
| 2900 | |
| 2901 | SmallVector<SDNode*,4> Parts(4, (SDNode*)0); |
| 2902 | // Look for either |
| 2903 | // (or (or (and), (and)), (or (and), (and))) |
| 2904 | // (or (or (or (and), (and)), (and)), (and)) |
| 2905 | if (N0.getOpcode() != ISD::OR) |
| 2906 | return SDValue(); |
| 2907 | SDValue N00 = N0.getOperand(0); |
| 2908 | SDValue N01 = N0.getOperand(1); |
| 2909 | |
| 2910 | if (N1.getOpcode() == ISD::OR) { |
| 2911 | // (or (or (and), (and)), (or (and), (and))) |
| 2912 | SDValue N000 = N00.getOperand(0); |
| 2913 | if (!isBSwapHWordElement(N000, Parts)) |
| 2914 | return SDValue(); |
| 2915 | |
| 2916 | SDValue N001 = N00.getOperand(1); |
| 2917 | if (!isBSwapHWordElement(N001, Parts)) |
| 2918 | return SDValue(); |
| 2919 | SDValue N010 = N01.getOperand(0); |
| 2920 | if (!isBSwapHWordElement(N010, Parts)) |
| 2921 | return SDValue(); |
| 2922 | SDValue N011 = N01.getOperand(1); |
| 2923 | if (!isBSwapHWordElement(N011, Parts)) |
| 2924 | return SDValue(); |
| 2925 | } else { |
| 2926 | // (or (or (or (and), (and)), (and)), (and)) |
| 2927 | if (!isBSwapHWordElement(N1, Parts)) |
| 2928 | return SDValue(); |
| 2929 | if (!isBSwapHWordElement(N01, Parts)) |
| 2930 | return SDValue(); |
| 2931 | if (N00.getOpcode() != ISD::OR) |
| 2932 | return SDValue(); |
| 2933 | SDValue N000 = N00.getOperand(0); |
| 2934 | if (!isBSwapHWordElement(N000, Parts)) |
| 2935 | return SDValue(); |
| 2936 | SDValue N001 = N00.getOperand(1); |
| 2937 | if (!isBSwapHWordElement(N001, Parts)) |
| 2938 | return SDValue(); |
| 2939 | } |
| 2940 | |
| 2941 | // Make sure the parts are all coming from the same node. |
| 2942 | if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3]) |
| 2943 | return SDValue(); |
| 2944 | |
| 2945 | SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, |
| 2946 | SDValue(Parts[0],0)); |
| 2947 | |
| 2948 | // Result of the bswap should be rotated by 16. If it's not legal, than |
| 2949 | // do (x << 16) | (x >> 16). |
| 2950 | SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT)); |
| 2951 | if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT)) |
| 2952 | return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt); |
| 2953 | else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT)) |
| 2954 | return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt); |
| 2955 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, |
| 2956 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt), |
| 2957 | DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt)); |
| 2958 | } |
| 2959 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2960 | SDValue DAGCombiner::visitOR(SDNode *N) { |
| 2961 | SDValue N0 = N->getOperand(0); |
| 2962 | SDValue N1 = N->getOperand(1); |
| 2963 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2964 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2965 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2966 | EVT VT = N1.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2967 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2968 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2969 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2970 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2971 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2972 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2973 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2974 | // fold (or x, undef) -> -1 |
Bob Wilson | 8674949 | 2010-06-28 23:40:25 +0000 | [diff] [blame] | 2975 | if (!LegalOperations && |
| 2976 | (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) { |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2977 | EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT; |
| 2978 | return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); |
| 2979 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2980 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2981 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2982 | return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2983 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2984 | if (N0C && !N1C) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2985 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2986 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2987 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2988 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2989 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2990 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2991 | return N1; |
| 2992 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2993 | if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2994 | return N1; |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2995 | |
| 2996 | // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16) |
| 2997 | SDValue BSwap = MatchBSwapHWord(N, N0, N1); |
| 2998 | if (BSwap.getNode() != 0) |
| 2999 | return BSwap; |
| 3000 | BSwap = MatchBSwapHWordLow(N, N0, N1); |
| 3001 | if (BSwap.getNode() != 0) |
| 3002 | return BSwap; |
| 3003 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3004 | // reassociate or |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 3005 | SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3006 | if (ROR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3007 | return ROR; |
| 3008 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 3009 | // iff (c1 & c2) == 0. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3010 | if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3011 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3012 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
Bill Wendling | 32f9eb2 | 2010-03-03 01:58:01 +0000 | [diff] [blame] | 3013 | if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 3014 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 3015 | DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 3016 | N0.getOperand(0), N1), |
| 3017 | DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3018 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3019 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 3020 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 3021 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 3022 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3023 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3024 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3025 | LL.getValueType().isInteger()) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3026 | // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) |
| 3027 | // 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] | 3028 | if (cast<ConstantSDNode>(LR)->isNullValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3029 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3030 | SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(), |
| 3031 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3032 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3033 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3034 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3035 | // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) |
| 3036 | // 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] | 3037 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3038 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3039 | SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(), |
| 3040 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3041 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3042 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | // canonicalize equivalent to ll == rl |
| 3046 | if (LL == RR && LR == RL) { |
| 3047 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 3048 | std::swap(RL, RR); |
| 3049 | } |
| 3050 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3051 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3052 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 3053 | if (Result != ISD::SETCC_INVALID && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3054 | (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType()))) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3055 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 3056 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3057 | } |
| 3058 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3059 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3060 | // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3061 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3062 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3063 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3064 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3065 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3066 | // (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] | 3067 | if (N0.getOpcode() == ISD::AND && |
| 3068 | N1.getOpcode() == ISD::AND && |
| 3069 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 3070 | N1.getOperand(1).getOpcode() == ISD::Constant && |
| 3071 | // Don't increase # computations. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3072 | (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3073 | // We can only do this xform if we know that bits from X that are set in C2 |
| 3074 | // but not in C1 are already zero. Likewise for Y. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3075 | const APInt &LHSMask = |
| 3076 | cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 3077 | const APInt &RHSMask = |
| 3078 | cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3079 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3080 | if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
| 3081 | DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3082 | SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 3083 | N0.getOperand(0), N1.getOperand(0)); |
| 3084 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X, |
| 3085 | DAG.getConstant(LHSMask | RHSMask, VT)); |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3088 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3089 | // See if this is some rotate idiom. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3090 | if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3091 | return SDValue(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3092 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3093 | // Simplify the operands using demanded-bits information. |
| 3094 | if (!VT.isVector() && |
| 3095 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3096 | return SDValue(N, 0); |
| 3097 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3098 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3101 | /// 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] | 3102 | static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3103 | if (Op.getOpcode() == ISD::AND) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3104 | if (isa<ConstantSDNode>(Op.getOperand(1))) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3105 | Mask = Op.getOperand(1); |
| 3106 | Op = Op.getOperand(0); |
| 3107 | } else { |
| 3108 | return false; |
| 3109 | } |
| 3110 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3112 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 3113 | Shift = Op; |
| 3114 | return true; |
| 3115 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3116 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3117 | return false; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3120 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 3121 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 3122 | // a rot[lr]. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3123 | SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3124 | // 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] | 3125 | EVT VT = LHS.getValueType(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3126 | if (!TLI.isTypeLegal(VT)) return 0; |
| 3127 | |
| 3128 | // The target must have at least one rotate flavor. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3129 | bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); |
| 3130 | bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3131 | if (!HasROTL && !HasROTR) return 0; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3132 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3133 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3134 | SDValue LHSShift; // The shift. |
| 3135 | SDValue LHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3136 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 3137 | return 0; // Not part of a rotate. |
| 3138 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3139 | SDValue RHSShift; // The shift. |
| 3140 | SDValue RHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3141 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 3142 | return 0; // Not part of a rotate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3143 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3144 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 3145 | return 0; // Not shifting the same value. |
| 3146 | |
| 3147 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 3148 | return 0; // Shifts must disagree. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3150 | // Canonicalize shl to left side in a shl/srl pair. |
| 3151 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 3152 | std::swap(LHS, RHS); |
| 3153 | std::swap(LHSShift, RHSShift); |
| 3154 | std::swap(LHSMask , RHSMask ); |
| 3155 | } |
| 3156 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3157 | unsigned OpSizeInBits = VT.getSizeInBits(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3158 | SDValue LHSShiftArg = LHSShift.getOperand(0); |
| 3159 | SDValue LHSShiftAmt = LHSShift.getOperand(1); |
| 3160 | SDValue RHSShiftAmt = RHSShift.getOperand(1); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3161 | |
| 3162 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 3163 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3164 | if (LHSShiftAmt.getOpcode() == ISD::Constant && |
| 3165 | RHSShiftAmt.getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3166 | uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue(); |
| 3167 | uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3168 | if ((LShVal + RShVal) != OpSizeInBits) |
| 3169 | return 0; |
| 3170 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3171 | SDValue Rot; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3172 | if (HasROTL) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3173 | Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3174 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3175 | Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3176 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3177 | // 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] | 3178 | if (LHSMask.getNode() || RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3179 | APInt Mask = APInt::getAllOnesValue(OpSizeInBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3180 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3181 | if (LHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3182 | APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal); |
| 3183 | Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3184 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3185 | if (RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3186 | APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal); |
| 3187 | Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3188 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3189 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3190 | Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT)); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3191 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3192 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3193 | return Rot.getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3194 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3195 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3196 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 3197 | // that we're masking out the right stuff. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3198 | if (LHSMask.getNode() || RHSMask.getNode()) |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3199 | return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3200 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3201 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 3202 | // 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] | 3203 | if (RHSShiftAmt.getOpcode() == ISD::SUB && |
| 3204 | LHSShiftAmt == RHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3205 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3206 | dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3207 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3208 | if (HasROTL) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3209 | return DAG.getNode(ISD::ROTL, DL, VT, |
| 3210 | LHSShiftArg, LHSShiftAmt).getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3211 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3212 | return DAG.getNode(ISD::ROTR, DL, VT, |
| 3213 | LHSShiftArg, RHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3214 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3217 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3218 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 3219 | // 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] | 3220 | if (LHSShiftAmt.getOpcode() == ISD::SUB && |
| 3221 | RHSShiftAmt == LHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3222 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3223 | dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3224 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 2692d59 | 2008-08-31 01:13:31 +0000 | [diff] [blame] | 3225 | if (HasROTR) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3226 | return DAG.getNode(ISD::ROTR, DL, VT, |
| 3227 | LHSShiftArg, RHSShiftAmt).getNode(); |
Bill Wendling | 2692d59 | 2008-08-31 01:13:31 +0000 | [diff] [blame] | 3228 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3229 | return DAG.getNode(ISD::ROTL, DL, VT, |
| 3230 | LHSShiftArg, LHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3231 | } |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3235 | // Look for sign/zext/any-extended or truncate cases: |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3236 | if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND |
| 3237 | || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3238 | || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND |
| 3239 | || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3240 | (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND |
| 3241 | || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3242 | || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND |
| 3243 | || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3244 | SDValue LExtOp0 = LHSShiftAmt.getOperand(0); |
| 3245 | SDValue RExtOp0 = RHSShiftAmt.getOperand(0); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3246 | if (RExtOp0.getOpcode() == ISD::SUB && |
| 3247 | RExtOp0.getOperand(1) == LExtOp0) { |
| 3248 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3249 | // (rotl x, y) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3250 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3251 | // (rotr x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3252 | if (ConstantSDNode *SUBC = |
| 3253 | dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3254 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3255 | return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
| 3256 | LHSShiftArg, |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3257 | HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | } else if (LExtOp0.getOpcode() == ISD::SUB && |
| 3261 | RExtOp0 == LExtOp0.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3262 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3263 | // (rotr x, y) |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 3264 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 3265 | // (rotl x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 3266 | if (ConstantSDNode *SUBC = |
| 3267 | dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3268 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3269 | return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, |
| 3270 | LHSShiftArg, |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 3271 | HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3272 | } |
| 3273 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3274 | } |
| 3275 | } |
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 | return 0; |
| 3278 | } |
| 3279 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3280 | SDValue DAGCombiner::visitXOR(SDNode *N) { |
| 3281 | SDValue N0 = N->getOperand(0); |
| 3282 | SDValue N1 = N->getOperand(1); |
| 3283 | SDValue LHS, RHS, CC; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3284 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3285 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3286 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3287 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3288 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3289 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3290 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3291 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3292 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3293 | |
Evan Cheng | 26471c4 | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 3294 | // fold (xor undef, undef) -> 0. This is a common idiom (misuse). |
| 3295 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 3296 | return DAG.getConstant(0, VT); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3297 | // fold (xor x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 3298 | if (N0.getOpcode() == ISD::UNDEF) |
| 3299 | return N0; |
| 3300 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3301 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3302 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3303 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3304 | return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3305 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3306 | if (N0C && !N1C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3307 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3308 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3309 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3310 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3311 | // reassociate xor |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 3312 | SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3313 | if (RXOR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3314 | return RXOR; |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3315 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3316 | // fold !(x cc y) -> (x !cc y) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3317 | if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3318 | bool isInt = LHS.getValueType().isInteger(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3319 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 3320 | isInt); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3321 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3322 | if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) { |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3323 | switch (N0.getOpcode()) { |
| 3324 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3325 | llvm_unreachable("Unhandled SetCC Equivalent!"); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3326 | case ISD::SETCC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3327 | return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3328 | case ISD::SELECT_CC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3329 | return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2), |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3330 | N0.getOperand(3), NotCC); |
| 3331 | } |
| 3332 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3333 | } |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3334 | |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3335 | // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3336 | if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3337 | N0.getNode()->hasOneUse() && |
| 3338 | isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3339 | SDValue V = N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3340 | V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V, |
Duncan Sands | 272dce0 | 2007-10-10 09:54:50 +0000 | [diff] [blame] | 3341 | DAG.getConstant(1, V.getValueType())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3342 | AddToWorkList(V.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3343 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V); |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3344 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3345 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3346 | // 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] | 3347 | if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3348 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3349 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3350 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 3351 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3352 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 3353 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3354 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3355 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3358 | // 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] | 3359 | if (N1C && N1C->isAllOnesValue() && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3360 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3361 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3362 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 3363 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3364 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 3365 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3366 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3367 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3368 | } |
| 3369 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3370 | // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3371 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 3372 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 3373 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3374 | if (N00C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3375 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1), |
| 3376 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3377 | N00C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3378 | if (N01C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3379 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3380 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3381 | N01C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3382 | } |
| 3383 | // fold (xor x, x) -> 0 |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 3384 | if (N0 == N1) |
| 3385 | return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3386 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3387 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 3388 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3389 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3390 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3391 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3392 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3393 | // Simplify the expression using non-local knowledge. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3394 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3395 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3396 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3397 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3398 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3401 | /// visitShiftByConstant - Handle transforms common to the three shifts, when |
| 3402 | /// the shift amount is a constant. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3403 | SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3404 | SDNode *LHS = N->getOperand(0).getNode(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3405 | if (!LHS->hasOneUse()) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3407 | // We want to pull some binops through shifts, so that we have (and (shift)) |
| 3408 | // instead of (shift (and)), likewise for add, or, xor, etc. This sort of |
| 3409 | // thing happens with address calculations, so it's important to canonicalize |
| 3410 | // it. |
| 3411 | 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] | 3412 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3413 | switch (LHS->getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3414 | default: return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3415 | case ISD::OR: |
| 3416 | case ISD::XOR: |
| 3417 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3418 | break; |
| 3419 | case ISD::AND: |
| 3420 | HighBitSet = true; // We can only transform sra if the high bit is set. |
| 3421 | break; |
| 3422 | case ISD::ADD: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3423 | if (N->getOpcode() != ISD::SHL) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3424 | return SDValue(); // only shl(add) not sr[al](add). |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3425 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3426 | break; |
| 3427 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3428 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3429 | // We require the RHS of the binop to be a constant as well. |
| 3430 | ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3431 | if (!BinOpCst) return SDValue(); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3432 | |
| 3433 | // FIXME: disable this unless the input to the binop is a shift by a constant. |
| 3434 | // If it is not a shift, it pessimizes some common cases like: |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3435 | // |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3436 | // void foo(int *X, int i) { X[i & 1235] = 1; } |
| 3437 | // int bar(int *X, int i) { return X[i & 255]; } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3438 | SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3439 | if ((BinOpLHSVal->getOpcode() != ISD::SHL && |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3440 | BinOpLHSVal->getOpcode() != ISD::SRA && |
| 3441 | BinOpLHSVal->getOpcode() != ISD::SRL) || |
| 3442 | !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3443 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3444 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3445 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3446 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3447 | // If this is a signed shift right, and the high bit is modified by the |
| 3448 | // logical operation, do not perform the transformation. The highBitSet |
| 3449 | // boolean indicates the value of the high bit of the constant which would |
| 3450 | // cause it to be modified for this operation. |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3451 | if (N->getOpcode() == ISD::SRA) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3452 | bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); |
| 3453 | if (BinOpRHSSignSet != HighBitSet) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3454 | return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3455 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3456 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3457 | // Fold the constants, shifting the binop RHS by the shift amount. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3458 | SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(), |
| 3459 | N->getValueType(0), |
| 3460 | LHS->getOperand(1), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3461 | |
| 3462 | // Create the new shift. |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3463 | SDValue NewShift = DAG.getNode(N->getOpcode(), |
| 3464 | LHS->getOperand(0).getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3465 | VT, LHS->getOperand(0), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3466 | |
| 3467 | // Create the new binop. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3468 | return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3471 | SDValue DAGCombiner::visitSHL(SDNode *N) { |
| 3472 | SDValue N0 = N->getOperand(0); |
| 3473 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3474 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3475 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3476 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3477 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3478 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3479 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3480 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3481 | return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3482 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3483 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3484 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3485 | // fold (shl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3486 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3487 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3488 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3489 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3490 | return N0; |
Chad Rosier | 92bcd96 | 2011-06-14 22:29:10 +0000 | [diff] [blame] | 3491 | // fold (shl undef, x) -> 0 |
| 3492 | if (N0.getOpcode() == ISD::UNDEF) |
| 3493 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3494 | // if (shl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3495 | if (DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3496 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3497 | return DAG.getConstant(0, VT); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3498 | // 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] | 3499 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3500 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3501 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3502 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3503 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3504 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3505 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3506 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3507 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3508 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3509 | DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT, |
| 3510 | DAG.getNode(ISD::TRUNCATE, |
| 3511 | N->getDebugLoc(), |
| 3512 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3513 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3517 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3518 | return SDValue(N, 0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3519 | |
| 3520 | // 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] | 3521 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3522 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3523 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3524 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3525 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3526 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3527 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3528 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3529 | } |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3530 | |
| 3531 | // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) |
| 3532 | // For this to be valid, the second form must not preserve any of the bits |
| 3533 | // that are shifted out by the inner shift in the first form. This means |
| 3534 | // the outer shift size must be >= the number of bits added by the ext. |
| 3535 | // As a corollary, we don't care what kind of ext it is. |
| 3536 | if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 3537 | N0.getOpcode() == ISD::ANY_EXTEND || |
| 3538 | N0.getOpcode() == ISD::SIGN_EXTEND) && |
| 3539 | N0.getOperand(0).getOpcode() == ISD::SHL && |
| 3540 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3541 | uint64_t c1 = |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3542 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3543 | uint64_t c2 = N1C->getZExtValue(); |
| 3544 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3545 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
| 3546 | if (c2 >= OpSizeInBits - InnerShiftSize) { |
| 3547 | if (c1 + c2 >= OpSizeInBits) |
| 3548 | return DAG.getConstant(0, VT); |
| 3549 | return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT, |
| 3550 | DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT, |
| 3551 | N0.getOperand(0)->getOperand(0)), |
| 3552 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| 3553 | } |
| 3554 | } |
| 3555 | |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3556 | // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or |
| 3557 | // (and (srl x, (sub c1, c2), MASK) |
Chandler Carruth | 62dfc51 | 2012-01-05 11:05:55 +0000 | [diff] [blame] | 3558 | // Only fold this if the inner shift has no other uses -- if it does, folding |
| 3559 | // this will increase the total number of instructions. |
| 3560 | if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3561 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3562 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3563 | if (c1 < VT.getSizeInBits()) { |
| 3564 | uint64_t c2 = N1C->getZExtValue(); |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3565 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3566 | VT.getSizeInBits() - c1); |
| 3567 | SDValue Shift; |
| 3568 | if (c2 > c1) { |
| 3569 | Mask = Mask.shl(c2-c1); |
| 3570 | Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3571 | DAG.getConstant(c2-c1, N1.getValueType())); |
| 3572 | } else { |
| 3573 | Mask = Mask.lshr(c1-c2); |
| 3574 | Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3575 | DAG.getConstant(c1-c2, N1.getValueType())); |
| 3576 | } |
| 3577 | return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift, |
| 3578 | DAG.getConstant(Mask, VT)); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3579 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3580 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3581 | // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3582 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) { |
| 3583 | SDValue HiBitsMask = |
| 3584 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3585 | VT.getSizeInBits() - |
| 3586 | N1C->getZExtValue()), |
| 3587 | VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3588 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3589 | HiBitsMask); |
| 3590 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3591 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3592 | if (N1C) { |
| 3593 | SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3594 | if (NewSHL.getNode()) |
| 3595 | return NewSHL; |
| 3596 | } |
| 3597 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3598 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3601 | SDValue DAGCombiner::visitSRA(SDNode *N) { |
| 3602 | SDValue N0 = N->getOperand(0); |
| 3603 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3604 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3605 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3606 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3607 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3608 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3609 | // fold (sra c1, c2) -> (sra c1, c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3610 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3611 | return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3612 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3613 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3614 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3615 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3616 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3617 | return N0; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3618 | // fold (sra x, (setge c, size(x))) -> undef |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3619 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3620 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3621 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3622 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3623 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3624 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 3625 | // sext_inreg. |
| 3626 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3627 | unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3628 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); |
| 3629 | if (VT.isVector()) |
| 3630 | ExtVT = EVT::getVectorVT(*DAG.getContext(), |
| 3631 | ExtVT, VT.getVectorNumElements()); |
| 3632 | if ((!LegalOperations || |
| 3633 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3634 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3635 | N0.getOperand(0), DAG.getValueType(ExtVT)); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3636 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3637 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3638 | // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3639 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 3640 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3641 | unsigned Sum = N1C->getZExtValue() + C1->getZExtValue(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3642 | if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3643 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3644 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 3645 | } |
| 3646 | } |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3647 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3648 | // fold (sra (shl X, m), (sub result_size, n)) |
| 3649 | // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3650 | // result_size - n != m. |
| 3651 | // 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] | 3652 | // code. |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3653 | if (N0.getOpcode() == ISD::SHL) { |
| 3654 | // Get the two constanst of the shifts, CN0 = m, CN = n. |
| 3655 | const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3656 | if (N01C && N1C) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3657 | // Determine what the truncate's result bitsize and type would be. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3658 | EVT TruncVT = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3659 | EVT::getIntegerVT(*DAG.getContext(), |
| 3660 | OpSizeInBits - N1C->getZExtValue()); |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3661 | // Determine the residual right-shift amount. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3662 | signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3663 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3664 | // If the shift is not a no-op (in which case this should be just a sign |
| 3665 | // extend already), the truncated to type is legal, sign_extend is legal |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 3666 | // 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] | 3667 | // perform the transform. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3668 | if ((ShiftAmt > 0) && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3669 | TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && |
| 3670 | TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 3671 | TLI.isTruncateFree(VT, TruncVT)) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3672 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3673 | SDValue Amt = DAG.getConstant(ShiftAmt, |
| 3674 | getShiftAmountTy(N0.getOperand(0).getValueType())); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3675 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, |
| 3676 | N0.getOperand(0), Amt); |
| 3677 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT, |
| 3678 | Shift); |
| 3679 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), |
| 3680 | N->getValueType(0), Trunc); |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3684 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3685 | // 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] | 3686 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3687 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3688 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3689 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3690 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3691 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3692 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3693 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3694 | TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3695 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3696 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3697 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3698 | DAG.getNode(ISD::TRUNCATE, |
| 3699 | N->getDebugLoc(), |
| 3700 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3701 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3702 | } |
| 3703 | } |
| 3704 | |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3705 | // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2)) |
| 3706 | // if c1 is equal to the number of bits the trunc removes |
| 3707 | if (N0.getOpcode() == ISD::TRUNCATE && |
| 3708 | (N0.getOperand(0).getOpcode() == ISD::SRL || |
| 3709 | N0.getOperand(0).getOpcode() == ISD::SRA) && |
| 3710 | N0.getOperand(0).hasOneUse() && |
| 3711 | N0.getOperand(0).getOperand(1).hasOneUse() && |
| 3712 | N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) { |
| 3713 | EVT LargeVT = N0.getOperand(0).getValueType(); |
| 3714 | ConstantSDNode *LargeShiftAmt = |
| 3715 | cast<ConstantSDNode>(N0.getOperand(0).getOperand(1)); |
| 3716 | |
| 3717 | if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits == |
| 3718 | LargeShiftAmt->getZExtValue()) { |
| 3719 | SDValue Amt = |
| 3720 | DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3721 | getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType())); |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3722 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT, |
| 3723 | N0.getOperand(0).getOperand(0), Amt); |
| 3724 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA); |
| 3725 | } |
| 3726 | } |
| 3727 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3728 | // Simplify, based on bits shifted out of the LHS. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3729 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3730 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3731 | |
| 3732 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3733 | // 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] | 3734 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3735 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3736 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3737 | if (N1C) { |
| 3738 | SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3739 | if (NewSRA.getNode()) |
| 3740 | return NewSRA; |
| 3741 | } |
| 3742 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3743 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3746 | SDValue DAGCombiner::visitSRL(SDNode *N) { |
| 3747 | SDValue N0 = N->getOperand(0); |
| 3748 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3749 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3750 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3751 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3752 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3753 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3754 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3755 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3756 | return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3757 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3758 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3759 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3760 | // fold (srl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3761 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3762 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3763 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3764 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3765 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3766 | // if (srl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3767 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3768 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3769 | return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3770 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3771 | // 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] | 3772 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3773 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3774 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3775 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3776 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3777 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3778 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3779 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3780 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3781 | |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3782 | // 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] | 3783 | if (N1C && N0.getOpcode() == ISD::TRUNCATE && |
| 3784 | N0.getOperand(0).getOpcode() == ISD::SRL && |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3785 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3786 | uint64_t c1 = |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3787 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3788 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3789 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3790 | EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3791 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3792 | // This is only valid if the OpSizeInBits + c1 = size of inner shift. |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3793 | if (c1 + OpSizeInBits == InnerShiftSize) { |
| 3794 | if (c1 + c2 >= InnerShiftSize) |
| 3795 | return DAG.getConstant(0, VT); |
| 3796 | return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3797 | DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT, |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3798 | N0.getOperand(0)->getOperand(0), |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3799 | DAG.getConstant(c1 + c2, ShiftCountVT))); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
Chris Lattner | efcddc3 | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 3803 | // fold (srl (shl x, c), c) -> (and x, cst2) |
| 3804 | if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && |
| 3805 | N0.getValueSizeInBits() <= 64) { |
| 3806 | uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits(); |
| 3807 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3808 | DAG.getConstant(~0ULL >> ShAmt, VT)); |
| 3809 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3810 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3811 | |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3812 | // fold (srl (anyextend x), c) -> (anyextend (srl x, c)) |
| 3813 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 3814 | // Shifting in all undef bits? |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3815 | EVT SmallVT = N0.getOperand(0).getValueType(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3816 | if (N1C->getZExtValue() >= SmallVT.getSizeInBits()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3817 | return DAG.getUNDEF(VT); |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3818 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3819 | if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 3820 | uint64_t ShiftAmt = N1C->getZExtValue(); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3821 | SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT, |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 3822 | N0.getOperand(0), |
| 3823 | DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT))); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3824 | AddToWorkList(SmallShift.getNode()); |
| 3825 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift); |
| 3826 | } |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3827 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3828 | |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3829 | // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign |
| 3830 | // bit, which is unmodified by sra. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3831 | if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) { |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3832 | if (N0.getOpcode() == ISD::SRA) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3833 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1); |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3834 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3835 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3836 | // 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] | 3837 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3838 | N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3839 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 3840 | DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3842 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 3843 | // zeros, thus the result of the srl will always be zero. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3844 | if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3846 | // If all of the bits input the to ctlz node are known to be zero, then |
| 3847 | // 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] | 3848 | APInt UnknownBits = ~KnownZero; |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3849 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3850 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3851 | // 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] | 3852 | if ((UnknownBits & (UnknownBits - 1)) == 0) { |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3853 | // Okay, we know that only that the single bit specified by UnknownBits |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3854 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 3855 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 3856 | // to an SRL/XOR pair, which is likely to simplify more. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3857 | unsigned ShAmt = UnknownBits.countTrailingZeros(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3858 | SDValue Op = N0.getOperand(0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3859 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3860 | if (ShAmt) { |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3861 | Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3862 | DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3863 | AddToWorkList(Op.getNode()); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3864 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3865 | |
| 3866 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 3867 | Op, DAG.getConstant(1, VT)); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3868 | } |
| 3869 | } |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3870 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3871 | // 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] | 3872 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3873 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3874 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3875 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3876 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3877 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3878 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3879 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3880 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3881 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3882 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3883 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3884 | DAG.getNode(ISD::TRUNCATE, |
| 3885 | N->getDebugLoc(), |
| 3886 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3887 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3888 | } |
| 3889 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3890 | |
Chris Lattner | 61a4c07 | 2007-04-18 03:06:49 +0000 | [diff] [blame] | 3891 | // fold operands of srl based on knowledge that the low bits are not |
| 3892 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3893 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3894 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3895 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3896 | if (N1C) { |
| 3897 | SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3898 | if (NewSRL.getNode()) |
| 3899 | return NewSRL; |
| 3900 | } |
| 3901 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3902 | // Attempt to convert a srl of a load into a narrower zero-extending load. |
| 3903 | SDValue NarrowLoad = ReduceLoadWidth(N); |
| 3904 | if (NarrowLoad.getNode()) |
| 3905 | return NarrowLoad; |
| 3906 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3907 | // Here is a common situation. We want to optimize: |
| 3908 | // |
| 3909 | // %a = ... |
| 3910 | // %b = and i32 %a, 2 |
| 3911 | // %c = srl i32 %b, 1 |
| 3912 | // brcond i32 %c ... |
| 3913 | // |
| 3914 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3915 | // |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3916 | // %a = ... |
| 3917 | // %b = and %a, 2 |
| 3918 | // %c = setcc eq %b, 0 |
| 3919 | // brcond %c ... |
| 3920 | // |
| 3921 | // However when after the source operand of SRL is optimized into AND, the SRL |
| 3922 | // itself may not be optimized further. Look for it and add the BRCOND into |
| 3923 | // the worklist. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 3924 | if (N->hasOneUse()) { |
| 3925 | SDNode *Use = *N->use_begin(); |
| 3926 | if (Use->getOpcode() == ISD::BRCOND) |
| 3927 | AddToWorkList(Use); |
| 3928 | else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { |
| 3929 | // Also look pass the truncate. |
| 3930 | Use = *Use->use_begin(); |
| 3931 | if (Use->getOpcode() == ISD::BRCOND) |
| 3932 | AddToWorkList(Use); |
| 3933 | } |
| 3934 | } |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3935 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3936 | return SDValue(); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3939 | SDValue DAGCombiner::visitCTLZ(SDNode *N) { |
| 3940 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3941 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3942 | |
| 3943 | // fold (ctlz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3944 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3945 | return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3946 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 3949 | SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) { |
| 3950 | SDValue N0 = N->getOperand(0); |
| 3951 | EVT VT = N->getValueType(0); |
| 3952 | |
| 3953 | // fold (ctlz_zero_undef c1) -> c2 |
| 3954 | if (isa<ConstantSDNode>(N0)) |
| 3955 | return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0); |
| 3956 | return SDValue(); |
| 3957 | } |
| 3958 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3959 | SDValue DAGCombiner::visitCTTZ(SDNode *N) { |
| 3960 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3961 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3962 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3963 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3964 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3965 | return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3966 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 3969 | SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) { |
| 3970 | SDValue N0 = N->getOperand(0); |
| 3971 | EVT VT = N->getValueType(0); |
| 3972 | |
| 3973 | // fold (cttz_zero_undef c1) -> c2 |
| 3974 | if (isa<ConstantSDNode>(N0)) |
| 3975 | return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0); |
| 3976 | return SDValue(); |
| 3977 | } |
| 3978 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3979 | SDValue DAGCombiner::visitCTPOP(SDNode *N) { |
| 3980 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3981 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3982 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3983 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3984 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3985 | return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3986 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3989 | SDValue DAGCombiner::visitSELECT(SDNode *N) { |
| 3990 | SDValue N0 = N->getOperand(0); |
| 3991 | SDValue N1 = N->getOperand(1); |
| 3992 | SDValue N2 = N->getOperand(2); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3993 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3994 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 3995 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3996 | EVT VT = N->getValueType(0); |
| 3997 | EVT VT0 = N0.getValueType(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3998 | |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3999 | // fold (select C, X, X) -> X |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4000 | if (N1 == N2) |
| 4001 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4002 | // fold (select true, X, Y) -> X |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4003 | if (N0C && !N0C->isNullValue()) |
| 4004 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4005 | // fold (select false, X, Y) -> Y |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4006 | if (N0C && N0C->isNullValue()) |
| 4007 | return N2; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4008 | // fold (select C, 1, X) -> (or C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4009 | if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4010 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 4011 | // fold (select C, 0, 1) -> (xor C, 1) |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4012 | if (VT.isInteger() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4013 | (VT0 == MVT::i1 || |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4014 | (VT0.isInteger() && |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4015 | TLI.getBooleanContents(false) == TargetLowering::ZeroOrOneBooleanContent)) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4016 | N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4017 | SDValue XORNode; |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4018 | if (VT == VT0) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4019 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0, |
| 4020 | N0, DAG.getConstant(1, VT0)); |
| 4021 | XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0, |
| 4022 | N0, DAG.getConstant(1, VT0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4023 | AddToWorkList(XORNode.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4024 | if (VT.bitsGT(VT0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4025 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode); |
| 4026 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode); |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4027 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4028 | // fold (select C, 0, X) -> (and (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4029 | if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) { |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4030 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4031 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4032 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4033 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4034 | // fold (select C, X, 1) -> (or (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4035 | if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4036 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4037 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 4038 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4039 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4040 | // fold (select C, X, 0) -> (and C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4041 | if (VT == MVT::i1 && N2C && N2C->isNullValue()) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4042 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
| 4043 | // fold (select X, X, Y) -> (or X, Y) |
| 4044 | // fold (select X, 1, Y) -> (or X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4045 | if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4046 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 4047 | // fold (select X, Y, X) -> (and X, Y) |
| 4048 | // fold (select X, Y, 0) -> (and X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4049 | if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4050 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4051 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4052 | // If we can fold this based on the true/false value, do so. |
| 4053 | if (SimplifySelectOps(N, N1, N2)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4054 | return SDValue(N, 0); // Don't revisit N. |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4055 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4056 | // 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] | 4057 | if (N0.getOpcode() == ISD::SETCC) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4058 | // FIXME: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4059 | // 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] | 4060 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 4061 | // 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] | 4062 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) && |
Dan Gohman | 4ea4804 | 2009-08-02 16:19:38 +0000 | [diff] [blame] | 4063 | TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4064 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, |
| 4065 | N0.getOperand(0), N0.getOperand(1), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4066 | N1, N2, N0.getOperand(2)); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 4067 | return SimplifySelect(N->getDebugLoc(), N0, N1, N2); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4068 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4069 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4070 | return SDValue(); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4073 | SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { |
| 4074 | SDValue N0 = N->getOperand(0); |
| 4075 | SDValue N1 = N->getOperand(1); |
| 4076 | SDValue N2 = N->getOperand(2); |
| 4077 | SDValue N3 = N->getOperand(3); |
| 4078 | SDValue N4 = N->getOperand(4); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4079 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4080 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4081 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 4082 | if (N2 == N3) |
| 4083 | return N2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4084 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4085 | // Determine if the condition we're dealing with is constant |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 4086 | SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4087 | N0, N1, CC, N->getDebugLoc(), false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4088 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4089 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4090 | if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4091 | if (!SCCC->isNullValue()) |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4092 | return N2; // cond always true -> true val |
| 4093 | else |
| 4094 | return N3; // cond always false -> false val |
| 4095 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4096 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4097 | // Fold to a simpler select_cc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4098 | if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4099 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(), |
| 4100 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4101 | SCC.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4102 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4103 | // If we can fold this based on the true/false value, do so. |
| 4104 | if (SimplifySelectOps(N, N2, N3)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4105 | return SDValue(N, 0); // Don't revisit N. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4106 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4107 | // fold select_cc into other things, such as min/max/abs |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4108 | return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4111 | SDValue DAGCombiner::visitSETCC(SDNode *N) { |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4112 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4113 | cast<CondCodeSDNode>(N->getOperand(2))->get(), |
| 4114 | N->getDebugLoc()); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4117 | // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4118 | // "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] | 4119 | // transformation. Returns true if extension are possible and the above |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4120 | // mentioned transformation is profitable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4121 | static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4122 | unsigned ExtOpc, |
| 4123 | SmallVector<SDNode*, 4> &ExtendNodes, |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 4124 | const TargetLowering &TLI) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4125 | bool HasCopyToRegUses = false; |
| 4126 | bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4127 | for (SDNode::use_iterator UI = N0.getNode()->use_begin(), |
| 4128 | UE = N0.getNode()->use_end(); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4129 | UI != UE; ++UI) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4130 | SDNode *User = *UI; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4131 | if (User == N) |
| 4132 | continue; |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4133 | if (UI.getUse().getResNo() != N0.getResNo()) |
| 4134 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4135 | // FIXME: Only extend SETCC N, N and SETCC N, c for now. |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4136 | if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4137 | ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); |
| 4138 | if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) |
| 4139 | // Sign bits will be lost after a zext. |
| 4140 | return false; |
| 4141 | bool Add = false; |
| 4142 | for (unsigned i = 0; i != 2; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4143 | SDValue UseOp = User->getOperand(i); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4144 | if (UseOp == N0) |
| 4145 | continue; |
| 4146 | if (!isa<ConstantSDNode>(UseOp)) |
| 4147 | return false; |
| 4148 | Add = true; |
| 4149 | } |
| 4150 | if (Add) |
| 4151 | ExtendNodes.push_back(User); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4152 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4153 | } |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4154 | // If truncates aren't free and there are users we can't |
| 4155 | // extend, it isn't worthwhile. |
| 4156 | if (!isTruncFree) |
| 4157 | return false; |
| 4158 | // Remember if this value is live-out. |
| 4159 | if (User->getOpcode() == ISD::CopyToReg) |
| 4160 | HasCopyToRegUses = true; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | if (HasCopyToRegUses) { |
| 4164 | bool BothLiveOut = false; |
| 4165 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 4166 | UI != UE; ++UI) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4167 | SDUse &Use = UI.getUse(); |
| 4168 | if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { |
| 4169 | BothLiveOut = true; |
| 4170 | break; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | if (BothLiveOut) |
| 4174 | // Both unextended and extended values are live out. There had better be |
Bob Wilson | bebfbc5 | 2010-11-28 06:51:19 +0000 | [diff] [blame] | 4175 | // a good reason for the transformation. |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4176 | return ExtendNodes.size(); |
| 4177 | } |
| 4178 | return true; |
| 4179 | } |
| 4180 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4181 | void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs, |
| 4182 | SDValue Trunc, SDValue ExtLoad, DebugLoc DL, |
| 4183 | ISD::NodeType ExtType) { |
| 4184 | // Extend SetCC uses if necessary. |
| 4185 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 4186 | SDNode *SetCC = SetCCs[i]; |
| 4187 | SmallVector<SDValue, 4> Ops; |
| 4188 | |
| 4189 | for (unsigned j = 0; j != 2; ++j) { |
| 4190 | SDValue SOp = SetCC->getOperand(j); |
| 4191 | if (SOp == Trunc) |
| 4192 | Ops.push_back(ExtLoad); |
| 4193 | else |
| 4194 | Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp)); |
| 4195 | } |
| 4196 | |
| 4197 | Ops.push_back(SetCC->getOperand(2)); |
| 4198 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), |
| 4199 | &Ops[0], Ops.size())); |
| 4200 | } |
| 4201 | } |
| 4202 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4203 | SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
| 4204 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4205 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4206 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4207 | // fold (sext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4208 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4209 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4210 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4211 | // fold (sext (sext x)) -> (sext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4212 | // fold (sext (aext x)) -> (sext x) |
| 4213 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4214 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, |
| 4215 | N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4216 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4217 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4218 | // fold (sext (truncate (load x))) -> (sext (smaller load x)) |
| 4219 | // 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] | 4220 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4221 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4222 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4223 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4224 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4225 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4226 | AddToWorkList(oye); |
| 4227 | } |
Dan Gohman | c7b3444 | 2009-04-27 02:00:55 +0000 | [diff] [blame] | 4228 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4229 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4230 | |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4231 | // See if the value being truncated is already sign extended. If so, just |
| 4232 | // eliminate the trunc/sext pair. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4233 | SDValue Op = N0.getOperand(0); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4234 | unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits(); |
| 4235 | unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits(); |
| 4236 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4237 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4238 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4239 | if (OpBits == DestBits) { |
| 4240 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 4241 | // bits, it is already ready. |
| 4242 | if (NumSignBits > DestBits-MidBits) |
| 4243 | return Op; |
| 4244 | } else if (OpBits < DestBits) { |
| 4245 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 4246 | // bits, just sext from i32. |
| 4247 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4248 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4249 | } else { |
| 4250 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 4251 | // bits, just truncate to i32. |
| 4252 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4253 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4254 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4255 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4256 | // fold (sext (truncate x)) -> (sextinreg x). |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4257 | if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
| 4258 | N0.getValueType())) { |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4259 | if (OpBits < DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4260 | Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4261 | else if (OpBits > DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4262 | Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op); |
| 4263 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4264 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4265 | } |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4266 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4267 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4268 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4269 | // 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] | 4270 | // on vectors in one instruction. We only perform this transformation on |
| 4271 | // scalars. |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4272 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4273 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4274 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4275 | bool DoXform = true; |
| 4276 | SmallVector<SDNode*, 4> SetCCs; |
| 4277 | if (!N0.hasOneUse()) |
| 4278 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); |
| 4279 | if (DoXform) { |
| 4280 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4281 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4282 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4283 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4284 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4285 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4286 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4287 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4288 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4289 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4290 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4291 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4292 | ISD::SIGN_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4293 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4294 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 4295 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4296 | |
| 4297 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 4298 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4299 | if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 4300 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4301 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4302 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4303 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4304 | TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4305 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4306 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4307 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4308 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4309 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4310 | LN0->getAlignment()); |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4311 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4312 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4313 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4314 | N0.getValueType(), ExtLoad), |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4315 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4316 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4317 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4318 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4319 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4320 | // fold (sext (and/or/xor (load x), cst)) -> |
| 4321 | // (and/or/xor (sextload x), (sext cst)) |
| 4322 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 4323 | N0.getOpcode() == ISD::XOR) && |
| 4324 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 4325 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4326 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) && |
| 4327 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 4328 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 4329 | if (LN0->getExtensionType() != ISD::ZEXTLOAD) { |
| 4330 | bool DoXform = true; |
| 4331 | SmallVector<SDNode*, 4> SetCCs; |
| 4332 | if (!N0.hasOneUse()) |
| 4333 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND, |
| 4334 | SetCCs, TLI); |
| 4335 | if (DoXform) { |
| 4336 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT, |
| 4337 | LN0->getChain(), LN0->getBasePtr(), |
| 4338 | LN0->getPointerInfo(), |
| 4339 | LN0->getMemoryVT(), |
| 4340 | LN0->isVolatile(), |
| 4341 | LN0->isNonTemporal(), |
| 4342 | LN0->getAlignment()); |
| 4343 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 4344 | Mask = Mask.sext(VT.getSizeInBits()); |
| 4345 | SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 4346 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 4347 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| 4348 | N0.getOperand(0).getDebugLoc(), |
| 4349 | N0.getOperand(0).getValueType(), ExtLoad); |
| 4350 | CombineTo(N, And); |
| 4351 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| 4352 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4353 | ISD::SIGN_EXTEND); |
| 4354 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4359 | if (N0.getOpcode() == ISD::SETCC) { |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4360 | // sext(setcc) -> sext_in_reg(vsetcc) for vectors. |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4361 | // Only do this before legalize for now. |
| 4362 | if (VT.isVector() && !LegalOperations) { |
| 4363 | EVT N0VT = N0.getOperand(0).getValueType(); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4364 | // On some architectures (such as SSE/NEON/etc) the SETCC result type is |
| 4365 | // of the same size as the compared operands. Only optimize sext(setcc()) |
| 4366 | // if this is the case. |
| 4367 | EVT SVT = TLI.getSetCCResultType(N0VT); |
| 4368 | |
| 4369 | // We know that the # elements of the results is the same as the |
| 4370 | // # elements of the compare (and the # elements of the compare result |
| 4371 | // for that matter). Check to see that they are the same size. If so, |
| 4372 | // we know that the element size of the sext'd result matches the |
| 4373 | // element size of the compare operands. |
| 4374 | if (VT.getSizeInBits() == SVT.getSizeInBits()) |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4375 | return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4376 | N0.getOperand(1), |
| 4377 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4378 | // If the desired elements are smaller or larger than the source |
| 4379 | // elements we can use a matching integer vector type and then |
| 4380 | // truncate/sign extend |
| 4381 | else { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4382 | EVT MatchingElementType = |
| 4383 | EVT::getIntegerVT(*DAG.getContext(), |
| 4384 | N0VT.getScalarType().getSizeInBits()); |
| 4385 | EVT MatchingVectorType = |
| 4386 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 4387 | N0VT.getVectorNumElements()); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4388 | |
| 4389 | if (SVT == MatchingVectorType) { |
| 4390 | SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, |
| 4391 | N0.getOperand(0), N0.getOperand(1), |
| 4392 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4393 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
| 4394 | } |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4395 | } |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4396 | } |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4398 | // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc) |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4399 | unsigned ElementWidth = VT.getScalarType().getSizeInBits(); |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4400 | SDValue NegOne = |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4401 | DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4402 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4403 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4404 | NegOne, DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4405 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4406 | if (SCC.getNode()) return SCC; |
Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 4407 | if (!LegalOperations || |
| 4408 | TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT))) |
| 4409 | return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT, |
| 4410 | DAG.getSetCC(N->getDebugLoc(), |
| 4411 | TLI.getSetCCResultType(VT), |
| 4412 | N0.getOperand(0), N0.getOperand(1), |
| 4413 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 4414 | NegOne, DAG.getConstant(0, VT)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4415 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4416 | |
Dan Gohman | 8f0ad58 | 2008-04-28 16:58:24 +0000 | [diff] [blame] | 4417 | // fold (sext x) -> (zext x) if the sign bit is known zero. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4418 | if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && |
Dan Gohman | 187db7b | 2008-04-28 18:47:17 +0000 | [diff] [blame] | 4419 | DAG.SignBitIsZero(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4420 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4421 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4422 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4425 | // isTruncateOf - If N is a truncate of some other value, return true, record |
| 4426 | // the value being truncated in Op and which of Op's bits are zero in KnownZero. |
| 4427 | // This function computes KnownZero to avoid a duplicated call to |
| 4428 | // ComputeMaskedBits in the caller. |
| 4429 | static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op, |
| 4430 | APInt &KnownZero) { |
| 4431 | APInt KnownOne; |
| 4432 | if (N->getOpcode() == ISD::TRUNCATE) { |
| 4433 | Op = N->getOperand(0); |
| 4434 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 4435 | return true; |
| 4436 | } |
| 4437 | |
| 4438 | if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 || |
| 4439 | cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE) |
| 4440 | return false; |
| 4441 | |
| 4442 | SDValue Op0 = N->getOperand(0); |
| 4443 | SDValue Op1 = N->getOperand(1); |
| 4444 | assert(Op0.getValueType() == Op1.getValueType()); |
| 4445 | |
| 4446 | ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0); |
| 4447 | ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1); |
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4448 | if (COp0 && COp0->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4449 | Op = Op1; |
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4450 | else if (COp1 && COp1->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4451 | Op = Op0; |
| 4452 | else |
| 4453 | return false; |
| 4454 | |
| 4455 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 4456 | |
| 4457 | if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue()) |
| 4458 | return false; |
| 4459 | |
| 4460 | return true; |
| 4461 | } |
| 4462 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4463 | SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
| 4464 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4465 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4466 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4467 | // fold (zext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4468 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4469 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4470 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4471 | // fold (zext (aext x)) -> (zext x) |
| 4472 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4473 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, |
| 4474 | N0.getOperand(0)); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4475 | |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4476 | // fold (zext (truncate x)) -> (zext x) or |
| 4477 | // (zext (truncate x)) -> (truncate x) |
| 4478 | // This is valid when the truncated bits of x are already zero. |
| 4479 | // FIXME: We should extend this to work for vectors too. |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4480 | SDValue Op; |
| 4481 | APInt KnownZero; |
| 4482 | if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) { |
| 4483 | APInt TruncatedBits = |
| 4484 | (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ? |
| 4485 | APInt(Op.getValueSizeInBits(), 0) : |
| 4486 | APInt::getBitsSet(Op.getValueSizeInBits(), |
| 4487 | N0.getValueSizeInBits(), |
| 4488 | std::min(Op.getValueSizeInBits(), |
| 4489 | VT.getSizeInBits())); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4490 | if (TruncatedBits == (KnownZero & TruncatedBits)) { |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4491 | if (VT.bitsGT(Op.getValueType())) |
| 4492 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op); |
| 4493 | if (VT.bitsLT(Op.getValueType())) |
| 4494 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
| 4495 | |
| 4496 | return Op; |
| 4497 | } |
| 4498 | } |
| 4499 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4500 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 4501 | // 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] | 4502 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4503 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4504 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4505 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4506 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4507 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4508 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4509 | AddToWorkList(oye); |
| 4510 | } |
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 4511 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4512 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4515 | // fold (zext (truncate x)) -> (and x, mask) |
| 4516 | if (N0.getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4517 | (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4518 | |
| 4519 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 4520 | // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) |
| 4521 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4522 | if (NarrowLoad.getNode()) { |
| 4523 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4524 | if (NarrowLoad.getNode() != N0.getNode()) { |
| 4525 | CombineTo(N0.getNode(), NarrowLoad); |
| 4526 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4527 | AddToWorkList(oye); |
| 4528 | } |
| 4529 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4530 | } |
| 4531 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4532 | SDValue Op = N0.getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4533 | if (Op.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4534 | Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4535 | AddToWorkList(Op.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4536 | } else if (Op.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4537 | Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4538 | AddToWorkList(Op.getNode()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4539 | } |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4540 | return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), |
| 4541 | N0.getValueType().getScalarType()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4542 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4543 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4544 | // Fold (zext (and (trunc x), cst)) -> (and x, cst), |
| 4545 | // if either of the casts is not free. |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4546 | if (N0.getOpcode() == ISD::AND && |
| 4547 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4548 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4549 | (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 4550 | N0.getValueType()) || |
| 4551 | !TLI.isZExtFree(N0.getValueType(), VT))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4552 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4553 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4554 | X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4555 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4556 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4557 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4558 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4559 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4560 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4561 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4562 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4563 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4564 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4565 | // 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] | 4566 | // on vectors in one instruction. We only perform this transformation on |
| 4567 | // scalars. |
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4568 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4569 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4570 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4571 | bool DoXform = true; |
| 4572 | SmallVector<SDNode*, 4> SetCCs; |
| 4573 | if (!N0.hasOneUse()) |
| 4574 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); |
| 4575 | if (DoXform) { |
| 4576 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4577 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4578 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4579 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4580 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4581 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4582 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4583 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4584 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4585 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4586 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4587 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4588 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4589 | ISD::ZERO_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4590 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4591 | } |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4592 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4593 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4594 | // fold (zext (and/or/xor (load x), cst)) -> |
| 4595 | // (and/or/xor (zextload x), (zext cst)) |
| 4596 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 4597 | N0.getOpcode() == ISD::XOR) && |
| 4598 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 4599 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4600 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) && |
| 4601 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 4602 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 4603 | if (LN0->getExtensionType() != ISD::SEXTLOAD) { |
| 4604 | bool DoXform = true; |
| 4605 | SmallVector<SDNode*, 4> SetCCs; |
| 4606 | if (!N0.hasOneUse()) |
| 4607 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND, |
| 4608 | SetCCs, TLI); |
| 4609 | if (DoXform) { |
| 4610 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, |
| 4611 | LN0->getChain(), LN0->getBasePtr(), |
| 4612 | LN0->getPointerInfo(), |
| 4613 | LN0->getMemoryVT(), |
| 4614 | LN0->isVolatile(), |
| 4615 | LN0->isNonTemporal(), |
| 4616 | LN0->getAlignment()); |
| 4617 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 4618 | Mask = Mask.zext(VT.getSizeInBits()); |
| 4619 | SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 4620 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 4621 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| 4622 | N0.getOperand(0).getDebugLoc(), |
| 4623 | N0.getOperand(0).getValueType(), ExtLoad); |
| 4624 | CombineTo(N, And); |
| 4625 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| 4626 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4627 | ISD::ZERO_EXTEND); |
| 4628 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4629 | } |
| 4630 | } |
| 4631 | } |
| 4632 | |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4633 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 4634 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4635 | if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 4636 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4637 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4638 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4639 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4640 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4641 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4642 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4643 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4644 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4645 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4646 | LN0->getAlignment()); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4647 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4648 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4649 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(), |
| 4650 | ExtLoad), |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4651 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4652 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4653 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4654 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4655 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4656 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4657 | if (!LegalOperations && VT.isVector()) { |
| 4658 | // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. |
| 4659 | // Only do this before legalize for now. |
| 4660 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 4661 | EVT EltVT = VT.getVectorElementType(); |
| 4662 | SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(), |
| 4663 | DAG.getConstant(1, EltVT)); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4664 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4665 | // We know that the # elements of the results is the same as the |
| 4666 | // # elements of the compare (and the # elements of the compare result |
| 4667 | // for that matter). Check to see that they are the same size. If so, |
| 4668 | // we know that the element size of the sext'd result matches the |
| 4669 | // element size of the compare operands. |
| 4670 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4671 | DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4672 | N0.getOperand(1), |
| 4673 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 4674 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 4675 | &OneOps[0], OneOps.size())); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4676 | |
| 4677 | // If the desired elements are smaller or larger than the source |
| 4678 | // elements we can use a matching integer vector type and then |
| 4679 | // truncate/sign extend |
| 4680 | EVT MatchingElementType = |
| 4681 | EVT::getIntegerVT(*DAG.getContext(), |
| 4682 | N0VT.getScalarType().getSizeInBits()); |
| 4683 | EVT MatchingVectorType = |
| 4684 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 4685 | N0VT.getVectorNumElements()); |
| 4686 | SDValue VsetCC = |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4687 | DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4688 | N0.getOperand(1), |
| 4689 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4690 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4691 | DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT), |
| 4692 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 4693 | &OneOps[0], OneOps.size())); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4697 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4698 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4699 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4700 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4701 | if (SCC.getNode()) return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4702 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4703 | |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4704 | // (zext (shl (zext x), cst)) -> (shl (zext x), cst) |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4705 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4706 | isa<ConstantSDNode>(N0.getOperand(1)) && |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4707 | N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && |
| 4708 | N0.hasOneUse()) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4709 | SDValue ShAmt = N0.getOperand(1); |
| 4710 | unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4711 | if (N0.getOpcode() == ISD::SHL) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4712 | SDValue InnerZExt = N0.getOperand(0); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4713 | // If the original shl may be shifting out bits, do not perform this |
| 4714 | // transformation. |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4715 | unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() - |
| 4716 | InnerZExt.getOperand(0).getValueType().getSizeInBits(); |
| 4717 | if (ShAmtVal > KnownZeroBits) |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4718 | return SDValue(); |
| 4719 | } |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4720 | |
| 4721 | DebugLoc DL = N->getDebugLoc(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Ensure that the shift amount is wide enough for the shifted value. |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4724 | if (VT.getSizeInBits() >= 256) |
| 4725 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4726 | |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4727 | return DAG.getNode(N0.getOpcode(), DL, VT, |
| 4728 | DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), |
| 4729 | ShAmt); |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4732 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4735 | SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 4736 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4737 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4738 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4739 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4740 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4741 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4742 | // fold (aext (aext x)) -> (aext x) |
| 4743 | // fold (aext (zext x)) -> (zext x) |
| 4744 | // fold (aext (sext x)) -> (sext x) |
| 4745 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 4746 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 4747 | N0.getOpcode() == ISD::SIGN_EXTEND) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4748 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4749 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4750 | // fold (aext (truncate (load x))) -> (aext (smaller load x)) |
| 4751 | // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) |
| 4752 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4753 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4754 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4755 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4756 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4757 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4758 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4759 | AddToWorkList(oye); |
| 4760 | } |
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 4761 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4762 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4765 | // fold (aext (truncate x)) |
| 4766 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4767 | SDValue TruncOp = N0.getOperand(0); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4768 | if (TruncOp.getValueType() == VT) |
| 4769 | return TruncOp; // x iff x size == zext size. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4770 | if (TruncOp.getValueType().bitsGT(VT)) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4771 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp); |
| 4772 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4773 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4774 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4775 | // Fold (aext (and (trunc x), cst)) -> (and x, cst) |
| 4776 | // if the trunc is not free. |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4777 | if (N0.getOpcode() == ISD::AND && |
| 4778 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4779 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4780 | !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 4781 | N0.getValueType())) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4782 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4783 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4784 | X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4785 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4786 | X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4787 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4788 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4789 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4790 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4791 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4792 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4793 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4794 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4795 | // 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] | 4796 | // on vectors in one instruction. We only perform this transformation on |
| 4797 | // scalars. |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4798 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4799 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4800 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4801 | bool DoXform = true; |
| 4802 | SmallVector<SDNode*, 4> SetCCs; |
| 4803 | if (!N0.hasOneUse()) |
| 4804 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); |
| 4805 | if (DoXform) { |
| 4806 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4807 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4808 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4809 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4810 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4811 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4812 | LN0->getAlignment()); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4813 | CombineTo(N, ExtLoad); |
| 4814 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4815 | N0.getValueType(), ExtLoad); |
| 4816 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4817 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(), |
| 4818 | ISD::ANY_EXTEND); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4819 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4820 | } |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4821 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4822 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4823 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 4824 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 4825 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 4826 | if (N0.getOpcode() == ISD::LOAD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4827 | !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4828 | N0.hasOneUse()) { |
| 4829 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4830 | EVT MemVT = LN0->getMemoryVT(); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4831 | SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(), |
| 4832 | VT, LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4833 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4834 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4835 | LN0->getAlignment()); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4836 | CombineTo(N, ExtLoad); |
Evan Cheng | 4529966 | 2008-08-29 23:20:46 +0000 | [diff] [blame] | 4837 | CombineTo(N0.getNode(), |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4838 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4839 | N0.getValueType(), ExtLoad), |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4840 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4841 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4842 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4843 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4844 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4845 | // aext(setcc) -> sext_in_reg(vsetcc) for vectors. |
| 4846 | // Only do this before legalize for now. |
| 4847 | if (VT.isVector() && !LegalOperations) { |
| 4848 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 4849 | // We know that the # elements of the results is the same as the |
| 4850 | // # elements of the compare (and the # elements of the compare result |
| 4851 | // for that matter). Check to see that they are the same size. If so, |
| 4852 | // we know that the element size of the sext'd result matches the |
| 4853 | // element size of the compare operands. |
| 4854 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4855 | return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4856 | N0.getOperand(1), |
| 4857 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4858 | // If the desired elements are smaller or larger than the source |
| 4859 | // elements we can use a matching integer vector type and then |
| 4860 | // truncate/sign extend |
| 4861 | else { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4862 | EVT MatchingElementType = |
| 4863 | EVT::getIntegerVT(*DAG.getContext(), |
| 4864 | N0VT.getScalarType().getSizeInBits()); |
| 4865 | EVT MatchingVectorType = |
| 4866 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 4867 | N0VT.getVectorNumElements()); |
| 4868 | SDValue VsetCC = |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 4869 | DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4870 | N0.getOperand(1), |
| 4871 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4872 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4877 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4878 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4879 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | c24bbad | 2007-04-11 16:51:53 +0000 | [diff] [blame] | 4880 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4881 | if (SCC.getNode()) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 4882 | return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4883 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4884 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4885 | return SDValue(); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4888 | /// GetDemandedBits - See if the specified operand can be simplified with the |
| 4889 | /// 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] | 4890 | /// simpler operand, otherwise return a null SDValue. |
| 4891 | SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4892 | switch (V.getOpcode()) { |
| 4893 | default: break; |
Lang Hames | 5207bf2 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 4894 | case ISD::Constant: { |
| 4895 | const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode()); |
| 4896 | assert(CV != 0 && "Const value should be ConstSDNode."); |
| 4897 | const APInt &CVal = CV->getAPIntValue(); |
| 4898 | APInt NewVal = CVal & Mask; |
| 4899 | if (NewVal != CVal) { |
| 4900 | return DAG.getConstant(NewVal, V.getValueType()); |
| 4901 | } |
| 4902 | break; |
| 4903 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4904 | case ISD::OR: |
| 4905 | case ISD::XOR: |
| 4906 | // If the LHS or RHS don't contribute bits to the or, drop them. |
| 4907 | if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) |
| 4908 | return V.getOperand(1); |
| 4909 | if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) |
| 4910 | return V.getOperand(0); |
| 4911 | break; |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4912 | case ISD::SRL: |
| 4913 | // Only look at single-use SRLs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4914 | if (!V.getNode()->hasOneUse()) |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4915 | break; |
| 4916 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) { |
| 4917 | // See if we can recursively simplify the LHS. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4918 | unsigned Amt = RHSC->getZExtValue(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4919 | |
Dan Gohman | cc91d63 | 2009-01-03 19:22:06 +0000 | [diff] [blame] | 4920 | // Watch out for shift count overflow though. |
| 4921 | if (Amt >= Mask.getBitWidth()) break; |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4922 | APInt NewMask = Mask << Amt; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4923 | SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4924 | if (SimplifyLHS.getNode()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4925 | return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(), |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4926 | SimplifyLHS, V.getOperand(1)); |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4927 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4928 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4929 | return SDValue(); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4932 | /// ReduceLoadWidth - If the result of a wider load is shifted to right of N |
| 4933 | /// bits and then truncated to a narrower type and where N is a multiple |
| 4934 | /// of number of bits of the narrower type, transform it to a narrower load |
| 4935 | /// from address + N / num of bits of new type. If the result is to be |
| 4936 | /// extended, also fold the extension to form a extending load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4937 | SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4938 | unsigned Opc = N->getOpcode(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4939 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4940 | ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4941 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4942 | EVT VT = N->getValueType(0); |
| 4943 | EVT ExtVT = VT; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4944 | |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 4945 | // This transformation isn't valid for vector loads. |
| 4946 | if (VT.isVector()) |
| 4947 | return SDValue(); |
| 4948 | |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4949 | // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then |
Evan Cheng | e177e30 | 2007-03-23 22:13:36 +0000 | [diff] [blame] | 4950 | // extended to VT. |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4951 | if (Opc == ISD::SIGN_EXTEND_INREG) { |
| 4952 | ExtType = ISD::SEXTLOAD; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4953 | ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4954 | } else if (Opc == ISD::SRL) { |
Chris Lattner | 90b0364 | 2010-12-21 18:05:22 +0000 | [diff] [blame] | 4955 | // Another special-case: SRL is basically zero-extending a narrower value. |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4956 | ExtType = ISD::ZEXTLOAD; |
| 4957 | N0 = SDValue(N, 0); |
| 4958 | ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 4959 | if (!N01) return SDValue(); |
| 4960 | ExtVT = EVT::getIntegerVT(*DAG.getContext(), |
| 4961 | VT.getSizeInBits() - N01->getZExtValue()); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4962 | } |
Richard Osborne | 4e3740e | 2011-01-31 17:41:44 +0000 | [diff] [blame] | 4963 | if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT)) |
| 4964 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4965 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4966 | unsigned EVTBits = ExtVT.getSizeInBits(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4967 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4968 | // Do not generate loads of non-round integer types since these can |
| 4969 | // be expensive (and would be wrong if the type is not byte sized). |
| 4970 | if (!ExtVT.isRound()) |
| 4971 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4972 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4973 | unsigned ShAmt = 0; |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4974 | if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4975 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4976 | ShAmt = N01->getZExtValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4977 | // Is the shift amount a multiple of size of VT? |
| 4978 | if ((ShAmt & (EVTBits-1)) == 0) { |
| 4979 | N0 = N0.getOperand(0); |
Eli Friedman | d68eea2 | 2009-08-19 08:46:10 +0000 | [diff] [blame] | 4980 | // Is the load width a multiple of size of VT? |
| 4981 | if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4982 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4983 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4984 | |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 4985 | // At this point, we must have a load or else we can't do the transform. |
| 4986 | if (!isa<LoadSDNode>(N0)) return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4987 | |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 4988 | // If the shift amount is larger than the input type then we're not |
| 4989 | // accessing any of the loaded bytes. If the load was a zextload/extload |
| 4990 | // then the result of the shift+trunc is zero/undef (handled elsewhere). |
| 4991 | // If the load was a sextload then the result is a splat of the sign bit |
| 4992 | // of the extended byte. This is not worth optimizing for. |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 4993 | if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 4994 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
| 4997 | |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4998 | // If the load is shifted left (and the result isn't shifted back right), |
| 4999 | // we can fold the truncate through the shift. |
| 5000 | unsigned ShLeftAmt = 0; |
| 5001 | if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5002 | ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5003 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 5004 | ShLeftAmt = N01->getZExtValue(); |
| 5005 | N0 = N0.getOperand(0); |
| 5006 | } |
| 5007 | } |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5008 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5009 | // If we haven't found a load, we can't narrow it. Don't transform one with |
| 5010 | // multiple uses, this would require adding a new load. |
| 5011 | if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() || |
| 5012 | // Don't change the width of a volatile load. |
| 5013 | cast<LoadSDNode>(N0)->isVolatile()) |
| 5014 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5015 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5016 | // Verify that we are actually reducing a load width here. |
| 5017 | if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits) |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5018 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5019 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5020 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| 5021 | EVT PtrType = N0.getOperand(1).getValueType(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5022 | |
Evan Cheng | 16436df | 2012-06-26 01:19:33 +0000 | [diff] [blame] | 5023 | if (PtrType == MVT::Untyped || PtrType.isExtended()) |
| 5024 | // It's not possible to generate a constant of extended or untyped type. |
| 5025 | return SDValue(); |
| 5026 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5027 | // For big endian targets, we need to adjust the offset to the pointer to |
| 5028 | // load the correct bytes. |
| 5029 | if (TLI.isBigEndian()) { |
| 5030 | unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); |
| 5031 | unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); |
| 5032 | ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5035 | uint64_t PtrOff = ShAmt / 8; |
| 5036 | unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); |
| 5037 | SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), |
| 5038 | PtrType, LN0->getBasePtr(), |
| 5039 | DAG.getConstant(PtrOff, PtrType)); |
| 5040 | AddToWorkList(NewPtr.getNode()); |
| 5041 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5042 | SDValue Load; |
| 5043 | if (ExtType == ISD::NON_EXTLOAD) |
| 5044 | Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr, |
| 5045 | LN0->getPointerInfo().getWithOffset(PtrOff), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5046 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5047 | LN0->isInvariant(), NewAlign); |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5048 | else |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5049 | Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr, |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5050 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 5051 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 5052 | NewAlign); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5053 | |
| 5054 | // Replace the old load's chain with the new load's chain. |
| 5055 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5056 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5057 | |
| 5058 | // Shift the result left, if we've swallowed a left shift. |
| 5059 | SDValue Result = Load; |
| 5060 | if (ShLeftAmt != 0) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5061 | EVT ShImmTy = getShiftAmountTy(Result.getValueType()); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5062 | if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) |
| 5063 | ShImmTy = VT; |
| 5064 | Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, |
| 5065 | Result, DAG.getConstant(ShLeftAmt, ShImmTy)); |
| 5066 | } |
| 5067 | |
| 5068 | // Return the new loaded value. |
| 5069 | return Result; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5072 | SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
| 5073 | SDValue N0 = N->getOperand(0); |
| 5074 | SDValue N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5075 | EVT VT = N->getValueType(0); |
| 5076 | EVT EVT = cast<VTSDNode>(N1)->getVT(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5077 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5078 | unsigned EVTBits = EVT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5079 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5080 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 5081 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5082 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5083 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 5084 | // If the input is already sign extended, just drop the extension. |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5085 | if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 5086 | return N0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5087 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5088 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 5089 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5090 | EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) { |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5091 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
| 5092 | N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5093 | } |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5094 | |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5095 | // fold (sext_in_reg (sext x)) -> (sext x) |
| 5096 | // fold (sext_in_reg (aext x)) -> (sext x) |
| 5097 | // if x is small enough. |
| 5098 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { |
| 5099 | SDValue N00 = N0.getOperand(0); |
Evan Cheng | 003d7c4 | 2010-04-16 22:26:19 +0000 | [diff] [blame] | 5100 | if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits && |
| 5101 | (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5102 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1); |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5105 | // 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] | 5106 | if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5107 | return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5108 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5109 | // fold operands of sext_in_reg based on knowledge that the top bits are not |
| 5110 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5111 | if (SimplifyDemandedBits(SDValue(N, 0))) |
| 5112 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5113 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5114 | // fold (sext_in_reg (load x)) -> (smaller sextload x) |
| 5115 | // 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] | 5116 | SDValue NarrowLoad = ReduceLoadWidth(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5117 | if (NarrowLoad.getNode()) |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5118 | return NarrowLoad; |
| 5119 | |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5120 | // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) |
| 5121 | // 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] | 5122 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 5123 | if (N0.getOpcode() == ISD::SRL) { |
| 5124 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5125 | if (ShAmt->getZExtValue()+EVTBits <= VTBits) { |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5126 | // We can turn this into an SRA iff the input to the SRL is already sign |
| 5127 | // extended enough. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5128 | unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5129 | if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5130 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, |
| 5131 | N0.getOperand(0), N0.getOperand(1)); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5132 | } |
| 5133 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5134 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5135 | // fold (sext_inreg (extload x)) -> (sextload x) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5136 | if (ISD::isEXTLoad(N0.getNode()) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5137 | ISD::isUNINDEXEDLoad(N0.getNode()) && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5138 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5139 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5140 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5141 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5142 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5143 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5144 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 5145 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5146 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5147 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5148 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5149 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5150 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5151 | } |
| 5152 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5153 | if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5154 | N0.hasOneUse() && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5155 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5156 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5157 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5158 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5159 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5160 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5161 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 5162 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5163 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5164 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5165 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5166 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5167 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5168 | } |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5169 | |
| 5170 | // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16)) |
| 5171 | if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) { |
| 5172 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), |
| 5173 | N0.getOperand(1), false); |
| 5174 | if (BSwap.getNode() != 0) |
| 5175 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
| 5176 | BSwap, N1); |
| 5177 | } |
| 5178 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5179 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5180 | } |
| 5181 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5182 | SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { |
| 5183 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5184 | EVT VT = N->getValueType(0); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5185 | bool isLE = TLI.isLittleEndian(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5186 | |
| 5187 | // noop truncate |
| 5188 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5189 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5190 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5191 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5192 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5193 | // fold (truncate (truncate x)) -> (truncate x) |
| 5194 | if (N0.getOpcode() == ISD::TRUNCATE) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5195 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5196 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | 7f893c0 | 2010-04-07 18:13:33 +0000 | [diff] [blame] | 5197 | if (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 5198 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 5199 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5200 | if (N0.getOperand(0).getValueType().bitsLT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5201 | // 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] | 5202 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 5203 | N0.getOperand(0)); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5204 | else if (N0.getOperand(0).getValueType().bitsGT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5205 | // 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] | 5206 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5207 | else |
| 5208 | // if the source and dest are the same type, we can drop both the extend |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 5209 | // and the truncate. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5210 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5211 | } |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5212 | |
Nadav Rotem | cc870a8 | 2012-02-05 11:39:23 +0000 | [diff] [blame] | 5213 | // Fold extract-and-trunc into a narrow extract. For example: |
| 5214 | // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1) |
| 5215 | // i32 y = TRUNCATE(i64 x) |
| 5216 | // -- becomes -- |
| 5217 | // v16i8 b = BITCAST (v2i64 val) |
| 5218 | // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8) |
| 5219 | // |
| 5220 | // Note: We only run this optimization after type legalization (which often |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5221 | // creates this pattern) and before operation legalization after which |
| 5222 | // we need to be more careful about the vector instructions that we generate. |
| 5223 | if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 5224 | LegalTypes && !LegalOperations && N0->hasOneUse()) { |
| 5225 | |
| 5226 | EVT VecTy = N0.getOperand(0).getValueType(); |
| 5227 | EVT ExTy = N0.getValueType(); |
| 5228 | EVT TrTy = N->getValueType(0); |
| 5229 | |
| 5230 | unsigned NumElem = VecTy.getVectorNumElements(); |
| 5231 | unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits(); |
| 5232 | |
| 5233 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem); |
| 5234 | assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size"); |
| 5235 | |
| 5236 | SDValue EltNo = N0->getOperand(1); |
| 5237 | if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) { |
| 5238 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5239 | EVT IndexTy = N0->getOperand(1).getValueType(); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5240 | int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1)); |
| 5241 | |
| 5242 | SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
| 5243 | NVT, N0.getOperand(0)); |
| 5244 | |
| 5245 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 5246 | N->getDebugLoc(), TrTy, V, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5247 | DAG.getConstant(Index, IndexTy)); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5251 | // 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] | 5252 | // only the low bits are being used. |
| 5253 | // For example "trunc (or (shl x, 8), y)" // -> trunc y |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5254 | // Currently we only perform this optimization on scalars because vectors |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5255 | // may have different active low bits. |
| 5256 | if (!VT.isVector()) { |
| 5257 | SDValue Shorter = |
| 5258 | GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), |
| 5259 | VT.getSizeInBits())); |
| 5260 | if (Shorter.getNode()) |
| 5261 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter); |
| 5262 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 5263 | // fold (truncate (load x)) -> (smaller load x) |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5264 | // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5265 | if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { |
| 5266 | SDValue Reduced = ReduceLoadWidth(N); |
| 5267 | if (Reduced.getNode()) |
| 5268 | return Reduced; |
| 5269 | } |
| 5270 | |
| 5271 | // Simplify the operands using demanded-bits information. |
| 5272 | if (!VT.isVector() && |
| 5273 | SimplifyDemandedBits(SDValue(N, 0))) |
| 5274 | return SDValue(N, 0); |
| 5275 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 5276 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5279 | static SDNode *getBuildPairElt(SDNode *N, unsigned i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5280 | SDValue Elt = N->getOperand(i); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5281 | if (Elt.getOpcode() != ISD::MERGE_VALUES) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5282 | return Elt.getNode(); |
| 5283 | return Elt.getOperand(Elt.getResNo()).getNode(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | /// CombineConsecutiveLoads - build_pair (load, load) -> load |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5287 | /// if load locations are consecutive. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5288 | SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5289 | assert(N->getOpcode() == ISD::BUILD_PAIR); |
| 5290 | |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5291 | LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); |
| 5292 | LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5293 | if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || |
| 5294 | LD1->getPointerInfo().getAddrSpace() != |
| 5295 | LD2->getPointerInfo().getAddrSpace()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5296 | return SDValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5297 | EVT LD1VT = LD1->getValueType(0); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5298 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5299 | if (ISD::isNON_EXTLoad(LD2) && |
| 5300 | LD2->hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5301 | // If both are volatile this would reduce the number of volatile loads. |
| 5302 | // 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] | 5303 | !LD1->isVolatile() && |
| 5304 | !LD2->isVolatile() && |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 5305 | DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) { |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5306 | unsigned Align = LD1->getAlignment(); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 5307 | unsigned NewAlign = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5308 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5309 | |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5310 | if (NewAlign <= Align && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5311 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5312 | return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5313 | LD1->getBasePtr(), LD1->getPointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5314 | false, false, false, Align); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5315 | } |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5316 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5317 | return SDValue(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5320 | SDValue DAGCombiner::visitBITCAST(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5321 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5322 | EVT VT = N->getValueType(0); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5323 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5324 | // If the input is a BUILD_VECTOR with all constant elements, fold this now. |
| 5325 | // Only do this before legalize, since afterward the target may be depending |
| 5326 | // on the bitconvert. |
| 5327 | // First check to see if this is all constant. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5328 | if (!LegalTypes && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5329 | N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5330 | VT.isVector()) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5331 | bool isSimple = true; |
| 5332 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) |
| 5333 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && |
| 5334 | N0.getOperand(i).getOpcode() != ISD::Constant && |
| 5335 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5336 | isSimple = false; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5339 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5340 | EVT DestEltVT = N->getValueType(0).getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5341 | assert(!DestEltVT.isVector() && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5342 | "Element type of vector ValueType must not be vector!"); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5343 | if (isSimple) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5344 | return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5345 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5346 | |
Dan Gohman | 3dd168d | 2008-09-05 01:58:21 +0000 | [diff] [blame] | 5347 | // If the input is a constant, let getNode fold it. |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5348 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5349 | SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0); |
Dan Gohman | a407ca1 | 2009-08-10 23:15:10 +0000 | [diff] [blame] | 5350 | if (Res.getNode() != N) { |
| 5351 | if (!LegalOperations || |
| 5352 | TLI.isOperationLegal(Res.getNode()->getOpcode(), VT)) |
| 5353 | return Res; |
| 5354 | |
| 5355 | // Folding it resulted in an illegal node, and it's too late to |
| 5356 | // do that. Clean up the old node and forego the transformation. |
| 5357 | // Ideally this won't happen very often, because instcombine |
| 5358 | // and the earlier dagcombine runs (where illegal nodes are |
| 5359 | // permitted) should have folded most of them already. |
| 5360 | DAG.DeleteNode(Res.getNode()); |
| 5361 | } |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5362 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5363 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5364 | // (conv (conv x, t1), t2) -> (conv x, t2) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5365 | if (N0.getOpcode() == ISD::BITCAST) |
| 5366 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5367 | N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5368 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5369 | // fold (conv (load x)) -> (load (conv*)x) |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 5370 | // 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] | 5371 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5372 | // Do not change the width of a volatile load. |
| 5373 | !cast<LoadSDNode>(N0)->isVolatile() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5374 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5375 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 5376 | unsigned Align = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5377 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5378 | unsigned OrigAlign = LN0->getAlignment(); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5379 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5380 | if (Align <= OrigAlign) { |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5381 | SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5382 | LN0->getBasePtr(), LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5383 | LN0->isVolatile(), LN0->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5384 | LN0->isInvariant(), OrigAlign); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5385 | AddToWorkList(N); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 5386 | CombineTo(N0.getNode(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5387 | DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5388 | N0.getValueType(), Load), |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5389 | Load.getValue(1)); |
| 5390 | return Load; |
| 5391 | } |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5392 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5393 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5394 | // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) |
| 5395 | // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5396 | // This often reduces constant pool loads. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 5397 | if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) || |
| 5398 | (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5399 | N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5400 | SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5401 | N0.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5402 | AddToWorkList(NewConv.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5403 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5404 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5405 | if (N0.getOpcode() == ISD::FNEG) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5406 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 5407 | NewConv, DAG.getConstant(SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5408 | assert(N0.getOpcode() == ISD::FABS); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5409 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 5410 | NewConv, DAG.getConstant(~SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5411 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5412 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5413 | // fold (bitconvert (fcopysign cst, x)) -> |
| 5414 | // (or (and (bitconvert x), sign), (and cst, (not sign))) |
| 5415 | // Note that we don't handle (copysign x, cst) because this can always be |
| 5416 | // folded to an fneg or fabs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5417 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 5418 | isa<ConstantFPSDNode>(N0.getOperand(0)) && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5419 | VT.isInteger() && !VT.isVector()) { |
| 5420 | unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5421 | EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5422 | if (isTypeLegal(IntXVT)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5423 | SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5424 | IntXVT, N0.getOperand(1)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5425 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5426 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5427 | // If X has a different width than the result/lhs, sext it or truncate it. |
| 5428 | unsigned VTWidth = VT.getSizeInBits(); |
| 5429 | if (OrigXWidth < VTWidth) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5430 | X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5431 | AddToWorkList(X.getNode()); |
| 5432 | } else if (OrigXWidth > VTWidth) { |
| 5433 | // To get the sign bit in the right place, we have to shift it right |
| 5434 | // before truncating. |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5435 | X = DAG.getNode(ISD::SRL, X.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5436 | X.getValueType(), X, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5437 | DAG.getConstant(OrigXWidth-VTWidth, X.getValueType())); |
| 5438 | AddToWorkList(X.getNode()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5439 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5440 | AddToWorkList(X.getNode()); |
| 5441 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5442 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5443 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5444 | X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5445 | X, DAG.getConstant(SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5446 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5447 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5448 | SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5449 | VT, N0.getOperand(0)); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5450 | Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5451 | Cst, DAG.getConstant(~SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5452 | AddToWorkList(Cst.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5453 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5454 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5455 | } |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5456 | } |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5457 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5458 | // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5459 | if (N0.getOpcode() == ISD::BUILD_PAIR) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5460 | SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT); |
| 5461 | if (CombineLD.getNode()) |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5462 | return CombineLD; |
| 5463 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5464 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5465 | return SDValue(); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5468 | SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5469 | EVT VT = N->getValueType(0); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5470 | return CombineConsecutiveLoads(N, VT); |
| 5471 | } |
| 5472 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5473 | /// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5474 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5475 | /// destination element value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5476 | SDValue DAGCombiner:: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5477 | ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5478 | EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5479 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5480 | // If this is already the right type, we're done. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5481 | if (SrcEltVT == DstEltVT) return SDValue(BV, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5482 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5483 | unsigned SrcBitSize = SrcEltVT.getSizeInBits(); |
| 5484 | unsigned DstBitSize = DstEltVT.getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5485 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5486 | // If this is a conversion of N elements of one type to N elements of another |
| 5487 | // type, convert each element. This handles FP<->INT cases. |
| 5488 | if (SrcBitSize == DstBitSize) { |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5489 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 5490 | BV->getValueType(0).getVectorNumElements()); |
| 5491 | |
| 5492 | // Due to the FP element handling below calling this routine recursively, |
| 5493 | // we can end up with a scalar-to-vector node here. |
| 5494 | if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5495 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 5496 | DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5497 | DstEltVT, BV->getOperand(0))); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5498 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5499 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5500 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5501 | SDValue Op = BV->getOperand(i); |
| 5502 | // If the vector element type is not legal, the BUILD_VECTOR operands |
| 5503 | // are promoted and implicitly truncated. Make that explicit here. |
Bob Wilson | c885165 | 2009-04-20 17:27:09 +0000 | [diff] [blame] | 5504 | if (Op.getValueType() != SrcEltVT) |
| 5505 | Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5506 | Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5507 | DstEltVT, Op)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5508 | AddToWorkList(Ops.back().getNode()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 5509 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5510 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5511 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5512 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5513 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5514 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 5515 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 5516 | // int first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5517 | if (SrcEltVT.isFloatingPoint()) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5518 | // Convert the input float vector to a int vector where the elements are the |
| 5519 | // same sizes. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5520 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5521 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5522 | BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5523 | SrcEltVT = IntVT; |
| 5524 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5525 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5526 | // Now we know the input is an integer vector. If the output is a FP type, |
| 5527 | // convert to integer first, then to FP of the right size. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5528 | if (DstEltVT.isFloatingPoint()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5529 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5530 | EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5531 | SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5532 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5533 | // Next, convert to FP elements of the same size. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5534 | return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5535 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5536 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5537 | // Okay, we know the src/dst types are both integers of differing types. |
| 5538 | // Handling growing first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5539 | assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5540 | if (SrcBitSize < DstBitSize) { |
| 5541 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5542 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5543 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5544 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5545 | i += NumInputsPerOutput) { |
| 5546 | bool isLE = TLI.isLittleEndian(); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5547 | APInt NewBits = APInt(DstBitSize, 0); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5548 | bool EltIsUndef = true; |
| 5549 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 5550 | // Shift the previously computed bits over. |
| 5551 | NewBits <<= SrcBitSize; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5552 | SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5553 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 5554 | EltIsUndef = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5555 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5556 | NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). |
Dan Gohman | 58c2587 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 5557 | zextOrTrunc(SrcBitSize).zext(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5558 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5559 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5560 | if (EltIsUndef) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5561 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5562 | else |
| 5563 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 5564 | } |
| 5565 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5566 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5567 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5568 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5569 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5570 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5571 | // Finally, this must be the case where we are shrinking elements: each input |
| 5572 | // turns into multiple outputs. |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5573 | bool isS2V = ISD::isScalarToVector(BV); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5574 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5575 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 5576 | NumOutputsPerInput*BV->getNumOperands()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5577 | SmallVector<SDValue, 8> Ops; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5578 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5579 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5580 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 5581 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5582 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5583 | continue; |
| 5584 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5585 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5586 | APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))-> |
| 5587 | getAPIntValue().zextOrTrunc(SrcBitSize); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5588 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5589 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5590 | APInt ThisVal = OpVal.trunc(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5591 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5592 | if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal) |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5593 | // Simply turn this into a SCALAR_TO_VECTOR of the new type. |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5594 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 5595 | Ops[0]); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5596 | OpVal = OpVal.lshr(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | // 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] | 5600 | if (TLI.isBigEndian()) |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5601 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 5602 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5603 | |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5604 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 5605 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5608 | SDValue DAGCombiner::visitFADD(SDNode *N) { |
| 5609 | SDValue N0 = N->getOperand(0); |
| 5610 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5611 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5612 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5613 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5614 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5615 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5616 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5617 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5618 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 5619 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5620 | |
Lang Hames | 0180694 | 2012-06-14 20:37:15 +0000 | [diff] [blame] | 5621 | // fold (fadd c1, c2) -> c1 + c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5622 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5623 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5624 | // canonicalize constant to RHS |
| 5625 | if (N0CFP && !N1CFP) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5626 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0); |
| 5627 | // fold (fadd A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5628 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 5629 | N1CFP->getValueAPF().isZero()) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5630 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5631 | // fold (fadd A, (fneg B)) -> (fsub A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5632 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
| 5633 | isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5634 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5635 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5636 | // fold (fadd (fneg A), B) -> (fsub B, A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5637 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
| 5638 | isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5639 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5640 | GetNegatedExpression(N0, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5641 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 5642 | // 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] | 5643 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 5644 | N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() && |
| 5645 | isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5646 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0), |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5647 | DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 5648 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5649 | |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 5650 | // FADD -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 5651 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 5652 | DAG.getTarget().Options.UnsafeFPMath) && |
| 5653 | DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) && |
| 5654 | TLI.isOperationLegal(ISD::FMA, VT)) { |
| 5655 | |
| 5656 | // fold (fadd (fmul x, y), z) -> (fma x, y, z) |
| 5657 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) { |
| 5658 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 5659 | N0.getOperand(0), N0.getOperand(1), N1); |
| 5660 | } |
| 5661 | |
| 5662 | // fold (fadd x, (fmul y, z)) -> (fma x, y, z) |
| 5663 | // Note: Commutes FADD operands. |
| 5664 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) { |
| 5665 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 5666 | N1.getOperand(0), N1.getOperand(1), N0); |
| 5667 | } |
| 5668 | } |
| 5669 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5670 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5673 | SDValue DAGCombiner::visitFSUB(SDNode *N) { |
| 5674 | SDValue N0 = N->getOperand(0); |
| 5675 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5676 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5677 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5678 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5679 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5680 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5681 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5682 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5683 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 5684 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5685 | |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5686 | // fold (fsub c1, c2) -> c1-c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5687 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5688 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5689 | // fold (fsub A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5690 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5691 | N1CFP && N1CFP->getValueAPF().isZero()) |
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 5692 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5693 | // fold (fsub 0, B) -> -B |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5694 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5695 | N0CFP && N0CFP->getValueAPF().isZero()) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5696 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5697 | return GetNegatedExpression(N1, DAG, LegalOperations); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5698 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5699 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 5700 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5701 | // fold (fsub A, (fneg B)) -> (fadd A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5702 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5703 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5704 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5705 | |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 5706 | // If 'unsafe math' is enabled, fold |
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 5707 | // (fsub x, x) -> 0.0 & |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 5708 | // (fsub x, (fadd x, y)) -> (fneg y) & |
| 5709 | // (fsub x, (fadd y, x)) -> (fneg y) |
| 5710 | if (DAG.getTarget().Options.UnsafeFPMath) { |
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 5711 | if (N0 == N1) |
| 5712 | return DAG.getConstantFP(0.0f, VT); |
| 5713 | |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 5714 | if (N1.getOpcode() == ISD::FADD) { |
| 5715 | SDValue N10 = N1->getOperand(0); |
| 5716 | SDValue N11 = N1->getOperand(1); |
| 5717 | |
| 5718 | if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, |
| 5719 | &DAG.getTarget().Options)) |
| 5720 | return GetNegatedExpression(N11, DAG, LegalOperations); |
| 5721 | else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, |
| 5722 | &DAG.getTarget().Options)) |
| 5723 | return GetNegatedExpression(N10, DAG, LegalOperations); |
| 5724 | } |
| 5725 | } |
| 5726 | |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 5727 | // FSUB -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 5728 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 5729 | DAG.getTarget().Options.UnsafeFPMath) && |
| 5730 | DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) && |
| 5731 | TLI.isOperationLegal(ISD::FMA, VT)) { |
| 5732 | |
| 5733 | // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z)) |
| 5734 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) { |
| 5735 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 5736 | N0.getOperand(0), N0.getOperand(1), |
| 5737 | DAG.getNode(ISD::FNEG, N1->getDebugLoc(), VT, N1)); |
| 5738 | } |
| 5739 | |
| 5740 | // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x) |
| 5741 | // Note: Commutes FSUB operands. |
| 5742 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) { |
| 5743 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, |
| 5744 | DAG.getNode(ISD::FNEG, N1->getDebugLoc(), VT, |
| 5745 | N1.getOperand(0)), |
| 5746 | N1.getOperand(1), N0); |
| 5747 | } |
| 5748 | } |
| 5749 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5750 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5753 | SDValue DAGCombiner::visitFMUL(SDNode *N) { |
| 5754 | SDValue N0 = N->getOperand(0); |
| 5755 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 5756 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5757 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5758 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5759 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5760 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5761 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5762 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5763 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5764 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 5765 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5766 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 5767 | // fold (fmul c1, c2) -> c1*c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5768 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5769 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 5770 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5771 | if (N0CFP && !N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5772 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0); |
| 5773 | // fold (fmul A, 0) -> 0 |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5774 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5775 | N1CFP && N1CFP->getValueAPF().isZero()) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5776 | return N1; |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 5777 | // fold (fmul A, 0) -> 0, vector edition. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5778 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5779 | ISD::isBuildVectorAllZeros(N1.getNode())) |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 5780 | return N1; |
Owen Anderson | 363e4b9 | 2012-05-02 21:32:35 +0000 | [diff] [blame] | 5781 | // fold (fmul A, 1.0) -> A |
| 5782 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| 5783 | return N0; |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 5784 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 5785 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5786 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0); |
Dan Gohman | eb1fedc | 2009-08-10 16:50:32 +0000 | [diff] [blame] | 5787 | // fold (fmul X, -1.0) -> (fneg X) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5788 | if (N1CFP && N1CFP->isExactlyValue(-1.0)) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5789 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5790 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5791 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5792 | // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5793 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5794 | &DAG.getTarget().Options)) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5795 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5796 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5797 | // Both can be negated for free, check to see if at least one is cheaper |
| 5798 | // negated. |
| 5799 | if (LHSNeg == 2 || RHSNeg == 2) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5800 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5801 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 5802 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5803 | } |
| 5804 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5805 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 5806 | // 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] | 5807 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 5808 | N1CFP && N0.getOpcode() == ISD::FMUL && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5809 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5810 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5811 | DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 5812 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5813 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5814 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 5817 | SDValue DAGCombiner::visitFMA(SDNode *N) { |
| 5818 | SDValue N0 = N->getOperand(0); |
| 5819 | SDValue N1 = N->getOperand(1); |
| 5820 | SDValue N2 = N->getOperand(2); |
| 5821 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5822 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 5823 | EVT VT = N->getValueType(0); |
| 5824 | |
| 5825 | if (N0CFP && N0CFP->isExactlyValue(1.0)) |
| 5826 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2); |
| 5827 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| 5828 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2); |
| 5829 | |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 5830 | // Canonicalize (fma c, x, y) -> (fma x, c, y) |
Owen Anderson | f917d20 | 2012-05-30 18:54:50 +0000 | [diff] [blame] | 5831 | if (N0CFP && !N1CFP) |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 5832 | return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2); |
| 5833 | |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 5834 | return SDValue(); |
| 5835 | } |
| 5836 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5837 | SDValue DAGCombiner::visitFDIV(SDNode *N) { |
| 5838 | SDValue N0 = N->getOperand(0); |
| 5839 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5840 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5841 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5842 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5843 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5844 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5845 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5846 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5847 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5848 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 5849 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5850 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5851 | // fold (fdiv c1, c2) -> c1/c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5852 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5853 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5854 | |
Duncan Sands | 3ef3fcf | 2012-04-08 18:08:12 +0000 | [diff] [blame] | 5855 | // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable. |
| 5856 | if (N1CFP && VT != MVT::ppcf128 && DAG.getTarget().Options.UnsafeFPMath) { |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 5857 | // Compute the reciprocal 1.0 / c2. |
| 5858 | APFloat N1APF = N1CFP->getValueAPF(); |
| 5859 | APFloat Recip(N1APF.getSemantics(), 1); // 1.0 |
| 5860 | APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven); |
Duncan Sands | 507bb7a | 2012-04-10 20:35:27 +0000 | [diff] [blame] | 5861 | // Only do the transform if the reciprocal is a legal fp immediate that |
| 5862 | // isn't too nasty (eg NaN, denormal, ...). |
| 5863 | if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty |
Anton Korobeynikov | 999821c | 2012-04-10 13:22:49 +0000 | [diff] [blame] | 5864 | (!LegalOperations || |
| 5865 | // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM |
| 5866 | // backend)... we should handle this gracefully after Legalize. |
| 5867 | // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) || |
| 5868 | TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) || |
| 5869 | TLI.isFPImmLegal(Recip, VT))) |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 5870 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, |
| 5871 | DAG.getConstantFP(Recip, VT)); |
| 5872 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5873 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5874 | // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5875 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5876 | &DAG.getTarget().Options)) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 5877 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5878 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5879 | // Both can be negated for free, check to see if at least one is cheaper |
| 5880 | // negated. |
| 5881 | if (LHSNeg == 2 || RHSNeg == 2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5882 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5883 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 5884 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5885 | } |
| 5886 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5887 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5888 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5891 | SDValue DAGCombiner::visitFREM(SDNode *N) { |
| 5892 | SDValue N0 = N->getOperand(0); |
| 5893 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5894 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5895 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5896 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5897 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5898 | // fold (frem c1, c2) -> fmod(c1,c2) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5899 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5900 | return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5901 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5902 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5905 | SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 5906 | SDValue N0 = N->getOperand(0); |
| 5907 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5908 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5909 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5910 | EVT VT = N->getValueType(0); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5911 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5912 | if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5913 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5914 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5915 | if (N1CFP) { |
Dale Johannesen | e6c1742 | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 5916 | const APFloat& V = N1CFP->getValueAPF(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5917 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 5918 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5919 | if (!V.isNegative()) { |
| 5920 | if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5921 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5922 | } else { |
| 5923 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5924 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5925 | DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0)); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5926 | } |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5927 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5928 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5929 | // copysign(fabs(x), y) -> copysign(x, y) |
| 5930 | // copysign(fneg(x), y) -> copysign(x, y) |
| 5931 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 5932 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 5933 | N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5934 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5935 | N0.getOperand(0), N1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5936 | |
| 5937 | // copysign(x, abs(y)) -> abs(x) |
| 5938 | if (N1.getOpcode() == ISD::FABS) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5939 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5940 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5941 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 5942 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5943 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5944 | N0, N1.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5945 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5946 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 5947 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 5948 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5949 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5950 | N0, N1.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5951 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5952 | return SDValue(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5955 | SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
| 5956 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5957 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5958 | EVT VT = N->getValueType(0); |
| 5959 | EVT OpVT = N0.getValueType(); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5960 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5961 | // fold (sint_to_fp c1) -> c1fp |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 5962 | if (N0C && OpVT != MVT::ppcf128 && |
| 5963 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 5964 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5965 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5966 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5967 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5968 | // If the input is a legal type, and SINT_TO_FP is not legal on this target, |
| 5969 | // but UINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 5970 | if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && |
| 5971 | TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5972 | // 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] | 5973 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5974 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5975 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5976 | |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 5977 | // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
Owen Anderson | 4529999 | 2012-07-11 06:38:55 +0000 | [diff] [blame^] | 5978 | if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 && |
| 5979 | !VT.isVector() && |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 5980 | (!LegalOperations || |
| 5981 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 5982 | SDValue Ops[] = |
| 5983 | { N0.getOperand(0), N0.getOperand(1), |
| 5984 | DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 5985 | N0.getOperand(2) }; |
| 5986 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 5987 | } |
| 5988 | |
| 5989 | // fold (sint_to_fp (zext (setcc x, y, cc))) -> |
| 5990 | // (select_cc x, y, 1.0, 0.0,, cc) |
| 5991 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 5992 | N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() && |
| 5993 | (!LegalOperations || |
| 5994 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 5995 | SDValue Ops[] = |
| 5996 | { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1), |
| 5997 | DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 5998 | N0.getOperand(0).getOperand(2) }; |
| 5999 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 6000 | } |
| 6001 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6002 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6005 | SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
| 6006 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6007 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6008 | EVT VT = N->getValueType(0); |
| 6009 | EVT OpVT = N0.getValueType(); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6010 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6011 | // fold (uint_to_fp c1) -> c1fp |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6012 | if (N0C && OpVT != MVT::ppcf128 && |
| 6013 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6014 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6015 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6016 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6017 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6018 | // If the input is a legal type, and UINT_TO_FP is not legal on this target, |
| 6019 | // but SINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6020 | if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && |
| 6021 | TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6022 | // 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] | 6023 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6024 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6025 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6026 | |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6027 | // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
| 6028 | if (N0.getOpcode() == ISD::SETCC && !VT.isVector() && |
| 6029 | (!LegalOperations || |
| 6030 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6031 | SDValue Ops[] = |
| 6032 | { N0.getOperand(0), N0.getOperand(1), |
| 6033 | DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT), |
| 6034 | N0.getOperand(2) }; |
| 6035 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5); |
| 6036 | } |
| 6037 | |
| 6038 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6039 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6040 | } |
| 6041 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6042 | SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
| 6043 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6044 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6045 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6046 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6047 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6048 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6049 | return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0); |
| 6050 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6051 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6054 | SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
| 6055 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6056 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6057 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6058 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6059 | // fold (fp_to_uint c1fp) -> c1 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6060 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6061 | return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0); |
| 6062 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6063 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6064 | } |
| 6065 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6066 | SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { |
| 6067 | SDValue N0 = N->getOperand(0); |
| 6068 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6069 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6070 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6071 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6072 | // fold (fp_round c1fp) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6073 | if (N0CFP && N0.getValueType() != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6074 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6075 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6076 | // fold (fp_round (fp_extend x)) -> x |
| 6077 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 6078 | return N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6079 | |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6080 | // fold (fp_round (fp_round x)) -> (fp_round x) |
| 6081 | if (N0.getOpcode() == ISD::FP_ROUND) { |
| 6082 | // This is a value preserving truncation if both round's are. |
| 6083 | bool IsTrunc = N->getConstantOperandVal(1) == 1 && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6084 | N0.getNode()->getConstantOperandVal(1) == 1; |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6085 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6086 | DAG.getIntPtrConstant(IsTrunc)); |
| 6087 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6088 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6089 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6090 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6091 | SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT, |
| 6092 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6093 | AddToWorkList(Tmp.getNode()); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6094 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 6095 | Tmp, N0.getOperand(1)); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6096 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6097 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6098 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6101 | SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
| 6102 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6103 | EVT VT = N->getValueType(0); |
| 6104 | EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6105 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6106 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6107 | // fold (fp_round_inreg c1fp) -> c1fp |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6108 | if (N0CFP && isTypeLegal(EVT)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 6109 | SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6110 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6111 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6112 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6113 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6116 | SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { |
| 6117 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6118 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6119 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6120 | |
Chris Lattner | 5938bef | 2007-12-29 06:55:23 +0000 | [diff] [blame] | 6121 | // 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] | 6122 | if (N->hasOneUse() && |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6123 | N->use_begin()->getOpcode() == ISD::FP_ROUND) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6124 | return SDValue(); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6125 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6126 | // fold (fp_extend c1fp) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6127 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6128 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6129 | |
| 6130 | // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the |
| 6131 | // value of X. |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 6132 | if (N0.getOpcode() == ISD::FP_ROUND |
| 6133 | && N0.getNode()->getConstantOperandVal(1) == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6134 | SDValue In = N0.getOperand(0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6135 | if (In.getValueType() == VT) return In; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 6136 | if (VT.bitsLT(In.getValueType())) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6137 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, |
| 6138 | In, N0.getOperand(1)); |
| 6139 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6140 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6141 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6142 | // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6143 | if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6144 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 6145 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 6146 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 6147 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6148 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 6149 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6150 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6151 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 6152 | LN0->getAlignment()); |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6153 | CombineTo(N, ExtLoad); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6154 | CombineTo(N0.getNode(), |
| 6155 | DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), |
| 6156 | N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)), |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6157 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6158 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6159 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6160 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6161 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6164 | SDValue DAGCombiner::visitFNEG(SDNode *N) { |
| 6165 | SDValue N0 = N->getOperand(0); |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6166 | EVT VT = N->getValueType(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6167 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6168 | if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(), |
| 6169 | &DAG.getTarget().Options)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6170 | return GetNegatedExpression(N0, DAG, LegalOperations); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6171 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6172 | // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading |
| 6173 | // constant pool values. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6174 | if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST && |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6175 | !VT.isVector() && |
| 6176 | N0.getNode()->hasOneUse() && |
| 6177 | N0.getOperand(0).getValueType().isInteger()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6178 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6179 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6180 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6181 | Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int, |
| 6182 | DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6183 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6184 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6185 | VT, Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6186 | } |
| 6187 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6188 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6189 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6190 | } |
| 6191 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6192 | SDValue DAGCombiner::visitFABS(SDNode *N) { |
| 6193 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6194 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6195 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6196 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6197 | // fold (fabs c1) -> fabs(c1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6198 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6199 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6200 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6201 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 6202 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6203 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6204 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 6205 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6206 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6207 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6208 | // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading |
| 6209 | // constant pool values. |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6210 | if (!TLI.isFAbsFree(VT) && |
| 6211 | N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6212 | N0.getOperand(0).getValueType().isInteger() && |
| 6213 | !N0.getOperand(0).getValueType().isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6214 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6215 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6216 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6217 | Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int, |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6218 | DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6219 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6220 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6221 | N->getValueType(0), Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6222 | } |
| 6223 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6224 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6225 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6228 | SDValue DAGCombiner::visitBRCOND(SDNode *N) { |
| 6229 | SDValue Chain = N->getOperand(0); |
| 6230 | SDValue N1 = N->getOperand(1); |
| 6231 | SDValue N2 = N->getOperand(2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6232 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 6233 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 6234 | // branch. However that doesn't happen very often in normal code, because |
| 6235 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 6236 | // If we did this folding here, it would be necessary to update the |
| 6237 | // MachineBasicBlock CFG, which is awkward. |
| 6238 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6239 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 6240 | // on the target. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6241 | if (N1.getOpcode() == ISD::SETCC && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6242 | TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) { |
| 6243 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6244 | Chain, N1.getOperand(2), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6245 | N1.getOperand(0), N1.getOperand(1), N2); |
| 6246 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6247 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6248 | if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || |
| 6249 | ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && |
| 6250 | (N1.getOperand(0).hasOneUse() && |
| 6251 | N1.getOperand(0).getOpcode() == ISD::SRL))) { |
| 6252 | SDNode *Trunc = 0; |
| 6253 | if (N1.getOpcode() == ISD::TRUNCATE) { |
| 6254 | // Look pass the truncate. |
| 6255 | Trunc = N1.getNode(); |
| 6256 | N1 = N1.getOperand(0); |
| 6257 | } |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6258 | |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6259 | // Match this pattern so that we can generate simpler code: |
| 6260 | // |
| 6261 | // %a = ... |
| 6262 | // %b = and i32 %a, 2 |
| 6263 | // %c = srl i32 %b, 1 |
| 6264 | // brcond i32 %c ... |
| 6265 | // |
| 6266 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6267 | // |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6268 | // %a = ... |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6269 | // %b = and i32 %a, 2 |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6270 | // %c = setcc eq %b, 0 |
| 6271 | // brcond %c ... |
| 6272 | // |
| 6273 | // This applies only when the AND constant value has one bit set and the |
| 6274 | // SRL constant is equal to the log2 of the AND constant. The back-end is |
| 6275 | // smart enough to convert the result into a TEST/JMP sequence. |
| 6276 | SDValue Op0 = N1.getOperand(0); |
| 6277 | SDValue Op1 = N1.getOperand(1); |
| 6278 | |
| 6279 | if (Op0.getOpcode() == ISD::AND && |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6280 | Op1.getOpcode() == ISD::Constant) { |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6281 | SDValue AndOp1 = Op0.getOperand(1); |
| 6282 | |
| 6283 | if (AndOp1.getOpcode() == ISD::Constant) { |
| 6284 | const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); |
| 6285 | |
| 6286 | if (AndConst.isPowerOf2() && |
| 6287 | cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { |
| 6288 | SDValue SetCC = |
| 6289 | DAG.getSetCC(N->getDebugLoc(), |
| 6290 | TLI.getSetCCResultType(Op0.getValueType()), |
| 6291 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 6292 | ISD::SETNE); |
| 6293 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6294 | SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6295 | MVT::Other, Chain, SetCC, N2); |
| 6296 | // Don't add the new BRCond into the worklist or else SimplifySelectCC |
| 6297 | // will convert it back to (X & C1) >> C2. |
| 6298 | CombineTo(N, NewBRCond, false); |
| 6299 | // Truncate is dead. |
| 6300 | if (Trunc) { |
| 6301 | removeFromWorkList(Trunc); |
| 6302 | DAG.DeleteNode(Trunc); |
| 6303 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6304 | // Replace the uses of SRL with SETCC |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6305 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6306 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6307 | removeFromWorkList(N1.getNode()); |
| 6308 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6309 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | } |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6313 | |
| 6314 | if (Trunc) |
| 6315 | // Restore N1 if the above transformation doesn't match. |
| 6316 | N1 = N->getOperand(1); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6317 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6318 | |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6319 | // Transform br(xor(x, y)) -> br(x != y) |
| 6320 | // Transform br(xor(xor(x,y), 1)) -> br (x == y) |
| 6321 | if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { |
| 6322 | SDNode *TheXor = N1.getNode(); |
| 6323 | SDValue Op0 = TheXor->getOperand(0); |
| 6324 | SDValue Op1 = TheXor->getOperand(1); |
| 6325 | if (Op0.getOpcode() == Op1.getOpcode()) { |
| 6326 | // Avoid missing important xor optimizations. |
| 6327 | SDValue Tmp = visitXOR(TheXor); |
Bill Wendling | 86c5abb | 2010-04-20 01:25:01 +0000 | [diff] [blame] | 6328 | if (Tmp.getNode() && Tmp.getNode() != TheXor) { |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6329 | DEBUG(dbgs() << "\nReplacing.8 "; |
| 6330 | TheXor->dump(&DAG); |
| 6331 | dbgs() << "\nWith: "; |
| 6332 | Tmp.getNode()->dump(&DAG); |
| 6333 | dbgs() << '\n'); |
| 6334 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6335 | DAG.ReplaceAllUsesOfValueWith(N1, Tmp); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6336 | removeFromWorkList(TheXor); |
| 6337 | DAG.DeleteNode(TheXor); |
| 6338 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6339 | MVT::Other, Chain, Tmp, N2); |
| 6340 | } |
| 6341 | } |
| 6342 | |
| 6343 | if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { |
| 6344 | bool Equal = false; |
| 6345 | if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0)) |
| 6346 | if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() && |
| 6347 | Op0.getOpcode() == ISD::XOR) { |
| 6348 | TheXor = Op0.getNode(); |
| 6349 | Equal = true; |
| 6350 | } |
| 6351 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6352 | EVT SetCCVT = N1.getValueType(); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6353 | if (LegalTypes) |
| 6354 | SetCCVT = TLI.getSetCCResultType(SetCCVT); |
| 6355 | SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(), |
| 6356 | SetCCVT, |
| 6357 | Op0, Op1, |
| 6358 | Equal ? ISD::SETEQ : ISD::SETNE); |
| 6359 | // Replace the uses of XOR with SETCC |
| 6360 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6361 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6362 | removeFromWorkList(N1.getNode()); |
| 6363 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6364 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 6365 | MVT::Other, Chain, SetCC, N2); |
| 6366 | } |
| 6367 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6368 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6369 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 6372 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 6373 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6374 | SDValue DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 6375 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6376 | SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6377 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 6378 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 6379 | // branch. However that doesn't happen very often in normal code, because |
| 6380 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 6381 | // If we did this folding here, it would be necessary to update the |
| 6382 | // MachineBasicBlock CFG, which is awkward. |
| 6383 | |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 6384 | // Use SimplifySetCC to simplify SETCC's. |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 6385 | SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 6386 | CondLHS, CondRHS, CC->get(), N->getDebugLoc(), |
| 6387 | false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6388 | if (Simp.getNode()) AddToWorkList(Simp.getNode()); |
Chris Lattner | 30f73e7 | 2006-10-14 03:52:46 +0000 | [diff] [blame] | 6389 | |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 6390 | // fold to a simpler setcc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6391 | if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6392 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6393 | N->getOperand(0), Simp.getOperand(2), |
| 6394 | Simp.getOperand(0), Simp.getOperand(1), |
| 6395 | N->getOperand(4)); |
| 6396 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6397 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6400 | /// canFoldInAddressingMode - Return true if 'Use' is a load or a store that |
| 6401 | /// 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] | 6402 | /// addressing mode. |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6403 | static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, |
| 6404 | SelectionDAG &DAG, |
| 6405 | const TargetLowering &TLI) { |
| 6406 | EVT VT; |
| 6407 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) { |
| 6408 | if (LD->isIndexed() || LD->getBasePtr().getNode() != N) |
| 6409 | return false; |
| 6410 | VT = Use->getValueType(0); |
| 6411 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) { |
| 6412 | if (ST->isIndexed() || ST->getBasePtr().getNode() != N) |
| 6413 | return false; |
| 6414 | VT = ST->getValue().getValueType(); |
| 6415 | } else |
| 6416 | return false; |
| 6417 | |
| 6418 | TargetLowering::AddrMode AM; |
| 6419 | if (N->getOpcode() == ISD::ADD) { |
| 6420 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 6421 | if (Offset) |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6422 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6423 | AM.BaseOffs = Offset->getSExtValue(); |
| 6424 | else |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6425 | // [reg +/- reg] |
| 6426 | AM.Scale = 1; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6427 | } else if (N->getOpcode() == ISD::SUB) { |
| 6428 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 6429 | if (Offset) |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6430 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6431 | AM.BaseOffs = -Offset->getSExtValue(); |
| 6432 | else |
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 6433 | // [reg +/- reg] |
| 6434 | AM.Scale = 1; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6435 | } else |
| 6436 | return false; |
| 6437 | |
| 6438 | return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext())); |
| 6439 | } |
| 6440 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 6441 | /// CombineToPreIndexedLoadStore - Try turning a load / store into a |
| 6442 | /// 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] | 6443 | /// and it has other uses besides the load / store. After the |
| 6444 | /// transformation, the new indexed load / store has effectively folded |
| 6445 | /// the add / subtract in and all of its other uses are redirected to the |
| 6446 | /// new load / store. |
| 6447 | bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6448 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6449 | return false; |
| 6450 | |
| 6451 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6452 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6453 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6454 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6455 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 6456 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6457 | VT = LD->getMemoryVT(); |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 6458 | if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6459 | !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) |
| 6460 | return false; |
| 6461 | Ptr = LD->getBasePtr(); |
| 6462 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6463 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 6464 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6465 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6466 | if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && |
| 6467 | !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) |
| 6468 | return false; |
| 6469 | Ptr = ST->getBasePtr(); |
| 6470 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6471 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6472 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6473 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6474 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6475 | // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail |
| 6476 | // out. There is no reason to make this a preinc/predec. |
| 6477 | if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6478 | Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6479 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6480 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6481 | // Ask the target to do addressing mode selection. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6482 | SDValue BasePtr; |
| 6483 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6484 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 6485 | if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) |
| 6486 | return false; |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 6487 | // Don't create a indexed load / store with zero offset. |
| 6488 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 6489 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 6490 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6491 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 6492 | // Try turning it into a pre-indexed load / store except when: |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 6493 | // 1) The new base ptr is a frame index. |
| 6494 | // 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] | 6495 | // predecessor of the value being stored. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 6496 | // 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] | 6497 | // that would create a cycle. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 6498 | // 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] | 6499 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 6500 | // Check #1. Preinc'ing a frame index would require copying the stack pointer |
| 6501 | // (plus the implicit offset) to a register to preinc anyway. |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 6502 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 6503 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6504 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 6505 | // Check #2. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6506 | if (!isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6507 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6508 | if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6509 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6510 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6511 | |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 6512 | // Now check for #3 and #4. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6513 | bool RealUse = false; |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6514 | |
| 6515 | // Caches for hasPredecessorHelper |
| 6516 | SmallPtrSet<const SDNode *, 32> Visited; |
| 6517 | SmallVector<const SDNode *, 16> Worklist; |
| 6518 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6519 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 6520 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 6521 | SDNode *Use = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6522 | if (Use == N) |
| 6523 | continue; |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6524 | if (N->hasPredecessorHelper(Use, Visited, Worklist)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6525 | return false; |
| 6526 | |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6527 | // If Ptr may be folded in addressing mode of other use, then it's |
| 6528 | // not profitable to do this transformation. |
| 6529 | if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6530 | RealUse = true; |
| 6531 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6532 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6533 | if (!RealUse) |
| 6534 | return false; |
| 6535 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6536 | SDValue Result; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6537 | if (isLoad) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6538 | Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 6539 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6540 | else |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6541 | Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 6542 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6543 | ++PreIndexedNodes; |
| 6544 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6545 | DEBUG(dbgs() << "\nReplacing.4 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6546 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6547 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6548 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6549 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 6550 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6551 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6552 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 6553 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6554 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6555 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6558 | // Finally, since the node is now dead, remove it from the graph. |
| 6559 | DAG.DeleteNode(N); |
| 6560 | |
| 6561 | // 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] | 6562 | DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6563 | removeFromWorkList(Ptr.getNode()); |
| 6564 | DAG.DeleteNode(Ptr.getNode()); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6565 | |
| 6566 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6567 | } |
| 6568 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 6569 | /// CombineToPostIndexedLoadStore - Try to combine a load / store with a |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6570 | /// add / sub of the base pointer node into a post-indexed load / store. |
| 6571 | /// The transformation folded the add / subtract into the new indexed |
| 6572 | /// load / store effectively and all of its uses are redirected to the |
| 6573 | /// new load / store. |
| 6574 | bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6575 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6576 | return false; |
| 6577 | |
| 6578 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6579 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6580 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6581 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6582 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 6583 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6584 | VT = LD->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6585 | if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && |
| 6586 | !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) |
| 6587 | return false; |
| 6588 | Ptr = LD->getBasePtr(); |
| 6589 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6590 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 6591 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6592 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6593 | if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && |
| 6594 | !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) |
| 6595 | return false; |
| 6596 | Ptr = ST->getBasePtr(); |
| 6597 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6598 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6599 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6600 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6601 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6602 | if (Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6603 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6604 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6605 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 6606 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 6607 | SDNode *Op = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6608 | if (Op == N || |
| 6609 | (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) |
| 6610 | continue; |
| 6611 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6612 | SDValue BasePtr; |
| 6613 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6614 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 6615 | if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 6616 | // Don't create a indexed load / store with zero offset. |
| 6617 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 6618 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 6619 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6620 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6621 | // Try turning it into a post-indexed load / store except when |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6622 | // 1) All uses are load / store ops that use it as base ptr (and |
| 6623 | // it may be folded as addressing mmode). |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6624 | // 2) Op must be independent of N, i.e. Op is neither a predecessor |
| 6625 | // nor a successor of N. Otherwise, if Op is folded that would |
| 6626 | // create a cycle. |
| 6627 | |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 6628 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
| 6629 | continue; |
| 6630 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6631 | // Check for #1. |
| 6632 | bool TryNext = false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6633 | for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(), |
| 6634 | EE = BasePtr.getNode()->use_end(); II != EE; ++II) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 6635 | SDNode *Use = *II; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6636 | if (Use == Ptr.getNode()) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6637 | continue; |
| 6638 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6639 | // If all the uses are load / store addresses, then don't do the |
| 6640 | // transformation. |
| 6641 | if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ |
| 6642 | bool RealUse = false; |
| 6643 | for (SDNode::use_iterator III = Use->use_begin(), |
| 6644 | EEE = Use->use_end(); III != EEE; ++III) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 6645 | SDNode *UseUse = *III; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 6646 | if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6647 | RealUse = true; |
| 6648 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6649 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6650 | if (!RealUse) { |
| 6651 | TryNext = true; |
| 6652 | break; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6653 | } |
| 6654 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6655 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6656 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6657 | if (TryNext) |
| 6658 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6659 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6660 | // Check for #2 |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 6661 | if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6662 | SDValue Result = isLoad |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6663 | ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 6664 | BasePtr, Offset, AM) |
| 6665 | : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 6666 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6667 | ++PostIndexedNodes; |
| 6668 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6669 | DEBUG(dbgs() << "\nReplacing.5 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6670 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6671 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6672 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6673 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 6674 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6675 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6676 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 6677 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6678 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6679 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6680 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6681 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6682 | // Finally, since the node is now dead, remove it from the graph. |
| 6683 | DAG.DeleteNode(N); |
| 6684 | |
| 6685 | // Replace the uses of Use with uses of the updated base value. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6686 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6687 | Result.getValue(isLoad ? 1 : 0)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6688 | removeFromWorkList(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6689 | DAG.DeleteNode(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 6690 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6691 | } |
| 6692 | } |
| 6693 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6694 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 6695 | return false; |
| 6696 | } |
| 6697 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6698 | SDValue DAGCombiner::visitLOAD(SDNode *N) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 6699 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6700 | SDValue Chain = LD->getChain(); |
| 6701 | SDValue Ptr = LD->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6702 | |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 6703 | // If load is not volatile and there are no uses of the loaded value (and |
| 6704 | // the updated indexed value in case of indexed loads), change uses of the |
| 6705 | // chain value into uses of the chain input (i.e. delete the dead load). |
| 6706 | if (!LD->isVolatile()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6707 | if (N->getValueType(1) == MVT::Other) { |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 6708 | // Unindexed loads. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 6709 | if (!N->hasAnyUseOfValue(0)) { |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 6710 | // It's not safe to use the two value CombineTo variant here. e.g. |
| 6711 | // v1, chain2 = load chain1, loc |
| 6712 | // v2, chain3 = load chain2, loc |
| 6713 | // v3 = add v2, c |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 6714 | // Now we replace use of chain2 with chain1. This makes the second load |
| 6715 | // 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] | 6716 | DEBUG(dbgs() << "\nReplacing.6 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6717 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6718 | dbgs() << "\nWith chain: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6719 | Chain.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6720 | dbgs() << "\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 6721 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6722 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6723 | |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 6724 | if (N->use_empty()) { |
| 6725 | removeFromWorkList(N); |
| 6726 | DAG.DeleteNode(N); |
| 6727 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6728 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6729 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 6730 | } |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 6731 | } else { |
| 6732 | // Indexed loads. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6733 | assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 6734 | if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 6735 | SDValue Undef = DAG.getUNDEF(N->getValueType(0)); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6736 | DEBUG(dbgs() << "\nReplacing.7 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6737 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6738 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 6739 | Undef.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 6740 | dbgs() << " and 2 other values\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 6741 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6742 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6743 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6744 | DAG.getUNDEF(N->getValueType(1))); |
| 6745 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 6746 | removeFromWorkList(N); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 6747 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6748 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 6749 | } |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 6750 | } |
| 6751 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6752 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 6753 | // If this load is directly stored, replace the load value with the stored |
| 6754 | // value. |
| 6755 | // TODO: Handle store large -> read small portion. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6756 | // TODO: Handle TRUNCSTORE/LOADEXT |
Evan Cheng | 9ef82ce | 2011-03-11 00:48:56 +0000 | [diff] [blame] | 6757 | if (ISD::isNormalLoad(N) && !LD->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6758 | if (ISD::isNON_TRUNCStore(Chain.getNode())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 6759 | StoreSDNode *PrevST = cast<StoreSDNode>(Chain); |
| 6760 | if (PrevST->getBasePtr() == Ptr && |
| 6761 | PrevST->getValue().getValueType() == N->getValueType(0)) |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6762 | return CombineTo(N, Chain.getOperand(1), Chain); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 6763 | } |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6764 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6765 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 6766 | // Try to infer better alignment information than the load already has. |
| 6767 | if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 6768 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 6769 | if (Align > LD->getAlignment()) |
| 6770 | return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(), |
| 6771 | LD->getValueType(0), |
| 6772 | Chain, Ptr, LD->getPointerInfo(), |
| 6773 | LD->getMemoryVT(), |
| 6774 | LD->isVolatile(), LD->isNonTemporal(), Align); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 6778 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6779 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6780 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6781 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 6782 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6783 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6784 | SDValue ReplLoad; |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6785 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6786 | // Replace the chain to void dependency. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6787 | if (LD->getExtensionType() == ISD::NON_EXTLOAD) { |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6788 | ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6789 | BetterChain, Ptr, LD->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6790 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 6791 | LD->isInvariant(), LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6792 | } else { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 6793 | ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(), |
| 6794 | LD->getValueType(0), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6795 | BetterChain, Ptr, LD->getPointerInfo(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6796 | LD->getMemoryVT(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6797 | LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6798 | LD->isNonTemporal(), |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 6799 | LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 6800 | } |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6801 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 6802 | // Create token factor to keep old chain connected. |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6803 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6804 | MVT::Other, Chain, ReplLoad.getValue(1)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6805 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 6806 | // Make sure the new and old chains are cleaned up. |
| 6807 | AddToWorkList(Token.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6808 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 6809 | // Replace uses with load result and token factor. Don't add users |
| 6810 | // to work list. |
| 6811 | return CombineTo(N, ReplLoad.getValue(0), Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6812 | } |
| 6813 | } |
| 6814 | |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 6815 | // Try transforming N to an indexed load. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 6816 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6817 | return SDValue(N, 0); |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 6818 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6819 | return SDValue(); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6822 | /// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the |
| 6823 | /// load is having specific bytes cleared out. If so, return the byte size |
| 6824 | /// being masked out and the shift amount. |
| 6825 | static std::pair<unsigned, unsigned> |
| 6826 | CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { |
| 6827 | std::pair<unsigned, unsigned> Result(0, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6828 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6829 | // Check for the structure we're looking for. |
| 6830 | if (V->getOpcode() != ISD::AND || |
| 6831 | !isa<ConstantSDNode>(V->getOperand(1)) || |
| 6832 | !ISD::isNormalLoad(V->getOperand(0).getNode())) |
| 6833 | return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6834 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 6835 | // Check the chain and pointer. |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6836 | LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 6837 | if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6838 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 6839 | // The store should be chained directly to the load or be an operand of a |
| 6840 | // tokenfactor. |
| 6841 | if (LD == Chain.getNode()) |
| 6842 | ; // ok. |
| 6843 | else if (Chain->getOpcode() != ISD::TokenFactor) |
| 6844 | return Result; // Fail. |
| 6845 | else { |
| 6846 | bool isOk = false; |
| 6847 | for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i) |
| 6848 | if (Chain->getOperand(i).getNode() == LD) { |
| 6849 | isOk = true; |
| 6850 | break; |
| 6851 | } |
| 6852 | if (!isOk) return Result; |
| 6853 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6854 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6855 | // This only handles simple types. |
| 6856 | if (V.getValueType() != MVT::i16 && |
| 6857 | V.getValueType() != MVT::i32 && |
| 6858 | V.getValueType() != MVT::i64) |
| 6859 | return Result; |
| 6860 | |
| 6861 | // Check the constant mask. Invert it so that the bits being masked out are |
| 6862 | // 0 and the bits being kept are 1. Use getSExtValue so that leading bits |
| 6863 | // follow the sign bit for uniformity. |
| 6864 | uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); |
| 6865 | unsigned NotMaskLZ = CountLeadingZeros_64(NotMask); |
| 6866 | if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. |
| 6867 | unsigned NotMaskTZ = CountTrailingZeros_64(NotMask); |
| 6868 | if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. |
| 6869 | if (NotMaskLZ == 64) return Result; // All zero mask. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6870 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6871 | // See if we have a continuous run of bits. If so, we have 0*1+0* |
| 6872 | if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64) |
| 6873 | return Result; |
| 6874 | |
| 6875 | // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. |
| 6876 | if (V.getValueType() != MVT::i64 && NotMaskLZ) |
| 6877 | NotMaskLZ -= 64-V.getValueSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6878 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6879 | unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; |
| 6880 | switch (MaskedBytes) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6881 | case 1: |
| 6882 | case 2: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6883 | case 4: break; |
| 6884 | default: return Result; // All one mask, or 5-byte mask. |
| 6885 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6886 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6887 | // Verify that the first bit starts at a multiple of mask so that the access |
| 6888 | // is aligned the same as the access width. |
| 6889 | if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6890 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6891 | Result.first = MaskedBytes; |
| 6892 | Result.second = NotMaskTZ/8; |
| 6893 | return Result; |
| 6894 | } |
| 6895 | |
| 6896 | |
| 6897 | /// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that |
| 6898 | /// provides a value as specified by MaskInfo. If so, replace the specified |
| 6899 | /// store with a narrower store of truncated IVal. |
| 6900 | static SDNode * |
| 6901 | ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, |
| 6902 | SDValue IVal, StoreSDNode *St, |
| 6903 | DAGCombiner *DC) { |
| 6904 | unsigned NumBytes = MaskInfo.first; |
| 6905 | unsigned ByteShift = MaskInfo.second; |
| 6906 | SelectionDAG &DAG = DC->getDAG(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6907 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6908 | // Check to see if IVal is all zeros in the part being masked in by the 'or' |
| 6909 | // that uses this. If not, this is not a replacement. |
| 6910 | APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), |
| 6911 | ByteShift*8, (ByteShift+NumBytes)*8); |
| 6912 | if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6913 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6914 | // Check that it is legal on the target to do this. It is legal if the new |
| 6915 | // VT we're shrinking to (i8/i16/i32) is legal or we're still before type |
| 6916 | // legalization. |
| 6917 | MVT VT = MVT::getIntegerVT(NumBytes*8); |
| 6918 | if (!DC->isTypeLegal(VT)) |
| 6919 | return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6920 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6921 | // Okay, we can do this! Replace the 'St' store with a store of IVal that is |
| 6922 | // shifted by ByteShift and truncated down to NumBytes. |
| 6923 | if (ByteShift) |
| 6924 | IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 6925 | DAG.getConstant(ByteShift*8, |
| 6926 | DC->getShiftAmountTy(IVal.getValueType()))); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6927 | |
| 6928 | // Figure out the offset for the store and the alignment of the access. |
| 6929 | unsigned StOffset; |
| 6930 | unsigned NewAlign = St->getAlignment(); |
| 6931 | |
| 6932 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 6933 | StOffset = ByteShift; |
| 6934 | else |
| 6935 | StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6936 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6937 | SDValue Ptr = St->getBasePtr(); |
| 6938 | if (StOffset) { |
| 6939 | Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(), |
| 6940 | Ptr, DAG.getConstant(StOffset, Ptr.getValueType())); |
| 6941 | NewAlign = MinAlign(NewAlign, StOffset); |
| 6942 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6943 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6944 | // Truncate down to the new size. |
| 6945 | IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6946 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6947 | ++OpsNarrowed; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6948 | return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6949 | St->getPointerInfo().getWithOffset(StOffset), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6950 | false, false, NewAlign).getNode(); |
| 6951 | } |
| 6952 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6953 | |
| 6954 | /// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is |
| 6955 | /// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some |
| 6956 | /// of the loaded bits, try narrowing the load and store if it would end up |
| 6957 | /// being a win for performance or code size. |
| 6958 | SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { |
| 6959 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6960 | if (ST->isVolatile()) |
| 6961 | return SDValue(); |
| 6962 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6963 | SDValue Chain = ST->getChain(); |
| 6964 | SDValue Value = ST->getValue(); |
| 6965 | SDValue Ptr = ST->getBasePtr(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6966 | EVT VT = Value.getValueType(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6967 | |
| 6968 | if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6969 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6970 | |
| 6971 | unsigned Opc = Value.getOpcode(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6972 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6973 | // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst |
| 6974 | // is a byte mask indicating a consecutive number of bytes, check to see if |
| 6975 | // Y is known to provide just those bytes. If so, we try to replace the |
| 6976 | // load + replace + store sequence with a single (narrower) store, which makes |
| 6977 | // the load dead. |
| 6978 | if (Opc == ISD::OR) { |
| 6979 | std::pair<unsigned, unsigned> MaskedLoad; |
| 6980 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); |
| 6981 | if (MaskedLoad.first) |
| 6982 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 6983 | Value.getOperand(1), ST,this)) |
| 6984 | return SDValue(NewST, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6985 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6986 | // Or is commutative, so try swapping X and Y. |
| 6987 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); |
| 6988 | if (MaskedLoad.first) |
| 6989 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 6990 | Value.getOperand(0), ST,this)) |
| 6991 | return SDValue(NewST, 0); |
| 6992 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6993 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6994 | if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || |
| 6995 | Value.getOperand(1).getOpcode() != ISD::Constant) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6996 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6997 | |
| 6998 | SDValue N0 = Value.getOperand(0); |
Dan Gohman | 24bde5b | 2010-09-02 21:18:42 +0000 | [diff] [blame] | 6999 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| 7000 | Chain == SDValue(N0.getNode(), 1)) { |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7001 | LoadSDNode *LD = cast<LoadSDNode>(N0); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7002 | if (LD->getBasePtr() != Ptr || |
| 7003 | LD->getPointerInfo().getAddrSpace() != |
| 7004 | ST->getPointerInfo().getAddrSpace()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7005 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7006 | |
| 7007 | // Find the type to narrow it the load / op / store to. |
| 7008 | SDValue N1 = Value.getOperand(1); |
| 7009 | unsigned BitWidth = N1.getValueSizeInBits(); |
| 7010 | APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); |
| 7011 | if (Opc == ISD::AND) |
| 7012 | Imm ^= APInt::getAllOnesValue(BitWidth); |
Evan Cheng | d3c76bb | 2009-05-28 23:52:18 +0000 | [diff] [blame] | 7013 | if (Imm == 0 || Imm.isAllOnesValue()) |
| 7014 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7015 | unsigned ShAmt = Imm.countTrailingZeros(); |
| 7016 | unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; |
| 7017 | unsigned NewBW = NextPowerOf2(MSB - ShAmt); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 7018 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7019 | while (NewBW < BitWidth && |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7020 | !(TLI.isOperationLegalOrCustom(Opc, NewVT) && |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7021 | TLI.isNarrowingProfitable(VT, NewVT))) { |
| 7022 | NewBW = NextPowerOf2(NewBW); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 7023 | NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7024 | } |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7025 | if (NewBW >= BitWidth) |
| 7026 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7027 | |
| 7028 | // If the lsb changed does not start at the type bitwidth boundary, |
| 7029 | // start at the previous one. |
| 7030 | if (ShAmt % NewBW) |
| 7031 | ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; |
| 7032 | APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW); |
| 7033 | if ((Imm & Mask) == Imm) { |
| 7034 | APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); |
| 7035 | if (Opc == ISD::AND) |
| 7036 | NewImm ^= APInt::getAllOnesValue(NewBW); |
| 7037 | uint64_t PtrOff = ShAmt / 8; |
| 7038 | // For big endian targets, we need to adjust the offset to the pointer to |
| 7039 | // load the correct bytes. |
| 7040 | if (TLI.isBigEndian()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7041 | PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7042 | |
| 7043 | unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7044 | Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7045 | if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy)) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7046 | return SDValue(); |
| 7047 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7048 | SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(), |
| 7049 | Ptr.getValueType(), Ptr, |
| 7050 | DAG.getConstant(PtrOff, Ptr.getValueType())); |
| 7051 | SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(), |
| 7052 | LD->getChain(), NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7053 | LD->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7054 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7055 | LD->isInvariant(), NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7056 | SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD, |
| 7057 | DAG.getConstant(NewImm, NewVT)); |
| 7058 | SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(), |
| 7059 | NewVal, NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7060 | ST->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7061 | false, false, NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7062 | |
| 7063 | AddToWorkList(NewPtr.getNode()); |
| 7064 | AddToWorkList(NewLD.getNode()); |
| 7065 | AddToWorkList(NewVal.getNode()); |
| 7066 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7067 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7068 | ++OpsNarrowed; |
| 7069 | return NewST; |
| 7070 | } |
| 7071 | } |
| 7072 | |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 7073 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7074 | } |
| 7075 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7076 | /// TransformFPLoadStorePair - For a given floating point load / store pair, |
| 7077 | /// if the load value isn't used by any other operations, then consider |
| 7078 | /// transforming the pair to integer load / store operations if the target |
| 7079 | /// deems the transformation profitable. |
| 7080 | SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { |
| 7081 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 7082 | SDValue Chain = ST->getChain(); |
| 7083 | SDValue Value = ST->getValue(); |
| 7084 | if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && |
| 7085 | Value.hasOneUse() && |
| 7086 | Chain == SDValue(Value.getNode(), 1)) { |
| 7087 | LoadSDNode *LD = cast<LoadSDNode>(Value); |
| 7088 | EVT VT = LD->getMemoryVT(); |
| 7089 | if (!VT.isFloatingPoint() || |
| 7090 | VT != ST->getMemoryVT() || |
| 7091 | LD->isNonTemporal() || |
| 7092 | ST->isNonTemporal() || |
| 7093 | LD->getPointerInfo().getAddrSpace() != 0 || |
| 7094 | ST->getPointerInfo().getAddrSpace() != 0) |
| 7095 | return SDValue(); |
| 7096 | |
| 7097 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); |
| 7098 | if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || |
| 7099 | !TLI.isOperationLegal(ISD::STORE, IntVT) || |
| 7100 | !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || |
| 7101 | !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) |
| 7102 | return SDValue(); |
| 7103 | |
| 7104 | unsigned LDAlign = LD->getAlignment(); |
| 7105 | unsigned STAlign = ST->getAlignment(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7106 | Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7107 | unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy); |
| 7108 | if (LDAlign < ABIAlign || STAlign < ABIAlign) |
| 7109 | return SDValue(); |
| 7110 | |
| 7111 | SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(), |
| 7112 | LD->getChain(), LD->getBasePtr(), |
| 7113 | LD->getPointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7114 | false, false, false, LDAlign); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7115 | |
| 7116 | SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(), |
| 7117 | NewLD, ST->getBasePtr(), |
| 7118 | ST->getPointerInfo(), |
| 7119 | false, false, STAlign); |
| 7120 | |
| 7121 | AddToWorkList(NewLD.getNode()); |
| 7122 | AddToWorkList(NewST.getNode()); |
| 7123 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7124 | DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7125 | ++LdStFP2Int; |
| 7126 | return NewST; |
| 7127 | } |
| 7128 | |
| 7129 | return SDValue(); |
| 7130 | } |
| 7131 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7132 | SDValue DAGCombiner::visitSTORE(SDNode *N) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7133 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7134 | SDValue Chain = ST->getChain(); |
| 7135 | SDValue Value = ST->getValue(); |
| 7136 | SDValue Ptr = ST->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7137 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 7138 | // 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] | 7139 | // resultant store does not need a higher alignment than the original. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7140 | if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7141 | ST->isUnindexed()) { |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 7142 | unsigned OrigAlign = ST->getAlignment(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7143 | EVT SVT = Value.getOperand(0).getValueType(); |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 7144 | unsigned Align = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 7145 | getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext())); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7146 | if (Align <= OrigAlign && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7147 | ((!LegalOperations && !ST->isVolatile()) || |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 7148 | TLI.isOperationLegalOrCustom(ISD::STORE, SVT))) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7149 | return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7150 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7151 | ST->isNonTemporal(), OrigAlign); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7152 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 7153 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 7154 | // Turn 'store undef, Ptr' -> nothing. |
| 7155 | if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed()) |
| 7156 | return Chain; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7157 | |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 7158 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 7159 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7160 | // NOTE: If the original store is volatile, this transform must not increase |
| 7161 | // the number of stores. For example, on x86-32 an f64 can be stored in one |
| 7162 | // processor operation but an i64 (which is not legal) requires two. So the |
| 7163 | // transform should not be done in this case. |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 7164 | if (Value.getOpcode() != ISD::TargetConstantFP) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7165 | SDValue Tmp; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7166 | switch (CFP->getValueType(0).getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 7167 | default: llvm_unreachable("Unknown FP type"); |
Pete Cooper | 438c040 | 2012-06-21 18:00:39 +0000 | [diff] [blame] | 7168 | case MVT::f16: // We don't do this for these yet. |
| 7169 | case MVT::f80: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7170 | case MVT::f128: |
| 7171 | case MVT::ppcf128: |
Dale Johannesen | c7b21d5 | 2007-09-18 18:36:59 +0000 | [diff] [blame] | 7172 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7173 | case MVT::f32: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7174 | if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7175 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 7176 | Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7177 | bitcastToAPInt().getZExtValue(), MVT::i32); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7178 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7179 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7180 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 7181 | } |
| 7182 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7183 | case MVT::f64: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7184 | if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 7185 | !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7186 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 7187 | Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7188 | getZExtValue(), MVT::i64); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7189 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7190 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7191 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 7192 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 7193 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 7194 | if (!ST->isVolatile() && |
| 7195 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 7196 | // 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] | 7197 | // argument passing. Since this is so common, custom legalize the |
| 7198 | // 64-bit integer store into two 32-bit stores. |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 7199 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7200 | SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); |
| 7201 | SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 7202 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 7203 | |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 7204 | unsigned Alignment = ST->getAlignment(); |
| 7205 | bool isVolatile = ST->isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7206 | bool isNonTemporal = ST->isNonTemporal(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 7207 | |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7208 | SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7209 | Ptr, ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7210 | isVolatile, isNonTemporal, |
| 7211 | ST->getAlignment()); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7212 | Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr, |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 7213 | DAG.getConstant(4, Ptr.getValueType())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 7214 | Alignment = MinAlign(Alignment, 4U); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7215 | SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7216 | Ptr, ST->getPointerInfo().getWithOffset(4), |
| 7217 | isVolatile, isNonTemporal, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7218 | Alignment); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7219 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7220 | St0, St1); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 7221 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7222 | |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 7223 | break; |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 7224 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 7225 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7228 | // Try to infer better alignment information than the store already has. |
| 7229 | if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7230 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 7231 | if (Align > ST->getAlignment()) |
| 7232 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value, |
| 7233 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
| 7234 | ST->isVolatile(), ST->isNonTemporal(), Align); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7235 | } |
| 7236 | } |
| 7237 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 7238 | // Try transforming a pair floating point load / store ops to integer |
| 7239 | // load / store ops. |
| 7240 | SDValue NewST = TransformFPLoadStorePair(N); |
| 7241 | if (NewST.getNode()) |
| 7242 | return NewST; |
| 7243 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7244 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7245 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7246 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7247 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7248 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7249 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7250 | SDValue ReplStore; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7251 | |
| 7252 | // Replace the chain to avoid dependency. |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 7253 | if (ST->isTruncatingStore()) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7254 | ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 7255 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7256 | ST->getMemoryVT(), ST->isVolatile(), |
| 7257 | ST->isNonTemporal(), ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 7258 | } else { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7259 | ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 7260 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7261 | ST->isVolatile(), ST->isNonTemporal(), |
| 7262 | ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 7263 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7264 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7265 | // Create token to keep both nodes around. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7266 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7267 | MVT::Other, Chain, ReplStore); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7268 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7269 | // Make sure the new and old chains are cleaned up. |
| 7270 | AddToWorkList(Token.getNode()); |
| 7271 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 7272 | // Don't add users to work list. |
| 7273 | return CombineTo(N, Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7274 | } |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 7275 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7276 | |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 7277 | // Try transforming N to an indexed store. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 7278 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7279 | return SDValue(N, 0); |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 7280 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 7281 | // FIXME: is there such a thing as a truncating indexed store? |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7282 | if (ST->isTruncatingStore() && ST->isUnindexed() && |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 7283 | Value.getValueType().isInteger()) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 7284 | // See if we can simplify the input to this truncstore with knowledge that |
| 7285 | // only the low bits are being used. For example: |
| 7286 | // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7287 | SDValue Shorter = |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 7288 | GetDemandedBits(Value, |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 7289 | APInt::getLowBitsSet( |
| 7290 | Value.getValueType().getScalarType().getSizeInBits(), |
| 7291 | ST->getMemoryVT().getScalarType().getSizeInBits())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7292 | AddToWorkList(Value.getNode()); |
| 7293 | if (Shorter.getNode()) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7294 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 7295 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7296 | ST->isVolatile(), ST->isNonTemporal(), |
| 7297 | ST->getAlignment()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7298 | |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 7299 | // Otherwise, see if we can simplify the operation with |
| 7300 | // SimplifyDemandedBits, which only works if the value has a single use. |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 7301 | if (SimplifyDemandedBits(Value, |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 7302 | APInt::getLowBitsSet( |
| 7303 | Value.getValueType().getScalarType().getSizeInBits(), |
| 7304 | ST->getMemoryVT().getScalarType().getSizeInBits()))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7305 | return SDValue(N, 0); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 7306 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7307 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 7308 | // If this is a load followed by a store to the same location, then the store |
| 7309 | // is dead/noop. |
| 7310 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7311 | if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7312 | ST->isUnindexed() && !ST->isVolatile() && |
Chris Lattner | 07649d9 | 2008-01-08 23:08:06 +0000 | [diff] [blame] | 7313 | // There can't be any side effects between the load and store, such as |
| 7314 | // a call or store. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7315 | Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 7316 | // The store is dead, remove it. |
| 7317 | return Chain; |
| 7318 | } |
| 7319 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7320 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7321 | // If this is an FP_ROUND or TRUNC followed by a store, fold this into a |
| 7322 | // truncating store. We can do this even if this is already a truncstore. |
| 7323 | if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7324 | && Value.getNode()->hasOneUse() && ST->isUnindexed() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7325 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7326 | ST->getMemoryVT())) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7327 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 7328 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7329 | ST->isVolatile(), ST->isNonTemporal(), |
| 7330 | ST->getAlignment()); |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7331 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7332 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7333 | return ReduceLoadOpStoreWidth(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 7334 | } |
| 7335 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7336 | SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 7337 | SDValue InVec = N->getOperand(0); |
| 7338 | SDValue InVal = N->getOperand(1); |
| 7339 | SDValue EltNo = N->getOperand(2); |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 7340 | DebugLoc dl = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7341 | |
Bob Wilson | 492fd45 | 2010-05-19 23:42:58 +0000 | [diff] [blame] | 7342 | // If the inserted element is an UNDEF, just use the input vector. |
| 7343 | if (InVal.getOpcode() == ISD::UNDEF) |
| 7344 | return InVec; |
| 7345 | |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 7346 | EVT VT = InVec.getValueType(); |
| 7347 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 7348 | // If we can't generate a legal BUILD_VECTOR, exit |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 7349 | if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
| 7350 | return SDValue(); |
| 7351 | |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 7352 | // Check that we know which element is being inserted |
| 7353 | if (!isa<ConstantSDNode>(EltNo)) |
| 7354 | return SDValue(); |
| 7355 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7356 | |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 7357 | // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially |
| 7358 | // be converted to a BUILD_VECTOR). Fill in the Ops vector with the |
| 7359 | // vector elements. |
| 7360 | SmallVector<SDValue, 8> Ops; |
| 7361 | if (InVec.getOpcode() == ISD::BUILD_VECTOR) { |
| 7362 | Ops.append(InVec.getNode()->op_begin(), |
| 7363 | InVec.getNode()->op_end()); |
| 7364 | } else if (InVec.getOpcode() == ISD::UNDEF) { |
| 7365 | unsigned NElts = VT.getVectorNumElements(); |
| 7366 | Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); |
| 7367 | } else { |
| 7368 | return SDValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7369 | } |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 7370 | |
| 7371 | // Insert the element |
| 7372 | if (Elt < Ops.size()) { |
| 7373 | // All the operands of BUILD_VECTOR must have the same type; |
| 7374 | // we enforce that here. |
| 7375 | EVT OpVT = Ops[0].getValueType(); |
| 7376 | if (InVal.getValueType() != OpVT) |
| 7377 | InVal = OpVT.bitsGT(InVal.getValueType()) ? |
| 7378 | DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) : |
| 7379 | DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal); |
| 7380 | Ops[Elt] = InVal; |
| 7381 | } |
| 7382 | |
| 7383 | // Return the new vector |
| 7384 | return DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 7385 | VT, &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 7386 | } |
| 7387 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7388 | SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 7389 | // (vextract (scalar_to_vector val, 0) -> val |
| 7390 | SDValue InVec = N->getOperand(0); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 7391 | EVT VT = InVec.getValueType(); |
| 7392 | EVT NVT = N->getValueType(0); |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 7393 | |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 7394 | if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 7395 | // Check if the result type doesn't match the inserted element type. A |
| 7396 | // SCALAR_TO_VECTOR may truncate the inserted element and the |
| 7397 | // EXTRACT_VECTOR_ELT may widen the extracted vector. |
| 7398 | SDValue InOp = InVec.getOperand(0); |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 7399 | if (InOp.getValueType() != NVT) { |
| 7400 | assert(InOp.getValueType().isInteger() && NVT.isInteger()); |
| 7401 | return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT); |
| 7402 | } |
| 7403 | return InOp; |
| 7404 | } |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7405 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 7406 | SDValue EltNo = N->getOperand(1); |
| 7407 | bool ConstEltNo = isa<ConstantSDNode>(EltNo); |
| 7408 | |
| 7409 | // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT. |
| 7410 | // We only perform this optimization before the op legalization phase because |
| 7411 | // we may introduce new vector instructions which are not backed by TD patterns. |
| 7412 | // For example on AVX, extracting elements from a wide vector without using |
| 7413 | // extract_subvector. |
| 7414 | if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE |
| 7415 | && ConstEltNo && !LegalOperations) { |
| 7416 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| 7417 | int NumElem = VT.getVectorNumElements(); |
| 7418 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec); |
| 7419 | // Find the new index to extract from. |
| 7420 | int OrigElt = SVOp->getMaskElt(Elt); |
| 7421 | |
| 7422 | // Extracting an undef index is undef. |
| 7423 | if (OrigElt == -1) |
| 7424 | return DAG.getUNDEF(NVT); |
| 7425 | |
| 7426 | // Select the right vector half to extract from. |
| 7427 | if (OrigElt < NumElem) { |
| 7428 | InVec = InVec->getOperand(0); |
| 7429 | } else { |
| 7430 | InVec = InVec->getOperand(1); |
| 7431 | OrigElt -= NumElem; |
| 7432 | } |
| 7433 | |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 7434 | EVT IndexTy = N->getOperand(1).getValueType(); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 7435 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 7436 | InVec, DAG.getConstant(OrigElt, IndexTy)); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 7437 | } |
| 7438 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7439 | // Perform only after legalization to ensure build_vector / vector_shuffle |
| 7440 | // optimizations have already been done. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7441 | if (!LegalOperations) return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7442 | |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 7443 | // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) |
| 7444 | // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) |
| 7445 | // (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] | 7446 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 7447 | if (ConstEltNo) { |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 7448 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7449 | bool NewLoad = false; |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 7450 | bool BCNumEltsChanged = false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7451 | EVT ExtVT = VT.getVectorElementType(); |
| 7452 | EVT LVT = ExtVT; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7453 | |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 7454 | // If the result of load has to be truncated, then it's not necessarily |
| 7455 | // profitable. |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 7456 | if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT)) |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 7457 | return SDValue(); |
| 7458 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7459 | if (InVec.getOpcode() == ISD::BITCAST) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7460 | // Don't duplicate a load with other uses. |
| 7461 | if (!InVec.hasOneUse()) |
| 7462 | return SDValue(); |
| 7463 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7464 | EVT BCVT = InVec.getOperand(0).getValueType(); |
| 7465 | if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7466 | return SDValue(); |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 7467 | if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) |
| 7468 | BCNumEltsChanged = true; |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7469 | InVec = InVec.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7470 | ExtVT = BCVT.getVectorElementType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7471 | NewLoad = true; |
| 7472 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7473 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7474 | LoadSDNode *LN0 = NULL; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7475 | const ShuffleVectorSDNode *SVN = NULL; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7476 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7477 | LN0 = cast<LoadSDNode>(InVec); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7478 | } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7479 | InVec.getOperand(0).getValueType() == ExtVT && |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7480 | ISD::isNormalLoad(InVec.getOperand(0).getNode())) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7481 | // Don't duplicate a load with other uses. |
| 7482 | if (!InVec.hasOneUse()) |
| 7483 | return SDValue(); |
| 7484 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7485 | LN0 = cast<LoadSDNode>(InVec.getOperand(0)); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7486 | } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7487 | // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) |
| 7488 | // => |
| 7489 | // (load $addr+1*size) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7490 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7491 | // Don't duplicate a load with other uses. |
| 7492 | if (!InVec.hasOneUse()) |
| 7493 | return SDValue(); |
| 7494 | |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 7495 | // If the bit convert changed the number of elements, it is unsafe |
| 7496 | // to examine the mask. |
| 7497 | if (BCNumEltsChanged) |
| 7498 | return SDValue(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7499 | |
| 7500 | // Select the input vector, guarding against out of range extract vector. |
| 7501 | unsigned NumElems = VT.getVectorNumElements(); |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 7502 | int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7503 | InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); |
| 7504 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7505 | if (InVec.getOpcode() == ISD::BITCAST) { |
| 7506 | // Don't duplicate a load with other uses. |
| 7507 | if (!InVec.hasOneUse()) |
| 7508 | return SDValue(); |
| 7509 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7510 | InVec = InVec.getOperand(0); |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7511 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7512 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7513 | LN0 = cast<LoadSDNode>(InVec); |
Ted Kremenek | d0e88f3 | 2010-04-08 18:49:30 +0000 | [diff] [blame] | 7514 | Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7515 | } |
| 7516 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7517 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7518 | // Make sure we found a non-volatile load and the extractelement is |
| 7519 | // the only use. |
Nadav Rotem | 42febc6 | 2011-05-11 14:40:50 +0000 | [diff] [blame] | 7520 | if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7521 | return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7522 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 7523 | // If Idx was -1 above, Elt is going to be -1, so just return undef. |
| 7524 | if (Elt == -1) |
Eli Friedman | ed4b427 | 2011-07-25 22:25:42 +0000 | [diff] [blame] | 7525 | return DAG.getUNDEF(LVT); |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 7526 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7527 | unsigned Align = LN0->getAlignment(); |
| 7528 | if (NewLoad) { |
| 7529 | // Check the resultant load doesn't need a higher alignment than the |
| 7530 | // original load. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7531 | unsigned NewAlign = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 7532 | TLI.getTargetData() |
| 7533 | ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7534 | |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 7535 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7536 | return SDValue(); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7537 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7538 | Align = NewAlign; |
| 7539 | } |
| 7540 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7541 | SDValue NewPtr = LN0->getBasePtr(); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7542 | unsigned PtrOff = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7543 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 7544 | if (Elt) { |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7545 | PtrOff = LVT.getSizeInBits() * Elt / 8; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7546 | EVT PtrType = NewPtr.getValueType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7547 | if (TLI.isBigEndian()) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7548 | PtrOff = VT.getSizeInBits() / 8 - PtrOff; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7549 | NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr, |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 7550 | DAG.getConstant(PtrOff, PtrType)); |
| 7551 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7552 | |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 7553 | // The replacement we need to do here is a little tricky: we need to |
| 7554 | // replace an extractelement of a load with a load. |
| 7555 | // Use ReplaceAllUsesOfValuesWith to do the replacement. |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 7556 | // Note that this replacement assumes that the extractvalue is the only |
| 7557 | // use of the load; that's okay because we don't want to perform this |
| 7558 | // transformation in other cases anyway. |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 7559 | SDValue Load; |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 7560 | SDValue Chain; |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 7561 | if (NVT.bitsGT(LVT)) { |
| 7562 | // If the result type of vextract is wider than the load, then issue an |
| 7563 | // extending load instead. |
| 7564 | ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT) |
| 7565 | ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| 7566 | Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(), |
| 7567 | NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff), |
| 7568 | LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 7569 | Chain = Load.getValue(1); |
| 7570 | } else { |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 7571 | Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr, |
| 7572 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 7573 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 7574 | LN0->isInvariant(), Align); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 7575 | Chain = Load.getValue(1); |
| 7576 | if (NVT.bitsLT(LVT)) |
| 7577 | Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load); |
| 7578 | else |
| 7579 | Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load); |
| 7580 | } |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 7581 | WorkListRemover DeadNodes(*this); |
| 7582 | SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) }; |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 7583 | SDValue To[] = { Load, Chain }; |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7584 | DAG.ReplaceAllUsesOfValuesWith(From, To, 2); |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 7585 | // Since we're explcitly calling ReplaceAllUses, add the new node to the |
| 7586 | // worklist explicitly as well. |
| 7587 | AddToWorkList(Load.getNode()); |
Craig Topper | 0c9da21 | 2012-03-20 05:28:39 +0000 | [diff] [blame] | 7588 | AddUsersToWorkList(Load.getNode()); // Add users too |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 7589 | // Make sure to revisit this node to clean it up; it will usually be dead. |
| 7590 | AddToWorkList(N); |
| 7591 | return SDValue(N, 0); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7592 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 7593 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7594 | return SDValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7595 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 7596 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7597 | SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7598 | unsigned NumInScalars = N->getNumOperands(); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7599 | DebugLoc dl = N->getDebugLoc(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7600 | EVT VT = N->getValueType(0); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7601 | // Check to see if this is a BUILD_VECTOR of a bunch of values |
| 7602 | // which come from any_extend or zero_extend nodes. If so, we can create |
| 7603 | // 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] | 7604 | // optimizations. We do not handle sign-extend because we can't fill the sign |
| 7605 | // using shuffles. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7606 | EVT SourceType = MVT::Other; |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 7607 | bool AllAnyExt = true; |
| 7608 | bool AllUndef = true; |
| 7609 | for (unsigned i = 0; i != NumInScalars; ++i) { |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7610 | SDValue In = N->getOperand(i); |
| 7611 | // Ignore undef inputs. |
| 7612 | if (In.getOpcode() == ISD::UNDEF) continue; |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 7613 | AllUndef = false; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7614 | |
| 7615 | bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND; |
| 7616 | bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND; |
| 7617 | |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7618 | // Abort if the element is not an extension. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7619 | if (!ZeroExt && !AnyExt) { |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7620 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7621 | break; |
| 7622 | } |
| 7623 | |
| 7624 | // The input is a ZeroExt or AnyExt. Check the original type. |
| 7625 | EVT InTy = In.getOperand(0).getValueType(); |
| 7626 | |
| 7627 | // Check that all of the widened source types are the same. |
| 7628 | if (SourceType == MVT::Other) |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7629 | // First time. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7630 | SourceType = InTy; |
| 7631 | else if (InTy != SourceType) { |
| 7632 | // Multiple income types. Abort. |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7633 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7634 | break; |
| 7635 | } |
| 7636 | |
| 7637 | // Check if all of the extends are ANY_EXTENDs. |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 7638 | AllAnyExt &= AnyExt; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 7641 | if (AllUndef) |
| 7642 | return DAG.getUNDEF(VT); |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7643 | |
| 7644 | // In order to have valid types, all of the inputs must be extended from the |
| 7645 | // same source type and all of the inputs must be any or zero extend. |
| 7646 | // Scalar sizes must be a power of two. |
| 7647 | EVT OutScalarTy = N->getValueType(0).getScalarType(); |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7648 | bool ValidTypes = SourceType != MVT::Other && |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7649 | isPowerOf2_32(OutScalarTy.getSizeInBits()) && |
| 7650 | isPowerOf2_32(SourceType.getSizeInBits()); |
| 7651 | |
| 7652 | // We perform this optimization post type-legalization because |
| 7653 | // the type-legalizer often scalarizes integer-promoted vectors. |
| 7654 | // Performing this optimization before may create bit-casts which |
| 7655 | // will be type-legalized to complex code sequences. |
| 7656 | // We perform this optimization only before the operation legalizer because we |
| 7657 | // may introduce illegal operations. |
Nadav Rotem | 6431ff9 | 2012-03-15 08:49:06 +0000 | [diff] [blame] | 7658 | // Create a new simpler BUILD_VECTOR sequence which other optimizations can |
| 7659 | // turn into a single shuffle instruction. |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7660 | if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) && |
| 7661 | ValidTypes) { |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7662 | bool isLE = TLI.isLittleEndian(); |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7663 | unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits(); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7664 | assert(ElemRatio > 1 && "Invalid element size ratio"); |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 7665 | SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType): |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7666 | DAG.getConstant(0, SourceType); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7667 | |
| 7668 | unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements(); |
Benjamin Kramer | 50bf86e | 2011-10-30 08:39:55 +0000 | [diff] [blame] | 7669 | SmallVector<SDValue, 8> Ops(NewBVElems, Filler); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7670 | |
| 7671 | // Populate the new build_vector |
| 7672 | for (unsigned i=0; i < N->getNumOperands(); ++i) { |
| 7673 | SDValue Cast = N->getOperand(i); |
Benjamin Kramer | 50bf86e | 2011-10-30 08:39:55 +0000 | [diff] [blame] | 7674 | assert((Cast.getOpcode() == ISD::ANY_EXTEND || |
| 7675 | Cast.getOpcode() == ISD::ZERO_EXTEND || |
| 7676 | Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode"); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7677 | SDValue In; |
| 7678 | if (Cast.getOpcode() == ISD::UNDEF) |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7679 | In = DAG.getUNDEF(SourceType); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7680 | else |
| 7681 | In = Cast->getOperand(0); |
| 7682 | unsigned Index = isLE ? (i * ElemRatio) : |
| 7683 | (i * ElemRatio + (ElemRatio - 1)); |
| 7684 | |
| 7685 | assert(Index < Ops.size() && "Invalid index"); |
| 7686 | Ops[Index] = In; |
| 7687 | } |
| 7688 | |
| 7689 | // The type of the new BUILD_VECTOR node. |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7690 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7691 | assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() && |
| 7692 | "Invalid vector size"); |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 7693 | // Check if the new vector type is legal. |
| 7694 | if (!isTypeLegal(VecVT)) return SDValue(); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7695 | |
| 7696 | // Make the new BUILD_VECTOR. |
| 7697 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 7698 | VecVT, &Ops[0], Ops.size()); |
| 7699 | |
Nadav Rotem | 6431ff9 | 2012-03-15 08:49:06 +0000 | [diff] [blame] | 7700 | // The new BUILD_VECTOR node has the potential to be further optimized. |
| 7701 | AddToWorkList(BV.getNode()); |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 7702 | // Bitcast to the desired type. |
| 7703 | return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV); |
| 7704 | } |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 7705 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7706 | // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT |
| 7707 | // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from |
| 7708 | // at most two distinct vectors, turn this into a shuffle node. |
Duncan Sands | 00294ca | 2012-03-19 15:35:44 +0000 | [diff] [blame] | 7709 | |
| 7710 | // May only combine to shuffle after legalize if shuffle is legal. |
| 7711 | if (LegalOperations && |
| 7712 | !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT)) |
| 7713 | return SDValue(); |
| 7714 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7715 | SDValue VecIn1, VecIn2; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7716 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 7717 | // Ignore undef inputs. |
| 7718 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7719 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7720 | // 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] | 7721 | // constant index, bail out. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7722 | if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7723 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7724 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7725 | break; |
| 7726 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7727 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7728 | // We allow up to two distinct input vectors. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7729 | SDValue ExtractedFromVec = N->getOperand(i).getOperand(0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7730 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 7731 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7732 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7733 | if (VecIn1.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7734 | VecIn1 = ExtractedFromVec; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7735 | } else if (VecIn2.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7736 | VecIn2 = ExtractedFromVec; |
| 7737 | } else { |
| 7738 | // Too many inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7739 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7740 | break; |
| 7741 | } |
| 7742 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7743 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7744 | // If everything is good, we can make a shuffle operation. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7745 | if (VecIn1.getNode()) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7746 | SmallVector<int, 8> Mask; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7747 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 7748 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7749 | Mask.push_back(-1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7750 | continue; |
| 7751 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7752 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 7753 | // If extracting from the first vector, just use the index directly. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7754 | SDValue Extract = N->getOperand(i); |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 7755 | SDValue ExtVal = Extract.getOperand(1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7756 | if (Extract.getOperand(0) == VecIn1) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7757 | unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
| 7758 | if (ExtIndex > VT.getVectorNumElements()) |
| 7759 | return SDValue(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7760 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7761 | Mask.push_back(ExtIndex); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7762 | continue; |
| 7763 | } |
| 7764 | |
| 7765 | // Otherwise, use InIdx + VecSize |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 7766 | unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7767 | Mask.push_back(Idx+NumInScalars); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7768 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7769 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7770 | // We can't generate a shuffle node with mismatched input and output types. |
| 7771 | // Attempt to transform a single input vector to the correct type. |
| 7772 | if ((VT != VecIn1.getValueType())) { |
| 7773 | // We don't support shuffeling between TWO values of different types. |
| 7774 | if (VecIn2.getNode() != 0) |
| 7775 | return SDValue(); |
| 7776 | |
| 7777 | // We only support widening of vectors which are half the size of the |
| 7778 | // output registers. For example XMM->YMM widening on X86 with AVX. |
| 7779 | if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits()) |
| 7780 | return SDValue(); |
| 7781 | |
| 7782 | // Widen the input vector by adding undef values. |
| 7783 | VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, |
| 7784 | VecIn1, DAG.getUNDEF(VecIn1.getValueType())); |
| 7785 | } |
| 7786 | |
| 7787 | // If VecIn2 is unused then change it to undef. |
| 7788 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); |
| 7789 | |
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 7790 | // Check that we were able to transform all incoming values to the same type. |
| 7791 | if (VecIn2.getValueType() != VecIn1.getValueType() || |
| 7792 | VecIn1.getValueType() != VT) |
| 7793 | return SDValue(); |
| 7794 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7795 | // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes. |
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 7796 | if (!isTypeLegal(VT)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7797 | return SDValue(); |
| 7798 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7799 | // Return the new VECTOR_SHUFFLE node. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7800 | SDValue Ops[2]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 7801 | Ops[0] = VecIn1; |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 7802 | Ops[1] = VecIn2; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7803 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7804 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7805 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7806 | return SDValue(); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 7807 | } |
| 7808 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7809 | SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7810 | // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of |
| 7811 | // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector |
| 7812 | // inputs come from at most two distinct vectors, turn this into a shuffle |
| 7813 | // node. |
| 7814 | |
| 7815 | // 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] | 7816 | if (N->getNumOperands() == 1) |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7817 | return N->getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7818 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7819 | return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7820 | } |
| 7821 | |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 7822 | SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) { |
| 7823 | EVT NVT = N->getValueType(0); |
| 7824 | SDValue V = N->getOperand(0); |
| 7825 | |
| 7826 | if (V->getOpcode() == ISD::INSERT_SUBVECTOR) { |
| 7827 | // Handle only simple case where vector being inserted and vector |
| 7828 | // being extracted are of same type, and are half size of larger vectors. |
| 7829 | EVT BigVT = V->getOperand(0).getValueType(); |
| 7830 | EVT SmallVT = V->getOperand(1).getValueType(); |
| 7831 | if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits()) |
| 7832 | return SDValue(); |
| 7833 | |
Eli Friedman | 2632344 | 2011-12-07 00:11:56 +0000 | [diff] [blame] | 7834 | // Only handle cases where both indexes are constants with the same type. |
| 7835 | ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 7836 | ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2)); |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 7837 | |
Eli Friedman | 2632344 | 2011-12-07 00:11:56 +0000 | [diff] [blame] | 7838 | if (InsIdx && ExtIdx && |
| 7839 | InsIdx->getValueType(0).getSizeInBits() <= 64 && |
| 7840 | ExtIdx->getValueType(0).getSizeInBits() <= 64) { |
| 7841 | // Combine: |
| 7842 | // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx) |
| 7843 | // Into: |
| 7844 | // indices are equal => V1 |
| 7845 | // otherwise => (extract_subvec V1, ExtIdx) |
| 7846 | if (InsIdx->getZExtValue() == ExtIdx->getZExtValue()) |
| 7847 | return V->getOperand(1); |
| 7848 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT, |
| 7849 | V->getOperand(0), N->getOperand(1)); |
| 7850 | } |
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 7851 | } |
| 7852 | |
| 7853 | return SDValue(); |
| 7854 | } |
| 7855 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7856 | SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7857 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7858 | unsigned NumElts = VT.getVectorNumElements(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 7859 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 7860 | SDValue N0 = N->getOperand(0); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 7861 | SDValue N1 = N->getOperand(1); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 7862 | |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 7863 | assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG"); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 7864 | |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 7865 | // Canonicalize shuffle undef, undef -> undef |
| 7866 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 7867 | return DAG.getUNDEF(VT); |
| 7868 | |
| 7869 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 7870 | |
| 7871 | // Canonicalize shuffle v, v -> v, undef |
| 7872 | if (N0 == N1) { |
| 7873 | SmallVector<int, 8> NewMask; |
| 7874 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7875 | int Idx = SVN->getMaskElt(i); |
| 7876 | if (Idx >= (int)NumElts) Idx -= NumElts; |
| 7877 | NewMask.push_back(Idx); |
| 7878 | } |
| 7879 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT), |
| 7880 | &NewMask[0]); |
| 7881 | } |
| 7882 | |
| 7883 | // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. |
| 7884 | if (N0.getOpcode() == ISD::UNDEF) { |
| 7885 | SmallVector<int, 8> NewMask; |
| 7886 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7887 | int Idx = SVN->getMaskElt(i); |
Craig Topper | 4b206bd | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 7888 | if (Idx >= 0) { |
| 7889 | if (Idx < (int)NumElts) |
| 7890 | Idx += NumElts; |
| 7891 | else |
| 7892 | Idx -= NumElts; |
| 7893 | } |
| 7894 | NewMask.push_back(Idx); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 7895 | } |
| 7896 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT), |
| 7897 | &NewMask[0]); |
| 7898 | } |
| 7899 | |
| 7900 | // Remove references to rhs if it is undef |
| 7901 | if (N1.getOpcode() == ISD::UNDEF) { |
| 7902 | bool Changed = false; |
| 7903 | SmallVector<int, 8> NewMask; |
| 7904 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7905 | int Idx = SVN->getMaskElt(i); |
| 7906 | if (Idx >= (int)NumElts) { |
| 7907 | Idx = -1; |
| 7908 | Changed = true; |
| 7909 | } |
| 7910 | NewMask.push_back(Idx); |
| 7911 | } |
| 7912 | if (Changed) |
| 7913 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]); |
| 7914 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 7915 | |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 7916 | // If it is a splat, check if the argument vector is another splat or a |
| 7917 | // build_vector with all scalar elements the same. |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 7918 | if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7919 | SDNode *V = N0.getNode(); |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 7920 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7921 | // 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] | 7922 | // not the number of vector elements, look through it. Be careful not to |
| 7923 | // look though conversions that change things like v4f32 to v2f64. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7924 | if (V->getOpcode() == ISD::BITCAST) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7925 | SDValue ConvInput = V->getOperand(0); |
Evan Cheng | 2925786 | 2008-07-22 20:42:56 +0000 | [diff] [blame] | 7926 | if (ConvInput.getValueType().isVector() && |
| 7927 | ConvInput.getValueType().getVectorNumElements() == NumElts) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7928 | V = ConvInput.getNode(); |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7931 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 7932 | assert(V->getNumOperands() == NumElts && |
| 7933 | "BUILD_VECTOR has wrong number of operands"); |
| 7934 | SDValue Base; |
| 7935 | bool AllSame = true; |
| 7936 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7937 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 7938 | Base = V->getOperand(i); |
| 7939 | break; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 7940 | } |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 7941 | } |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 7942 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 7943 | if (!Base.getNode()) |
| 7944 | return N0; |
| 7945 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7946 | if (V->getOperand(i) != Base) { |
| 7947 | AllSame = false; |
| 7948 | break; |
| 7949 | } |
| 7950 | } |
| 7951 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 7952 | if (AllSame) |
| 7953 | return N0; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 7954 | } |
| 7955 | } |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7956 | |
| 7957 | // If this shuffle node is simply a swizzle of another shuffle node, |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 7958 | // and it reverses the swizzle of the previous shuffle then we can |
| 7959 | // optimize shuffle(shuffle(x, undef), undef) -> x. |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7960 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 7961 | N1.getOpcode() == ISD::UNDEF) { |
| 7962 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7963 | ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0); |
| 7964 | |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 7965 | // Shuffle nodes can only reverse shuffles with a single non-undef value. |
| 7966 | if (N0.getOperand(1).getOpcode() != ISD::UNDEF) |
| 7967 | return SDValue(); |
| 7968 | |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 7969 | // The incoming shuffle must be of the same type as the result of the |
| 7970 | // current shuffle. |
| 7971 | assert(OtherSV->getOperand(0).getValueType() == VT && |
| 7972 | "Shuffle types don't match"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7973 | |
| 7974 | for (unsigned i = 0; i != NumElts; ++i) { |
| 7975 | int Idx = SVN->getMaskElt(i); |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 7976 | assert(Idx < (int)NumElts && "Index references undef operand"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7977 | // Next, this index comes from the first value, which is the incoming |
| 7978 | // shuffle. Adopt the incoming index. |
| 7979 | if (Idx >= 0) |
| 7980 | Idx = OtherSV->getMaskElt(Idx); |
| 7981 | |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 7982 | // The combined shuffle must map each index to itself. |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 7983 | if (Idx >= 0 && (unsigned)Idx != i) |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 7984 | return SDValue(); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7985 | } |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 7986 | |
| 7987 | return OtherSV->getOperand(0); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 7988 | } |
| 7989 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7990 | return SDValue(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 7993 | SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) { |
| 7994 | if (!TLI.getShouldFoldAtomicFences()) |
| 7995 | return SDValue(); |
| 7996 | |
| 7997 | SDValue atomic = N->getOperand(0); |
| 7998 | switch (atomic.getOpcode()) { |
| 7999 | case ISD::ATOMIC_CMP_SWAP: |
| 8000 | case ISD::ATOMIC_SWAP: |
| 8001 | case ISD::ATOMIC_LOAD_ADD: |
| 8002 | case ISD::ATOMIC_LOAD_SUB: |
| 8003 | case ISD::ATOMIC_LOAD_AND: |
| 8004 | case ISD::ATOMIC_LOAD_OR: |
| 8005 | case ISD::ATOMIC_LOAD_XOR: |
| 8006 | case ISD::ATOMIC_LOAD_NAND: |
| 8007 | case ISD::ATOMIC_LOAD_MIN: |
| 8008 | case ISD::ATOMIC_LOAD_MAX: |
| 8009 | case ISD::ATOMIC_LOAD_UMIN: |
| 8010 | case ISD::ATOMIC_LOAD_UMAX: |
| 8011 | break; |
| 8012 | default: |
| 8013 | return SDValue(); |
| 8014 | } |
| 8015 | |
| 8016 | SDValue fence = atomic.getOperand(0); |
| 8017 | if (fence.getOpcode() != ISD::MEMBARRIER) |
| 8018 | return SDValue(); |
| 8019 | |
| 8020 | switch (atomic.getOpcode()) { |
| 8021 | case ISD::ATOMIC_CMP_SWAP: |
| 8022 | return SDValue(DAG.UpdateNodeOperands(atomic.getNode(), |
| 8023 | fence.getOperand(0), |
| 8024 | atomic.getOperand(1), atomic.getOperand(2), |
| 8025 | atomic.getOperand(3)), atomic.getResNo()); |
| 8026 | case ISD::ATOMIC_SWAP: |
| 8027 | case ISD::ATOMIC_LOAD_ADD: |
| 8028 | case ISD::ATOMIC_LOAD_SUB: |
| 8029 | case ISD::ATOMIC_LOAD_AND: |
| 8030 | case ISD::ATOMIC_LOAD_OR: |
| 8031 | case ISD::ATOMIC_LOAD_XOR: |
| 8032 | case ISD::ATOMIC_LOAD_NAND: |
| 8033 | case ISD::ATOMIC_LOAD_MIN: |
| 8034 | case ISD::ATOMIC_LOAD_MAX: |
| 8035 | case ISD::ATOMIC_LOAD_UMIN: |
| 8036 | case ISD::ATOMIC_LOAD_UMAX: |
| 8037 | return SDValue(DAG.UpdateNodeOperands(atomic.getNode(), |
| 8038 | fence.getOperand(0), |
| 8039 | atomic.getOperand(1), atomic.getOperand(2)), |
| 8040 | atomic.getResNo()); |
| 8041 | default: |
| 8042 | return SDValue(); |
| 8043 | } |
| 8044 | } |
| 8045 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8046 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8047 | /// an AND to a vector_shuffle with the destination vector and a zero vector. |
| 8048 | /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8049 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8050 | SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8051 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8052 | DebugLoc dl = N->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8053 | SDValue LHS = N->getOperand(0); |
| 8054 | SDValue RHS = N->getOperand(1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8055 | if (N->getOpcode() == ISD::AND) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8056 | if (RHS.getOpcode() == ISD::BITCAST) |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8057 | RHS = RHS.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8058 | if (RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8059 | SmallVector<int, 8> Indices; |
| 8060 | unsigned NumElts = RHS.getNumOperands(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8061 | for (unsigned i = 0; i != NumElts; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8062 | SDValue Elt = RHS.getOperand(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8063 | if (!isa<ConstantSDNode>(Elt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8064 | return SDValue(); |
Craig Topper | b7135e5 | 2012-04-09 05:59:53 +0000 | [diff] [blame] | 8065 | |
| 8066 | if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8067 | Indices.push_back(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8068 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8069 | Indices.push_back(NumElts); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8070 | else |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8071 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8072 | } |
| 8073 | |
| 8074 | // Let's see if the target supports this vector_shuffle. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8075 | EVT RVT = RHS.getValueType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8076 | if (!TLI.isVectorClearMaskLegal(Indices, RVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8077 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8078 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8079 | // Return the new VECTOR_SHUFFLE node. |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 8080 | EVT EltVT = RVT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8081 | SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(), |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 8082 | DAG.getConstant(0, EltVT)); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8083 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 8084 | RVT, &ZeroOps[0], ZeroOps.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8085 | LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8086 | SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8087 | return DAG.getNode(ISD::BITCAST, dl, VT, Shuf); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8088 | } |
| 8089 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8090 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8091 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8094 | /// SimplifyVBinOp - Visit a binary vector operation, like ADD. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8095 | SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8096 | // After legalize, the target may be depending on adds and other |
| 8097 | // binary ops to provide legal ways to construct constants or other |
| 8098 | // things. Simplifying them may result in a loss of legality. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8099 | if (LegalOperations) return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8100 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 8101 | assert(N->getValueType(0).isVector() && |
| 8102 | "SimplifyVBinOp only works on vectors!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8103 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8104 | SDValue LHS = N->getOperand(0); |
| 8105 | SDValue RHS = N->getOperand(1); |
| 8106 | SDValue Shuffle = XformToShuffleWithZero(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8107 | if (Shuffle.getNode()) return Shuffle; |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 8108 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8109 | // 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] | 8110 | // this operation. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8111 | if (LHS.getOpcode() == ISD::BUILD_VECTOR && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8112 | RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8113 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8114 | for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8115 | SDValue LHSOp = LHS.getOperand(i); |
| 8116 | SDValue RHSOp = RHS.getOperand(i); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 8117 | // If these two elements can't be folded, bail out. |
| 8118 | if ((LHSOp.getOpcode() != ISD::UNDEF && |
| 8119 | LHSOp.getOpcode() != ISD::Constant && |
| 8120 | LHSOp.getOpcode() != ISD::ConstantFP) || |
| 8121 | (RHSOp.getOpcode() != ISD::UNDEF && |
| 8122 | RHSOp.getOpcode() != ISD::Constant && |
| 8123 | RHSOp.getOpcode() != ISD::ConstantFP)) |
| 8124 | break; |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8125 | |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 8126 | // Can't fold divide by zero. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 8127 | if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV || |
| 8128 | N->getOpcode() == ISD::FDIV) { |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 8129 | if ((RHSOp.getOpcode() == ISD::Constant && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8130 | cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) || |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 8131 | (RHSOp.getOpcode() == ISD::ConstantFP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8132 | cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero())) |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 8133 | break; |
| 8134 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8135 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 8136 | EVT VT = LHSOp.getValueType(); |
Bob Wilson | db2b18f | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 8137 | EVT RVT = RHSOp.getValueType(); |
| 8138 | if (RVT != VT) { |
| 8139 | // Integer BUILD_VECTOR operands may have types larger than the element |
| 8140 | // size (e.g., when the element type is not legal). Prior to type |
| 8141 | // legalization, the types may not match between the two BUILD_VECTORS. |
| 8142 | // Truncate one of the operands to make them match. |
| 8143 | if (RVT.getSizeInBits() > VT.getSizeInBits()) { |
| 8144 | RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp); |
| 8145 | } else { |
| 8146 | LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp); |
| 8147 | VT = RVT; |
| 8148 | } |
| 8149 | } |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 8150 | SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT, |
Evan Cheng | a083988 | 2010-05-18 00:03:40 +0000 | [diff] [blame] | 8151 | LHSOp, RHSOp); |
| 8152 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 8153 | FoldOp.getOpcode() != ISD::Constant && |
| 8154 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 8155 | break; |
| 8156 | Ops.push_back(FoldOp); |
| 8157 | AddToWorkList(FoldOp.getNode()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 8158 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8159 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 8160 | if (Ops.size() == LHS.getNumOperands()) |
| 8161 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 8162 | LHS.getValueType(), &Ops[0], Ops.size()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 8163 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8164 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8165 | return SDValue(); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 8166 | } |
| 8167 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8168 | SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0, |
| 8169 | SDValue N1, SDValue N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8170 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8171 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8172 | SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8173 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8174 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8175 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 8176 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 8177 | // the SELECT node, since we were called with a SELECT node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8178 | if (SCC.getNode()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8179 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 8180 | // Otherwise, just return whatever node we got back, like fabs. |
| 8181 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8182 | SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(), |
| 8183 | N0.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8184 | SCC.getOperand(0), SCC.getOperand(1), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8185 | SCC.getOperand(4)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8186 | AddToWorkList(SETCC.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8187 | return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(), |
| 8188 | SCC.getOperand(2), SCC.getOperand(3), SETCC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8189 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8190 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8191 | return SCC; |
| 8192 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8193 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 8194 | } |
| 8195 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 8196 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 8197 | /// 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] | 8198 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 8199 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 8200 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 8201 | /// looked at. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8202 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8203 | SDValue RHS) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8204 | |
Nadav Rotem | f94fdb6 | 2011-02-11 19:57:47 +0000 | [diff] [blame] | 8205 | // Cannot simplify select with vector condition |
| 8206 | if (TheSelect->getOperand(0).getValueType().isVector()) return false; |
| 8207 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 8208 | // If this is a select from two identical things, try to pull the operation |
| 8209 | // through the select. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8210 | if (LHS.getOpcode() != RHS.getOpcode() || |
| 8211 | !LHS.hasOneUse() || !RHS.hasOneUse()) |
| 8212 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8213 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8214 | // If this is a load and the token chain is identical, replace the select |
| 8215 | // of two loads with a load through a select of the address to load from. |
| 8216 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 8217 | // constants have been dropped into the constant pool. |
| 8218 | if (LHS.getOpcode() == ISD::LOAD) { |
| 8219 | LoadSDNode *LLD = cast<LoadSDNode>(LHS); |
| 8220 | LoadSDNode *RLD = cast<LoadSDNode>(RHS); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8221 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8222 | // Token chains must be identical. |
| 8223 | if (LHS.getOperand(0) != RHS.getOperand(0) || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 8224 | // Do not let this transformation reduce the number of volatile loads. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8225 | LLD->isVolatile() || RLD->isVolatile() || |
| 8226 | // If this is an EXTLOAD, the VT's must match. |
| 8227 | LLD->getMemoryVT() != RLD->getMemoryVT() || |
Duncan Sands | dcfd3a7 | 2010-11-18 20:05:18 +0000 | [diff] [blame] | 8228 | // If this is an EXTLOAD, the kind of extension must match. |
| 8229 | (LLD->getExtensionType() != RLD->getExtensionType() && |
| 8230 | // The only exception is if one of the extensions is anyext. |
| 8231 | LLD->getExtensionType() != ISD::EXTLOAD && |
| 8232 | RLD->getExtensionType() != ISD::EXTLOAD) || |
Dan Gohman | 75832d7 | 2009-10-31 14:14:04 +0000 | [diff] [blame] | 8233 | // FIXME: this discards src value information. This is |
| 8234 | // over-conservative. It would be beneficial to be able to remember |
Mon P Wang | fe240b1 | 2010-01-11 20:12:49 +0000 | [diff] [blame] | 8235 | // both potential memory locations. Since we are discarding |
| 8236 | // src value info, don't do the transformation if the memory |
| 8237 | // locations are not in the default address space. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8238 | LLD->getPointerInfo().getAddrSpace() != 0 || |
| 8239 | RLD->getPointerInfo().getAddrSpace() != 0) |
| 8240 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8241 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8242 | // Check that the select condition doesn't reach either load. If so, |
| 8243 | // folding this will induce a cycle into the DAG. If not, this is safe to |
| 8244 | // xform, so create a select of the addresses. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8245 | SDValue Addr; |
| 8246 | if (TheSelect->getOpcode() == ISD::SELECT) { |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8247 | SDNode *CondNode = TheSelect->getOperand(0).getNode(); |
| 8248 | if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || |
| 8249 | (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) |
| 8250 | return false; |
| 8251 | Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(), |
| 8252 | LLD->getBasePtr().getValueType(), |
| 8253 | TheSelect->getOperand(0), LLD->getBasePtr(), |
| 8254 | RLD->getBasePtr()); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8255 | } else { // Otherwise SELECT_CC |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8256 | SDNode *CondLHS = TheSelect->getOperand(0).getNode(); |
| 8257 | SDNode *CondRHS = TheSelect->getOperand(1).getNode(); |
| 8258 | |
| 8259 | if ((LLD->hasAnyUseOfValue(1) && |
| 8260 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || |
Chris Lattner | 77d9521 | 2012-03-27 16:27:21 +0000 | [diff] [blame] | 8261 | (RLD->hasAnyUseOfValue(1) && |
| 8262 | (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS)))) |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8263 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8264 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8265 | Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(), |
| 8266 | LLD->getBasePtr().getValueType(), |
| 8267 | TheSelect->getOperand(0), |
| 8268 | TheSelect->getOperand(1), |
| 8269 | LLD->getBasePtr(), RLD->getBasePtr(), |
| 8270 | TheSelect->getOperand(4)); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8273 | SDValue Load; |
| 8274 | if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { |
| 8275 | Load = DAG.getLoad(TheSelect->getValueType(0), |
| 8276 | TheSelect->getDebugLoc(), |
| 8277 | // FIXME: Discards pointer info. |
| 8278 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 8279 | LLD->isVolatile(), LLD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8280 | LLD->isInvariant(), LLD->getAlignment()); |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8281 | } else { |
Duncan Sands | b9064bb | 2010-11-18 21:16:28 +0000 | [diff] [blame] | 8282 | Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ? |
| 8283 | RLD->getExtensionType() : LLD->getExtensionType(), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8284 | TheSelect->getDebugLoc(), |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 8285 | TheSelect->getValueType(0), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8286 | // FIXME: Discards pointer info. |
| 8287 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 8288 | LLD->getMemoryVT(), LLD->isVolatile(), |
| 8289 | LLD->isNonTemporal(), LLD->getAlignment()); |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 8290 | } |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 8291 | |
| 8292 | // Users of the select now use the result of the load. |
| 8293 | CombineTo(TheSelect, Load); |
| 8294 | |
| 8295 | // Users of the old loads now use the new load's chain. We know the |
| 8296 | // old-load value is dead now. |
| 8297 | CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 8298 | CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 8299 | return true; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 8300 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8301 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 8302 | return false; |
| 8303 | } |
| 8304 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8305 | /// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3 |
| 8306 | /// where 'cond' is the comparison specified by CC. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8307 | SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8308 | SDValue N2, SDValue N3, |
| 8309 | ISD::CondCode CC, bool NotExtCompare) { |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8310 | // (x ? y : y) -> y. |
| 8311 | if (N2 == N3) return N2; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8312 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8313 | EVT VT = N2.getValueType(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8314 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| 8315 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); |
| 8316 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8317 | |
| 8318 | // Determine if the condition we're dealing with is constant |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 8319 | SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 8320 | N0, N1, CC, DL, false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8321 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
| 8322 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8323 | |
| 8324 | // fold select_cc true, x, y -> x |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8325 | if (SCCC && !SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8326 | return N2; |
| 8327 | // fold select_cc false, x, y -> y |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8328 | if (SCCC && SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8329 | return N3; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8330 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8331 | // Check to see if we can simplify the select into an fabs node |
| 8332 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 8333 | // Allow either -0.0 or 0.0 |
Dale Johannesen | 87503a6 | 2007-08-25 22:10:57 +0000 | [diff] [blame] | 8334 | if (CFP->getValueAPF().isZero()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8335 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 8336 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 8337 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 8338 | N2 == N3.getOperand(0)) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8339 | return DAG.getNode(ISD::FABS, DL, VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8340 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8341 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 8342 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 8343 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 8344 | N2.getOperand(0) == N3) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8345 | return DAG.getNode(ISD::FABS, DL, VT, N3); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8346 | } |
| 8347 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8348 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8349 | // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" |
| 8350 | // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 |
| 8351 | // in it. This is a win when the constant is not otherwise available because |
| 8352 | // it replaces two constant pool loads with one. We only do this if the FP |
| 8353 | // type is known to be legal, because if it isn't, then we are before legalize |
| 8354 | // 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] | 8355 | // messing with soft float) and if the ConstantFP is not legal, because if |
| 8356 | // 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] | 8357 | if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) |
| 8358 | if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { |
| 8359 | if (TLI.isTypeLegal(N2.getValueType()) && |
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 8360 | (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != |
| 8361 | TargetLowering::Legal) && |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8362 | // If both constants have multiple uses, then we won't need to do an |
| 8363 | // extra load, they are likely around in registers for other users. |
| 8364 | (TV->hasOneUse() || FV->hasOneUse())) { |
| 8365 | Constant *Elts[] = { |
| 8366 | const_cast<ConstantFP*>(FV->getConstantFPValue()), |
| 8367 | const_cast<ConstantFP*>(TV->getConstantFPValue()) |
| 8368 | }; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 8369 | Type *FPTy = Elts[0]->getType(); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8370 | const TargetData &TD = *TLI.getTargetData(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8371 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8372 | // Create a ConstantArray of the two constants. |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 8373 | Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8374 | SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(), |
| 8375 | TD.getPrefTypeAlignment(FPTy)); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 8376 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8377 | |
| 8378 | // Get the offsets to the 0 and 1 element of the array so that we can |
| 8379 | // select between them. |
| 8380 | SDValue Zero = DAG.getIntPtrConstant(0); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 8381 | unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8382 | SDValue One = DAG.getIntPtrConstant(EltSize); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8383 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8384 | SDValue Cond = DAG.getSetCC(DL, |
| 8385 | TLI.getSetCCResultType(N0.getValueType()), |
| 8386 | N0, N1, CC); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 8387 | AddToWorkList(Cond.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8388 | SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(), |
| 8389 | Cond, One, Zero); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 8390 | AddToWorkList(CstOffset.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8391 | CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx, |
| 8392 | CstOffset); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 8393 | AddToWorkList(CPIdx.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8394 | return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 8395 | MachinePointerInfo::getConstantPool(), false, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8396 | false, false, Alignment); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 8397 | |
| 8398 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8399 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8400 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8401 | // Check to see if we can perform the "gzip trick", transforming |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8402 | // (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] | 8403 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8404 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
| 8405 | (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8406 | EVT XType = N0.getValueType(); |
| 8407 | EVT AType = N2.getValueType(); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8408 | if (XType.bitsGE(AType)) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8409 | // 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] | 8410 | // single-bit constant. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8411 | if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) { |
| 8412 | unsigned ShCtV = N2C->getAPIntValue().logBase2(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8413 | ShCtV = XType.getSizeInBits()-ShCtV-1; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8414 | SDValue ShCt = DAG.getConstant(ShCtV, |
| 8415 | getShiftAmountTy(N0.getValueType())); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8416 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8417 | XType, N0, ShCt); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8418 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8419 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8420 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8421 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8422 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8423 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8424 | |
| 8425 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8426 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8427 | |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8428 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8429 | XType, N0, |
| 8430 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8431 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8432 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8433 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8434 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8435 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8436 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8437 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8438 | |
| 8439 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8440 | } |
| 8441 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8442 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8443 | // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) |
| 8444 | // where y is has a single bit set. |
| 8445 | // A plaintext description would be, we can turn the SELECT_CC into an AND |
| 8446 | // when the condition can be materialized as an all-ones register. Any |
| 8447 | // single bit-test can be materialized as an all-ones register with |
| 8448 | // shift-left and shift-right-arith. |
| 8449 | if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && |
| 8450 | N0->getValueType(0) == VT && |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8451 | N1C && N1C->isNullValue() && |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8452 | N2C && N2C->isNullValue()) { |
| 8453 | SDValue AndLHS = N0->getOperand(0); |
| 8454 | ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); |
| 8455 | if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { |
| 8456 | // Shift the tested bit over the sign bit. |
| 8457 | APInt AndMask = ConstAndRHS->getAPIntValue(); |
| 8458 | SDValue ShlAmt = |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8459 | DAG.getConstant(AndMask.countLeadingZeros(), |
| 8460 | getShiftAmountTy(AndLHS.getValueType())); |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8461 | SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8462 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8463 | // Now arithmetic right shift it all the way over, so the result is either |
| 8464 | // all-ones, or zero. |
| 8465 | SDValue ShrAmt = |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8466 | DAG.getConstant(AndMask.getBitWidth()-1, |
| 8467 | getShiftAmountTy(Shl.getValueType())); |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8468 | SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8469 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 8470 | return DAG.getNode(ISD::AND, DL, VT, Shr, N3); |
| 8471 | } |
| 8472 | } |
| 8473 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 8474 | // fold select C, 16, 0 -> shl C, 4 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8475 | if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() && |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 8476 | TLI.getBooleanContents(N0.getValueType().isVector()) == |
| 8477 | TargetLowering::ZeroOrOneBooleanContent) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8478 | |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 8479 | // If the caller doesn't want us to simplify this into a zext of a compare, |
| 8480 | // don't do it. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8481 | if (NotExtCompare && N2C->getAPIntValue() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8482 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8483 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 8484 | // Get a SetCC of the condition |
| 8485 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 8486 | // target where it isn't. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8487 | SDValue Temp, SCC; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 8488 | // cast from setcc result type to select result type |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8489 | if (LegalTypes) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8490 | SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 8491 | N0, N1, CC); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8492 | if (N2.getValueType().bitsLT(SCC.getValueType())) |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8493 | Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType()); |
Chris Lattner | 555d8d6 | 2006-12-07 22:36:47 +0000 | [diff] [blame] | 8494 | else |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8495 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8496 | N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 8497 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8498 | SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8499 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8500 | N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 8501 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8502 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8503 | AddToWorkList(SCC.getNode()); |
| 8504 | AddToWorkList(Temp.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8505 | |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8506 | if (N2C->getAPIntValue() == 1) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 8507 | return Temp; |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8508 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 8509 | // shl setcc result by log2 n2c |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8510 | return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8511 | DAG.getConstant(N2C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8512 | getShiftAmountTy(Temp.getValueType()))); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 8513 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8514 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8515 | // Check to see if this is the equivalent of setcc |
| 8516 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 8517 | // otherwise, go ahead with the folds. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 8518 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8519 | EVT XType = N0.getValueType(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8520 | if (!LegalOperations || |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 8521 | TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8522 | SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8523 | if (Res.getValueType() != VT) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8524 | Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8525 | return Res; |
| 8526 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8527 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8528 | // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X)))) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8529 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 8530 | (!LegalOperations || |
Duncan Sands | 184a876 | 2008-06-14 17:48:34 +0000 | [diff] [blame] | 8531 | TLI.isOperationLegal(ISD::CTLZ, XType))) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8532 | SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8533 | return DAG.getNode(ISD::SRL, DL, XType, Ctlz, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8534 | DAG.getConstant(Log2_32(XType.getSizeInBits()), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8535 | getShiftAmountTy(Ctlz.getValueType()))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8536 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8537 | // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8538 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8539 | SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(), |
| 8540 | XType, DAG.getConstant(0, XType), N0); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 8541 | SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8542 | return DAG.getNode(ISD::SRL, DL, XType, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 8543 | DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0), |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8544 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8545 | getShiftAmountTy(XType))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8546 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8547 | // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1)) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8548 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 8549 | SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8550 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8551 | getShiftAmountTy(N0.getValueType()))); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8552 | return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType)); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8553 | } |
| 8554 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8555 | |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 8556 | // Check to see if this is an integer abs. |
| 8557 | // select_cc setg[te] X, 0, X, -X -> |
| 8558 | // select_cc setgt X, -1, X, -X -> |
| 8559 | // select_cc setl[te] X, 0, -X, X -> |
| 8560 | // select_cc setlt X, 1, -X, X -> |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8561 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 8562 | if (N1C) { |
| 8563 | ConstantSDNode *SubC = NULL; |
| 8564 | if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 8565 | (N1C->isAllOnesValue() && CC == ISD::SETGT)) && |
| 8566 | N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) |
| 8567 | SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); |
| 8568 | else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || |
| 8569 | (N1C->isOne() && CC == ISD::SETLT)) && |
| 8570 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) |
| 8571 | SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); |
| 8572 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8573 | EVT XType = N0.getValueType(); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 8574 | if (SubC && SubC->isNullValue() && XType.isInteger()) { |
| 8575 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, |
| 8576 | N0, |
| 8577 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8578 | getShiftAmountTy(N0.getValueType()))); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 8579 | SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), |
| 8580 | XType, N0, Shift); |
| 8581 | AddToWorkList(Shift.getNode()); |
| 8582 | AddToWorkList(Add.getNode()); |
| 8583 | return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 8584 | } |
| 8585 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8586 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8587 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 8588 | } |
| 8589 | |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 8590 | /// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8591 | SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8592 | SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 8593 | DebugLoc DL, bool foldBooleans) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8594 | TargetLowering::DAGCombinerInfo |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 8595 | DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this); |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 8596 | return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); |
Nate Begeman | 452d7beb | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 8599 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 8600 | /// return a DAG expression to select that will generate the same value by |
| 8601 | /// multiplying by a magic number. See: |
| 8602 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8603 | SDValue DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 8604 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 8605 | SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 8606 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 8607 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 8608 | ii != ee; ++ii) |
| 8609 | AddToWorkList(*ii); |
| 8610 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
| 8613 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 8614 | /// return a DAG expression to select that will generate the same value by |
| 8615 | /// multiplying by a magic number. See: |
| 8616 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8617 | SDValue DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 8618 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 8619 | SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 8620 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 8621 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 8622 | ii != ee; ++ii) |
| 8623 | AddToWorkList(*ii); |
| 8624 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 8625 | } |
| 8626 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8627 | /// 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] | 8628 | // to alias with anything but itself. Provides base object and offset as |
| 8629 | // results. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8630 | static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 8631 | const GlobalValue *&GV, void *&CV) { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8632 | // Assume it is a primitive operation. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8633 | Base = Ptr; Offset = 0; GV = 0; CV = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8634 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8635 | // If it's an adding a simple constant then integrate the offset. |
| 8636 | if (Base.getOpcode() == ISD::ADD) { |
| 8637 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { |
| 8638 | Base = Base.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8639 | Offset += C->getZExtValue(); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8640 | } |
| 8641 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8642 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8643 | // Return the underlying GlobalValue, and update the Offset. Return false |
| 8644 | // for GlobalAddressSDNode since the same GlobalAddress may be represented |
| 8645 | // by multiple nodes with different offsets. |
| 8646 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { |
| 8647 | GV = G->getGlobal(); |
| 8648 | Offset += G->getOffset(); |
| 8649 | return false; |
| 8650 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8651 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8652 | // Return the underlying Constant value, and update the Offset. Return false |
| 8653 | // for ConstantSDNodes since the same constant pool entry may be represented |
| 8654 | // by multiple nodes with different offsets. |
| 8655 | if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { |
| 8656 | CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal() |
| 8657 | : (void *)C->getConstVal(); |
| 8658 | Offset += C->getOffset(); |
| 8659 | return false; |
| 8660 | } |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8661 | // 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] | 8662 | return isa<FrameIndexSDNode>(Base); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8663 | } |
| 8664 | |
| 8665 | /// isAlias - Return true if there is any possibility that the two addresses |
| 8666 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8667 | bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 8668 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8669 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8670 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8671 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8672 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8673 | unsigned SrcValueAlign2, |
| 8674 | const MDNode *TBAAInfo2) const { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8675 | // If they are the same then they must be aliases. |
| 8676 | if (Ptr1 == Ptr2) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8677 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8678 | // Gather base node and offset information. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8679 | SDValue Base1, Base2; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8680 | int64_t Offset1, Offset2; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 8681 | const GlobalValue *GV1, *GV2; |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8682 | void *CV1, *CV2; |
| 8683 | bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1); |
| 8684 | bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8685 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8686 | // If they have a same base address then check to see if they overlap. |
| 8687 | if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 8688 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8689 | |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 8690 | // It is possible for different frame indices to alias each other, mostly |
| 8691 | // when tail call optimization reuses return address slots for arguments. |
| 8692 | // To catch this case, look up the actual index of frame indices to compute |
| 8693 | // the real alias relationship. |
| 8694 | if (isFrameIndex1 && isFrameIndex2) { |
| 8695 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 8696 | Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); |
| 8697 | Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); |
| 8698 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
| 8699 | } |
| 8700 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8701 | // 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] | 8702 | // we know they cannot alias. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 8703 | if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) |
| 8704 | return false; |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 8705 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8706 | // If we know required SrcValue1 and SrcValue2 have relatively large alignment |
| 8707 | // compared to the size and offset of the access, we may be able to prove they |
| 8708 | // do not alias. This check is conservative for now to catch cases created by |
| 8709 | // splitting vector types. |
| 8710 | if ((SrcValueAlign1 == SrcValueAlign2) && |
| 8711 | (SrcValueOffset1 != SrcValueOffset2) && |
| 8712 | (Size1 == Size2) && (SrcValueAlign1 > Size1)) { |
| 8713 | int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1; |
| 8714 | int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8715 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8716 | // There is no overlap between these relatively aligned accesses of similar |
| 8717 | // size, return no alias. |
| 8718 | if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1) |
| 8719 | return false; |
| 8720 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8721 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 8722 | if (CombinerGlobalAA) { |
| 8723 | // Use alias analysis information. |
Dan Gohman | e9c8fa0 | 2007-08-27 16:32:11 +0000 | [diff] [blame] | 8724 | int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2); |
| 8725 | int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset; |
| 8726 | int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8727 | AliasAnalysis::AliasResult AAResult = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8728 | AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1), |
| 8729 | AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2)); |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 8730 | if (AAResult == AliasAnalysis::NoAlias) |
| 8731 | return false; |
| 8732 | } |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 8733 | |
| 8734 | // Otherwise we have to assume they alias. |
| 8735 | return true; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8736 | } |
| 8737 | |
| 8738 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 8739 | /// node. Returns true if the operand was a load. |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 8740 | bool DAGCombiner::FindAliasInfo(SDNode *N, |
Benjamin Kramer | ae4746b | 2012-01-15 11:50:43 +0000 | [diff] [blame] | 8741 | SDValue &Ptr, int64_t &Size, |
| 8742 | const Value *&SrcValue, |
| 8743 | int &SrcValueOffset, |
| 8744 | unsigned &SrcValueAlign, |
| 8745 | const MDNode *&TBAAInfo) const { |
| 8746 | LSBaseSDNode *LS = cast<LSBaseSDNode>(N); |
| 8747 | |
| 8748 | Ptr = LS->getBasePtr(); |
| 8749 | Size = LS->getMemoryVT().getSizeInBits() >> 3; |
| 8750 | SrcValue = LS->getSrcValue(); |
| 8751 | SrcValueOffset = LS->getSrcValueOffset(); |
| 8752 | SrcValueAlign = LS->getOriginalAlignment(); |
| 8753 | TBAAInfo = LS->getTBAAInfo(); |
| 8754 | return isa<LoadSDNode>(LS); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 8755 | } |
| 8756 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8757 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 8758 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8759 | void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 8760 | SmallVector<SDValue, 8> &Aliases) { |
| 8761 | SmallVector<SDValue, 8> Chains; // List of chains to visit. |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8762 | SmallPtrSet<SDNode *, 16> Visited; // Visited node set. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8763 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8764 | // Get alias information for node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8765 | SDValue Ptr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8766 | int64_t Size; |
| 8767 | const Value *SrcValue; |
| 8768 | int SrcValueOffset; |
| 8769 | unsigned SrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8770 | const MDNode *SrcTBAAInfo; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8771 | bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8772 | SrcValueAlign, SrcTBAAInfo); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8773 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8774 | // Starting off. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8775 | Chains.push_back(OriginalChain); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 8776 | unsigned Depth = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8777 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8778 | // Look at each chain and determine if it is an alias. If so, add it to the |
| 8779 | // 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] | 8780 | // candidate. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8781 | while (!Chains.empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8782 | SDValue Chain = Chains.back(); |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8783 | Chains.pop_back(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8784 | |
| 8785 | // For TokenFactor nodes, look at each operand and only continue up the |
| 8786 | // 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] | 8787 | // find more and revert to original chain since the xform is unlikely to be |
| 8788 | // profitable. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8789 | // |
| 8790 | // 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] | 8791 | // chain we found before we hit a tokenfactor rather than the original |
| 8792 | // chain. |
| 8793 | if (Depth > 6 || Aliases.size() == 2) { |
| 8794 | Aliases.clear(); |
| 8795 | Aliases.push_back(OriginalChain); |
| 8796 | break; |
| 8797 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8798 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8799 | // Don't bother if we've been before. |
| 8800 | if (!Visited.insert(Chain.getNode())) |
| 8801 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8802 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8803 | switch (Chain.getOpcode()) { |
| 8804 | case ISD::EntryToken: |
| 8805 | // Entry token is ideal chain operand, but handled in FindBetterChain. |
| 8806 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8807 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8808 | case ISD::LOAD: |
| 8809 | case ISD::STORE: { |
| 8810 | // Get alias information for Chain. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8811 | SDValue OpPtr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8812 | int64_t OpSize; |
| 8813 | const Value *OpSrcValue; |
| 8814 | int OpSrcValueOffset; |
| 8815 | unsigned OpSrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8816 | const MDNode *OpSrcTBAAInfo; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8817 | bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8818 | OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8819 | OpSrcValueAlign, |
| 8820 | OpSrcTBAAInfo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8821 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8822 | // If chain is alias then stop here. |
| 8823 | if (!(IsLoad && IsOpLoad) && |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8824 | isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8825 | SrcTBAAInfo, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8826 | OpPtr, OpSize, OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 8827 | OpSrcValueAlign, OpSrcTBAAInfo)) { |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8828 | Aliases.push_back(Chain); |
| 8829 | } else { |
| 8830 | // Look further up the chain. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8831 | Chains.push_back(Chain.getOperand(0)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 8832 | ++Depth; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8833 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8834 | break; |
| 8835 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8836 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8837 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8838 | // We have to check each of the operands of the token factor for "small" |
| 8839 | // token factors, so we queue them up. Adding the operands to the queue |
| 8840 | // (stack) in reverse order maintains the original order and increases the |
| 8841 | // likelihood that getNode will find a matching token factor (CSE.) |
| 8842 | if (Chain.getNumOperands() > 16) { |
| 8843 | Aliases.push_back(Chain); |
| 8844 | break; |
| 8845 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8846 | for (unsigned n = Chain.getNumOperands(); n;) |
| 8847 | Chains.push_back(Chain.getOperand(--n)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 8848 | ++Depth; |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8849 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8850 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 8851 | default: |
| 8852 | // For all other instructions we will just have to take what we can get. |
| 8853 | Aliases.push_back(Chain); |
| 8854 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8855 | } |
| 8856 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8857 | } |
| 8858 | |
| 8859 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking |
| 8860 | /// for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8861 | SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { |
| 8862 | SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8863 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8864 | // Accumulate all the aliases to this node. |
| 8865 | GatherAllAliases(N, OldChain, Aliases); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8866 | |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 8867 | // If no operands then chain to entry token. |
| 8868 | if (Aliases.size() == 0) |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8869 | return DAG.getEntryNode(); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 8870 | |
| 8871 | // If a single operand then chain to it. We don't need to revisit it. |
| 8872 | if (Aliases.size() == 1) |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8873 | return Aliases[0]; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8874 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 8875 | // Construct a custom tailored token factor. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8876 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 8877 | &Aliases[0], Aliases.size()); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 8878 | } |
| 8879 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 8880 | // SelectionDAG::Combine - This is the entry point for the file. |
| 8881 | // |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 8882 | void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 8883 | CodeGenOpt::Level OptLevel) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 8884 | /// run - This is the main entry point to this class. |
| 8885 | /// |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 8886 | DAGCombiner(*this, AA, OptLevel).Run(Level); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 8887 | } |