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 | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLowering.h" |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 41 | STATISTIC(NodesCombined , "Number of dag nodes combined"); |
| 42 | STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created"); |
| 43 | STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created"); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 44 | STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 45 | STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 46 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 47 | namespace { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
Owen Anderson | 0dcc814 | 2010-09-19 21:01:26 +0000 | [diff] [blame] | 49 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 50 | cl::desc("Turn on alias analysis during testing")); |
Jim Laskey | 3ad175b | 2006-10-12 15:22:24 +0000 | [diff] [blame] | 51 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
| 53 | CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, |
| 54 | cl::desc("Include global information in alias analysis")); |
| 55 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 56 | //------------------------------ DAGCombiner ---------------------------------// |
| 57 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 58 | class DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 59 | SelectionDAG &DAG; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 60 | const TargetLowering &TLI; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 61 | CombineLevel Level; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 62 | CodeGenOpt::Level OptLevel; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 63 | bool LegalOperations; |
| 64 | bool LegalTypes; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 65 | |
| 66 | // Worklist of all of the nodes that need to be simplified. |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 67 | std::vector<SDNode*> WorkList; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 68 | |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 69 | // AA - Used for DAG load/store alias analysis. |
| 70 | AliasAnalysis &AA; |
| 71 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 72 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 73 | /// the instruction to the work lists because they might get more simplified |
| 74 | /// now. |
| 75 | /// |
| 76 | void AddUsersToWorkList(SDNode *N) { |
| 77 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 78 | UI != UE; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 79 | AddToWorkList(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 82 | /// visit - call the node-specific routine that knows how to fold each |
| 83 | /// particular type of node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 84 | SDValue visit(SDNode *N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 86 | public: |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 87 | /// AddToWorkList - Add to the work list making sure it's instance is at the |
| 88 | /// the back (next to be processed.) |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 89 | void AddToWorkList(SDNode *N) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 90 | removeFromWorkList(N); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 91 | WorkList.push_back(N); |
| 92 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 93 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 94 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 95 | /// |
| 96 | void removeFromWorkList(SDNode *N) { |
| 97 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 98 | WorkList.end()); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 99 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 101 | SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 102 | bool AddTo = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 104 | SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 105 | return CombineTo(N, &Res, 1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 106 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 107 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 108 | SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 109 | bool AddTo = true) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 110 | SDValue To[] = { Res0, Res1 }; |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 111 | return CombineTo(N, To, 2, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 112 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 113 | |
| 114 | void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 118 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 119 | /// 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] | 120 | /// propagation. If so, return true. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 121 | bool SimplifyDemandedBits(SDValue Op) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 122 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
| 123 | APInt Demanded = APInt::getAllOnesValue(BitWidth); |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 124 | return SimplifyDemandedBits(Op, Demanded); |
| 125 | } |
| 126 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 127 | bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 129 | bool CombineToPreIndexedLoadStore(SDNode *N); |
| 130 | bool CombineToPostIndexedLoadStore(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 131 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 132 | void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad); |
| 133 | SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace); |
| 134 | SDValue SExtPromoteOperand(SDValue Op, EVT PVT); |
| 135 | SDValue ZExtPromoteOperand(SDValue Op, EVT PVT); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 136 | SDValue PromoteIntBinOp(SDValue Op); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 137 | SDValue PromoteIntShiftOp(SDValue Op); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 138 | SDValue PromoteExtend(SDValue Op); |
| 139 | bool PromoteLoad(SDValue Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 140 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 141 | /// combine - call the node-specific routine that knows how to fold each |
| 142 | /// particular type of node. If that doesn't do anything, try the |
| 143 | /// target-specific DAG combines. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 144 | SDValue combine(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 145 | |
| 146 | // Visitation implementation - Implement dag node combining for different |
| 147 | // node types. The semantics are as follows: |
| 148 | // Return Value: |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 149 | // SDValue.getNode() == 0 - No change was made |
| 150 | // SDValue.getNode() == N - N was replaced, is dead and has been handled. |
| 151 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 152 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 153 | SDValue visitTokenFactor(SDNode *N); |
| 154 | SDValue visitMERGE_VALUES(SDNode *N); |
| 155 | SDValue visitADD(SDNode *N); |
| 156 | SDValue visitSUB(SDNode *N); |
| 157 | SDValue visitADDC(SDNode *N); |
| 158 | SDValue visitADDE(SDNode *N); |
| 159 | SDValue visitMUL(SDNode *N); |
| 160 | SDValue visitSDIV(SDNode *N); |
| 161 | SDValue visitUDIV(SDNode *N); |
| 162 | SDValue visitSREM(SDNode *N); |
| 163 | SDValue visitUREM(SDNode *N); |
| 164 | SDValue visitMULHU(SDNode *N); |
| 165 | SDValue visitMULHS(SDNode *N); |
| 166 | SDValue visitSMUL_LOHI(SDNode *N); |
| 167 | SDValue visitUMUL_LOHI(SDNode *N); |
| 168 | SDValue visitSDIVREM(SDNode *N); |
| 169 | SDValue visitUDIVREM(SDNode *N); |
| 170 | SDValue visitAND(SDNode *N); |
| 171 | SDValue visitOR(SDNode *N); |
| 172 | SDValue visitXOR(SDNode *N); |
| 173 | SDValue SimplifyVBinOp(SDNode *N); |
| 174 | SDValue visitSHL(SDNode *N); |
| 175 | SDValue visitSRA(SDNode *N); |
| 176 | SDValue visitSRL(SDNode *N); |
| 177 | SDValue visitCTLZ(SDNode *N); |
| 178 | SDValue visitCTTZ(SDNode *N); |
| 179 | SDValue visitCTPOP(SDNode *N); |
| 180 | SDValue visitSELECT(SDNode *N); |
| 181 | SDValue visitSELECT_CC(SDNode *N); |
| 182 | SDValue visitSETCC(SDNode *N); |
| 183 | SDValue visitSIGN_EXTEND(SDNode *N); |
| 184 | SDValue visitZERO_EXTEND(SDNode *N); |
| 185 | SDValue visitANY_EXTEND(SDNode *N); |
| 186 | SDValue visitSIGN_EXTEND_INREG(SDNode *N); |
| 187 | SDValue visitTRUNCATE(SDNode *N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 188 | SDValue visitBITCAST(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 189 | SDValue visitBUILD_PAIR(SDNode *N); |
| 190 | SDValue visitFADD(SDNode *N); |
| 191 | SDValue visitFSUB(SDNode *N); |
| 192 | SDValue visitFMUL(SDNode *N); |
| 193 | SDValue visitFDIV(SDNode *N); |
| 194 | SDValue visitFREM(SDNode *N); |
| 195 | SDValue visitFCOPYSIGN(SDNode *N); |
| 196 | SDValue visitSINT_TO_FP(SDNode *N); |
| 197 | SDValue visitUINT_TO_FP(SDNode *N); |
| 198 | SDValue visitFP_TO_SINT(SDNode *N); |
| 199 | SDValue visitFP_TO_UINT(SDNode *N); |
| 200 | SDValue visitFP_ROUND(SDNode *N); |
| 201 | SDValue visitFP_ROUND_INREG(SDNode *N); |
| 202 | SDValue visitFP_EXTEND(SDNode *N); |
| 203 | SDValue visitFNEG(SDNode *N); |
| 204 | SDValue visitFABS(SDNode *N); |
| 205 | SDValue visitBRCOND(SDNode *N); |
| 206 | SDValue visitBR_CC(SDNode *N); |
| 207 | SDValue visitLOAD(SDNode *N); |
| 208 | SDValue visitSTORE(SDNode *N); |
| 209 | SDValue visitINSERT_VECTOR_ELT(SDNode *N); |
| 210 | SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); |
| 211 | SDValue visitBUILD_VECTOR(SDNode *N); |
| 212 | SDValue visitCONCAT_VECTORS(SDNode *N); |
| 213 | SDValue visitVECTOR_SHUFFLE(SDNode *N); |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 214 | SDValue visitMEMBARRIER(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 215 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 216 | SDValue XformToShuffleWithZero(SDNode *N); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 217 | SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 218 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 219 | SDValue visitShiftByConstant(SDNode *N, unsigned Amt); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 220 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 221 | bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); |
| 222 | SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 223 | SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 224 | SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2, |
| 225 | SDValue N3, ISD::CondCode CC, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 226 | bool NotExtCompare = false); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 227 | SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 228 | DebugLoc DL, bool foldBooleans = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 229 | SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 230 | unsigned HiOp); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 231 | SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 232 | SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 233 | SDValue BuildSDIV(SDNode *N); |
| 234 | SDValue BuildUDIV(SDNode *N); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 235 | SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 236 | SDValue ReduceLoadWidth(SDNode *N); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 237 | SDValue ReduceLoadOpStoreWidth(SDNode *N); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 238 | SDValue TransformFPLoadStorePair(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 239 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 240 | SDValue GetDemandedBits(SDValue V, const APInt &Mask); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 241 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 242 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 243 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 244 | void GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 245 | SmallVector<SDValue, 8> &Aliases); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 246 | |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 247 | /// isAlias - Return true if there is any possibility that the two addresses |
| 248 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 249 | bool isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 250 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 251 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 252 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 253 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 254 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 255 | unsigned SrcValueAlign2, |
| 256 | const MDNode *TBAAInfo2) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 257 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 258 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 259 | /// node. Returns true if the operand was a load. |
| 260 | bool FindAliasInfo(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 261 | SDValue &Ptr, int64_t &Size, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 262 | const Value *&SrcValue, int &SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 263 | unsigned &SrcValueAlignment, |
| 264 | const MDNode *&TBAAInfo) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 265 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 266 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 267 | /// looking for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 268 | SDValue FindBetterChain(SDNode *N, SDValue Chain); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 270 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 271 | DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 272 | : DAG(D), TLI(D.getTargetLoweringInfo()), Level(Unrestricted), |
| 273 | OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 274 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 275 | /// Run - runs the dag combiner on all nodes in the work list |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 276 | void Run(CombineLevel AtLevel); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 278 | SelectionDAG &getDAG() const { return DAG; } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 280 | /// getShiftAmountTy - Returns a type large enough to hold any valid |
| 281 | /// shift amount - before type legalization these can be huge. |
| 282 | EVT getShiftAmountTy() { |
| 283 | return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy(); |
| 284 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 286 | /// isTypeLegal - This method returns true if we are running before type |
| 287 | /// legalization or if the specified VT is legal. |
| 288 | bool isTypeLegal(const EVT &VT) { |
| 289 | if (!LegalTypes) return true; |
| 290 | return TLI.isTypeLegal(VT); |
| 291 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 292 | }; |
| 293 | } |
| 294 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 295 | |
| 296 | namespace { |
| 297 | /// WorkListRemover - This class is a DAGUpdateListener that removes any deleted |
| 298 | /// nodes from the worklist. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 299 | class WorkListRemover : public SelectionDAG::DAGUpdateListener { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 300 | DAGCombiner &DC; |
| 301 | public: |
Dan Gohman | b5660dc | 2008-02-20 16:44:09 +0000 | [diff] [blame] | 302 | explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {} |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 303 | |
Duncan Sands | edfcf59 | 2008-06-11 11:42:12 +0000 | [diff] [blame] | 304 | virtual void NodeDeleted(SDNode *N, SDNode *E) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 305 | DC.removeFromWorkList(N); |
| 306 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 307 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 308 | virtual void NodeUpdated(SDNode *N) { |
| 309 | // Ignore updates. |
| 310 | } |
| 311 | }; |
| 312 | } |
| 313 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | // TargetLowering::DAGCombinerInfo implementation |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
| 318 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 319 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 320 | } |
| 321 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 322 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 323 | CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) { |
| 324 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 327 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 328 | CombineTo(SDNode *N, SDValue Res, bool AddTo) { |
| 329 | return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 333 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 334 | CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { |
| 335 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 338 | void TargetLowering::DAGCombinerInfo:: |
| 339 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
| 340 | return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); |
| 341 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 343 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 344 | // Helper Functions |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | |
| 347 | /// isNegatibleForFree - Return 1 if we can compute the negated form of the |
| 348 | /// specified expression for the same cost as the expression itself, or 2 if we |
| 349 | /// can compute the negated form more cheaply than the expression itself. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 350 | static char isNegatibleForFree(SDValue Op, bool LegalOperations, |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 351 | unsigned Depth = 0) { |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 352 | // No compile time optimizations on this type. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 353 | if (Op.getValueType() == MVT::ppcf128) |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 356 | // fneg is removable even if it has multiple uses. |
| 357 | if (Op.getOpcode() == ISD::FNEG) return 2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 359 | // Don't allow anything with multiple uses. |
| 360 | if (!Op.hasOneUse()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 362 | // Don't recurse exponentially. |
| 363 | if (Depth > 6) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 365 | switch (Op.getOpcode()) { |
| 366 | default: return false; |
| 367 | case ISD::ConstantFP: |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 368 | // Don't invert constant FP values after legalize. The negated constant |
| 369 | // isn't necessarily legal. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 370 | return LegalOperations ? 0 : 1; |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 371 | case ISD::FADD: |
| 372 | // FIXME: determine better conditions for this xform. |
| 373 | if (!UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 374 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 375 | // fold (fsub (fadd A, B)) -> (fsub (fneg A), B) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 376 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1)) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 377 | return V; |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 378 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 379 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 380 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 381 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 382 | if (!UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 383 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 384 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 385 | return 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 387 | case ISD::FMUL: |
| 388 | case ISD::FDIV: |
| 389 | if (HonorSignDependentRoundingFPMath()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 390 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 391 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 392 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1)) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 393 | return V; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 394 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 395 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 397 | case ISD::FP_EXTEND: |
| 398 | case ISD::FP_ROUND: |
| 399 | case ISD::FSIN: |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 400 | return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | /// GetNegatedExpression - If isNegatibleForFree returns true, this function |
| 405 | /// returns the newly negated expression. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 406 | static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 407 | bool LegalOperations, unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 408 | // fneg is removable even if it has multiple uses. |
| 409 | if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 411 | // Don't allow anything with multiple uses. |
| 412 | assert(Op.hasOneUse() && "Unknown reuse!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 414 | assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 415 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 416 | default: llvm_unreachable("Unknown code"); |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 417 | case ISD::ConstantFP: { |
| 418 | APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); |
| 419 | V.changeSign(); |
| 420 | return DAG.getConstantFP(V, Op.getValueType()); |
| 421 | } |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 422 | case ISD::FADD: |
| 423 | // FIXME: determine better conditions for this xform. |
| 424 | assert(UnsafeFPMath); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 425 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 426 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 427 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 428 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 429 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 430 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 431 | Op.getOperand(1)); |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 432 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 433 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 434 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 435 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 436 | Op.getOperand(0)); |
| 437 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 438 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 439 | assert(UnsafeFPMath); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 440 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 441 | // fold (fneg (fsub 0, B)) -> B |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 442 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 443 | if (N0CFP->getValueAPF().isZero()) |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 444 | return Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 445 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 446 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 447 | return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(), |
| 448 | Op.getOperand(1), Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 450 | case ISD::FMUL: |
| 451 | case ISD::FDIV: |
| 452 | assert(!HonorSignDependentRoundingFPMath()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 453 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 454 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 455 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 456 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 457 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 458 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 459 | Op.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 460 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 461 | // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 462 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 463 | Op.getOperand(0), |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 464 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 465 | LegalOperations, Depth+1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 467 | case ISD::FP_EXTEND: |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 468 | case ISD::FSIN: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 469 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 470 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 471 | LegalOperations, Depth+1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 472 | case ISD::FP_ROUND: |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 473 | return DAG.getNode(ISD::FP_ROUND, 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 | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 476 | Op.getOperand(1)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 479 | |
| 480 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 481 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 482 | // 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] | 483 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 484 | // nodes based on the type of node we are checking. This simplifies life a |
| 485 | // bit for the callers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 486 | static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, |
| 487 | SDValue &CC) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 488 | if (N.getOpcode() == ISD::SETCC) { |
| 489 | LHS = N.getOperand(0); |
| 490 | RHS = N.getOperand(1); |
| 491 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 492 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 493 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 494 | if (N.getOpcode() == ISD::SELECT_CC && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 495 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 496 | N.getOperand(3).getOpcode() == ISD::Constant && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 497 | cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 498 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 499 | LHS = N.getOperand(0); |
| 500 | RHS = N.getOperand(1); |
| 501 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 502 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 503 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 507 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 508 | // one use. If this is true, it allows the users to invert the operation for |
| 509 | // free when it is profitable to do so. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 510 | static bool isOneUseSetCC(SDValue N) { |
| 511 | SDValue N0, N1, N2; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 512 | if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 513 | return true; |
| 514 | return false; |
| 515 | } |
| 516 | |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 517 | SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL, |
| 518 | SDValue N0, SDValue N1) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 519 | EVT VT = N0.getValueType(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 520 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
| 521 | if (isa<ConstantSDNode>(N1)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 522 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 523 | SDValue OpNode = |
| 524 | DAG.FoldConstantArithmetic(Opc, VT, |
| 525 | cast<ConstantSDNode>(N0.getOperand(1)), |
| 526 | cast<ConstantSDNode>(N1)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 527 | return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 528 | } else if (N0.hasOneUse()) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 529 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
| 530 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
| 531 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 532 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 533 | return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 536 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 537 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
| 538 | if (isa<ConstantSDNode>(N0)) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 539 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 540 | SDValue OpNode = |
| 541 | DAG.FoldConstantArithmetic(Opc, VT, |
| 542 | cast<ConstantSDNode>(N1.getOperand(1)), |
| 543 | cast<ConstantSDNode>(N0)); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 544 | return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 545 | } else if (N1.hasOneUse()) { |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 546 | // 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] | 547 | SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 548 | N1.getOperand(0), N0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 549 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 550 | return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 553 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 554 | return SDValue(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 557 | SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
| 558 | bool AddTo) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 559 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
| 560 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 561 | DEBUG(dbgs() << "\nReplacing.1 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 562 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 563 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 564 | To[0].getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 565 | dbgs() << " and " << NumTo-1 << " other values\n"; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 566 | for (unsigned i = 0, e = NumTo; i != e; ++i) |
Jakob Stoklund Olesen | 9f0d4e6 | 2009-12-03 05:15:35 +0000 | [diff] [blame] | 567 | assert((!To[i].getNode() || |
| 568 | N->getValueType(i) == To[i].getValueType()) && |
Dan Gohman | 764fd0c | 2009-01-21 15:17:51 +0000 | [diff] [blame] | 569 | "Cannot combine value to value of different type!")); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 570 | WorkListRemover DeadNodes(*this); |
| 571 | DAG.ReplaceAllUsesWith(N, To, &DeadNodes); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 572 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 573 | if (AddTo) { |
| 574 | // Push the new nodes and any users onto the worklist |
| 575 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 576 | if (To[i].getNode()) { |
| 577 | AddToWorkList(To[i].getNode()); |
| 578 | AddUsersToWorkList(To[i].getNode()); |
| 579 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 582 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 583 | // Finally, if the node is now dead, remove it from the graph. The node |
| 584 | // may not be dead if the replacement process recursively simplified to |
| 585 | // something else needing this node. |
| 586 | if (N->use_empty()) { |
| 587 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 588 | // process a node that has been replaced. |
| 589 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 590 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 591 | // Finally, since the node is now dead, remove it from the graph. |
| 592 | DAG.DeleteNode(N); |
| 593 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 594 | return SDValue(N, 0); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 597 | void DAGCombiner:: |
| 598 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 599 | // Replace all uses. If any nodes become isomorphic to other nodes and |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 600 | // are deleted, make sure to remove them from our worklist. |
| 601 | WorkListRemover DeadNodes(*this); |
| 602 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 603 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 604 | // Push the new node and any (possibly new) users onto the worklist. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 605 | AddToWorkList(TLO.New.getNode()); |
| 606 | AddUsersToWorkList(TLO.New.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 607 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 608 | // Finally, if the node is now dead, remove it from the graph. The node |
| 609 | // may not be dead if the replacement process recursively simplified to |
| 610 | // something else needing this node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 611 | if (TLO.Old.getNode()->use_empty()) { |
| 612 | removeFromWorkList(TLO.Old.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 613 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 614 | // If the operands of this node are only used by the node, they will now |
| 615 | // 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] | 616 | for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i) |
| 617 | if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse()) |
| 618 | AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 619 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 620 | DAG.DeleteNode(TLO.Old.getNode()); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 621 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
| 625 | /// it can be simplified or if things it uses can be simplified by bit |
| 626 | /// propagation. If so, return true. |
| 627 | bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 628 | TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 629 | APInt KnownZero, KnownOne; |
| 630 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 631 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 632 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 633 | // Revisit the node. |
| 634 | AddToWorkList(Op.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 635 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 636 | // Replace the old value with the new one. |
| 637 | ++NodesCombined; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 638 | DEBUG(dbgs() << "\nReplacing.2 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 639 | TLO.Old.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 640 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 641 | TLO.New.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 642 | dbgs() << '\n'); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 643 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 644 | CommitTargetLoweringOpt(TLO); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 645 | return true; |
| 646 | } |
| 647 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 648 | void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { |
| 649 | DebugLoc dl = Load->getDebugLoc(); |
| 650 | EVT VT = Load->getValueType(0); |
| 651 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 652 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 653 | DEBUG(dbgs() << "\nReplacing.9 "; |
| 654 | Load->dump(&DAG); |
| 655 | dbgs() << "\nWith: "; |
| 656 | Trunc.getNode()->dump(&DAG); |
| 657 | dbgs() << '\n'); |
| 658 | WorkListRemover DeadNodes(*this); |
| 659 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc, &DeadNodes); |
| 660 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1), |
| 661 | &DeadNodes); |
| 662 | removeFromWorkList(Load); |
| 663 | DAG.DeleteNode(Load); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 664 | AddToWorkList(Trunc.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { |
| 668 | Replace = false; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 669 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 670 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 671 | EVT MemVT = LD->getMemoryVT(); |
| 672 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 673 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
| 674 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 675 | : LD->getExtensionType(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 676 | Replace = true; |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 677 | return DAG.getExtLoad(ExtType, PVT, dl, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 678 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 679 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 680 | MemVT, LD->isVolatile(), |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 681 | LD->isNonTemporal(), LD->getAlignment()); |
| 682 | } |
| 683 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 684 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 685 | switch (Opc) { |
| 686 | default: break; |
| 687 | case ISD::AssertSext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 688 | return DAG.getNode(ISD::AssertSext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 689 | SExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 690 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 691 | case ISD::AssertZext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 692 | return DAG.getNode(ISD::AssertZext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 693 | ZExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 694 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 695 | case ISD::Constant: { |
| 696 | unsigned ExtOpc = |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 697 | Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 698 | return DAG.getNode(ExtOpc, dl, PVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 699 | } |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 703 | return SDValue(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 704 | return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 707 | SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 708 | if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) |
| 709 | return SDValue(); |
| 710 | EVT OldVT = Op.getValueType(); |
| 711 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 712 | bool Replace = false; |
| 713 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 714 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 715 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 716 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 717 | |
| 718 | if (Replace) |
| 719 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 720 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 721 | DAG.getValueType(OldVT)); |
| 722 | } |
| 723 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 724 | SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 725 | EVT OldVT = Op.getValueType(); |
| 726 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 727 | bool Replace = false; |
| 728 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 729 | if (NewOp.getNode() == 0) |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 730 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 731 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 732 | |
| 733 | if (Replace) |
| 734 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 735 | return DAG.getZeroExtendInReg(NewOp, dl, OldVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 738 | /// PromoteIntBinOp - Promote the specified integer binary operation if the |
| 739 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 740 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 741 | SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { |
| 742 | if (!LegalOperations) |
| 743 | return SDValue(); |
| 744 | |
| 745 | EVT VT = Op.getValueType(); |
| 746 | if (VT.isVector() || !VT.isInteger()) |
| 747 | return SDValue(); |
| 748 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 749 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 750 | // promoting it. |
| 751 | unsigned Opc = Op.getOpcode(); |
| 752 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 753 | return SDValue(); |
| 754 | |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 755 | EVT PVT = VT; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 756 | // Consult target whether it is a good idea to promote this operation and |
| 757 | // what's the right type to promote it to. |
| 758 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 759 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 760 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 761 | bool Replace0 = false; |
| 762 | SDValue N0 = Op.getOperand(0); |
| 763 | SDValue NN0 = PromoteOperand(N0, PVT, Replace0); |
| 764 | if (NN0.getNode() == 0) |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 765 | return SDValue(); |
| 766 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 767 | bool Replace1 = false; |
| 768 | SDValue N1 = Op.getOperand(1); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 769 | SDValue NN1; |
| 770 | if (N0 == N1) |
| 771 | NN1 = NN0; |
| 772 | else { |
| 773 | NN1 = PromoteOperand(N1, PVT, Replace1); |
| 774 | if (NN1.getNode() == 0) |
| 775 | return SDValue(); |
| 776 | } |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 777 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 778 | AddToWorkList(NN0.getNode()); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 779 | if (NN1.getNode()) |
| 780 | AddToWorkList(NN1.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 781 | |
| 782 | if (Replace0) |
| 783 | ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); |
| 784 | if (Replace1) |
| 785 | ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 786 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 787 | DEBUG(dbgs() << "\nPromoting "; |
| 788 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 789 | DebugLoc dl = Op.getDebugLoc(); |
| 790 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 791 | DAG.getNode(Opc, dl, PVT, NN0, NN1)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 792 | } |
| 793 | return SDValue(); |
| 794 | } |
| 795 | |
| 796 | /// PromoteIntShiftOp - Promote the specified integer shift operation if the |
| 797 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 798 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 799 | SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { |
| 800 | if (!LegalOperations) |
| 801 | return SDValue(); |
| 802 | |
| 803 | EVT VT = Op.getValueType(); |
| 804 | if (VT.isVector() || !VT.isInteger()) |
| 805 | return SDValue(); |
| 806 | |
| 807 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 808 | // promoting it. |
| 809 | unsigned Opc = Op.getOpcode(); |
| 810 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 811 | return SDValue(); |
| 812 | |
| 813 | EVT PVT = VT; |
| 814 | // Consult target whether it is a good idea to promote this operation and |
| 815 | // what's the right type to promote it to. |
| 816 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 817 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 818 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 819 | bool Replace = false; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 820 | SDValue N0 = Op.getOperand(0); |
| 821 | if (Opc == ISD::SRA) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 822 | N0 = SExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 823 | else if (Opc == ISD::SRL) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 824 | N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 825 | else |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 826 | N0 = PromoteOperand(N0, PVT, Replace); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 827 | if (N0.getNode() == 0) |
| 828 | return SDValue(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 829 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 830 | AddToWorkList(N0.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 831 | if (Replace) |
| 832 | ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +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 | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 836 | DebugLoc dl = Op.getDebugLoc(); |
| 837 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 838 | DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1))); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 839 | } |
| 840 | return SDValue(); |
| 841 | } |
| 842 | |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 843 | SDValue DAGCombiner::PromoteExtend(SDValue Op) { |
| 844 | if (!LegalOperations) |
| 845 | return SDValue(); |
| 846 | |
| 847 | EVT VT = Op.getValueType(); |
| 848 | if (VT.isVector() || !VT.isInteger()) |
| 849 | return SDValue(); |
| 850 | |
| 851 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 852 | // promoting it. |
| 853 | unsigned Opc = Op.getOpcode(); |
| 854 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 855 | return SDValue(); |
| 856 | |
| 857 | EVT PVT = VT; |
| 858 | // Consult target whether it is a good idea to promote this operation and |
| 859 | // what's the right type to promote it to. |
| 860 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 861 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 862 | // fold (aext (aext x)) -> (aext x) |
| 863 | // fold (aext (zext x)) -> (zext x) |
| 864 | // fold (aext (sext x)) -> (sext x) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 865 | DEBUG(dbgs() << "\nPromoting "; |
| 866 | Op.getNode()->dump(&DAG)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 867 | return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0)); |
| 868 | } |
| 869 | return SDValue(); |
| 870 | } |
| 871 | |
| 872 | bool DAGCombiner::PromoteLoad(SDValue Op) { |
| 873 | if (!LegalOperations) |
| 874 | return false; |
| 875 | |
| 876 | EVT VT = Op.getValueType(); |
| 877 | if (VT.isVector() || !VT.isInteger()) |
| 878 | return false; |
| 879 | |
| 880 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 881 | // promoting it. |
| 882 | unsigned Opc = Op.getOpcode(); |
| 883 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 884 | return false; |
| 885 | |
| 886 | EVT PVT = VT; |
| 887 | // Consult target whether it is a good idea to promote this operation and |
| 888 | // what's the right type to promote it to. |
| 889 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 890 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 891 | |
| 892 | DebugLoc dl = Op.getDebugLoc(); |
| 893 | SDNode *N = Op.getNode(); |
| 894 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 895 | EVT MemVT = LD->getMemoryVT(); |
| 896 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 897 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
| 898 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 899 | : LD->getExtensionType(); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 900 | SDValue NewLD = DAG.getExtLoad(ExtType, PVT, dl, |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 901 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 902 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 903 | MemVT, LD->isVolatile(), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 904 | LD->isNonTemporal(), LD->getAlignment()); |
| 905 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD); |
| 906 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 907 | DEBUG(dbgs() << "\nPromoting "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 908 | N->dump(&DAG); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 909 | dbgs() << "\nTo: "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 910 | Result.getNode()->dump(&DAG); |
| 911 | dbgs() << '\n'); |
| 912 | WorkListRemover DeadNodes(*this); |
| 913 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result, &DeadNodes); |
| 914 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1), &DeadNodes); |
| 915 | removeFromWorkList(N); |
| 916 | DAG.DeleteNode(N); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 917 | AddToWorkList(Result.getNode()); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 918 | return true; |
| 919 | } |
| 920 | return false; |
| 921 | } |
| 922 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 923 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 924 | //===----------------------------------------------------------------------===// |
| 925 | // Main DAG Combiner implementation |
| 926 | //===----------------------------------------------------------------------===// |
| 927 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 928 | void DAGCombiner::Run(CombineLevel AtLevel) { |
| 929 | // set the instance variables, so that the various visit routines may use it. |
| 930 | Level = AtLevel; |
| 931 | LegalOperations = Level >= NoIllegalOperations; |
| 932 | LegalTypes = Level >= NoIllegalTypes; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 933 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 934 | // Add all the dag nodes to the worklist. |
| 935 | WorkList.reserve(DAG.allnodes_size()); |
| 936 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 937 | E = DAG.allnodes_end(); I != E; ++I) |
| 938 | WorkList.push_back(I); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 939 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 940 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 941 | // to the root node, preventing it from being deleted, and tracking any |
| 942 | // changes of the root. |
| 943 | HandleSDNode Dummy(DAG.getRoot()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 944 | |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 945 | // The root of the dag may dangle to deleted nodes until the dag combiner is |
| 946 | // done. Set it to null to avoid confusion. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 947 | DAG.setRoot(SDValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 948 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 949 | // while the worklist isn't empty, inspect the node on the end of it and |
| 950 | // try and combine it. |
| 951 | while (!WorkList.empty()) { |
| 952 | SDNode *N = WorkList.back(); |
| 953 | WorkList.pop_back(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 954 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 955 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
| 956 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 957 | // reduced number of uses, allowing other xforms. |
| 958 | if (N->use_empty() && N != &Dummy) { |
| 959 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 960 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 961 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 962 | DAG.DeleteNode(N); |
| 963 | continue; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 964 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 965 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 966 | SDValue RV = combine(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 967 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 968 | if (RV.getNode() == 0) |
| 969 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 970 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 971 | ++NodesCombined; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 972 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 973 | // If we get back the same node we passed in, rather than a new node or |
| 974 | // zero, we know that the node must have defined multiple values and |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 975 | // CombineTo was used. Since CombineTo takes care of the worklist |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 976 | // mechanics for us, we have no work to do in this case. |
| 977 | if (RV.getNode() == N) |
| 978 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 979 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 980 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 981 | RV.getNode()->getOpcode() != ISD::DELETED_NODE && |
| 982 | "Node was deleted but visit returned new node!"); |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 983 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 984 | DEBUG(dbgs() << "\nReplacing.3 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 985 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 986 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 987 | RV.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 988 | dbgs() << '\n'); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 989 | WorkListRemover DeadNodes(*this); |
| 990 | if (N->getNumValues() == RV.getNode()->getNumValues()) |
| 991 | DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes); |
| 992 | else { |
| 993 | assert(N->getValueType(0) == RV.getValueType() && |
| 994 | N->getNumValues() == 1 && "Type mismatch"); |
| 995 | SDValue OpV = RV; |
| 996 | DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes); |
| 997 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 998 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 999 | // Push the new node and any users onto the worklist |
| 1000 | AddToWorkList(RV.getNode()); |
| 1001 | AddUsersToWorkList(RV.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1002 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1003 | // Add any uses of the old node to the worklist in case this node is the |
| 1004 | // last one that uses them. They may become dead after this node is |
| 1005 | // deleted. |
| 1006 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1007 | AddToWorkList(N->getOperand(i).getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1008 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1009 | // Finally, if the node is now dead, remove it from the graph. The node |
| 1010 | // may not be dead if the replacement process recursively simplified to |
| 1011 | // something else needing this node. |
| 1012 | if (N->use_empty()) { |
| 1013 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 1014 | // process a node that has been replaced. |
| 1015 | removeFromWorkList(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1016 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1017 | // Finally, since the node is now dead, remove it from the graph. |
| 1018 | DAG.DeleteNode(N); |
| 1019 | } |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1020 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 1022 | // If the root changed (e.g. it was a dead load, update the root). |
| 1023 | DAG.setRoot(Dummy.getValue()); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1026 | SDValue DAGCombiner::visit(SDNode *N) { |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1027 | switch (N->getOpcode()) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1028 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 1029 | case ISD::TokenFactor: return visitTokenFactor(N); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1030 | case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1031 | case ISD::ADD: return visitADD(N); |
| 1032 | case ISD::SUB: return visitSUB(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1033 | case ISD::ADDC: return visitADDC(N); |
| 1034 | case ISD::ADDE: return visitADDE(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1035 | case ISD::MUL: return visitMUL(N); |
| 1036 | case ISD::SDIV: return visitSDIV(N); |
| 1037 | case ISD::UDIV: return visitUDIV(N); |
| 1038 | case ISD::SREM: return visitSREM(N); |
| 1039 | case ISD::UREM: return visitUREM(N); |
| 1040 | case ISD::MULHU: return visitMULHU(N); |
| 1041 | case ISD::MULHS: return visitMULHS(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1042 | case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); |
| 1043 | case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); |
| 1044 | case ISD::SDIVREM: return visitSDIVREM(N); |
| 1045 | case ISD::UDIVREM: return visitUDIVREM(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1046 | case ISD::AND: return visitAND(N); |
| 1047 | case ISD::OR: return visitOR(N); |
| 1048 | case ISD::XOR: return visitXOR(N); |
| 1049 | case ISD::SHL: return visitSHL(N); |
| 1050 | case ISD::SRA: return visitSRA(N); |
| 1051 | case ISD::SRL: return visitSRL(N); |
| 1052 | case ISD::CTLZ: return visitCTLZ(N); |
| 1053 | case ISD::CTTZ: return visitCTTZ(N); |
| 1054 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1055 | case ISD::SELECT: return visitSELECT(N); |
| 1056 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 1057 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1058 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 1059 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1060 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1061 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 1062 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1063 | case ISD::BITCAST: return visitBITCAST(N); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 1064 | case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1065 | case ISD::FADD: return visitFADD(N); |
| 1066 | case ISD::FSUB: return visitFSUB(N); |
| 1067 | case ISD::FMUL: return visitFMUL(N); |
| 1068 | case ISD::FDIV: return visitFDIV(N); |
| 1069 | case ISD::FREM: return visitFREM(N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 1070 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1071 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 1072 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 1073 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 1074 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 1075 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 1076 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 1077 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 1078 | case ISD::FNEG: return visitFNEG(N); |
| 1079 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1080 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1081 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 1082 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 1083 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 1084 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 1085 | case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1086 | case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); |
| 1087 | case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 1088 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 1089 | case ISD::MEMBARRIER: return visitMEMBARRIER(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1090 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1091 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1094 | SDValue DAGCombiner::combine(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1095 | SDValue RV = visit(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1096 | |
| 1097 | // If nothing happened, try a target-specific DAG combine. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1098 | if (RV.getNode() == 0) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1099 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1100 | "Node was deleted but visit returned NULL!"); |
| 1101 | |
| 1102 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 1103 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { |
| 1104 | |
| 1105 | // Expose the DAG combiner to the target combiner impls. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1106 | TargetLowering::DAGCombinerInfo |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 1107 | DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1108 | |
| 1109 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 1110 | } |
| 1111 | } |
| 1112 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1113 | // If nothing happened still, try promoting the operation. |
| 1114 | if (RV.getNode() == 0) { |
| 1115 | switch (N->getOpcode()) { |
| 1116 | default: break; |
| 1117 | case ISD::ADD: |
| 1118 | case ISD::SUB: |
| 1119 | case ISD::MUL: |
| 1120 | case ISD::AND: |
| 1121 | case ISD::OR: |
| 1122 | case ISD::XOR: |
| 1123 | RV = PromoteIntBinOp(SDValue(N, 0)); |
| 1124 | break; |
| 1125 | case ISD::SHL: |
| 1126 | case ISD::SRA: |
| 1127 | case ISD::SRL: |
| 1128 | RV = PromoteIntShiftOp(SDValue(N, 0)); |
| 1129 | break; |
| 1130 | case ISD::SIGN_EXTEND: |
| 1131 | case ISD::ZERO_EXTEND: |
| 1132 | case ISD::ANY_EXTEND: |
| 1133 | RV = PromoteExtend(SDValue(N, 0)); |
| 1134 | break; |
| 1135 | case ISD::LOAD: |
| 1136 | if (PromoteLoad(SDValue(N, 0))) |
| 1137 | RV = SDValue(N, 0); |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1142 | // 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] | 1143 | // sdisel CSE. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1144 | if (RV.getNode() == 0 && |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1145 | SelectionDAG::isCommutativeBinOp(N->getOpcode()) && |
| 1146 | N->getNumValues() == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1147 | SDValue N0 = N->getOperand(0); |
| 1148 | SDValue N1 = N->getOperand(1); |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1149 | |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1150 | // Constant operands are canonicalized to RHS. |
| 1151 | if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1152 | SDValue Ops[] = { N1, N0 }; |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1153 | SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), |
| 1154 | Ops, 2); |
Evan Cheng | ea10046 | 2008-03-24 23:55:16 +0000 | [diff] [blame] | 1155 | if (CSENode) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1156 | return SDValue(CSENode, 0); |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1160 | return RV; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1161 | } |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1163 | /// getInputChainForNode - Given a node, return its input chain if it has one, |
| 1164 | /// otherwise return a null sd operand. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1165 | static SDValue getInputChainForNode(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1166 | if (unsigned NumOps = N->getNumOperands()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1167 | if (N->getOperand(0).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1168 | return N->getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1169 | else if (N->getOperand(NumOps-1).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1170 | return N->getOperand(NumOps-1); |
| 1171 | for (unsigned i = 1; i < NumOps-1; ++i) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1172 | if (N->getOperand(i).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1173 | return N->getOperand(i); |
| 1174 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1175 | return SDValue(); |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1178 | SDValue DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1179 | // If N has two operands, where one has an input chain equal to the other, |
| 1180 | // the 'other' chain is redundant. |
| 1181 | if (N->getNumOperands() == 2) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1182 | if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1183 | return N->getOperand(0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1184 | if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1185 | return N->getOperand(1); |
| 1186 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1188 | SmallVector<SDNode *, 8> TFs; // List of token factors to visit. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1189 | SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1190 | SmallPtrSet<SDNode*, 16> SeenOps; |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1191 | bool Changed = false; // If we should replace this token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1192 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1193 | // Start out with this token factor. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1194 | TFs.push_back(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1195 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 1196 | // Iterate through token factors. The TFs grows when new token factors are |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1197 | // encountered. |
| 1198 | for (unsigned i = 0; i < TFs.size(); ++i) { |
| 1199 | SDNode *TF = TFs[i]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1200 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1201 | // Check each of the operands. |
| 1202 | for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1203 | SDValue Op = TF->getOperand(i); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1204 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1205 | switch (Op.getOpcode()) { |
| 1206 | case ISD::EntryToken: |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1207 | // Entry tokens don't need to be added to the list. They are |
| 1208 | // rededundant. |
| 1209 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1210 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1211 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1212 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 1213 | if (Op.hasOneUse() && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1214 | std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1215 | // Queue up for processing. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1216 | TFs.push_back(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1217 | // Clean up in case the token factor is removed. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1218 | AddToWorkList(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1219 | Changed = true; |
| 1220 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1221 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1222 | // Fall thru |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1223 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1224 | default: |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1225 | // Only add if it isn't already in the list. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1226 | if (SeenOps.insert(Op.getNode())) |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1227 | Ops.push_back(Op); |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1228 | else |
| 1229 | Changed = true; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1230 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1231 | } |
| 1232 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1233 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1234 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1235 | SDValue Result; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1236 | |
| 1237 | // If we've change things around then replace token factor. |
| 1238 | if (Changed) { |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1239 | if (Ops.empty()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1240 | // The entry token is the only possible outcome. |
| 1241 | Result = DAG.getEntryNode(); |
| 1242 | } else { |
| 1243 | // New and improved token factor. |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1244 | Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1245 | MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1246 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1247 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 1248 | // Don't add users to work list. |
| 1249 | return CombineTo(N, Result, false); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1250 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1251 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1252 | return Result; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1255 | /// MERGE_VALUES can always be eliminated. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1256 | SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1257 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1258 | // Replacing results may cause a different MERGE_VALUES to suddenly |
| 1259 | // be CSE'd with N, and carry its uses with it. Iterate until no |
| 1260 | // uses remain, to ensure that the node can be safely deleted. |
| 1261 | do { |
| 1262 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1263 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i), |
| 1264 | &DeadNodes); |
| 1265 | } while (!N->use_empty()); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1266 | removeFromWorkList(N); |
| 1267 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1268 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1271 | static |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1272 | SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1, |
| 1273 | SelectionDAG &DAG) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1274 | EVT VT = N0.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1275 | SDValue N00 = N0.getOperand(0); |
| 1276 | SDValue N01 = N0.getOperand(1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1277 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1278 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1279 | if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() && |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1280 | isa<ConstantSDNode>(N00.getOperand(1))) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1281 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
| 1282 | N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, |
| 1283 | DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT, |
| 1284 | N00.getOperand(0), N01), |
| 1285 | DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT, |
| 1286 | N00.getOperand(1), N01)); |
| 1287 | return DAG.getNode(ISD::ADD, DL, VT, N0, N1); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1288 | } |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1289 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1290 | return SDValue(); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1293 | SDValue DAGCombiner::visitADD(SDNode *N) { |
| 1294 | SDValue N0 = N->getOperand(0); |
| 1295 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1296 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1297 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1298 | EVT VT = N0.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1299 | |
| 1300 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1301 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1302 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1303 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1304 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1305 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1306 | // fold (add x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1307 | if (N0.getOpcode() == ISD::UNDEF) |
| 1308 | return N0; |
| 1309 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1310 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1311 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1312 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1313 | return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1314 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1315 | if (N0C && !N1C) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1316 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1317 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1318 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1319 | return N0; |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1320 | // fold (add Sym, c) -> Sym+c |
| 1321 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1322 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C && |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1323 | GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1324 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1325 | GA->getOffset() + |
| 1326 | (uint64_t)N1C->getSExtValue()); |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1327 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 1328 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 1329 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1330 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1331 | DAG.getConstant(N1C->getAPIntValue()+ |
| 1332 | N0C->getAPIntValue(), VT), |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1333 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1334 | // reassociate add |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1335 | SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1336 | if (RADD.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1337 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1338 | // fold ((0-A) + B) -> B-A |
| 1339 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 1340 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1341 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1342 | // fold (A + (0-B)) -> A-B |
| 1343 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 1344 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1345 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1346 | // fold (A+(B-A)) -> B |
| 1347 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1348 | return N1.getOperand(0); |
Dale Johannesen | 56eca91 | 2008-11-27 00:43:21 +0000 | [diff] [blame] | 1349 | // fold ((B-A)+A) -> B |
| 1350 | if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) |
| 1351 | return N0.getOperand(0); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1352 | // fold (A+(B-(A+C))) to (B-C) |
| 1353 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1354 | N0 == N1.getOperand(1).getOperand(0)) |
| 1355 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1356 | N1.getOperand(1).getOperand(1)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1357 | // fold (A+(B-(C+A))) to (B-C) |
| 1358 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1359 | N0 == N1.getOperand(1).getOperand(1)) |
| 1360 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1361 | N1.getOperand(1).getOperand(0)); |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1362 | // fold (A+((B-A)+or-C)) to (B+or-C) |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1363 | if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && |
| 1364 | N1.getOperand(0).getOpcode() == ISD::SUB && |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1365 | N0 == N1.getOperand(0).getOperand(1)) |
| 1366 | return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT, |
| 1367 | N1.getOperand(0).getOperand(0), N1.getOperand(1)); |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1368 | |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1369 | // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant |
| 1370 | if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { |
| 1371 | SDValue N00 = N0.getOperand(0); |
| 1372 | SDValue N01 = N0.getOperand(1); |
| 1373 | SDValue N10 = N1.getOperand(0); |
| 1374 | SDValue N11 = N1.getOperand(1); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) |
| 1377 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1378 | DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10), |
| 1379 | DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1380 | } |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1381 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1382 | if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) |
| 1383 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1385 | // fold (a+b) -> (a|b) iff a and b share no bits. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1386 | if (VT.isInteger() && !VT.isVector()) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1387 | APInt LHSZero, LHSOne; |
| 1388 | APInt RHSZero, RHSOne; |
Dan Gohman | 5b870af | 2010-03-02 02:14:38 +0000 | [diff] [blame] | 1389 | APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1390 | DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1391 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1392 | if (LHSZero.getBoolValue()) { |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1393 | DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1395 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1396 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| 1397 | if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) || |
| 1398 | (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask)) |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1399 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1400 | } |
| 1401 | } |
Evan Cheng | 3ef554d | 2006-11-06 08:14:30 +0000 | [diff] [blame] | 1402 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1403 | // 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] | 1404 | if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1405 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1406 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1407 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1408 | if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1409 | SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1410 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1413 | // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) |
| 1414 | if (N1.getOpcode() == ISD::SHL && |
| 1415 | N1.getOperand(0).getOpcode() == ISD::SUB) |
| 1416 | if (ConstantSDNode *C = |
| 1417 | dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0))) |
| 1418 | if (C->getAPIntValue() == 0) |
| 1419 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, |
| 1420 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1421 | N1.getOperand(0).getOperand(1), |
| 1422 | N1.getOperand(1))); |
| 1423 | if (N0.getOpcode() == ISD::SHL && |
| 1424 | N0.getOperand(0).getOpcode() == ISD::SUB) |
| 1425 | if (ConstantSDNode *C = |
| 1426 | dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0))) |
| 1427 | if (C->getAPIntValue() == 0) |
| 1428 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, |
| 1429 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1430 | N0.getOperand(0).getOperand(1), |
| 1431 | N0.getOperand(1))); |
| 1432 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1433 | if (N1.getOpcode() == ISD::AND) { |
| 1434 | SDValue AndOp0 = N1.getOperand(0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1435 | ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1)); |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1436 | unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); |
| 1437 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1438 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1439 | // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) |
| 1440 | // and similar xforms where the inner op is either ~0 or 0. |
| 1441 | if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) { |
| 1442 | DebugLoc DL = N->getDebugLoc(); |
| 1443 | return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Benjamin Kramer | f50125e | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1447 | // add (sext i1), X -> sub X, (zext i1) |
| 1448 | if (N0.getOpcode() == ISD::SIGN_EXTEND && |
| 1449 | N0.getOperand(0).getValueType() == MVT::i1 && |
| 1450 | !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { |
| 1451 | DebugLoc DL = N->getDebugLoc(); |
| 1452 | SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); |
| 1453 | return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); |
| 1454 | } |
| 1455 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1456 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1459 | SDValue DAGCombiner::visitADDC(SDNode *N) { |
| 1460 | SDValue N0 = N->getOperand(0); |
| 1461 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1462 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1463 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1464 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1466 | // If the flag result is dead, turn this into an ADD. |
| 1467 | if (N->hasNUsesOfValue(0, 1)) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1468 | return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1469 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1470 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1472 | // canonicalize constant to RHS. |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1473 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1474 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1476 | // fold (addc x, 0) -> x + no carry out |
| 1477 | if (N1C && N1C->isNullValue()) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1478 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1479 | N->getDebugLoc(), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1480 | |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1481 | // 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] | 1482 | APInt LHSZero, LHSOne; |
| 1483 | APInt RHSZero, RHSOne; |
Dan Gohman | 5b870af | 2010-03-02 02:14:38 +0000 | [diff] [blame] | 1484 | APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1485 | DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne); |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1486 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1487 | if (LHSZero.getBoolValue()) { |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1488 | DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1490 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1491 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| 1492 | if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) || |
| 1493 | (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask)) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1494 | return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1495 | DAG.getNode(ISD::CARRY_FALSE, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1496 | N->getDebugLoc(), MVT::Glue)); |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1497 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1498 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1499 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1502 | SDValue DAGCombiner::visitADDE(SDNode *N) { |
| 1503 | SDValue N0 = N->getOperand(0); |
| 1504 | SDValue N1 = N->getOperand(1); |
| 1505 | SDValue CarryIn = N->getOperand(2); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1506 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1507 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1508 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1509 | // canonicalize constant to RHS |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1510 | if (N0C && !N1C) |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1511 | return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(), |
| 1512 | N1, N0, CarryIn); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1514 | // fold (adde x, y, false) -> (addc x, y) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1515 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
| 1516 | return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1517 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1518 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1521 | SDValue DAGCombiner::visitSUB(SDNode *N) { |
| 1522 | SDValue N0 = N->getOperand(0); |
| 1523 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1524 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1525 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1526 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1527 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1528 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1529 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1530 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1531 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1532 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 1534 | // fold (sub x, x) -> 0 |
Evan Cheng | c8e3b14 | 2008-03-12 07:02:50 +0000 | [diff] [blame] | 1535 | if (N0 == N1) |
Nadav Rotem | 5a4552c | 2011-02-11 19:20:37 +0000 | [diff] [blame] | 1536 | return DAG.getConstant(0, N->getValueType(0), LegalTypes); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1537 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1538 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1539 | return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 1540 | // fold (sub x, c) -> (add x, -c) |
| 1541 | if (N1C) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1542 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1543 | DAG.getConstant(-N1C->getAPIntValue(), VT)); |
Evan Cheng | 1ad0e8b | 2010-01-18 21:38:44 +0000 | [diff] [blame] | 1544 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) |
| 1545 | if (N0C && N0C->isAllOnesValue()) |
| 1546 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Benjamin Kramer | 2c94b42 | 2011-01-29 12:34:05 +0000 | [diff] [blame] | 1547 | // fold A-(A-B) -> B |
| 1548 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) |
| 1549 | return N1.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1550 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1551 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1552 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1553 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1554 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1555 | return N0.getOperand(0); |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1556 | // fold ((A+(B+or-C))-B) -> A+or-C |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1557 | if (N0.getOpcode() == ISD::ADD && |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1558 | (N0.getOperand(1).getOpcode() == ISD::SUB || |
| 1559 | N0.getOperand(1).getOpcode() == ISD::ADD) && |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1560 | N0.getOperand(1).getOperand(0) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1561 | return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT, |
| 1562 | N0.getOperand(0), N0.getOperand(1).getOperand(1)); |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1563 | // fold ((A+(C+B))-B) -> A+C |
| 1564 | if (N0.getOpcode() == ISD::ADD && |
| 1565 | N0.getOperand(1).getOpcode() == ISD::ADD && |
| 1566 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1567 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1568 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Dale Johannesen | 58e39b0 | 2008-12-23 01:59:54 +0000 | [diff] [blame] | 1569 | // fold ((A-(B-C))-C) -> A-B |
| 1570 | if (N0.getOpcode() == ISD::SUB && |
| 1571 | N0.getOperand(1).getOpcode() == ISD::SUB && |
| 1572 | N0.getOperand(1).getOperand(1) == N1) |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1573 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1574 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1575 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1576 | // If either operand of a sub is undef, the result is undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1577 | if (N0.getOpcode() == ISD::UNDEF) |
| 1578 | return N0; |
| 1579 | if (N1.getOpcode() == ISD::UNDEF) |
| 1580 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1581 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1582 | // If the relocation model supports it, consider symbol offsets. |
| 1583 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1584 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1585 | // fold (sub Sym, c) -> Sym-c |
| 1586 | if (N1C && GA->getOpcode() == ISD::GlobalAddress) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 1587 | return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1588 | GA->getOffset() - |
| 1589 | (uint64_t)N1C->getSExtValue()); |
| 1590 | // fold (sub Sym+c1, Sym+c2) -> c1-c2 |
| 1591 | if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) |
| 1592 | if (GA->getGlobal() == GB->getGlobal()) |
| 1593 | return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), |
| 1594 | VT); |
| 1595 | } |
| 1596 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1597 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1600 | SDValue DAGCombiner::visitMUL(SDNode *N) { |
| 1601 | SDValue N0 = N->getOperand(0); |
| 1602 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1603 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1604 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1605 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1606 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1607 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1608 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1609 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1610 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1611 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1612 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1613 | // fold (mul x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1614 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1615 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1616 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1617 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1618 | return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1619 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1620 | if (N0C && !N1C) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1621 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1622 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1623 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1624 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1625 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1626 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1627 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1628 | DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1629 | // fold (mul x, (1 << c)) -> x << c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1630 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1631 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1632 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1633 | getShiftAmountTy())); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1634 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1635 | if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) { |
| 1636 | unsigned Log2Val = (-N1C->getAPIntValue()).logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1637 | // 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] | 1638 | // single-use add), we should put the negate there. |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1639 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1640 | DAG.getConstant(0, VT), |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1641 | DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1642 | DAG.getConstant(Log2Val, getShiftAmountTy()))); |
| 1643 | } |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1644 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1645 | if (N1C && N0.getOpcode() == ISD::SHL && |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1646 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1647 | SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1648 | N1, N0.getOperand(1)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1649 | AddToWorkList(C3.getNode()); |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1650 | return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1651 | N0.getOperand(0), C3); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1652 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1653 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1654 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 1655 | // use. |
| 1656 | { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1657 | SDValue Sh(0,0), Y(0,0); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1658 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| 1659 | if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1660 | N0.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1661 | Sh = N0; Y = N1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1662 | } else if (N1.getOpcode() == ISD::SHL && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 1663 | isa<ConstantSDNode>(N1.getOperand(1)) && |
| 1664 | N1.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1665 | Sh = N1; Y = N0; |
| 1666 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1667 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1668 | if (Sh.getNode()) { |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1669 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1670 | Sh.getOperand(0), Y); |
| 1671 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, |
| 1672 | Mul, Sh.getOperand(1)); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1673 | } |
| 1674 | } |
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 1676 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1677 | if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1678 | isa<ConstantSDNode>(N0.getOperand(1))) |
| 1679 | return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, |
| 1680 | DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT, |
| 1681 | N0.getOperand(0), N1), |
| 1682 | DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT, |
| 1683 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1684 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1685 | // reassociate mul |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 1686 | SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1687 | if (RMUL.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1688 | return RMUL; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1689 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1690 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1693 | SDValue DAGCombiner::visitSDIV(SDNode *N) { |
| 1694 | SDValue N0 = N->getOperand(0); |
| 1695 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1696 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1697 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1698 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1699 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1700 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1701 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1702 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1703 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1704 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1705 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1706 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1707 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1708 | return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1709 | // fold (sdiv X, 1) -> X |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 1710 | if (N1C && N1C->getSExtValue() == 1LL) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1711 | return N0; |
| 1712 | // fold (sdiv X, -1) -> 0-X |
| 1713 | if (N1C && N1C->isAllOnesValue()) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1714 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1715 | DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 1716 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1717 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1718 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1719 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1720 | return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(), |
| 1721 | N0, N1); |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 1722 | } |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 1723 | // fold (sdiv X, pow2) -> simple ops after legalize |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1724 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() && |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1725 | (isPowerOf2_64(N1C->getSExtValue()) || |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 1726 | isPowerOf2_64(-N1C->getSExtValue()))) { |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1727 | // If dividing by powers of two is cheap, then don't perform the following |
| 1728 | // fold. |
| 1729 | if (TLI.isPow2DivCheap()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1730 | return SDValue(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1731 | |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 1732 | int64_t pow2 = N1C->getSExtValue(); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1733 | int64_t abs2 = pow2 > 0 ? pow2 : -pow2; |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1734 | unsigned lg2 = Log2_64(abs2); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1736 | // Splat the sign bit into the register |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1737 | SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
| 1738 | DAG.getConstant(VT.getSizeInBits()-1, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1739 | getShiftAmountTy())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1740 | AddToWorkList(SGN.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1742 | // Add (N0 < 0) ? abs2 - 1 : 0; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1743 | SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN, |
| 1744 | DAG.getConstant(VT.getSizeInBits() - lg2, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1745 | getShiftAmountTy())); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1746 | SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1747 | AddToWorkList(SRL.getNode()); |
| 1748 | AddToWorkList(ADD.getNode()); // Divide by pow2 |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1749 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1750 | DAG.getConstant(lg2, getShiftAmountTy())); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1751 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1752 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 1753 | // negate the result. |
| 1754 | if (pow2 > 0) |
| 1755 | return SRA; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1756 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1757 | AddToWorkList(SRA.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1758 | return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, |
| 1759 | DAG.getConstant(0, VT), SRA); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1760 | } |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1761 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1762 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 1763 | // alternate sequence. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1764 | if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) && |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 1765 | !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1766 | SDValue Op = BuildSDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1767 | if (Op.getNode()) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1768 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1769 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1770 | // undef / X -> 0 |
| 1771 | if (N0.getOpcode() == ISD::UNDEF) |
| 1772 | return DAG.getConstant(0, VT); |
| 1773 | // X / undef -> undef |
| 1774 | if (N1.getOpcode() == ISD::UNDEF) |
| 1775 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1776 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1777 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1780 | SDValue DAGCombiner::visitUDIV(SDNode *N) { |
| 1781 | SDValue N0 = N->getOperand(0); |
| 1782 | SDValue N1 = N->getOperand(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1783 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1784 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1785 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1786 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1787 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1788 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1789 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1790 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1791 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1792 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1793 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1794 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1795 | return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1796 | // fold (udiv x, (1 << c)) -> x >>u c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1797 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1798 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1799 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1800 | getShiftAmountTy())); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1801 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
| 1802 | if (N1.getOpcode() == ISD::SHL) { |
| 1803 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1804 | if (SHC->getAPIntValue().isPowerOf2()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1805 | EVT ADDVT = N1.getOperand(1).getValueType(); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1806 | SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT, |
| 1807 | N1.getOperand(1), |
| 1808 | DAG.getConstant(SHC->getAPIntValue() |
| 1809 | .logBase2(), |
| 1810 | ADDVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1811 | AddToWorkList(Add.getNode()); |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 1812 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1816 | // fold (udiv x, c) -> alternate |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1817 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1818 | SDValue Op = BuildUDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1819 | if (Op.getNode()) return Op; |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 1820 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1821 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1822 | // undef / X -> 0 |
| 1823 | if (N0.getOpcode() == ISD::UNDEF) |
| 1824 | return DAG.getConstant(0, VT); |
| 1825 | // X / undef -> undef |
| 1826 | if (N1.getOpcode() == ISD::UNDEF) |
| 1827 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1828 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1829 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1832 | SDValue DAGCombiner::visitSREM(SDNode *N) { |
| 1833 | SDValue N0 = N->getOperand(0); |
| 1834 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1835 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1836 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1837 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1838 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1839 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1840 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1841 | return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1842 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1843 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1844 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1845 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1846 | return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | ee339f4 | 2008-01-27 23:21:58 +0000 | [diff] [blame] | 1847 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1848 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1849 | // If X/C can be simplified by the division-by-constant logic, lower |
| 1850 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1851 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1852 | SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1853 | AddToWorkList(Div.getNode()); |
| 1854 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 1855 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1856 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1857 | OptimizedDiv, N1); |
| 1858 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1859 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1860 | return Sub; |
| 1861 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1862 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1863 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1864 | // undef % X -> 0 |
| 1865 | if (N0.getOpcode() == ISD::UNDEF) |
| 1866 | return DAG.getConstant(0, VT); |
| 1867 | // X % undef -> undef |
| 1868 | if (N1.getOpcode() == ISD::UNDEF) |
| 1869 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1870 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1871 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1874 | SDValue DAGCombiner::visitUREM(SDNode *N) { |
| 1875 | SDValue N0 = N->getOperand(0); |
| 1876 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1877 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1878 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1879 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1880 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1881 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1882 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1883 | return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1884 | // fold (urem x, pow2) -> (and x, pow2-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1885 | if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1886 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1887 | DAG.getConstant(N1C->getAPIntValue()-1,VT)); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 1888 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 1889 | if (N1.getOpcode() == ISD::SHL) { |
| 1890 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1891 | if (SHC->getAPIntValue().isPowerOf2()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1892 | SDValue Add = |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1893 | DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1894 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1895 | VT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1896 | AddToWorkList(Add.getNode()); |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1897 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1901 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1902 | // If X/C can be simplified by the division-by-constant logic, lower |
| 1903 | // X%C to the equivalent of X-X/C*C. |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1904 | if (N1C && !N1C->isNullValue()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1905 | SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 942ca7f | 2008-09-08 16:59:01 +0000 | [diff] [blame] | 1906 | AddToWorkList(Div.getNode()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1907 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 1908 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 1909 | SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, |
| 1910 | OptimizedDiv, N1); |
| 1911 | SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1912 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 1913 | return Sub; |
| 1914 | } |
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 1915 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1916 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1917 | // undef % X -> 0 |
| 1918 | if (N0.getOpcode() == ISD::UNDEF) |
| 1919 | return DAG.getConstant(0, VT); |
| 1920 | // X % undef -> undef |
| 1921 | if (N1.getOpcode() == ISD::UNDEF) |
| 1922 | return N1; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1923 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1924 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1927 | SDValue DAGCombiner::visitMULHS(SDNode *N) { |
| 1928 | SDValue N0 = N->getOperand(0); |
| 1929 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1930 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1931 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 1932 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1933 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1934 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1935 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1936 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1937 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1938 | if (N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 326411d | 2009-01-30 03:00:18 +0000 | [diff] [blame] | 1939 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0, |
| 1940 | DAG.getConstant(N0.getValueType().getSizeInBits() - 1, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1941 | getShiftAmountTy())); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1942 | // fold (mulhs x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1943 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1944 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1945 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 1946 | // If the type twice as wide is legal, transform the mulhs to a wider multiply |
| 1947 | // plus a shift. |
| 1948 | if (VT.isSimple() && !VT.isVector()) { |
| 1949 | MVT Simple = VT.getSimpleVT(); |
| 1950 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 1951 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 1952 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 1953 | N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); |
| 1954 | N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); |
| 1955 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
Chris Lattner | 1a0fbe2 | 2010-12-15 05:51:39 +0000 | [diff] [blame] | 1956 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 1957 | DAG.getConstant(SimpleSize, getShiftAmountTy())); |
| 1958 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 1959 | } |
| 1960 | } |
| 1961 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1962 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1965 | SDValue DAGCombiner::visitMULHU(SDNode *N) { |
| 1966 | SDValue N0 = N->getOperand(0); |
| 1967 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1968 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1969 | EVT VT = N->getValueType(0); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 1970 | DebugLoc DL = N->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1971 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1972 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1973 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1974 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1975 | // fold (mulhu x, 1) -> 0 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1976 | if (N1C && N1C->getAPIntValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1977 | return DAG.getConstant(0, N0.getValueType()); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1978 | // fold (mulhu x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1979 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1980 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1981 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 1982 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 1983 | // plus a shift. |
| 1984 | if (VT.isSimple() && !VT.isVector()) { |
| 1985 | MVT Simple = VT.getSimpleVT(); |
| 1986 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 1987 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 1988 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 1989 | N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); |
| 1990 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); |
| 1991 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
| 1992 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
| 1993 | DAG.getConstant(SimpleSize, getShiftAmountTy())); |
| 1994 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 1995 | } |
| 1996 | } |
| 1997 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1998 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2001 | /// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that |
| 2002 | /// compute two values. LoOp and HiOp give the opcodes for the two computations |
| 2003 | /// that are being performed. Return true if a simplification was made. |
| 2004 | /// |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2005 | SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2006 | unsigned HiOp) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2007 | // If the high half is not needed, just compute the low half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2008 | bool HiExists = N->hasAnyUseOfValue(1); |
| 2009 | if (!HiExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2010 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2011 | TLI.isOperationLegal(LoOp, N->getValueType(0)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2012 | SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2013 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2014 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | // If the low half is not needed, just compute the high half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2018 | bool LoExists = N->hasAnyUseOfValue(0); |
| 2019 | if (!LoExists && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2020 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2021 | TLI.isOperationLegal(HiOp, N->getValueType(1)))) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2022 | SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
| 2023 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2024 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2027 | // If both halves are used, return as it is. |
| 2028 | if (LoExists && HiExists) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2029 | return SDValue(); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2030 | |
| 2031 | // If the two computed results can be simplified separately, separate them. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2032 | if (LoExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2033 | SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0), |
| 2034 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2035 | AddToWorkList(Lo.getNode()); |
| 2036 | SDValue LoOpt = combine(Lo.getNode()); |
| 2037 | if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2038 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2039 | TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2040 | return CombineTo(N, LoOpt, LoOpt); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2043 | if (HiExists) { |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2044 | SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2045 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2046 | AddToWorkList(Hi.getNode()); |
| 2047 | SDValue HiOpt = combine(Hi.getNode()); |
| 2048 | if (HiOpt.getNode() && HiOpt != Hi && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2049 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2050 | TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2051 | return CombineTo(N, HiOpt, HiOpt); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2052 | } |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2053 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2054 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2057 | SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { |
| 2058 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2059 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2061 | EVT VT = N->getValueType(0); |
| 2062 | DebugLoc DL = N->getDebugLoc(); |
| 2063 | |
| 2064 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2065 | // plus a shift. |
| 2066 | if (VT.isSimple() && !VT.isVector()) { |
| 2067 | MVT Simple = VT.getSimpleVT(); |
| 2068 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2069 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2070 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2071 | SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2072 | SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2073 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2074 | // Compute the high part as N1. |
| 2075 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
| 2076 | DAG.getConstant(SimpleSize, getShiftAmountTy())); |
| 2077 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2078 | // Compute the low part as N0. |
| 2079 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2080 | return CombineTo(N, Lo, Hi); |
| 2081 | } |
| 2082 | } |
| 2083 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2084 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2087 | SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { |
| 2088 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2089 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2090 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2091 | EVT VT = N->getValueType(0); |
| 2092 | DebugLoc DL = N->getDebugLoc(); |
| 2093 | |
| 2094 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2095 | // plus a shift. |
| 2096 | if (VT.isSimple() && !VT.isVector()) { |
| 2097 | MVT Simple = VT.getSimpleVT(); |
| 2098 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2099 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2100 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2101 | SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2102 | SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2103 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2104 | // Compute the high part as N1. |
| 2105 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
| 2106 | DAG.getConstant(SimpleSize, getShiftAmountTy())); |
| 2107 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2108 | // Compute the low part as N0. |
| 2109 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2110 | return CombineTo(N, Lo, Hi); |
| 2111 | } |
| 2112 | } |
| 2113 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2114 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2117 | SDValue DAGCombiner::visitSDIVREM(SDNode *N) { |
| 2118 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2119 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2120 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2121 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2124 | SDValue DAGCombiner::visitUDIVREM(SDNode *N) { |
| 2125 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2126 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2127 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2128 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2131 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 2132 | /// two operands of the same opcode, try to simplify it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2133 | SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 2134 | SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2135 | EVT VT = N0.getValueType(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2136 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2137 | |
Dan Gohman | ff00a55 | 2010-01-14 03:08:49 +0000 | [diff] [blame] | 2138 | // Bail early if none of these transforms apply. |
| 2139 | if (N0.getNode()->getNumOperands() == 0) return SDValue(); |
| 2140 | |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 2141 | // For each of OP in AND/OR/XOR: |
| 2142 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 2143 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 2144 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2145 | // 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] | 2146 | // |
| 2147 | // do not sink logical op inside of a vector extend, since it may combine |
| 2148 | // into a vsetcc. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2149 | EVT Op0VT = N0.getOperand(0).getValueType(); |
| 2150 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 2151 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 2152 | // Avoid infinite looping with PromoteIntBinOp. |
| 2153 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2154 | (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2155 | (N0.getOpcode() == ISD::TRUNCATE && |
| 2156 | (!TLI.isZExtFree(VT, Op0VT) || |
| 2157 | !TLI.isTruncateFree(Op0VT, VT)) && |
| 2158 | TLI.isTypeLegal(Op0VT))) && |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2159 | !VT.isVector() && |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2160 | Op0VT == N1.getOperand(0).getValueType() && |
| 2161 | (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2162 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2163 | N0.getOperand(0).getValueType(), |
| 2164 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2165 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2166 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2167 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2169 | // For each of OP in SHL/SRL/SRA/AND... |
| 2170 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 2171 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 2172 | // 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] | 2173 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2174 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2175 | N0.getOperand(1) == N1.getOperand(1)) { |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2176 | SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), |
| 2177 | N0.getOperand(0).getValueType(), |
| 2178 | N0.getOperand(0), N1.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2179 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2180 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 2181 | ORNode, N0.getOperand(1)); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2182 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2183 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2184 | return SDValue(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2187 | SDValue DAGCombiner::visitAND(SDNode *N) { |
| 2188 | SDValue N0 = N->getOperand(0); |
| 2189 | SDValue N1 = N->getOperand(1); |
| 2190 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2191 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2192 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2193 | EVT VT = N1.getValueType(); |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2194 | unsigned BitWidth = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2195 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2196 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2197 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2198 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2199 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2200 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2201 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2202 | // fold (and x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2203 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2204 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2205 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2206 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2207 | return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2208 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2209 | if (N0C && !N1C) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 2210 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2211 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2212 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2213 | return N0; |
| 2214 | // if (and x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2215 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2216 | APInt::getAllOnesValue(BitWidth))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2217 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2218 | // reassociate and |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 2219 | SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2220 | if (RAND.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2221 | return RAND; |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2222 | // fold (and (or x, C), D) -> D if (C & D) == D |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 2223 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2224 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2225 | if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2226 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2227 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 2228 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2229 | SDValue N0Op0 = N0.getOperand(0); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2230 | APInt Mask = ~N1C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2231 | Mask = Mask.trunc(N0Op0.getValueSizeInBits()); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2232 | if (DAG.MaskedValueIsZero(N0Op0, Mask)) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2233 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), |
| 2234 | N0.getValueType(), N0Op0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2235 | |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 2236 | // Replace uses of the AND with uses of the Zero extend node. |
| 2237 | CombineTo(N, Zext); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2239 | // We actually want to replace all uses of the any_extend with the |
| 2240 | // zero_extend, to avoid duplicating things. This will later cause this |
| 2241 | // AND to be folded. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2242 | CombineTo(N0.getNode(), Zext); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2243 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2246 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 2247 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 2248 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 2249 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2250 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2251 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2252 | LL.getValueType().isInteger()) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2253 | // 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] | 2254 | if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2255 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2256 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2257 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2258 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2259 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2260 | // 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] | 2261 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2262 | SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(), |
| 2263 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2264 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2265 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2266 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2267 | // 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] | 2268 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2269 | SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(), |
| 2270 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2271 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2272 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | // canonicalize equivalent to ll == rl |
| 2276 | if (LL == RR && LR == RL) { |
| 2277 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 2278 | std::swap(RL, RR); |
| 2279 | } |
| 2280 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2281 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2282 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2283 | if (Result != ISD::SETCC_INVALID && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2284 | (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType()))) |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2285 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 2286 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2287 | } |
| 2288 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2289 | |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2290 | // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2291 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2292 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2293 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2294 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2295 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 2296 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 2297 | // fold (and (sra)) -> (and (srl)) when possible. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2298 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2299 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2300 | return SDValue(N, 0); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2301 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2302 | // fold (zext_inreg (extload x)) -> (zextload x) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2303 | if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2304 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2305 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2306 | // If we zero all the possible extended bits, then we can turn this into |
| 2307 | // 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] | 2308 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2309 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2310 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2311 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2312 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 2313 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getDebugLoc(), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2314 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2315 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2316 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2317 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2318 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2319 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2320 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2324 | if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 2325 | N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2326 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2327 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2328 | // If we zero all the possible extended bits, then we can turn this into |
| 2329 | // 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] | 2330 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2331 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2332 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2333 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2334 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 2335 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getDebugLoc(), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2336 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2337 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 2338 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2339 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 2340 | LN0->getAlignment()); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2341 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2342 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2343 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2344 | } |
| 2345 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2346 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 2347 | // fold (and (load x), 255) -> (zextload x, i8) |
| 2348 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2349 | // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) |
| 2350 | if (N1C && (N0.getOpcode() == ISD::LOAD || |
| 2351 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2352 | N0.getOperand(0).getOpcode() == ISD::LOAD))) { |
| 2353 | bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; |
| 2354 | LoadSDNode *LN0 = HasAnyExt |
| 2355 | ? cast<LoadSDNode>(N0.getOperand(0)) |
| 2356 | : cast<LoadSDNode>(N0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2357 | if (LN0->getExtensionType() != ISD::SEXTLOAD && |
Chris Lattner | bd1fccf | 2010-01-07 21:59:23 +0000 | [diff] [blame] | 2358 | LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) { |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2359 | uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits(); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2360 | if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){ |
| 2361 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); |
| 2362 | EVT LoadedVT = LN0->getMemoryVT(); |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2363 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2364 | if (ExtVT == LoadedVT && |
| 2365 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2366 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2367 | |
| 2368 | SDValue NewLoad = |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 2369 | DAG.getExtLoad(ISD::ZEXTLOAD, LoadResultTy, LN0->getDebugLoc(), |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2370 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2371 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2372 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2373 | LN0->getAlignment()); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2374 | AddToWorkList(N); |
| 2375 | CombineTo(LN0, NewLoad, NewLoad.getValue(1)); |
| 2376 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2377 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2378 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2379 | // Do not change the width of a volatile load. |
| 2380 | // Do not generate loads of non-round integer types since these can |
| 2381 | // be expensive (and would be wrong if the type is not byte sized). |
| 2382 | if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() && |
| 2383 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
| 2384 | EVT PtrType = LN0->getOperand(1).getValueType(); |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2385 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2386 | unsigned Alignment = LN0->getAlignment(); |
| 2387 | SDValue NewPtr = LN0->getBasePtr(); |
| 2388 | |
| 2389 | // For big endian targets, we need to add an offset to the pointer |
| 2390 | // to load the correct bytes. For little endian systems, we merely |
| 2391 | // need to read fewer bytes from the same pointer. |
| 2392 | if (TLI.isBigEndian()) { |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2393 | unsigned LVTStoreBytes = LoadedVT.getStoreSize(); |
| 2394 | unsigned EVTStoreBytes = ExtVT.getStoreSize(); |
| 2395 | unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2396 | NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType, |
| 2397 | NewPtr, DAG.getConstant(PtrOff, PtrType)); |
| 2398 | Alignment = MinAlign(Alignment, PtrOff); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2399 | } |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2400 | |
| 2401 | AddToWorkList(NewPtr.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2403 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
| 2404 | SDValue Load = |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 2405 | DAG.getExtLoad(ISD::ZEXTLOAD, LoadResultTy, LN0->getDebugLoc(), |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2406 | LN0->getChain(), NewPtr, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2407 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2408 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 2409 | Alignment); |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2410 | AddToWorkList(N); |
| 2411 | CombineTo(LN0, Load, Load.getValue(1)); |
| 2412 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2413 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2414 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 2415 | } |
| 2416 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2417 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2418 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2421 | SDValue DAGCombiner::visitOR(SDNode *N) { |
| 2422 | SDValue N0 = N->getOperand(0); |
| 2423 | SDValue N1 = N->getOperand(1); |
| 2424 | SDValue LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2425 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2426 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2427 | EVT VT = N1.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2428 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2429 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2430 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2431 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2432 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2433 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2434 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2435 | // fold (or x, undef) -> -1 |
Bob Wilson | 8674949 | 2010-06-28 23:40:25 +0000 | [diff] [blame] | 2436 | if (!LegalOperations && |
| 2437 | (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) { |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2438 | EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT; |
| 2439 | return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); |
| 2440 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2441 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2442 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2443 | return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2444 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2445 | if (N0C && !N1C) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2446 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2447 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2448 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2449 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2450 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2451 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2452 | return N1; |
| 2453 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2454 | if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2455 | return N1; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2456 | // reassociate or |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 2457 | SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2458 | if (ROR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2459 | return ROR; |
| 2460 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2461 | // iff (c1 & c2) == 0. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2462 | if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 2463 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 2464 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
Bill Wendling | 32f9eb2 | 2010-03-03 01:58:01 +0000 | [diff] [blame] | 2465 | if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2466 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 2467 | DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 2468 | N0.getOperand(0), N1), |
| 2469 | DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 2470 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2471 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 2472 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 2473 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 2474 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2475 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2476 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2477 | LL.getValueType().isInteger()) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2478 | // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) |
| 2479 | // 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] | 2480 | if (cast<ConstantSDNode>(LR)->isNullValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2481 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2482 | SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(), |
| 2483 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2484 | AddToWorkList(ORNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2485 | return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2486 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2487 | // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) |
| 2488 | // 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] | 2489 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2490 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2491 | SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(), |
| 2492 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2493 | AddToWorkList(ANDNode.getNode()); |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2494 | return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | // canonicalize equivalent to ll == rl |
| 2498 | if (LL == RR && LR == RL) { |
| 2499 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 2500 | std::swap(RL, RR); |
| 2501 | } |
| 2502 | if (LL == RL && LR == RR) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2503 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2504 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2505 | if (Result != ISD::SETCC_INVALID && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2506 | (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType()))) |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2507 | return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(), |
| 2508 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2511 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2512 | // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2513 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2514 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2515 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2516 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2517 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2518 | // (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] | 2519 | if (N0.getOpcode() == ISD::AND && |
| 2520 | N1.getOpcode() == ISD::AND && |
| 2521 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 2522 | N1.getOperand(1).getOpcode() == ISD::Constant && |
| 2523 | // Don't increase # computations. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2524 | (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 2525 | // We can only do this xform if we know that bits from X that are set in C2 |
| 2526 | // but not in C1 are already zero. Likewise for Y. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2527 | const APInt &LHSMask = |
| 2528 | cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 2529 | const APInt &RHSMask = |
| 2530 | cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2531 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2532 | if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
| 2533 | DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2534 | SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT, |
| 2535 | N0.getOperand(0), N1.getOperand(0)); |
| 2536 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X, |
| 2537 | DAG.getConstant(LHSMask | RHSMask, VT)); |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2540 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2541 | // See if this is some rotate idiom. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2542 | if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2543 | return SDValue(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2544 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2545 | // Simplify the operands using demanded-bits information. |
| 2546 | if (!VT.isVector() && |
| 2547 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2548 | return SDValue(N, 0); |
| 2549 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2550 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2553 | /// 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] | 2554 | static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2555 | if (Op.getOpcode() == ISD::AND) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 2556 | if (isa<ConstantSDNode>(Op.getOperand(1))) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2557 | Mask = Op.getOperand(1); |
| 2558 | Op = Op.getOperand(0); |
| 2559 | } else { |
| 2560 | return false; |
| 2561 | } |
| 2562 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2564 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 2565 | Shift = Op; |
| 2566 | return true; |
| 2567 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 2568 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2569 | return false; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2572 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 2573 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 2574 | // a rot[lr]. |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2575 | SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2576 | // 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] | 2577 | EVT VT = LHS.getValueType(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2578 | if (!TLI.isTypeLegal(VT)) return 0; |
| 2579 | |
| 2580 | // The target must have at least one rotate flavor. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2581 | bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); |
| 2582 | bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2583 | if (!HasROTL && !HasROTR) return 0; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2584 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2585 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2586 | SDValue LHSShift; // The shift. |
| 2587 | SDValue LHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2588 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 2589 | return 0; // Not part of a rotate. |
| 2590 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2591 | SDValue RHSShift; // The shift. |
| 2592 | SDValue RHSMask; // AND value if any. |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2593 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 2594 | return 0; // Not part of a rotate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2596 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 2597 | return 0; // Not shifting the same value. |
| 2598 | |
| 2599 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 2600 | return 0; // Shifts must disagree. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2601 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2602 | // Canonicalize shl to left side in a shl/srl pair. |
| 2603 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 2604 | std::swap(LHS, RHS); |
| 2605 | std::swap(LHSShift, RHSShift); |
| 2606 | std::swap(LHSMask , RHSMask ); |
| 2607 | } |
| 2608 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2609 | unsigned OpSizeInBits = VT.getSizeInBits(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2610 | SDValue LHSShiftArg = LHSShift.getOperand(0); |
| 2611 | SDValue LHSShiftAmt = LHSShift.getOperand(1); |
| 2612 | SDValue RHSShiftAmt = RHSShift.getOperand(1); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2613 | |
| 2614 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 2615 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2616 | if (LHSShiftAmt.getOpcode() == ISD::Constant && |
| 2617 | RHSShiftAmt.getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2618 | uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue(); |
| 2619 | uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2620 | if ((LShVal + RShVal) != OpSizeInBits) |
| 2621 | return 0; |
| 2622 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2623 | SDValue Rot; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2624 | if (HasROTL) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2625 | Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2626 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2627 | Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2629 | // 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] | 2630 | if (LHSMask.getNode() || RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 2631 | APInt Mask = APInt::getAllOnesValue(OpSizeInBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2632 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2633 | if (LHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 2634 | APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal); |
| 2635 | Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2636 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2637 | if (RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 2638 | APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal); |
| 2639 | Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2640 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2641 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2642 | Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT)); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2643 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2644 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2645 | return Rot.getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2646 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2648 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 2649 | // that we're masking out the right stuff. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2650 | if (LHSMask.getNode() || RHSMask.getNode()) |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2651 | return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2653 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 2654 | // 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] | 2655 | if (RHSShiftAmt.getOpcode() == ISD::SUB && |
| 2656 | LHSShiftAmt == RHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2657 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2658 | dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2659 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2660 | if (HasROTL) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2661 | return DAG.getNode(ISD::ROTL, DL, VT, |
| 2662 | LHSShiftArg, LHSShiftAmt).getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2663 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2664 | return DAG.getNode(ISD::ROTR, DL, VT, |
| 2665 | LHSShiftArg, RHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 2666 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2669 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2670 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 2671 | // 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] | 2672 | if (LHSShiftAmt.getOpcode() == ISD::SUB && |
| 2673 | RHSShiftAmt == LHSShiftAmt.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2674 | if (ConstantSDNode *SUBC = |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2675 | dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2676 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 2692d59 | 2008-08-31 01:13:31 +0000 | [diff] [blame] | 2677 | if (HasROTR) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2678 | return DAG.getNode(ISD::ROTR, DL, VT, |
| 2679 | LHSShiftArg, RHSShiftAmt).getNode(); |
Bill Wendling | 2692d59 | 2008-08-31 01:13:31 +0000 | [diff] [blame] | 2680 | else |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2681 | return DAG.getNode(ISD::ROTL, DL, VT, |
| 2682 | LHSShiftArg, LHSShiftAmt).getNode(); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 2683 | } |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2684 | } |
| 2685 | } |
| 2686 | |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 2687 | // Look for sign/zext/any-extended or truncate cases: |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2688 | if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND |
| 2689 | || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 2690 | || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND |
| 2691 | || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2692 | (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND |
| 2693 | || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 2694 | || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND |
| 2695 | || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2696 | SDValue LExtOp0 = LHSShiftAmt.getOperand(0); |
| 2697 | SDValue RExtOp0 = RHSShiftAmt.getOperand(0); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2698 | if (RExtOp0.getOpcode() == ISD::SUB && |
| 2699 | RExtOp0.getOperand(1) == LExtOp0) { |
| 2700 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 2701 | // (rotl x, y) |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2702 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 2703 | // (rotr x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 2704 | if (ConstantSDNode *SUBC = |
| 2705 | dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2706 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2707 | return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
| 2708 | LHSShiftArg, |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 2709 | HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | } else if (LExtOp0.getOpcode() == ISD::SUB && |
| 2713 | RExtOp0 == LExtOp0.getOperand(1)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2714 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 2715 | // (rotr x, y) |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 2716 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> |
Bill Wendling | c5cbda1 | 2008-08-31 00:37:27 +0000 | [diff] [blame] | 2717 | // (rotl x, (sub 32, y)) |
Dan Gohman | 74feef2 | 2008-10-17 01:23:35 +0000 | [diff] [blame] | 2718 | if (ConstantSDNode *SUBC = |
| 2719 | dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2720 | if (SUBC->getAPIntValue() == OpSizeInBits) { |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2721 | return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, |
| 2722 | LHSShiftArg, |
Bill Wendling | 353dea2 | 2008-08-31 01:04:56 +0000 | [diff] [blame] | 2723 | HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode(); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2724 | } |
| 2725 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2726 | } |
| 2727 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2728 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 2729 | return 0; |
| 2730 | } |
| 2731 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2732 | SDValue DAGCombiner::visitXOR(SDNode *N) { |
| 2733 | SDValue N0 = N->getOperand(0); |
| 2734 | SDValue N1 = N->getOperand(1); |
| 2735 | SDValue LHS, RHS, CC; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2736 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2737 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2738 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2739 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2740 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2741 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2742 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2743 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2744 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2745 | |
Evan Cheng | 26471c4 | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 2746 | // fold (xor undef, undef) -> 0. This is a common idiom (misuse). |
| 2747 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 2748 | return DAG.getConstant(0, VT); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2749 | // fold (xor x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 2750 | if (N0.getOpcode() == ISD::UNDEF) |
| 2751 | return N0; |
| 2752 | if (N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2753 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2754 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2755 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2756 | return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2757 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2758 | if (N0C && !N1C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2759 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2760 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2761 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2762 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2763 | // reassociate xor |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 2764 | SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2765 | if (RXOR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2766 | return RXOR; |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2767 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2768 | // fold !(x cc y) -> (x !cc y) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2769 | if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2770 | bool isInt = LHS.getValueType().isInteger(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2771 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 2772 | isInt); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2773 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2774 | if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) { |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2775 | switch (N0.getOpcode()) { |
| 2776 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2777 | llvm_unreachable("Unhandled SetCC Equivalent!"); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2778 | case ISD::SETCC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2779 | return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2780 | case ISD::SELECT_CC: |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2781 | return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2), |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2782 | N0.getOperand(3), NotCC); |
| 2783 | } |
| 2784 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2785 | } |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 2787 | // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2788 | if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 2789 | N0.getNode()->hasOneUse() && |
| 2790 | isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2791 | SDValue V = N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2792 | V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V, |
Duncan Sands | 272dce0 | 2007-10-10 09:54:50 +0000 | [diff] [blame] | 2793 | DAG.getConstant(1, V.getValueType())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2794 | AddToWorkList(V.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2795 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V); |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 2796 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2797 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2798 | // 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] | 2799 | if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2800 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2801 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2802 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 2803 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2804 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 2805 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2806 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2807 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2808 | } |
| 2809 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2810 | // 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] | 2811 | if (N1C && N1C->isAllOnesValue() && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2812 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2813 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2814 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 2815 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2816 | LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS |
| 2817 | RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2818 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2819 | return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2820 | } |
| 2821 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2822 | // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 2823 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 2824 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 2825 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2826 | if (N00C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2827 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1), |
| 2828 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2829 | N00C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 2830 | if (N01C) |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 2831 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0), |
| 2832 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2833 | N01C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 2834 | } |
| 2835 | // fold (xor x, x) -> 0 |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 2836 | if (N0 == N1) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2837 | if (!VT.isVector()) { |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 2838 | return DAG.getConstant(0, VT); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2839 | } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){ |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 2840 | // Produce a vector of zeros. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2841 | SDValue El = DAG.getConstant(0, VT.getVectorElementType()); |
| 2842 | std::vector<SDValue> Ops(VT.getVectorNumElements(), El); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2843 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 2844 | &Ops[0], Ops.size()); |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2848 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 2849 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2850 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2851 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2852 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2853 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2854 | // Simplify the expression using non-local knowledge. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2855 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2856 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2857 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2858 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2859 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2862 | /// visitShiftByConstant - Handle transforms common to the three shifts, when |
| 2863 | /// the shift amount is a constant. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2864 | SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2865 | SDNode *LHS = N->getOperand(0).getNode(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2866 | if (!LHS->hasOneUse()) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2867 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2868 | // We want to pull some binops through shifts, so that we have (and (shift)) |
| 2869 | // instead of (shift (and)), likewise for add, or, xor, etc. This sort of |
| 2870 | // thing happens with address calculations, so it's important to canonicalize |
| 2871 | // it. |
| 2872 | 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] | 2873 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2874 | switch (LHS->getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2875 | default: return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2876 | case ISD::OR: |
| 2877 | case ISD::XOR: |
| 2878 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 2879 | break; |
| 2880 | case ISD::AND: |
| 2881 | HighBitSet = true; // We can only transform sra if the high bit is set. |
| 2882 | break; |
| 2883 | case ISD::ADD: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2884 | if (N->getOpcode() != ISD::SHL) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2885 | return SDValue(); // only shl(add) not sr[al](add). |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2886 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 2887 | break; |
| 2888 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2889 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2890 | // We require the RHS of the binop to be a constant as well. |
| 2891 | ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2892 | if (!BinOpCst) return SDValue(); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2893 | |
| 2894 | // FIXME: disable this unless the input to the binop is a shift by a constant. |
| 2895 | // If it is not a shift, it pessimizes some common cases like: |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 2896 | // |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2897 | // void foo(int *X, int i) { X[i & 1235] = 1; } |
| 2898 | // int bar(int *X, int i) { return X[i & 255]; } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2899 | SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2900 | if ((BinOpLHSVal->getOpcode() != ISD::SHL && |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 2901 | BinOpLHSVal->getOpcode() != ISD::SRA && |
| 2902 | BinOpLHSVal->getOpcode() != ISD::SRL) || |
| 2903 | !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2904 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2905 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2906 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2907 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2908 | // If this is a signed shift right, and the high bit is modified by the |
| 2909 | // logical operation, do not perform the transformation. The highBitSet |
| 2910 | // boolean indicates the value of the high bit of the constant which would |
| 2911 | // cause it to be modified for this operation. |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2912 | if (N->getOpcode() == ISD::SRA) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 2913 | bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); |
| 2914 | if (BinOpRHSSignSet != HighBitSet) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2915 | return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2916 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2917 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2918 | // Fold the constants, shifting the binop RHS by the shift amount. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2919 | SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(), |
| 2920 | N->getValueType(0), |
| 2921 | LHS->getOperand(1), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Create the new shift. |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 2924 | SDValue NewShift = DAG.getNode(N->getOpcode(), |
| 2925 | LHS->getOperand(0).getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2926 | VT, LHS->getOperand(0), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2927 | |
| 2928 | // Create the new binop. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2929 | return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2932 | SDValue DAGCombiner::visitSHL(SDNode *N) { |
| 2933 | SDValue N0 = N->getOperand(0); |
| 2934 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2935 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2936 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2937 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2938 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2939 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2940 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2941 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2942 | return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2943 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2944 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2945 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2946 | // fold (shl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2947 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2948 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2949 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2950 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2951 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2952 | // if (shl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2953 | if (DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2954 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2955 | return DAG.getConstant(0, VT); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 2956 | // 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] | 2957 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 2958 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 2959 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 2960 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 2961 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2962 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 2963 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 2964 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2965 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2966 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 2967 | DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT, |
| 2968 | DAG.getNode(ISD::TRUNCATE, |
| 2969 | N->getDebugLoc(), |
| 2970 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 2971 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 2972 | } |
| 2973 | } |
| 2974 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2975 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 2976 | return SDValue(N, 0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2977 | |
| 2978 | // 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] | 2979 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2980 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2981 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 2982 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 2983 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2984 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 2985 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2986 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2987 | } |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 2988 | |
| 2989 | // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) |
| 2990 | // For this to be valid, the second form must not preserve any of the bits |
| 2991 | // that are shifted out by the inner shift in the first form. This means |
| 2992 | // the outer shift size must be >= the number of bits added by the ext. |
| 2993 | // As a corollary, we don't care what kind of ext it is. |
| 2994 | if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 2995 | N0.getOpcode() == ISD::ANY_EXTEND || |
| 2996 | N0.getOpcode() == ISD::SIGN_EXTEND) && |
| 2997 | N0.getOperand(0).getOpcode() == ISD::SHL && |
| 2998 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
| 2999 | uint64_t c1 = |
| 3000 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3001 | uint64_t c2 = N1C->getZExtValue(); |
| 3002 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3003 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
| 3004 | if (c2 >= OpSizeInBits - InnerShiftSize) { |
| 3005 | if (c1 + c2 >= OpSizeInBits) |
| 3006 | return DAG.getConstant(0, VT); |
| 3007 | return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT, |
| 3008 | DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT, |
| 3009 | N0.getOperand(0)->getOperand(0)), |
| 3010 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| 3011 | } |
| 3012 | } |
| 3013 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3014 | // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or |
| 3015 | // (srl (and x, (shl -1, c1)), (sub c1, c2)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3016 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3017 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3018 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3019 | if (c1 < VT.getSizeInBits()) { |
| 3020 | uint64_t c2 = N1C->getZExtValue(); |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3021 | SDValue HiBitsMask = |
| 3022 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3023 | VT.getSizeInBits() - c1), |
| 3024 | VT); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3025 | SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, |
| 3026 | N0.getOperand(0), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3027 | HiBitsMask); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3028 | if (c2 > c1) |
| 3029 | return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask, |
| 3030 | DAG.getConstant(c2-c1, N1.getValueType())); |
| 3031 | else |
| 3032 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask, |
| 3033 | DAG.getConstant(c1-c2, N1.getValueType())); |
| 3034 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3035 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3036 | // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3037 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) { |
| 3038 | SDValue HiBitsMask = |
| 3039 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3040 | VT.getSizeInBits() - |
| 3041 | N1C->getZExtValue()), |
| 3042 | VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3043 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3044 | HiBitsMask); |
| 3045 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3046 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3047 | if (N1C) { |
| 3048 | SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3049 | if (NewSHL.getNode()) |
| 3050 | return NewSHL; |
| 3051 | } |
| 3052 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3053 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3056 | SDValue DAGCombiner::visitSRA(SDNode *N) { |
| 3057 | SDValue N0 = N->getOperand(0); |
| 3058 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3059 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3060 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3061 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3062 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3063 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3064 | // fold (sra c1, c2) -> (sra c1, c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3065 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3066 | return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3067 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3068 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3069 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3070 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3071 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3072 | return N0; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3073 | // fold (sra x, (setge c, size(x))) -> undef |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3074 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3075 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3076 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3077 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3078 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3079 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 3080 | // sext_inreg. |
| 3081 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3082 | unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3083 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); |
| 3084 | if (VT.isVector()) |
| 3085 | ExtVT = EVT::getVectorVT(*DAG.getContext(), |
| 3086 | ExtVT, VT.getVectorNumElements()); |
| 3087 | if ((!LegalOperations || |
| 3088 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3089 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3090 | N0.getOperand(0), DAG.getValueType(ExtVT)); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3091 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3092 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3093 | // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3094 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 3095 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3096 | unsigned Sum = N1C->getZExtValue() + C1->getZExtValue(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3097 | if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3098 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3099 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 3100 | } |
| 3101 | } |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3102 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3103 | // fold (sra (shl X, m), (sub result_size, n)) |
| 3104 | // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3105 | // result_size - n != m. |
| 3106 | // 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] | 3107 | // code. |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3108 | if (N0.getOpcode() == ISD::SHL) { |
| 3109 | // Get the two constanst of the shifts, CN0 = m, CN = n. |
| 3110 | const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3111 | if (N01C && N1C) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3112 | // Determine what the truncate's result bitsize and type would be. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3113 | EVT TruncVT = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3114 | EVT::getIntegerVT(*DAG.getContext(), |
| 3115 | OpSizeInBits - N1C->getZExtValue()); |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3116 | // Determine the residual right-shift amount. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3117 | signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3118 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3119 | // If the shift is not a no-op (in which case this should be just a sign |
| 3120 | // extend already), the truncated to type is legal, sign_extend is legal |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 3121 | // 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] | 3122 | // perform the transform. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3123 | if ((ShiftAmt > 0) && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3124 | TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && |
| 3125 | TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 3126 | TLI.isTruncateFree(VT, TruncVT)) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3127 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3128 | SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3129 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, |
| 3130 | N0.getOperand(0), Amt); |
| 3131 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT, |
| 3132 | Shift); |
| 3133 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), |
| 3134 | N->getValueType(0), Trunc); |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3135 | } |
| 3136 | } |
| 3137 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3138 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3139 | // 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] | 3140 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3141 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3142 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3143 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3144 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3145 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3146 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3147 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3148 | TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3149 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3150 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3151 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3152 | DAG.getNode(ISD::TRUNCATE, |
| 3153 | N->getDebugLoc(), |
| 3154 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3155 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3156 | } |
| 3157 | } |
| 3158 | |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3159 | // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2)) |
| 3160 | // if c1 is equal to the number of bits the trunc removes |
| 3161 | if (N0.getOpcode() == ISD::TRUNCATE && |
| 3162 | (N0.getOperand(0).getOpcode() == ISD::SRL || |
| 3163 | N0.getOperand(0).getOpcode() == ISD::SRA) && |
| 3164 | N0.getOperand(0).hasOneUse() && |
| 3165 | N0.getOperand(0).getOperand(1).hasOneUse() && |
| 3166 | N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) { |
| 3167 | EVT LargeVT = N0.getOperand(0).getValueType(); |
| 3168 | ConstantSDNode *LargeShiftAmt = |
| 3169 | cast<ConstantSDNode>(N0.getOperand(0).getOperand(1)); |
| 3170 | |
| 3171 | if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits == |
| 3172 | LargeShiftAmt->getZExtValue()) { |
| 3173 | SDValue Amt = |
| 3174 | DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(), |
| 3175 | getShiftAmountTy()); |
| 3176 | SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT, |
| 3177 | N0.getOperand(0).getOperand(0), Amt); |
| 3178 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA); |
| 3179 | } |
| 3180 | } |
| 3181 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3182 | // Simplify, based on bits shifted out of the LHS. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3183 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3184 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3185 | |
| 3186 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3187 | // 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] | 3188 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3189 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3190 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3191 | if (N1C) { |
| 3192 | SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3193 | if (NewSRA.getNode()) |
| 3194 | return NewSRA; |
| 3195 | } |
| 3196 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3197 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3200 | SDValue DAGCombiner::visitSRL(SDNode *N) { |
| 3201 | SDValue N0 = N->getOperand(0); |
| 3202 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3203 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3204 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3205 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3206 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3207 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3208 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3209 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3210 | return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3211 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3212 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3213 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3214 | // fold (srl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3215 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3216 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3217 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3218 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3219 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3220 | // if (srl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3221 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3222 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3223 | return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3224 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3225 | // 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] | 3226 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3227 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3228 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3229 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3230 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3231 | return DAG.getConstant(0, VT); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3232 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3233 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3234 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3235 | |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3236 | // 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] | 3237 | if (N1C && N0.getOpcode() == ISD::TRUNCATE && |
| 3238 | N0.getOperand(0).getOpcode() == ISD::SRL && |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3239 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3240 | uint64_t c1 = |
| 3241 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3242 | uint64_t c2 = N1C->getZExtValue(); |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3243 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3244 | EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3245 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 3246 | // This is only valid if the OpSizeInBits + c1 = size of inner shift. |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3247 | if (c1 + OpSizeInBits == InnerShiftSize) { |
| 3248 | if (c1 + c2 >= InnerShiftSize) |
| 3249 | return DAG.getConstant(0, VT); |
| 3250 | return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT, |
| 3251 | DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT, |
| 3252 | N0.getOperand(0)->getOperand(0), |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3253 | DAG.getConstant(c1 + c2, ShiftCountVT))); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 3254 | } |
| 3255 | } |
| 3256 | |
Chris Lattner | efcddc3 | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 3257 | // fold (srl (shl x, c), c) -> (and x, cst2) |
| 3258 | if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && |
| 3259 | N0.getValueSizeInBits() <= 64) { |
| 3260 | uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits(); |
| 3261 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0), |
| 3262 | DAG.getConstant(~0ULL >> ShAmt, VT)); |
| 3263 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3264 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3266 | // fold (srl (anyextend x), c) -> (anyextend (srl x, c)) |
| 3267 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 3268 | // Shifting in all undef bits? |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3269 | EVT SmallVT = N0.getOperand(0).getValueType(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3270 | if (N1C->getZExtValue() >= SmallVT.getSizeInBits()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3271 | return DAG.getUNDEF(VT); |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3272 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3273 | if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { |
| 3274 | SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT, |
| 3275 | N0.getOperand(0), N1); |
| 3276 | AddToWorkList(SmallShift.getNode()); |
| 3277 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift); |
| 3278 | } |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 3279 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3281 | // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign |
| 3282 | // bit, which is unmodified by sra. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3283 | if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) { |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3284 | if (N0.getOpcode() == ISD::SRA) |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3285 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1); |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 3286 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3287 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3288 | // 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] | 3289 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3290 | N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3291 | APInt KnownZero, KnownOne; |
Dan Gohman | 5b870af | 2010-03-02 02:14:38 +0000 | [diff] [blame] | 3292 | APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3293 | DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3295 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 3296 | // zeros, thus the result of the srl will always be zero. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3297 | if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3298 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3299 | // If all of the bits input the to ctlz node are known to be zero, then |
| 3300 | // the result of the ctlz is "32" and the result of the shift is one. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3301 | APInt UnknownBits = ~KnownZero & Mask; |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3302 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3303 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3304 | // 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] | 3305 | if ((UnknownBits & (UnknownBits - 1)) == 0) { |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3306 | // Okay, we know that only that the single bit specified by UnknownBits |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3307 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 3308 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 3309 | // to an SRL/XOR pair, which is likely to simplify more. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 3310 | unsigned ShAmt = UnknownBits.countTrailingZeros(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3311 | SDValue Op = N0.getOperand(0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3312 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3313 | if (ShAmt) { |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3314 | Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 3315 | DAG.getConstant(ShAmt, getShiftAmountTy())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3316 | AddToWorkList(Op.getNode()); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3317 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3318 | |
| 3319 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 3320 | Op, DAG.getConstant(1, VT)); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 3321 | } |
| 3322 | } |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3323 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3324 | // 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] | 3325 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3326 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3327 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3328 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3329 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3330 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3331 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3332 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3333 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3334 | return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3335 | DAG.getNode(ISD::AND, N->getDebugLoc(), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3336 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3337 | DAG.getNode(ISD::TRUNCATE, |
| 3338 | N->getDebugLoc(), |
| 3339 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3340 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3341 | } |
| 3342 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3343 | |
Chris Lattner | 61a4c07 | 2007-04-18 03:06:49 +0000 | [diff] [blame] | 3344 | // fold operands of srl based on knowledge that the low bits are not |
| 3345 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3346 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3347 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3348 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3349 | if (N1C) { |
| 3350 | SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3351 | if (NewSRL.getNode()) |
| 3352 | return NewSRL; |
| 3353 | } |
| 3354 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3355 | // Attempt to convert a srl of a load into a narrower zero-extending load. |
| 3356 | SDValue NarrowLoad = ReduceLoadWidth(N); |
| 3357 | if (NarrowLoad.getNode()) |
| 3358 | return NarrowLoad; |
| 3359 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3360 | // Here is a common situation. We want to optimize: |
| 3361 | // |
| 3362 | // %a = ... |
| 3363 | // %b = and i32 %a, 2 |
| 3364 | // %c = srl i32 %b, 1 |
| 3365 | // brcond i32 %c ... |
| 3366 | // |
| 3367 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3368 | // |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3369 | // %a = ... |
| 3370 | // %b = and %a, 2 |
| 3371 | // %c = setcc eq %b, 0 |
| 3372 | // brcond %c ... |
| 3373 | // |
| 3374 | // However when after the source operand of SRL is optimized into AND, the SRL |
| 3375 | // itself may not be optimized further. Look for it and add the BRCOND into |
| 3376 | // the worklist. |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 3377 | if (N->hasOneUse()) { |
| 3378 | SDNode *Use = *N->use_begin(); |
| 3379 | if (Use->getOpcode() == ISD::BRCOND) |
| 3380 | AddToWorkList(Use); |
| 3381 | else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { |
| 3382 | // Also look pass the truncate. |
| 3383 | Use = *Use->use_begin(); |
| 3384 | if (Use->getOpcode() == ISD::BRCOND) |
| 3385 | AddToWorkList(Use); |
| 3386 | } |
| 3387 | } |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 3388 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3389 | return SDValue(); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3392 | SDValue DAGCombiner::visitCTLZ(SDNode *N) { |
| 3393 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3394 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3395 | |
| 3396 | // fold (ctlz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3397 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3398 | return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3399 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3402 | SDValue DAGCombiner::visitCTTZ(SDNode *N) { |
| 3403 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3404 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3405 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3406 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3407 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3408 | return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3409 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3412 | SDValue DAGCombiner::visitCTPOP(SDNode *N) { |
| 3413 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3414 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3415 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3416 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3417 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3418 | return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3419 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3422 | SDValue DAGCombiner::visitSELECT(SDNode *N) { |
| 3423 | SDValue N0 = N->getOperand(0); |
| 3424 | SDValue N1 = N->getOperand(1); |
| 3425 | SDValue N2 = N->getOperand(2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3426 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3427 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 3428 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3429 | EVT VT = N->getValueType(0); |
| 3430 | EVT VT0 = N0.getValueType(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3431 | |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3432 | // fold (select C, X, X) -> X |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3433 | if (N1 == N2) |
| 3434 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3435 | // fold (select true, X, Y) -> X |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3436 | if (N0C && !N0C->isNullValue()) |
| 3437 | return N1; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3438 | // fold (select false, X, Y) -> Y |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3439 | if (N0C && N0C->isNullValue()) |
| 3440 | return N2; |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3441 | // fold (select C, 1, X) -> (or C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3442 | if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3443 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 3444 | // fold (select C, 0, 1) -> (xor C, 1) |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 3445 | if (VT.isInteger() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3446 | (VT0 == MVT::i1 || |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 3447 | (VT0.isInteger() && |
| 3448 | TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3449 | N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3450 | SDValue XORNode; |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 3451 | if (VT == VT0) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3452 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0, |
| 3453 | N0, DAG.getConstant(1, VT0)); |
| 3454 | XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0, |
| 3455 | N0, DAG.getConstant(1, VT0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3456 | AddToWorkList(XORNode.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3457 | if (VT.bitsGT(VT0)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3458 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode); |
| 3459 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode); |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 3460 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3461 | // fold (select C, 0, X) -> (and (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3462 | if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) { |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 3463 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 3464 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 3465 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3466 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3467 | // fold (select C, X, 1) -> (or (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3468 | if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3469 | SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 3470 | AddToWorkList(NOTNode.getNode()); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 3471 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3472 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3473 | // fold (select C, X, 0) -> (and C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3474 | if (VT == MVT::i1 && N2C && N2C->isNullValue()) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3475 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
| 3476 | // fold (select X, X, Y) -> (or X, Y) |
| 3477 | // fold (select X, 1, Y) -> (or X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3478 | if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3479 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2); |
| 3480 | // fold (select X, Y, X) -> (and X, Y) |
| 3481 | // fold (select X, Y, 0) -> (and X, Y) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3482 | if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0))) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3483 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3484 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3485 | // If we can fold this based on the true/false value, do so. |
| 3486 | if (SimplifySelectOps(N, N1, N2)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3487 | return SDValue(N, 0); // Don't revisit N. |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3488 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3489 | // 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] | 3490 | if (N0.getOpcode() == ISD::SETCC) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3491 | // FIXME: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3492 | // 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] | 3493 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 3494 | // 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] | 3495 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) && |
Dan Gohman | 4ea4804 | 2009-08-02 16:19:38 +0000 | [diff] [blame] | 3496 | TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3497 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, |
| 3498 | N0.getOperand(0), N0.getOperand(1), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3499 | N1, N2, N0.getOperand(2)); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 3500 | return SimplifySelect(N->getDebugLoc(), N0, N1, N2); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3501 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 3502 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3503 | return SDValue(); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3506 | SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { |
| 3507 | SDValue N0 = N->getOperand(0); |
| 3508 | SDValue N1 = N->getOperand(1); |
| 3509 | SDValue N2 = N->getOperand(2); |
| 3510 | SDValue N3 = N->getOperand(3); |
| 3511 | SDValue N4 = N->getOperand(4); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3512 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3513 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3514 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 3515 | if (N2 == N3) |
| 3516 | return N2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3517 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3518 | // Determine if the condition we're dealing with is constant |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 3519 | SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 3520 | N0, N1, CC, N->getDebugLoc(), false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3521 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3522 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3523 | if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3524 | if (!SCCC->isNullValue()) |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3525 | return N2; // cond always true -> true val |
| 3526 | else |
| 3527 | return N3; // cond always false -> false val |
| 3528 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3529 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3530 | // Fold to a simpler select_cc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3531 | if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3532 | return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(), |
| 3533 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3534 | SCC.getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3535 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3536 | // If we can fold this based on the true/false value, do so. |
| 3537 | if (SimplifySelectOps(N, N2, N3)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3538 | return SDValue(N, 0); // Don't revisit N. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3539 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3540 | // fold select_cc into other things, such as min/max/abs |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 3541 | return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3544 | SDValue DAGCombiner::visitSETCC(SDNode *N) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3545 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 3546 | cast<CondCodeSDNode>(N->getOperand(2))->get(), |
| 3547 | N->getDebugLoc()); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3550 | // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3551 | // "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] | 3552 | // transformation. Returns true if extension are possible and the above |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3553 | // mentioned transformation is profitable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3554 | static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3555 | unsigned ExtOpc, |
| 3556 | SmallVector<SDNode*, 4> &ExtendNodes, |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 3557 | const TargetLowering &TLI) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3558 | bool HasCopyToRegUses = false; |
| 3559 | bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3560 | for (SDNode::use_iterator UI = N0.getNode()->use_begin(), |
| 3561 | UE = N0.getNode()->use_end(); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3562 | UI != UE; ++UI) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 3563 | SDNode *User = *UI; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3564 | if (User == N) |
| 3565 | continue; |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3566 | if (UI.getUse().getResNo() != N0.getResNo()) |
| 3567 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3568 | // FIXME: Only extend SETCC N, N and SETCC N, c for now. |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3569 | if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3570 | ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); |
| 3571 | if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) |
| 3572 | // Sign bits will be lost after a zext. |
| 3573 | return false; |
| 3574 | bool Add = false; |
| 3575 | for (unsigned i = 0; i != 2; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3576 | SDValue UseOp = User->getOperand(i); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3577 | if (UseOp == N0) |
| 3578 | continue; |
| 3579 | if (!isa<ConstantSDNode>(UseOp)) |
| 3580 | return false; |
| 3581 | Add = true; |
| 3582 | } |
| 3583 | if (Add) |
| 3584 | ExtendNodes.push_back(User); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3585 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3586 | } |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3587 | // If truncates aren't free and there are users we can't |
| 3588 | // extend, it isn't worthwhile. |
| 3589 | if (!isTruncFree) |
| 3590 | return false; |
| 3591 | // Remember if this value is live-out. |
| 3592 | if (User->getOpcode() == ISD::CopyToReg) |
| 3593 | HasCopyToRegUses = true; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | if (HasCopyToRegUses) { |
| 3597 | bool BothLiveOut = false; |
| 3598 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 3599 | UI != UE; ++UI) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3600 | SDUse &Use = UI.getUse(); |
| 3601 | if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { |
| 3602 | BothLiveOut = true; |
| 3603 | break; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | if (BothLiveOut) |
| 3607 | // Both unextended and extended values are live out. There had better be |
Bob Wilson | bebfbc5 | 2010-11-28 06:51:19 +0000 | [diff] [blame] | 3608 | // a good reason for the transformation. |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3609 | return ExtendNodes.size(); |
| 3610 | } |
| 3611 | return true; |
| 3612 | } |
| 3613 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3614 | SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
| 3615 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3616 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3617 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3618 | // fold (sext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3619 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3620 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3621 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3622 | // fold (sext (sext x)) -> (sext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3623 | // fold (sext (aext x)) -> (sext x) |
| 3624 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3625 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, |
| 3626 | N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3627 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 3628 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 3629 | // fold (sext (truncate (load x))) -> (sext (smaller load x)) |
| 3630 | // 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] | 3631 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 3632 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 3633 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 3634 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3635 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 3636 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 3637 | AddToWorkList(oye); |
| 3638 | } |
Dan Gohman | c7b3444 | 2009-04-27 02:00:55 +0000 | [diff] [blame] | 3639 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 3640 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 3641 | |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 3642 | // See if the value being truncated is already sign extended. If so, just |
| 3643 | // eliminate the trunc/sext pair. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3644 | SDValue Op = N0.getOperand(0); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3645 | unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits(); |
| 3646 | unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits(); |
| 3647 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3648 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3649 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 3650 | if (OpBits == DestBits) { |
| 3651 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 3652 | // bits, it is already ready. |
| 3653 | if (NumSignBits > DestBits-MidBits) |
| 3654 | return Op; |
| 3655 | } else if (OpBits < DestBits) { |
| 3656 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 3657 | // bits, just sext from i32. |
| 3658 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3659 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 3660 | } else { |
| 3661 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 3662 | // bits, just truncate to i32. |
| 3663 | if (NumSignBits > OpBits-MidBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3664 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3665 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3666 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 3667 | // fold (sext (truncate x)) -> (sextinreg x). |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3668 | if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
| 3669 | N0.getValueType())) { |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3670 | if (OpBits < DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3671 | Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3672 | else if (OpBits > DestBits) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3673 | Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op); |
| 3674 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3675 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 3676 | } |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3677 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3678 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 3679 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3680 | if (ISD::isNON_EXTLoad(N0.getNode()) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3681 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 3682 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3683 | bool DoXform = true; |
| 3684 | SmallVector<SDNode*, 4> SetCCs; |
| 3685 | if (!N0.hasOneUse()) |
| 3686 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); |
| 3687 | if (DoXform) { |
| 3688 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 3689 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N->getDebugLoc(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3690 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 3691 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3692 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3693 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 3694 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3695 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3696 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 3697 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3698 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3699 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3700 | // Extend SetCC uses if necessary. |
| 3701 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 3702 | SDNode *SetCC = SetCCs[i]; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3703 | SmallVector<SDValue, 4> Ops; |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3704 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3705 | for (unsigned j = 0; j != 2; ++j) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3706 | SDValue SOp = SetCC->getOperand(j); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3707 | if (SOp == Trunc) |
| 3708 | Ops.push_back(ExtLoad); |
| 3709 | else |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 3710 | Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, |
| 3711 | N->getDebugLoc(), VT, SOp)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3714 | Ops.push_back(SetCC->getOperand(2)); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3715 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3716 | SetCC->getValueType(0), |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3717 | &Ops[0], Ops.size())); |
| 3718 | } |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3719 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3720 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3721 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 3722 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 3723 | |
| 3724 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 3725 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3726 | if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 3727 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3728 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 3729 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3730 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 3731 | TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) { |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 3732 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3733 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 3734 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 3735 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3736 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 3737 | LN0->getAlignment()); |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 3738 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3739 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3740 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 3741 | N0.getValueType(), ExtLoad), |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 3742 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3743 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 3744 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 3745 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3746 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 3747 | if (N0.getOpcode() == ISD::SETCC) { |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 3748 | // sext(setcc) -> sext_in_reg(vsetcc) for vectors. |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 3749 | // Only do this before legalize for now. |
| 3750 | if (VT.isVector() && !LegalOperations) { |
| 3751 | EVT N0VT = N0.getOperand(0).getValueType(); |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 3752 | // We know that the # elements of the results is the same as the |
| 3753 | // # elements of the compare (and the # elements of the compare result |
| 3754 | // for that matter). Check to see that they are the same size. If so, |
| 3755 | // we know that the element size of the sext'd result matches the |
| 3756 | // element size of the compare operands. |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 3757 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 3758 | return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
| 3759 | N0.getOperand(1), |
| 3760 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 3761 | // If the desired elements are smaller or larger than the source |
| 3762 | // elements we can use a matching integer vector type and then |
| 3763 | // truncate/sign extend |
| 3764 | else { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 3765 | EVT MatchingElementType = |
| 3766 | EVT::getIntegerVT(*DAG.getContext(), |
| 3767 | N0VT.getScalarType().getSizeInBits()); |
| 3768 | EVT MatchingVectorType = |
| 3769 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 3770 | N0VT.getVectorNumElements()); |
| 3771 | SDValue VsetCC = |
| 3772 | DAG.getVSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
| 3773 | N0.getOperand(1), |
| 3774 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 3775 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 3776 | } |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 3777 | } |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 3778 | |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 3779 | // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc) |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 3780 | unsigned ElementWidth = VT.getScalarType().getSizeInBits(); |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3781 | SDValue NegOne = |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 3782 | DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3783 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 3784 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3785 | NegOne, DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 3786 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3787 | if (SCC.getNode()) return SCC; |
Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 3788 | if (!LegalOperations || |
| 3789 | TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT))) |
| 3790 | return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT, |
| 3791 | DAG.getSetCC(N->getDebugLoc(), |
| 3792 | TLI.getSetCCResultType(VT), |
| 3793 | N0.getOperand(0), N0.getOperand(1), |
| 3794 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 3795 | NegOne, DAG.getConstant(0, VT)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3796 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3797 | |
Dan Gohman | 8f0ad58 | 2008-04-28 16:58:24 +0000 | [diff] [blame] | 3798 | // fold (sext x) -> (zext x) if the sign bit is known zero. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3799 | if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && |
Dan Gohman | 187db7b | 2008-04-28 18:47:17 +0000 | [diff] [blame] | 3800 | DAG.SignBitIsZero(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3801 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3802 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3803 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3806 | SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
| 3807 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3808 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3809 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3810 | // fold (zext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3811 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3812 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3813 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 3814 | // fold (zext (aext x)) -> (zext x) |
| 3815 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3816 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, |
| 3817 | N0.getOperand(0)); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3818 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 3819 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 3820 | // 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] | 3821 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3822 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 3823 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 3824 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 3825 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3826 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 3827 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 3828 | AddToWorkList(oye); |
| 3829 | } |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3830 | return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad); |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 3831 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3834 | // fold (zext (truncate x)) -> (and x, mask) |
| 3835 | if (N0.getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3836 | (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 3837 | |
| 3838 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 3839 | // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) |
| 3840 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 3841 | if (NarrowLoad.getNode()) { |
| 3842 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 3843 | if (NarrowLoad.getNode() != N0.getNode()) { |
| 3844 | CombineTo(N0.getNode(), NarrowLoad); |
| 3845 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 3846 | AddToWorkList(oye); |
| 3847 | } |
| 3848 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 3849 | } |
| 3850 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3851 | SDValue Op = N0.getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3852 | if (Op.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3853 | Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3854 | } else if (Op.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3855 | Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3856 | } |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3857 | return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), |
| 3858 | N0.getValueType().getScalarType()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 3859 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3860 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 3861 | // Fold (zext (and (trunc x), cst)) -> (and x, cst), |
| 3862 | // if either of the casts is not free. |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 3863 | if (N0.getOpcode() == ISD::AND && |
| 3864 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 3865 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 3866 | (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 3867 | N0.getValueType()) || |
| 3868 | !TLI.isZExtFree(N0.getValueType(), VT))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3869 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3870 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3871 | X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3872 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3873 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 3874 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3875 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3876 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3877 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 3878 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 3879 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3880 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 3881 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3882 | if (ISD::isNON_EXTLoad(N0.getNode()) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3883 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 3884 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3885 | bool DoXform = true; |
| 3886 | SmallVector<SDNode*, 4> SetCCs; |
| 3887 | if (!N0.hasOneUse()) |
| 3888 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); |
| 3889 | if (DoXform) { |
| 3890 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 3891 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3892 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 3893 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3894 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3895 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 3896 | LN0->getAlignment()); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3897 | CombineTo(N, ExtLoad); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3898 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 3899 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3900 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3901 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3902 | // Extend SetCC uses if necessary. |
| 3903 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 3904 | SDNode *SetCC = SetCCs[i]; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3905 | SmallVector<SDValue, 4> Ops; |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3906 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3907 | for (unsigned j = 0; j != 2; ++j) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3908 | SDValue SOp = SetCC->getOperand(j); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3909 | if (SOp == Trunc) |
| 3910 | Ops.push_back(ExtLoad); |
| 3911 | else |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3912 | Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, |
| 3913 | N->getDebugLoc(), VT, SOp)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3916 | Ops.push_back(SetCC->getOperand(2)); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3917 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3918 | SetCC->getValueType(0), |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3919 | &Ops[0], Ops.size())); |
| 3920 | } |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3921 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3922 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 3923 | } |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 3924 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 3925 | |
| 3926 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 3927 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3928 | if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 3929 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3930 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 3931 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 3932 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 3933 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) { |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 3934 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3935 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 3936 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 3937 | MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3938 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 3939 | LN0->getAlignment()); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3940 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3941 | CombineTo(N0.getNode(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 3942 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(), |
| 3943 | ExtLoad), |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3944 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3945 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3946 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 3947 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3948 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 3949 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 3950 | if (!LegalOperations && VT.isVector()) { |
| 3951 | // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. |
| 3952 | // Only do this before legalize for now. |
| 3953 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 3954 | EVT EltVT = VT.getVectorElementType(); |
| 3955 | SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(), |
| 3956 | DAG.getConstant(1, EltVT)); |
| 3957 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) { |
| 3958 | // We know that the # elements of the results is the same as the |
| 3959 | // # elements of the compare (and the # elements of the compare result |
| 3960 | // for that matter). Check to see that they are the same size. If so, |
| 3961 | // we know that the element size of the sext'd result matches the |
| 3962 | // element size of the compare operands. |
| 3963 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 3964 | DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
| 3965 | N0.getOperand(1), |
| 3966 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| 3967 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 3968 | &OneOps[0], OneOps.size())); |
| 3969 | } else { |
| 3970 | // If the desired elements are smaller or larger than the source |
| 3971 | // elements we can use a matching integer vector type and then |
| 3972 | // truncate/sign extend |
| 3973 | EVT MatchingElementType = |
| 3974 | EVT::getIntegerVT(*DAG.getContext(), |
| 3975 | N0VT.getScalarType().getSizeInBits()); |
| 3976 | EVT MatchingVectorType = |
| 3977 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 3978 | N0VT.getVectorNumElements()); |
| 3979 | SDValue VsetCC = |
| 3980 | DAG.getVSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
| 3981 | N0.getOperand(1), |
| 3982 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 3983 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 3984 | DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT), |
| 3985 | DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, |
| 3986 | &OneOps[0], OneOps.size())); |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3991 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 3992 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 3993 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 3994 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3995 | if (SCC.getNode()) return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 3996 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3997 | |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 3998 | // (zext (shl (zext x), cst)) -> (shl (zext x), cst) |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 3999 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4000 | isa<ConstantSDNode>(N0.getOperand(1)) && |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4001 | N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && |
| 4002 | N0.hasOneUse()) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4003 | SDValue ShAmt = N0.getOperand(1); |
| 4004 | unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4005 | if (N0.getOpcode() == ISD::SHL) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4006 | SDValue InnerZExt = N0.getOperand(0); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4007 | // If the original shl may be shifting out bits, do not perform this |
| 4008 | // transformation. |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4009 | unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() - |
| 4010 | InnerZExt.getOperand(0).getValueType().getSizeInBits(); |
| 4011 | if (ShAmtVal > KnownZeroBits) |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4012 | return SDValue(); |
| 4013 | } |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4014 | |
| 4015 | DebugLoc DL = N->getDebugLoc(); |
| 4016 | |
| 4017 | // Ensure that the shift amount is wide enough for the shifted value. |
| 4018 | if (VT.getSizeInBits() >= 256) |
| 4019 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); |
| 4020 | |
| 4021 | return DAG.getNode(N0.getOpcode(), DL, VT, |
| 4022 | DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), |
| 4023 | ShAmt); |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4026 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4029 | SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 4030 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4031 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4032 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4033 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4034 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4035 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4036 | // fold (aext (aext x)) -> (aext x) |
| 4037 | // fold (aext (zext x)) -> (zext x) |
| 4038 | // fold (aext (sext x)) -> (sext x) |
| 4039 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 4040 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 4041 | N0.getOpcode() == ISD::SIGN_EXTEND) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4042 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4043 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4044 | // fold (aext (truncate (load x))) -> (aext (smaller load x)) |
| 4045 | // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) |
| 4046 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4047 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4048 | if (NarrowLoad.getNode()) { |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4049 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4050 | if (NarrowLoad.getNode() != N0.getNode()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4051 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 4052 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4053 | AddToWorkList(oye); |
| 4054 | } |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4055 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad); |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4056 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4059 | // fold (aext (truncate x)) |
| 4060 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4061 | SDValue TruncOp = N0.getOperand(0); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4062 | if (TruncOp.getValueType() == VT) |
| 4063 | return TruncOp; // x iff x size == zext size. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4064 | if (TruncOp.getValueType().bitsGT(VT)) |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4065 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp); |
| 4066 | return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 4067 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4068 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4069 | // Fold (aext (and (trunc x), cst)) -> (and x, cst) |
| 4070 | // if the trunc is not free. |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4071 | if (N0.getOpcode() == ISD::AND && |
| 4072 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4073 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4074 | !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 4075 | N0.getValueType())) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4076 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4077 | if (X.getValueType().bitsLT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4078 | X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4079 | } else if (X.getValueType().bitsGT(VT)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4080 | X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4081 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4082 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4083 | Mask = Mask.zext(VT.getSizeInBits()); |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4084 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4085 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 4086 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4087 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4088 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4089 | if (ISD::isNON_EXTLoad(N0.getNode()) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4090 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4091 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4092 | bool DoXform = true; |
| 4093 | SmallVector<SDNode*, 4> SetCCs; |
| 4094 | if (!N0.hasOneUse()) |
| 4095 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); |
| 4096 | if (DoXform) { |
| 4097 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4098 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N->getDebugLoc(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4099 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4100 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4101 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4102 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4103 | LN0->getAlignment()); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4104 | CombineTo(N, ExtLoad); |
| 4105 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4106 | N0.getValueType(), ExtLoad); |
| 4107 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
| 4108 | |
| 4109 | // Extend SetCC uses if necessary. |
| 4110 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 4111 | SDNode *SetCC = SetCCs[i]; |
| 4112 | SmallVector<SDValue, 4> Ops; |
| 4113 | |
| 4114 | for (unsigned j = 0; j != 2; ++j) { |
| 4115 | SDValue SOp = SetCC->getOperand(j); |
| 4116 | if (SOp == Trunc) |
| 4117 | Ops.push_back(ExtLoad); |
| 4118 | else |
| 4119 | Ops.push_back(DAG.getNode(ISD::ANY_EXTEND, |
| 4120 | N->getDebugLoc(), VT, SOp)); |
| 4121 | } |
| 4122 | |
| 4123 | Ops.push_back(SetCC->getOperand(2)); |
| 4124 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(), |
| 4125 | SetCC->getValueType(0), |
| 4126 | &Ops[0], Ops.size())); |
| 4127 | } |
| 4128 | |
| 4129 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4130 | } |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4131 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4132 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4133 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 4134 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 4135 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 4136 | if (N0.getOpcode() == ISD::LOAD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4137 | !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4138 | N0.hasOneUse()) { |
| 4139 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4140 | EVT MemVT = LN0->getMemoryVT(); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4141 | SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT, |
| 4142 | N->getDebugLoc(), |
| 4143 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4144 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4145 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4146 | LN0->getAlignment()); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4147 | CombineTo(N, ExtLoad); |
Evan Cheng | 4529966 | 2008-08-29 23:20:46 +0000 | [diff] [blame] | 4148 | CombineTo(N0.getNode(), |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 4149 | DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), |
| 4150 | N0.getValueType(), ExtLoad), |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4151 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4152 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4153 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4155 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4156 | // aext(setcc) -> sext_in_reg(vsetcc) for vectors. |
| 4157 | // Only do this before legalize for now. |
| 4158 | if (VT.isVector() && !LegalOperations) { |
| 4159 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 4160 | // We know that the # elements of the results is the same as the |
| 4161 | // # elements of the compare (and the # elements of the compare result |
| 4162 | // for that matter). Check to see that they are the same size. If so, |
| 4163 | // we know that the element size of the sext'd result matches the |
| 4164 | // element size of the compare operands. |
| 4165 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4166 | return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0), |
| 4167 | N0.getOperand(1), |
| 4168 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4169 | // If the desired elements are smaller or larger than the source |
| 4170 | // elements we can use a matching integer vector type and then |
| 4171 | // truncate/sign extend |
| 4172 | else { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4173 | EVT MatchingElementType = |
| 4174 | EVT::getIntegerVT(*DAG.getContext(), |
| 4175 | N0VT.getScalarType().getSizeInBits()); |
| 4176 | EVT MatchingVectorType = |
| 4177 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 4178 | N0VT.getVectorNumElements()); |
| 4179 | SDValue VsetCC = |
| 4180 | DAG.getVSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0), |
| 4181 | N0.getOperand(1), |
| 4182 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 4183 | return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4188 | SDValue SCC = |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 4189 | SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4190 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | c24bbad | 2007-04-11 16:51:53 +0000 | [diff] [blame] | 4191 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4192 | if (SCC.getNode()) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 4193 | return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4194 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4195 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4196 | return SDValue(); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4199 | /// GetDemandedBits - See if the specified operand can be simplified with the |
| 4200 | /// 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] | 4201 | /// simpler operand, otherwise return a null SDValue. |
| 4202 | SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4203 | switch (V.getOpcode()) { |
| 4204 | default: break; |
| 4205 | case ISD::OR: |
| 4206 | case ISD::XOR: |
| 4207 | // If the LHS or RHS don't contribute bits to the or, drop them. |
| 4208 | if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) |
| 4209 | return V.getOperand(1); |
| 4210 | if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) |
| 4211 | return V.getOperand(0); |
| 4212 | break; |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4213 | case ISD::SRL: |
| 4214 | // Only look at single-use SRLs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4215 | if (!V.getNode()->hasOneUse()) |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4216 | break; |
| 4217 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) { |
| 4218 | // See if we can recursively simplify the LHS. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4219 | unsigned Amt = RHSC->getZExtValue(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4220 | |
Dan Gohman | cc91d63 | 2009-01-03 19:22:06 +0000 | [diff] [blame] | 4221 | // Watch out for shift count overflow though. |
| 4222 | if (Amt >= Mask.getBitWidth()) break; |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4223 | APInt NewMask = Mask << Amt; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4224 | SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4225 | if (SimplifyLHS.getNode()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4226 | return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(), |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4227 | SimplifyLHS, V.getOperand(1)); |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 4228 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4229 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4230 | return SDValue(); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4233 | /// ReduceLoadWidth - If the result of a wider load is shifted to right of N |
| 4234 | /// bits and then truncated to a narrower type and where N is a multiple |
| 4235 | /// of number of bits of the narrower type, transform it to a narrower load |
| 4236 | /// from address + N / num of bits of new type. If the result is to be |
| 4237 | /// extended, also fold the extension to form a extending load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4238 | SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4239 | unsigned Opc = N->getOpcode(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4240 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4241 | ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4242 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4243 | EVT VT = N->getValueType(0); |
| 4244 | EVT ExtVT = VT; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4245 | |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 4246 | // This transformation isn't valid for vector loads. |
| 4247 | if (VT.isVector()) |
| 4248 | return SDValue(); |
| 4249 | |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4250 | // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then |
Evan Cheng | e177e30 | 2007-03-23 22:13:36 +0000 | [diff] [blame] | 4251 | // extended to VT. |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4252 | if (Opc == ISD::SIGN_EXTEND_INREG) { |
| 4253 | ExtType = ISD::SEXTLOAD; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4254 | ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4255 | } else if (Opc == ISD::SRL) { |
Chris Lattner | 90b0364 | 2010-12-21 18:05:22 +0000 | [diff] [blame] | 4256 | // Another special-case: SRL is basically zero-extending a narrower value. |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4257 | ExtType = ISD::ZEXTLOAD; |
| 4258 | N0 = SDValue(N, 0); |
| 4259 | ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 4260 | if (!N01) return SDValue(); |
| 4261 | ExtVT = EVT::getIntegerVT(*DAG.getContext(), |
| 4262 | VT.getSizeInBits() - N01->getZExtValue()); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4263 | } |
Richard Osborne | 4e3740e | 2011-01-31 17:41:44 +0000 | [diff] [blame] | 4264 | if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT)) |
| 4265 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4266 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4267 | unsigned EVTBits = ExtVT.getSizeInBits(); |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4268 | |
| 4269 | // Do not generate loads of non-round integer types since these can |
| 4270 | // be expensive (and would be wrong if the type is not byte sized). |
| 4271 | if (!ExtVT.isRound()) |
| 4272 | return SDValue(); |
| 4273 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4274 | unsigned ShAmt = 0; |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4275 | if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4276 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4277 | ShAmt = N01->getZExtValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4278 | // Is the shift amount a multiple of size of VT? |
| 4279 | if ((ShAmt & (EVTBits-1)) == 0) { |
| 4280 | N0 = N0.getOperand(0); |
Eli Friedman | d68eea2 | 2009-08-19 08:46:10 +0000 | [diff] [blame] | 4281 | // Is the load width a multiple of size of VT? |
| 4282 | if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4283 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4284 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4285 | |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 4286 | // At this point, we must have a load or else we can't do the transform. |
| 4287 | if (!isa<LoadSDNode>(N0)) return SDValue(); |
| 4288 | |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 4289 | // If the shift amount is larger than the input type then we're not |
| 4290 | // accessing any of the loaded bytes. If the load was a zextload/extload |
| 4291 | // then the result of the shift+trunc is zero/undef (handled elsewhere). |
| 4292 | // If the load was a sextload then the result is a splat of the sign bit |
| 4293 | // of the extended byte. This is not worth optimizing for. |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 4294 | if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 4295 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4299 | // If the load is shifted left (and the result isn't shifted back right), |
| 4300 | // we can fold the truncate through the shift. |
| 4301 | unsigned ShLeftAmt = 0; |
| 4302 | if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4303 | ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4304 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 4305 | ShLeftAmt = N01->getZExtValue(); |
| 4306 | N0 = N0.getOperand(0); |
| 4307 | } |
| 4308 | } |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4309 | |
| 4310 | // If we haven't found a load, we can't narrow it. Don't transform one with |
| 4311 | // multiple uses, this would require adding a new load. |
| 4312 | if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() || |
| 4313 | // Don't change the width of a volatile load. |
| 4314 | cast<LoadSDNode>(N0)->isVolatile()) |
| 4315 | return SDValue(); |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4316 | |
| 4317 | // Verify that we are actually reducing a load width here. |
| 4318 | if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits) |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4319 | return SDValue(); |
| 4320 | |
| 4321 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| 4322 | EVT PtrType = N0.getOperand(1).getValueType(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4323 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4324 | // For big endian targets, we need to adjust the offset to the pointer to |
| 4325 | // load the correct bytes. |
| 4326 | if (TLI.isBigEndian()) { |
| 4327 | unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); |
| 4328 | unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); |
| 4329 | ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4332 | uint64_t PtrOff = ShAmt / 8; |
| 4333 | unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); |
| 4334 | SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), |
| 4335 | PtrType, LN0->getBasePtr(), |
| 4336 | DAG.getConstant(PtrOff, PtrType)); |
| 4337 | AddToWorkList(NewPtr.getNode()); |
| 4338 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 4339 | SDValue Load; |
| 4340 | if (ExtType == ISD::NON_EXTLOAD) |
| 4341 | Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr, |
| 4342 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 4343 | LN0->isVolatile(), LN0->isNonTemporal(), NewAlign); |
| 4344 | else |
| 4345 | Load = DAG.getExtLoad(ExtType, VT, N0.getDebugLoc(), LN0->getChain(),NewPtr, |
| 4346 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 4347 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 4348 | NewAlign); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 4349 | |
| 4350 | // Replace the old load's chain with the new load's chain. |
| 4351 | WorkListRemover DeadNodes(*this); |
| 4352 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), |
| 4353 | &DeadNodes); |
| 4354 | |
| 4355 | // Shift the result left, if we've swallowed a left shift. |
| 4356 | SDValue Result = Load; |
| 4357 | if (ShLeftAmt != 0) { |
| 4358 | EVT ShImmTy = getShiftAmountTy(); |
| 4359 | if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) |
| 4360 | ShImmTy = VT; |
| 4361 | Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, |
| 4362 | Result, DAG.getConstant(ShLeftAmt, ShImmTy)); |
| 4363 | } |
| 4364 | |
| 4365 | // Return the new loaded value. |
| 4366 | return Result; |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4369 | SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
| 4370 | SDValue N0 = N->getOperand(0); |
| 4371 | SDValue N1 = N->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4372 | EVT VT = N->getValueType(0); |
| 4373 | EVT EVT = cast<VTSDNode>(N1)->getVT(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4374 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4375 | unsigned EVTBits = EVT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4376 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4377 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 4378 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4379 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4380 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 4381 | // If the input is already sign extended, just drop the extension. |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4382 | if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 4383 | return N0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4384 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4385 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 4386 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4387 | EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) { |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4388 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, |
| 4389 | N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4390 | } |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 4391 | |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 4392 | // fold (sext_in_reg (sext x)) -> (sext x) |
| 4393 | // fold (sext_in_reg (aext x)) -> (sext x) |
| 4394 | // if x is small enough. |
| 4395 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { |
| 4396 | SDValue N00 = N0.getOperand(0); |
Evan Cheng | 003d7c4 | 2010-04-16 22:26:19 +0000 | [diff] [blame] | 4397 | if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits && |
| 4398 | (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4399 | return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1); |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 4402 | // 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] | 4403 | if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4404 | return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4405 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 4406 | // fold operands of sext_in_reg based on knowledge that the top bits are not |
| 4407 | // demanded. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4408 | if (SimplifyDemandedBits(SDValue(N, 0))) |
| 4409 | return SDValue(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4410 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4411 | // fold (sext_in_reg (load x)) -> (smaller sextload x) |
| 4412 | // 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] | 4413 | SDValue NarrowLoad = ReduceLoadWidth(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4414 | if (NarrowLoad.getNode()) |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4415 | return NarrowLoad; |
| 4416 | |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4417 | // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) |
| 4418 | // 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] | 4419 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 4420 | if (N0.getOpcode() == ISD::SRL) { |
| 4421 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4422 | if (ShAmt->getZExtValue()+EVTBits <= VTBits) { |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 4423 | // We can turn this into an SRA iff the input to the SRL is already sign |
| 4424 | // extended enough. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4425 | unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4426 | if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4427 | return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, |
| 4428 | N0.getOperand(0), N0.getOperand(1)); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 4429 | } |
| 4430 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4431 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 4432 | // fold (sext_inreg (extload x)) -> (sextload x) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4433 | if (ISD::isEXTLoad(N0.getNode()) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4434 | ISD::isUNINDEXEDLoad(N0.getNode()) && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 4435 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4436 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4437 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4438 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4439 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4440 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4441 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4442 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4443 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4444 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 4445 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4446 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4447 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 4448 | } |
| 4449 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4450 | if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 4451 | N0.hasOneUse() && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 4452 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4453 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4454 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4455 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4456 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 4457 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4458 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| 4459 | EVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4460 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4461 | LN0->getAlignment()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 4462 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4463 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4464 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 4465 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4466 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4469 | SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { |
| 4470 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4471 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4472 | |
| 4473 | // noop truncate |
| 4474 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4475 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4476 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4477 | if (isa<ConstantSDNode>(N0)) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4478 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4479 | // fold (truncate (truncate x)) -> (truncate x) |
| 4480 | if (N0.getOpcode() == ISD::TRUNCATE) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4481 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4482 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | 7f893c0 | 2010-04-07 18:13:33 +0000 | [diff] [blame] | 4483 | if (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 4484 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 4485 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4486 | if (N0.getOperand(0).getValueType().bitsLT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4487 | // 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] | 4488 | return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, |
| 4489 | N0.getOperand(0)); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4490 | else if (N0.getOperand(0).getValueType().bitsGT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4491 | // 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] | 4492 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4493 | else |
| 4494 | // 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] | 4495 | // and the truncate. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4496 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4497 | } |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 4498 | |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4499 | // See if we can simplify the input to this truncate through knowledge that |
| 4500 | // only the low bits are being used. For example "trunc (or (shl x, 8), y)" |
| 4501 | // -> trunc y |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4502 | SDValue Shorter = |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4503 | GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4504 | VT.getSizeInBits())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4505 | if (Shorter.getNode()) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4506 | return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 4507 | |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 4508 | // fold (truncate (load x)) -> (smaller load x) |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 4509 | // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4510 | if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { |
| 4511 | SDValue Reduced = ReduceLoadWidth(N); |
| 4512 | if (Reduced.getNode()) |
| 4513 | return Reduced; |
| 4514 | } |
| 4515 | |
| 4516 | // Simplify the operands using demanded-bits information. |
| 4517 | if (!VT.isVector() && |
| 4518 | SimplifyDemandedBits(SDValue(N, 0))) |
| 4519 | return SDValue(N, 0); |
| 4520 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4521 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4524 | static SDNode *getBuildPairElt(SDNode *N, unsigned i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4525 | SDValue Elt = N->getOperand(i); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4526 | if (Elt.getOpcode() != ISD::MERGE_VALUES) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4527 | return Elt.getNode(); |
| 4528 | return Elt.getOperand(Elt.getResNo()).getNode(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | /// CombineConsecutiveLoads - build_pair (load, load) -> load |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4532 | /// if load locations are consecutive. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4533 | SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4534 | assert(N->getOpcode() == ISD::BUILD_PAIR); |
| 4535 | |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 4536 | LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); |
| 4537 | LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 4538 | if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || |
| 4539 | LD1->getPointerInfo().getAddrSpace() != |
| 4540 | LD2->getPointerInfo().getAddrSpace()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4541 | return SDValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4542 | EVT LD1VT = LD1->getValueType(0); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4543 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4544 | if (ISD::isNON_EXTLoad(LD2) && |
| 4545 | LD2->hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4546 | // If both are volatile this would reduce the number of volatile loads. |
| 4547 | // 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] | 4548 | !LD1->isVolatile() && |
| 4549 | !LD2->isVolatile() && |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 4550 | DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) { |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 4551 | unsigned Align = LD1->getAlignment(); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 4552 | unsigned NewAlign = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4553 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4554 | |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4555 | if (NewAlign <= Align && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4556 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 4557 | return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 4558 | LD1->getBasePtr(), LD1->getPointerInfo(), |
| 4559 | false, false, Align); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4560 | } |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4561 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4562 | return SDValue(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4565 | SDValue DAGCombiner::visitBITCAST(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4566 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4567 | EVT VT = N->getValueType(0); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 4568 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4569 | // If the input is a BUILD_VECTOR with all constant elements, fold this now. |
| 4570 | // Only do this before legalize, since afterward the target may be depending |
| 4571 | // on the bitconvert. |
| 4572 | // First check to see if this is all constant. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4573 | if (!LegalTypes && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4574 | N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4575 | VT.isVector()) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4576 | bool isSimple = true; |
| 4577 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) |
| 4578 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && |
| 4579 | N0.getOperand(i).getOpcode() != ISD::Constant && |
| 4580 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4581 | isSimple = false; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4582 | break; |
| 4583 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4584 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4585 | EVT DestEltVT = N->getValueType(0).getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4586 | assert(!DestEltVT.isVector() && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4587 | "Element type of vector ValueType must not be vector!"); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4588 | if (isSimple) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4589 | return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4590 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4591 | |
Dan Gohman | 3dd168d | 2008-09-05 01:58:21 +0000 | [diff] [blame] | 4592 | // If the input is a constant, let getNode fold it. |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 4593 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4594 | SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0); |
Dan Gohman | a407ca1 | 2009-08-10 23:15:10 +0000 | [diff] [blame] | 4595 | if (Res.getNode() != N) { |
| 4596 | if (!LegalOperations || |
| 4597 | TLI.isOperationLegal(Res.getNode()->getOpcode(), VT)) |
| 4598 | return Res; |
| 4599 | |
| 4600 | // Folding it resulted in an illegal node, and it's too late to |
| 4601 | // do that. Clean up the old node and forego the transformation. |
| 4602 | // Ideally this won't happen very often, because instcombine |
| 4603 | // and the earlier dagcombine runs (where illegal nodes are |
| 4604 | // permitted) should have folded most of them already. |
| 4605 | DAG.DeleteNode(Res.getNode()); |
| 4606 | } |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 4607 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4608 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4609 | // (conv (conv x, t1), t2) -> (conv x, t2) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4610 | if (N0.getOpcode() == ISD::BITCAST) |
| 4611 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4612 | N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4613 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 4614 | // fold (conv (load x)) -> (load (conv*)x) |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 4615 | // 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] | 4616 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4617 | // Do not change the width of a volatile load. |
| 4618 | !cast<LoadSDNode>(N0)->isVolatile() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4619 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4620 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 4621 | unsigned Align = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4622 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 4623 | unsigned OrigAlign = LN0->getAlignment(); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4624 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 4625 | if (Align <= OrigAlign) { |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4626 | SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 4627 | LN0->getBasePtr(), LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4628 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 4629 | OrigAlign); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 4630 | AddToWorkList(N); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4631 | CombineTo(N0.getNode(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4632 | DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4633 | N0.getValueType(), Load), |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 4634 | Load.getValue(1)); |
| 4635 | return Load; |
| 4636 | } |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 4637 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4638 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4639 | // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) |
| 4640 | // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4641 | // This often reduces constant pool loads. |
| 4642 | if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4643 | N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4644 | SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4645 | N0.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4646 | AddToWorkList(NewConv.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4647 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4648 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4649 | if (N0.getOpcode() == ISD::FNEG) |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4650 | return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, |
| 4651 | NewConv, DAG.getConstant(SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4652 | assert(N0.getOpcode() == ISD::FABS); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4653 | return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, |
| 4654 | NewConv, DAG.getConstant(~SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4655 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4656 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4657 | // fold (bitconvert (fcopysign cst, x)) -> |
| 4658 | // (or (and (bitconvert x), sign), (and cst, (not sign))) |
| 4659 | // Note that we don't handle (copysign x, cst) because this can always be |
| 4660 | // folded to an fneg or fabs. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4661 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 4662 | isa<ConstantFPSDNode>(N0.getOperand(0)) && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4663 | VT.isInteger() && !VT.isVector()) { |
| 4664 | unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits(); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4665 | EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 4666 | if (isTypeLegal(IntXVT)) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4667 | SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4668 | IntXVT, N0.getOperand(1)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4669 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4670 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4671 | // If X has a different width than the result/lhs, sext it or truncate it. |
| 4672 | unsigned VTWidth = VT.getSizeInBits(); |
| 4673 | if (OrigXWidth < VTWidth) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4674 | X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4675 | AddToWorkList(X.getNode()); |
| 4676 | } else if (OrigXWidth > VTWidth) { |
| 4677 | // To get the sign bit in the right place, we have to shift it right |
| 4678 | // before truncating. |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4679 | X = DAG.getNode(ISD::SRL, X.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4680 | X.getValueType(), X, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4681 | DAG.getConstant(OrigXWidth-VTWidth, X.getValueType())); |
| 4682 | AddToWorkList(X.getNode()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4683 | X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4684 | AddToWorkList(X.getNode()); |
| 4685 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4686 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4687 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4688 | X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4689 | X, DAG.getConstant(SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4690 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4691 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4692 | SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4693 | VT, N0.getOperand(0)); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4694 | Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4695 | Cst, DAG.getConstant(~SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4696 | AddToWorkList(Cst.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4697 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 4698 | return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4699 | } |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 4700 | } |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4701 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4702 | // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4703 | if (N0.getOpcode() == ISD::BUILD_PAIR) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4704 | SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT); |
| 4705 | if (CombineLD.getNode()) |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4706 | return CombineLD; |
| 4707 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4708 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4709 | return SDValue(); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4712 | SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4713 | EVT VT = N->getValueType(0); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 4714 | return CombineConsecutiveLoads(N, VT); |
| 4715 | } |
| 4716 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4717 | /// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4718 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4719 | /// destination element value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4720 | SDValue DAGCombiner:: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4721 | ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4722 | EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4723 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4724 | // If this is already the right type, we're done. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4725 | if (SrcEltVT == DstEltVT) return SDValue(BV, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4726 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4727 | unsigned SrcBitSize = SrcEltVT.getSizeInBits(); |
| 4728 | unsigned DstBitSize = DstEltVT.getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4729 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4730 | // If this is a conversion of N elements of one type to N elements of another |
| 4731 | // type, convert each element. This handles FP<->INT cases. |
| 4732 | if (SrcBitSize == DstBitSize) { |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 4733 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 4734 | BV->getValueType(0).getVectorNumElements()); |
| 4735 | |
| 4736 | // Due to the FP element handling below calling this routine recursively, |
| 4737 | // we can end up with a scalar-to-vector node here. |
| 4738 | if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4739 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 4740 | DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 4741 | DstEltVT, BV->getOperand(0))); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4742 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4743 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4744 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 4745 | SDValue Op = BV->getOperand(i); |
| 4746 | // If the vector element type is not legal, the BUILD_VECTOR operands |
| 4747 | // are promoted and implicitly truncated. Make that explicit here. |
Bob Wilson | c885165 | 2009-04-20 17:27:09 +0000 | [diff] [blame] | 4748 | if (Op.getValueType() != SrcEltVT) |
| 4749 | Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4750 | Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(), |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 4751 | DstEltVT, Op)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4752 | AddToWorkList(Ops.back().getNode()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 4753 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 4754 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 4755 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4756 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4757 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4758 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 4759 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 4760 | // int first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4761 | if (SrcEltVT.isFloatingPoint()) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4762 | // Convert the input float vector to a int vector where the elements are the |
| 4763 | // same sizes. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4764 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4765 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4766 | BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4767 | SrcEltVT = IntVT; |
| 4768 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4770 | // Now we know the input is an integer vector. If the output is a FP type, |
| 4771 | // convert to integer first, then to FP of the right size. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4772 | if (DstEltVT.isFloatingPoint()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4773 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4774 | EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4775 | SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4776 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4777 | // Next, convert to FP elements of the same size. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4778 | return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4779 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4780 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4781 | // Okay, we know the src/dst types are both integers of differing types. |
| 4782 | // Handling growing first. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4783 | assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4784 | if (SrcBitSize < DstBitSize) { |
| 4785 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4786 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4787 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4788 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4789 | i += NumInputsPerOutput) { |
| 4790 | bool isLE = TLI.isLittleEndian(); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4791 | APInt NewBits = APInt(DstBitSize, 0); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4792 | bool EltIsUndef = true; |
| 4793 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 4794 | // Shift the previously computed bits over. |
| 4795 | NewBits <<= SrcBitSize; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4796 | SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4797 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 4798 | EltIsUndef = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4799 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4800 | NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). |
Dan Gohman | 58c2587 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 4801 | zextOrTrunc(SrcBitSize).zext(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4802 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4803 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4804 | if (EltIsUndef) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4805 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4806 | else |
| 4807 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 4808 | } |
| 4809 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4810 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 4811 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 4812 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4813 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4814 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4815 | // Finally, this must be the case where we are shrinking elements: each input |
| 4816 | // turns into multiple outputs. |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 4817 | bool isS2V = ISD::isScalarToVector(BV); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4818 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 4819 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 4820 | NumOutputsPerInput*BV->getNumOperands()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4821 | SmallVector<SDValue, 8> Ops; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4822 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4823 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4824 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 4825 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4826 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4827 | continue; |
| 4828 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4829 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4830 | APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))-> |
| 4831 | getAPIntValue().zextOrTrunc(SrcBitSize); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4832 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4833 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4834 | APInt ThisVal = OpVal.trunc(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4835 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4836 | if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal) |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 4837 | // Simply turn this into a SCALAR_TO_VECTOR of the new type. |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4838 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT, |
| 4839 | Ops[0]); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4840 | OpVal = OpVal.lshr(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | // 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] | 4844 | if (TLI.isBigEndian()) |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4845 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 4846 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4847 | |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 4848 | return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT, |
| 4849 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4852 | SDValue DAGCombiner::visitFADD(SDNode *N) { |
| 4853 | SDValue N0 = N->getOperand(0); |
| 4854 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 4855 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 4856 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4857 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4858 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4859 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4860 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4861 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4862 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 4863 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4864 | |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4865 | // fold (fadd c1, c2) -> (fadd c1, c2) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4866 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4867 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 4868 | // canonicalize constant to RHS |
| 4869 | if (N0CFP && !N1CFP) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4870 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0); |
| 4871 | // fold (fadd A, 0) -> A |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 4872 | if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero()) |
| 4873 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4874 | // fold (fadd A, (fneg B)) -> (fsub A, B) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4875 | if (isNegatibleForFree(N1, LegalOperations) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4876 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4877 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4878 | // fold (fadd (fneg A), B) -> (fsub B, A) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4879 | if (isNegatibleForFree(N0, LegalOperations) == 2) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4880 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4881 | GetNegatedExpression(N0, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4882 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 4883 | // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2)) |
| 4884 | if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4885 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4886 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0), |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4887 | DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, |
| 4888 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4889 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4890 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4893 | SDValue DAGCombiner::visitFSUB(SDNode *N) { |
| 4894 | SDValue N0 = N->getOperand(0); |
| 4895 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 4896 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 4897 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4898 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4899 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4900 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4901 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4902 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4903 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 4904 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4905 | |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 4906 | // fold (fsub c1, c2) -> c1-c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4907 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 4908 | return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4909 | // fold (fsub A, 0) -> A |
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 4910 | if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero()) |
| 4911 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4912 | // fold (fsub 0, B) -> -B |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 4913 | if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) { |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4914 | if (isNegatibleForFree(N1, LegalOperations)) |
| 4915 | return GetNegatedExpression(N1, DAG, LegalOperations); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 4916 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4917 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 4918 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4919 | // fold (fsub A, (fneg B)) -> (fadd A, B) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4920 | if (isNegatibleForFree(N1, LegalOperations)) |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 4921 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4922 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4923 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4924 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4927 | SDValue DAGCombiner::visitFMUL(SDNode *N) { |
| 4928 | SDValue N0 = N->getOperand(0); |
| 4929 | SDValue N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 4930 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 4931 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4932 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4933 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4934 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4935 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4936 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4937 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 4938 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4939 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 4940 | // fold (fmul c1, c2) -> c1*c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4941 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4942 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 4943 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 4944 | if (N0CFP && !N1CFP) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4945 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0); |
| 4946 | // fold (fmul A, 0) -> 0 |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 4947 | if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero()) |
| 4948 | return N1; |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 4949 | // fold (fmul A, 0) -> 0, vector edition. |
| 4950 | if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode())) |
| 4951 | return N1; |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 4952 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 4953 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4954 | return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0); |
Dan Gohman | eb1fedc | 2009-08-10 16:50:32 +0000 | [diff] [blame] | 4955 | // fold (fmul X, -1.0) -> (fneg X) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 4956 | if (N1CFP && N1CFP->isExactlyValue(-1.0)) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 4957 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4958 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4959 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4960 | // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4961 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) { |
| 4962 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 4963 | // Both can be negated for free, check to see if at least one is cheaper |
| 4964 | // negated. |
| 4965 | if (LHSNeg == 2 || RHSNeg == 2) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4966 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4967 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 4968 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 4969 | } |
| 4970 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 4972 | // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) |
| 4973 | if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4974 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4975 | return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4976 | DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4977 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4978 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4979 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4982 | SDValue DAGCombiner::visitFDIV(SDNode *N) { |
| 4983 | SDValue N0 = N->getOperand(0); |
| 4984 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 4985 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 4986 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4987 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4988 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4989 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4990 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4991 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4992 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 4993 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4994 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 4995 | // fold (fdiv c1, c2) -> c1/c2 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4996 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 4997 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4998 | |
| 4999 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5000 | // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5001 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) { |
| 5002 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5003 | // Both can be negated for free, check to see if at least one is cheaper |
| 5004 | // negated. |
| 5005 | if (LHSNeg == 2 || RHSNeg == 2) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5006 | return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5007 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 5008 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 5009 | } |
| 5010 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5011 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5012 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5015 | SDValue DAGCombiner::visitFREM(SDNode *N) { |
| 5016 | SDValue N0 = N->getOperand(0); |
| 5017 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5018 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5019 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5020 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5021 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5022 | // fold (frem c1, c2) -> fmod(c1,c2) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5023 | if (N0CFP && N1CFP && VT != MVT::ppcf128) |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 5024 | return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5025 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5026 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5029 | SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 5030 | SDValue N0 = N->getOperand(0); |
| 5031 | SDValue N1 = N->getOperand(1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5032 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 5033 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5034 | EVT VT = N->getValueType(0); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5035 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5036 | if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 5037 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5038 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5039 | if (N1CFP) { |
Dale Johannesen | e6c1742 | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 5040 | const APFloat& V = N1CFP->getValueAPF(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5041 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 5042 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5043 | if (!V.isNegative()) { |
| 5044 | if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5045 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5046 | } else { |
| 5047 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5048 | return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 5049 | DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0)); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 5050 | } |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5051 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5052 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5053 | // copysign(fabs(x), y) -> copysign(x, y) |
| 5054 | // copysign(fneg(x), y) -> copysign(x, y) |
| 5055 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 5056 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 5057 | N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5058 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5059 | N0.getOperand(0), N1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5060 | |
| 5061 | // copysign(x, abs(y)) -> abs(x) |
| 5062 | if (N1.getOpcode() == ISD::FABS) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5063 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5064 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5065 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 5066 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5067 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5068 | N0, N1.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5069 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5070 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 5071 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 5072 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5073 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5074 | N0, N1.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5075 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5076 | return SDValue(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5079 | SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
| 5080 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5081 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5082 | EVT VT = N->getValueType(0); |
| 5083 | EVT OpVT = N0.getValueType(); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5084 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5085 | // fold (sint_to_fp c1) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5086 | if (N0C && OpVT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5087 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5088 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5089 | // If the input is a legal type, and SINT_TO_FP is not legal on this target, |
| 5090 | // but UINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 5091 | if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && |
| 5092 | TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5093 | // 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] | 5094 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5095 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5096 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5097 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5098 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5101 | SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
| 5102 | SDValue N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5103 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5104 | EVT VT = N->getValueType(0); |
| 5105 | EVT OpVT = N0.getValueType(); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5106 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5107 | // fold (uint_to_fp c1) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5108 | if (N0C && OpVT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5109 | return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5111 | // If the input is a legal type, and UINT_TO_FP is not legal on this target, |
| 5112 | // but SINT_TO_FP is legal on this target, try to convert. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 5113 | if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && |
| 5114 | TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5115 | // 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] | 5116 | if (DAG.SignBitIsZero(N0)) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5117 | return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 5118 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5119 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5120 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5123 | SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
| 5124 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5125 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5126 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5127 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5128 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5129 | if (N0CFP) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5130 | return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0); |
| 5131 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5132 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5135 | SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
| 5136 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5137 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5138 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5139 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5140 | // fold (fp_to_uint c1fp) -> c1 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5141 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5142 | return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0); |
| 5143 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5144 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5147 | SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { |
| 5148 | SDValue N0 = N->getOperand(0); |
| 5149 | SDValue N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5150 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5151 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5152 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5153 | // fold (fp_round c1fp) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5154 | if (N0CFP && N0.getValueType() != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5155 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5156 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 5157 | // fold (fp_round (fp_extend x)) -> x |
| 5158 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 5159 | return N0.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5160 | |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 5161 | // fold (fp_round (fp_round x)) -> (fp_round x) |
| 5162 | if (N0.getOpcode() == ISD::FP_ROUND) { |
| 5163 | // This is a value preserving truncation if both round's are. |
| 5164 | bool IsTrunc = N->getConstantOperandVal(1) == 1 && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5165 | N0.getNode()->getConstantOperandVal(1) == 1; |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5166 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0), |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 5167 | DAG.getIntPtrConstant(IsTrunc)); |
| 5168 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5169 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 5170 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5171 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5172 | SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT, |
| 5173 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5174 | AddToWorkList(Tmp.getNode()); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5175 | return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, |
| 5176 | Tmp, N0.getOperand(1)); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 5177 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 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::visitFP_ROUND_INREG(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); |
| 5185 | EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5186 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5187 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5188 | // fold (fp_round_inreg c1fp) -> c1fp |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5189 | if (N0CFP && isTypeLegal(EVT)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 5190 | SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5191 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5192 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5193 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5194 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5197 | SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { |
| 5198 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5199 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5200 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5201 | |
Chris Lattner | 5938bef | 2007-12-29 06:55:23 +0000 | [diff] [blame] | 5202 | // 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] | 5203 | if (N->hasOneUse() && |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5204 | N->use_begin()->getOpcode() == ISD::FP_ROUND) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5205 | return SDValue(); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5206 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5207 | // fold (fp_extend c1fp) -> c1fp |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5208 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5209 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5210 | |
| 5211 | // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the |
| 5212 | // value of X. |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 5213 | if (N0.getOpcode() == ISD::FP_ROUND |
| 5214 | && N0.getNode()->getConstantOperandVal(1) == 1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5215 | SDValue In = N0.getOperand(0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5216 | if (In.getValueType() == VT) return In; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5217 | if (VT.bitsLT(In.getValueType())) |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5218 | return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, |
| 5219 | In, N0.getOperand(1)); |
| 5220 | return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5221 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5222 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5223 | // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5224 | if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5225 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5226 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5227 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 5228 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N->getDebugLoc(), |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5229 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5230 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5231 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5232 | LN0->isVolatile(), LN0->isNonTemporal(), |
| 5233 | LN0->getAlignment()); |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 5234 | CombineTo(N, ExtLoad); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 5235 | CombineTo(N0.getNode(), |
| 5236 | DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), |
| 5237 | N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)), |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 5238 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5239 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 5240 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5241 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5242 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5245 | SDValue DAGCombiner::visitFNEG(SDNode *N) { |
| 5246 | SDValue N0 = N->getOperand(0); |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 5247 | EVT VT = N->getValueType(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5248 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5249 | if (isNegatibleForFree(N0, LegalOperations)) |
| 5250 | return GetNegatedExpression(N0, DAG, LegalOperations); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 5251 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5252 | // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading |
| 5253 | // constant pool values. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5254 | if (N0.getOpcode() == ISD::BITCAST && |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 5255 | !VT.isVector() && |
| 5256 | N0.getNode()->hasOneUse() && |
| 5257 | N0.getOperand(0).getValueType().isInteger()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5258 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5259 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5260 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 5261 | Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int, |
| 5262 | DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5263 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5264 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 5265 | VT, Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5266 | } |
| 5267 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5268 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5269 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5272 | SDValue DAGCombiner::visitFABS(SDNode *N) { |
| 5273 | SDValue N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 5274 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5275 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5276 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5277 | // fold (fabs c1) -> fabs(c1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5278 | if (N0CFP && VT != MVT::ppcf128) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5279 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5280 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5281 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5282 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5283 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 5284 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 5285 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5286 | return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5287 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5288 | // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading |
| 5289 | // constant pool values. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5290 | if (N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5291 | N0.getOperand(0).getValueType().isInteger() && |
| 5292 | !N0.getOperand(0).getValueType().isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5293 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5294 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5295 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5296 | Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int, |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 5297 | DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5298 | AddToWorkList(Int.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5299 | return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5300 | N->getValueType(0), Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5301 | } |
| 5302 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5303 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5304 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5307 | SDValue DAGCombiner::visitBRCOND(SDNode *N) { |
| 5308 | SDValue Chain = N->getOperand(0); |
| 5309 | SDValue N1 = N->getOperand(1); |
| 5310 | SDValue N2 = N->getOperand(2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5311 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 5312 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 5313 | // branch. However that doesn't happen very often in normal code, because |
| 5314 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 5315 | // If we did this folding here, it would be necessary to update the |
| 5316 | // MachineBasicBlock CFG, which is awkward. |
| 5317 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 5318 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 5319 | // on the target. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5320 | if (N1.getOpcode() == ISD::SETCC && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5321 | TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) { |
| 5322 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5323 | Chain, N1.getOperand(2), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 5324 | N1.getOperand(0), N1.getOperand(1), N2); |
| 5325 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5326 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 5327 | if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || |
| 5328 | ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && |
| 5329 | (N1.getOperand(0).hasOneUse() && |
| 5330 | N1.getOperand(0).getOpcode() == ISD::SRL))) { |
| 5331 | SDNode *Trunc = 0; |
| 5332 | if (N1.getOpcode() == ISD::TRUNCATE) { |
| 5333 | // Look pass the truncate. |
| 5334 | Trunc = N1.getNode(); |
| 5335 | N1 = N1.getOperand(0); |
| 5336 | } |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 5337 | |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5338 | // Match this pattern so that we can generate simpler code: |
| 5339 | // |
| 5340 | // %a = ... |
| 5341 | // %b = and i32 %a, 2 |
| 5342 | // %c = srl i32 %b, 1 |
| 5343 | // brcond i32 %c ... |
| 5344 | // |
| 5345 | // into |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5346 | // |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5347 | // %a = ... |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 5348 | // %b = and i32 %a, 2 |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5349 | // %c = setcc eq %b, 0 |
| 5350 | // brcond %c ... |
| 5351 | // |
| 5352 | // This applies only when the AND constant value has one bit set and the |
| 5353 | // SRL constant is equal to the log2 of the AND constant. The back-end is |
| 5354 | // smart enough to convert the result into a TEST/JMP sequence. |
| 5355 | SDValue Op0 = N1.getOperand(0); |
| 5356 | SDValue Op1 = N1.getOperand(1); |
| 5357 | |
| 5358 | if (Op0.getOpcode() == ISD::AND && |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5359 | Op1.getOpcode() == ISD::Constant) { |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5360 | SDValue AndOp1 = Op0.getOperand(1); |
| 5361 | |
| 5362 | if (AndOp1.getOpcode() == ISD::Constant) { |
| 5363 | const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); |
| 5364 | |
| 5365 | if (AndConst.isPowerOf2() && |
| 5366 | cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { |
| 5367 | SDValue SetCC = |
| 5368 | DAG.getSetCC(N->getDebugLoc(), |
| 5369 | TLI.getSetCCResultType(Op0.getValueType()), |
| 5370 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 5371 | ISD::SETNE); |
| 5372 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 5373 | SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 5374 | MVT::Other, Chain, SetCC, N2); |
| 5375 | // Don't add the new BRCond into the worklist or else SimplifySelectCC |
| 5376 | // will convert it back to (X & C1) >> C2. |
| 5377 | CombineTo(N, NewBRCond, false); |
| 5378 | // Truncate is dead. |
| 5379 | if (Trunc) { |
| 5380 | removeFromWorkList(Trunc); |
| 5381 | DAG.DeleteNode(Trunc); |
| 5382 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5383 | // Replace the uses of SRL with SETCC |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5384 | WorkListRemover DeadNodes(*this); |
| 5385 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5386 | removeFromWorkList(N1.getNode()); |
| 5387 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 5388 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | } |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 5392 | |
| 5393 | if (Trunc) |
| 5394 | // Restore N1 if the above transformation doesn't match. |
| 5395 | N1 = N->getOperand(1); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5396 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5397 | |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5398 | // Transform br(xor(x, y)) -> br(x != y) |
| 5399 | // Transform br(xor(xor(x,y), 1)) -> br (x == y) |
| 5400 | if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { |
| 5401 | SDNode *TheXor = N1.getNode(); |
| 5402 | SDValue Op0 = TheXor->getOperand(0); |
| 5403 | SDValue Op1 = TheXor->getOperand(1); |
| 5404 | if (Op0.getOpcode() == Op1.getOpcode()) { |
| 5405 | // Avoid missing important xor optimizations. |
| 5406 | SDValue Tmp = visitXOR(TheXor); |
Bill Wendling | 86c5abb | 2010-04-20 01:25:01 +0000 | [diff] [blame] | 5407 | if (Tmp.getNode() && Tmp.getNode() != TheXor) { |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5408 | DEBUG(dbgs() << "\nReplacing.8 "; |
| 5409 | TheXor->dump(&DAG); |
| 5410 | dbgs() << "\nWith: "; |
| 5411 | Tmp.getNode()->dump(&DAG); |
| 5412 | dbgs() << '\n'); |
| 5413 | WorkListRemover DeadNodes(*this); |
| 5414 | DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes); |
| 5415 | removeFromWorkList(TheXor); |
| 5416 | DAG.DeleteNode(TheXor); |
| 5417 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 5418 | MVT::Other, Chain, Tmp, N2); |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { |
| 5423 | bool Equal = false; |
| 5424 | if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0)) |
| 5425 | if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() && |
| 5426 | Op0.getOpcode() == ISD::XOR) { |
| 5427 | TheXor = Op0.getNode(); |
| 5428 | Equal = true; |
| 5429 | } |
| 5430 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 5431 | EVT SetCCVT = N1.getValueType(); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5432 | if (LegalTypes) |
| 5433 | SetCCVT = TLI.getSetCCResultType(SetCCVT); |
| 5434 | SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(), |
| 5435 | SetCCVT, |
| 5436 | Op0, Op1, |
| 5437 | Equal ? ISD::SETEQ : ISD::SETNE); |
| 5438 | // Replace the uses of XOR with SETCC |
| 5439 | WorkListRemover DeadNodes(*this); |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 5440 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes); |
| 5441 | removeFromWorkList(N1.getNode()); |
| 5442 | DAG.DeleteNode(N1.getNode()); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5443 | return DAG.getNode(ISD::BRCOND, N->getDebugLoc(), |
| 5444 | MVT::Other, Chain, SetCC, N2); |
| 5445 | } |
| 5446 | } |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 5447 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5448 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 5451 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 5452 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5453 | SDValue DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 5454 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5455 | SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5456 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 5457 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 5458 | // branch. However that doesn't happen very often in normal code, because |
| 5459 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 5460 | // If we did this folding here, it would be necessary to update the |
| 5461 | // MachineBasicBlock CFG, which is awkward. |
| 5462 | |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 5463 | // Use SimplifySetCC to simplify SETCC's. |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 5464 | SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 5465 | CondLHS, CondRHS, CC->get(), N->getDebugLoc(), |
| 5466 | false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5467 | if (Simp.getNode()) AddToWorkList(Simp.getNode()); |
Chris Lattner | 30f73e7 | 2006-10-14 03:52:46 +0000 | [diff] [blame] | 5468 | |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 5469 | // fold to a simpler setcc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5470 | if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5471 | return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5472 | N->getOperand(0), Simp.getOperand(2), |
| 5473 | Simp.getOperand(0), Simp.getOperand(1), |
| 5474 | N->getOperand(4)); |
| 5475 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5476 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 5479 | /// CombineToPreIndexedLoadStore - Try turning a load / store into a |
| 5480 | /// 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] | 5481 | /// and it has other uses besides the load / store. After the |
| 5482 | /// transformation, the new indexed load / store has effectively folded |
| 5483 | /// the add / subtract in and all of its other uses are redirected to the |
| 5484 | /// new load / store. |
| 5485 | bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5486 | if (!LegalOperations) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5487 | return false; |
| 5488 | |
| 5489 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5490 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5491 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5492 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 5493 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 5494 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5495 | VT = LD->getMemoryVT(); |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5496 | if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5497 | !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) |
| 5498 | return false; |
| 5499 | Ptr = LD->getBasePtr(); |
| 5500 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 5501 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 5502 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5503 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5504 | if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && |
| 5505 | !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) |
| 5506 | return false; |
| 5507 | Ptr = ST->getBasePtr(); |
| 5508 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5509 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5510 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5511 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5512 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5513 | // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail |
| 5514 | // out. There is no reason to make this a preinc/predec. |
| 5515 | if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5516 | Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5517 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5518 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5519 | // Ask the target to do addressing mode selection. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5520 | SDValue BasePtr; |
| 5521 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5522 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 5523 | if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) |
| 5524 | return false; |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 5525 | // Don't create a indexed load / store with zero offset. |
| 5526 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 5527 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 5528 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5529 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 5530 | // Try turning it into a pre-indexed load / store except when: |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 5531 | // 1) The new base ptr is a frame index. |
| 5532 | // 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] | 5533 | // predecessor of the value being stored. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 5534 | // 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] | 5535 | // that would create a cycle. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 5536 | // 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] | 5537 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 5538 | // Check #1. Preinc'ing a frame index would require copying the stack pointer |
| 5539 | // (plus the implicit offset) to a register to preinc anyway. |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 5540 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 5541 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5542 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 5543 | // Check #2. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5544 | if (!isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5545 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5546 | if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5547 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5548 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5549 | |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 5550 | // Now check for #3 and #4. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5551 | bool RealUse = false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5552 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 5553 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 5554 | SDNode *Use = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5555 | if (Use == N) |
| 5556 | continue; |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 5557 | if (Use->isPredecessorOf(N)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5558 | return false; |
| 5559 | |
| 5560 | if (!((Use->getOpcode() == ISD::LOAD && |
| 5561 | cast<LoadSDNode>(Use)->getBasePtr() == Ptr) || |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 5562 | (Use->getOpcode() == ISD::STORE && |
| 5563 | cast<StoreSDNode>(Use)->getBasePtr() == Ptr))) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5564 | RealUse = true; |
| 5565 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5566 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5567 | if (!RealUse) |
| 5568 | return false; |
| 5569 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5570 | SDValue Result; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5571 | if (isLoad) |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5572 | Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 5573 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5574 | else |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5575 | Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 5576 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5577 | ++PreIndexedNodes; |
| 5578 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5579 | DEBUG(dbgs() << "\nReplacing.4 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5580 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5581 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5582 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5583 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5584 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5585 | if (isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5586 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5587 | &DeadNodes); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5588 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5589 | &DeadNodes); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5590 | } else { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5591 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5592 | &DeadNodes); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5595 | // Finally, since the node is now dead, remove it from the graph. |
| 5596 | DAG.DeleteNode(N); |
| 5597 | |
| 5598 | // Replace the uses of Ptr with uses of the updated base value. |
| 5599 | DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5600 | &DeadNodes); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5601 | removeFromWorkList(Ptr.getNode()); |
| 5602 | DAG.DeleteNode(Ptr.getNode()); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5603 | |
| 5604 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 5607 | /// CombineToPostIndexedLoadStore - Try to combine a load / store with a |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5608 | /// add / sub of the base pointer node into a post-indexed load / store. |
| 5609 | /// The transformation folded the add / subtract into the new indexed |
| 5610 | /// load / store effectively and all of its uses are redirected to the |
| 5611 | /// new load / store. |
| 5612 | bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5613 | if (!LegalOperations) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5614 | return false; |
| 5615 | |
| 5616 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5617 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5618 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5619 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 5620 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 5621 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5622 | VT = LD->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5623 | if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && |
| 5624 | !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) |
| 5625 | return false; |
| 5626 | Ptr = LD->getBasePtr(); |
| 5627 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 5628 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 5629 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5630 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5631 | if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && |
| 5632 | !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) |
| 5633 | return false; |
| 5634 | Ptr = ST->getBasePtr(); |
| 5635 | isLoad = false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5636 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5637 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5638 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5639 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5640 | if (Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5641 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5642 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5643 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 5644 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 5645 | SDNode *Op = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5646 | if (Op == N || |
| 5647 | (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) |
| 5648 | continue; |
| 5649 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5650 | SDValue BasePtr; |
| 5651 | SDValue Offset; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5652 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 5653 | if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 5654 | // Don't create a indexed load / store with zero offset. |
| 5655 | if (isa<ConstantSDNode>(Offset) && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 5656 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 5657 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5658 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5659 | // Try turning it into a post-indexed load / store except when |
| 5660 | // 1) All uses are load / store ops that use it as base ptr. |
| 5661 | // 2) Op must be independent of N, i.e. Op is neither a predecessor |
| 5662 | // nor a successor of N. Otherwise, if Op is folded that would |
| 5663 | // create a cycle. |
| 5664 | |
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 5665 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
| 5666 | continue; |
| 5667 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5668 | // Check for #1. |
| 5669 | bool TryNext = false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5670 | for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(), |
| 5671 | EE = BasePtr.getNode()->use_end(); II != EE; ++II) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 5672 | SDNode *Use = *II; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5673 | if (Use == Ptr.getNode()) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5674 | continue; |
| 5675 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5676 | // If all the uses are load / store addresses, then don't do the |
| 5677 | // transformation. |
| 5678 | if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ |
| 5679 | bool RealUse = false; |
| 5680 | for (SDNode::use_iterator III = Use->use_begin(), |
| 5681 | EEE = Use->use_end(); III != EEE; ++III) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 5682 | SDNode *UseUse = *III; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5683 | if (!((UseUse->getOpcode() == ISD::LOAD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5684 | cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) || |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 5685 | (UseUse->getOpcode() == ISD::STORE && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5686 | cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use))) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5687 | RealUse = true; |
| 5688 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5689 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5690 | if (!RealUse) { |
| 5691 | TryNext = true; |
| 5692 | break; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5693 | } |
| 5694 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5695 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5696 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5697 | if (TryNext) |
| 5698 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5699 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5700 | // Check for #2 |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 5701 | if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5702 | SDValue Result = isLoad |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5703 | ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(), |
| 5704 | BasePtr, Offset, AM) |
| 5705 | : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(), |
| 5706 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5707 | ++PostIndexedNodes; |
| 5708 | ++NodesCombined; |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5709 | DEBUG(dbgs() << "\nReplacing.5 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5710 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5711 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5712 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5713 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5714 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5715 | if (isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5716 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5717 | &DeadNodes); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5718 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5719 | &DeadNodes); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5720 | } else { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5721 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5722 | &DeadNodes); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5723 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5724 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5725 | // Finally, since the node is now dead, remove it from the graph. |
| 5726 | DAG.DeleteNode(N); |
| 5727 | |
| 5728 | // Replace the uses of Use with uses of the updated base value. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5729 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5730 | Result.getValue(isLoad ? 1 : 0), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5731 | &DeadNodes); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5732 | removeFromWorkList(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5733 | DAG.DeleteNode(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 5734 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5735 | } |
| 5736 | } |
| 5737 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5738 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 5739 | return false; |
| 5740 | } |
| 5741 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5742 | SDValue DAGCombiner::visitLOAD(SDNode *N) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5743 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5744 | SDValue Chain = LD->getChain(); |
| 5745 | SDValue Ptr = LD->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5746 | |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 5747 | // If load is not volatile and there are no uses of the loaded value (and |
| 5748 | // the updated indexed value in case of indexed loads), change uses of the |
| 5749 | // chain value into uses of the chain input (i.e. delete the dead load). |
| 5750 | if (!LD->isVolatile()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5751 | if (N->getValueType(1) == MVT::Other) { |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 5752 | // Unindexed loads. |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 5753 | if (N->hasNUsesOfValue(0, 0)) { |
| 5754 | // It's not safe to use the two value CombineTo variant here. e.g. |
| 5755 | // v1, chain2 = load chain1, loc |
| 5756 | // v2, chain3 = load chain2, loc |
| 5757 | // v3 = add v2, c |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 5758 | // Now we replace use of chain2 with chain1. This makes the second load |
| 5759 | // 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] | 5760 | DEBUG(dbgs() << "\nReplacing.6 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5761 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5762 | dbgs() << "\nWith chain: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5763 | Chain.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5764 | dbgs() << "\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5765 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5766 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes); |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5767 | |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 5768 | if (N->use_empty()) { |
| 5769 | removeFromWorkList(N); |
| 5770 | DAG.DeleteNode(N); |
| 5771 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5772 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5773 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 5774 | } |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 5775 | } else { |
| 5776 | // Indexed loads. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5777 | assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 5778 | if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5779 | SDValue Undef = DAG.getUNDEF(N->getValueType(0)); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 5780 | DEBUG(dbgs() << "\nReplacing.7 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5781 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5782 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 5783 | Undef.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 5784 | dbgs() << " and 2 other values\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5785 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5786 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes); |
| 5787 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5788 | DAG.getUNDEF(N->getValueType(1)), |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5789 | &DeadNodes); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5790 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 5791 | removeFromWorkList(N); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 5792 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5793 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 5794 | } |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 5795 | } |
| 5796 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5797 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 5798 | // If this load is directly stored, replace the load value with the stored |
| 5799 | // value. |
| 5800 | // TODO: Handle store large -> read small portion. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5801 | // TODO: Handle TRUNCSTORE/LOADEXT |
Dan Gohman | b061c4b | 2008-03-31 20:32:52 +0000 | [diff] [blame] | 5802 | if (LD->getExtensionType() == ISD::NON_EXTLOAD && |
| 5803 | !LD->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5804 | if (ISD::isNON_TRUNCStore(Chain.getNode())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5805 | StoreSDNode *PrevST = cast<StoreSDNode>(Chain); |
| 5806 | if (PrevST->getBasePtr() == Ptr && |
| 5807 | PrevST->getValue().getValueType() == N->getValueType(0)) |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5808 | return CombineTo(N, Chain.getOperand(1), Chain); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5809 | } |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5810 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5811 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 5812 | // Try to infer better alignment information than the load already has. |
| 5813 | if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { |
| 5814 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 5815 | if (Align > LD->getAlignment()) |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 5816 | return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0), |
| 5817 | N->getDebugLoc(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5818 | Chain, Ptr, LD->getPointerInfo(), |
| 5819 | LD->getMemoryVT(), |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 5820 | LD->isVolatile(), LD->isNonTemporal(), Align); |
| 5821 | } |
| 5822 | } |
| 5823 | |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 5824 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 5825 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5826 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5827 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 5828 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 5829 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5830 | SDValue ReplLoad; |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5831 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 5832 | // Replace the chain to void dependency. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5833 | if (LD->getExtensionType() == ISD::NON_EXTLOAD) { |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5834 | ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5835 | BetterChain, Ptr, LD->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5836 | LD->isVolatile(), LD->isNonTemporal(), |
| 5837 | LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5838 | } else { |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 5839 | ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0), |
| 5840 | LD->getDebugLoc(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5841 | BetterChain, Ptr, LD->getPointerInfo(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5842 | LD->getMemoryVT(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5843 | LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5844 | LD->isNonTemporal(), |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 5845 | LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 5846 | } |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 5847 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 5848 | // Create token factor to keep old chain connected. |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 5849 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5850 | MVT::Other, Chain, ReplLoad.getValue(1)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5851 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 5852 | // Make sure the new and old chains are cleaned up. |
| 5853 | AddToWorkList(Token.getNode()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5854 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 5855 | // Replace uses with load result and token factor. Don't add users |
| 5856 | // to work list. |
| 5857 | return CombineTo(N, ReplLoad.getValue(0), Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 5861 | // Try transforming N to an indexed load. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 5862 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5863 | return SDValue(N, 0); |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 5864 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5865 | return SDValue(); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5868 | /// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the |
| 5869 | /// load is having specific bytes cleared out. If so, return the byte size |
| 5870 | /// being masked out and the shift amount. |
| 5871 | static std::pair<unsigned, unsigned> |
| 5872 | CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { |
| 5873 | std::pair<unsigned, unsigned> Result(0, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5874 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5875 | // Check for the structure we're looking for. |
| 5876 | if (V->getOpcode() != ISD::AND || |
| 5877 | !isa<ConstantSDNode>(V->getOperand(1)) || |
| 5878 | !ISD::isNormalLoad(V->getOperand(0).getNode())) |
| 5879 | return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5880 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 5881 | // Check the chain and pointer. |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5882 | LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 5883 | if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5884 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 5885 | // The store should be chained directly to the load or be an operand of a |
| 5886 | // tokenfactor. |
| 5887 | if (LD == Chain.getNode()) |
| 5888 | ; // ok. |
| 5889 | else if (Chain->getOpcode() != ISD::TokenFactor) |
| 5890 | return Result; // Fail. |
| 5891 | else { |
| 5892 | bool isOk = false; |
| 5893 | for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i) |
| 5894 | if (Chain->getOperand(i).getNode() == LD) { |
| 5895 | isOk = true; |
| 5896 | break; |
| 5897 | } |
| 5898 | if (!isOk) return Result; |
| 5899 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5900 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5901 | // This only handles simple types. |
| 5902 | if (V.getValueType() != MVT::i16 && |
| 5903 | V.getValueType() != MVT::i32 && |
| 5904 | V.getValueType() != MVT::i64) |
| 5905 | return Result; |
| 5906 | |
| 5907 | // Check the constant mask. Invert it so that the bits being masked out are |
| 5908 | // 0 and the bits being kept are 1. Use getSExtValue so that leading bits |
| 5909 | // follow the sign bit for uniformity. |
| 5910 | uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); |
| 5911 | unsigned NotMaskLZ = CountLeadingZeros_64(NotMask); |
| 5912 | if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. |
| 5913 | unsigned NotMaskTZ = CountTrailingZeros_64(NotMask); |
| 5914 | if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. |
| 5915 | if (NotMaskLZ == 64) return Result; // All zero mask. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5916 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5917 | // See if we have a continuous run of bits. If so, we have 0*1+0* |
| 5918 | if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64) |
| 5919 | return Result; |
| 5920 | |
| 5921 | // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. |
| 5922 | if (V.getValueType() != MVT::i64 && NotMaskLZ) |
| 5923 | NotMaskLZ -= 64-V.getValueSizeInBits(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5924 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5925 | unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; |
| 5926 | switch (MaskedBytes) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5927 | case 1: |
| 5928 | case 2: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5929 | case 4: break; |
| 5930 | default: return Result; // All one mask, or 5-byte mask. |
| 5931 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5932 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5933 | // Verify that the first bit starts at a multiple of mask so that the access |
| 5934 | // is aligned the same as the access width. |
| 5935 | if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5936 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5937 | Result.first = MaskedBytes; |
| 5938 | Result.second = NotMaskTZ/8; |
| 5939 | return Result; |
| 5940 | } |
| 5941 | |
| 5942 | |
| 5943 | /// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that |
| 5944 | /// provides a value as specified by MaskInfo. If so, replace the specified |
| 5945 | /// store with a narrower store of truncated IVal. |
| 5946 | static SDNode * |
| 5947 | ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, |
| 5948 | SDValue IVal, StoreSDNode *St, |
| 5949 | DAGCombiner *DC) { |
| 5950 | unsigned NumBytes = MaskInfo.first; |
| 5951 | unsigned ByteShift = MaskInfo.second; |
| 5952 | SelectionDAG &DAG = DC->getDAG(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5953 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5954 | // Check to see if IVal is all zeros in the part being masked in by the 'or' |
| 5955 | // that uses this. If not, this is not a replacement. |
| 5956 | APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), |
| 5957 | ByteShift*8, (ByteShift+NumBytes)*8); |
| 5958 | if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5959 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5960 | // Check that it is legal on the target to do this. It is legal if the new |
| 5961 | // VT we're shrinking to (i8/i16/i32) is legal or we're still before type |
| 5962 | // legalization. |
| 5963 | MVT VT = MVT::getIntegerVT(NumBytes*8); |
| 5964 | if (!DC->isTypeLegal(VT)) |
| 5965 | return 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5966 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5967 | // Okay, we can do this! Replace the 'St' store with a store of IVal that is |
| 5968 | // shifted by ByteShift and truncated down to NumBytes. |
| 5969 | if (ByteShift) |
| 5970 | IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal, |
| 5971 | DAG.getConstant(ByteShift*8, DC->getShiftAmountTy())); |
| 5972 | |
| 5973 | // Figure out the offset for the store and the alignment of the access. |
| 5974 | unsigned StOffset; |
| 5975 | unsigned NewAlign = St->getAlignment(); |
| 5976 | |
| 5977 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 5978 | StOffset = ByteShift; |
| 5979 | else |
| 5980 | StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5981 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5982 | SDValue Ptr = St->getBasePtr(); |
| 5983 | if (StOffset) { |
| 5984 | Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(), |
| 5985 | Ptr, DAG.getConstant(StOffset, Ptr.getValueType())); |
| 5986 | NewAlign = MinAlign(NewAlign, StOffset); |
| 5987 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5988 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5989 | // Truncate down to the new size. |
| 5990 | IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5991 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5992 | ++OpsNarrowed; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5993 | return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 5994 | St->getPointerInfo().getWithOffset(StOffset), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5995 | false, false, NewAlign).getNode(); |
| 5996 | } |
| 5997 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 5998 | |
| 5999 | /// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is |
| 6000 | /// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some |
| 6001 | /// of the loaded bits, try narrowing the load and store if it would end up |
| 6002 | /// being a win for performance or code size. |
| 6003 | SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { |
| 6004 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6005 | if (ST->isVolatile()) |
| 6006 | return SDValue(); |
| 6007 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6008 | SDValue Chain = ST->getChain(); |
| 6009 | SDValue Value = ST->getValue(); |
| 6010 | SDValue Ptr = ST->getBasePtr(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6011 | EVT VT = Value.getValueType(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6012 | |
| 6013 | if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6014 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6015 | |
| 6016 | unsigned Opc = Value.getOpcode(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6017 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6018 | // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst |
| 6019 | // is a byte mask indicating a consecutive number of bytes, check to see if |
| 6020 | // Y is known to provide just those bytes. If so, we try to replace the |
| 6021 | // load + replace + store sequence with a single (narrower) store, which makes |
| 6022 | // the load dead. |
| 6023 | if (Opc == ISD::OR) { |
| 6024 | std::pair<unsigned, unsigned> MaskedLoad; |
| 6025 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); |
| 6026 | if (MaskedLoad.first) |
| 6027 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 6028 | Value.getOperand(1), ST,this)) |
| 6029 | return SDValue(NewST, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6030 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6031 | // Or is commutative, so try swapping X and Y. |
| 6032 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); |
| 6033 | if (MaskedLoad.first) |
| 6034 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 6035 | Value.getOperand(0), ST,this)) |
| 6036 | return SDValue(NewST, 0); |
| 6037 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6038 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6039 | if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || |
| 6040 | Value.getOperand(1).getOpcode() != ISD::Constant) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6041 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6042 | |
| 6043 | SDValue N0 = Value.getOperand(0); |
Dan Gohman | 24bde5b | 2010-09-02 21:18:42 +0000 | [diff] [blame] | 6044 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| 6045 | Chain == SDValue(N0.getNode(), 1)) { |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6046 | LoadSDNode *LD = cast<LoadSDNode>(N0); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6047 | if (LD->getBasePtr() != Ptr || |
| 6048 | LD->getPointerInfo().getAddrSpace() != |
| 6049 | ST->getPointerInfo().getAddrSpace()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6050 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6051 | |
| 6052 | // Find the type to narrow it the load / op / store to. |
| 6053 | SDValue N1 = Value.getOperand(1); |
| 6054 | unsigned BitWidth = N1.getValueSizeInBits(); |
| 6055 | APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); |
| 6056 | if (Opc == ISD::AND) |
| 6057 | Imm ^= APInt::getAllOnesValue(BitWidth); |
Evan Cheng | d3c76bb | 2009-05-28 23:52:18 +0000 | [diff] [blame] | 6058 | if (Imm == 0 || Imm.isAllOnesValue()) |
| 6059 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6060 | unsigned ShAmt = Imm.countTrailingZeros(); |
| 6061 | unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; |
| 6062 | unsigned NewBW = NextPowerOf2(MSB - ShAmt); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6063 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6064 | while (NewBW < BitWidth && |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6065 | !(TLI.isOperationLegalOrCustom(Opc, NewVT) && |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6066 | TLI.isNarrowingProfitable(VT, NewVT))) { |
| 6067 | NewBW = NextPowerOf2(NewBW); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6068 | NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6069 | } |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6070 | if (NewBW >= BitWidth) |
| 6071 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6072 | |
| 6073 | // If the lsb changed does not start at the type bitwidth boundary, |
| 6074 | // start at the previous one. |
| 6075 | if (ShAmt % NewBW) |
| 6076 | ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; |
| 6077 | APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW); |
| 6078 | if ((Imm & Mask) == Imm) { |
| 6079 | APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); |
| 6080 | if (Opc == ISD::AND) |
| 6081 | NewImm ^= APInt::getAllOnesValue(NewBW); |
| 6082 | uint64_t PtrOff = ShAmt / 8; |
| 6083 | // For big endian targets, we need to adjust the offset to the pointer to |
| 6084 | // load the correct bytes. |
| 6085 | if (TLI.isBigEndian()) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6086 | PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6087 | |
| 6088 | unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6089 | const Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); |
| 6090 | if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy)) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6091 | return SDValue(); |
| 6092 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6093 | SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(), |
| 6094 | Ptr.getValueType(), Ptr, |
| 6095 | DAG.getConstant(PtrOff, Ptr.getValueType())); |
| 6096 | SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(), |
| 6097 | LD->getChain(), NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6098 | LD->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6099 | LD->isVolatile(), LD->isNonTemporal(), |
| 6100 | NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6101 | SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD, |
| 6102 | DAG.getConstant(NewImm, NewVT)); |
| 6103 | SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(), |
| 6104 | NewVal, NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6105 | ST->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6106 | false, false, NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6107 | |
| 6108 | AddToWorkList(NewPtr.getNode()); |
| 6109 | AddToWorkList(NewLD.getNode()); |
| 6110 | AddToWorkList(NewVal.getNode()); |
| 6111 | WorkListRemover DeadNodes(*this); |
| 6112 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1), |
| 6113 | &DeadNodes); |
| 6114 | ++OpsNarrowed; |
| 6115 | return NewST; |
| 6116 | } |
| 6117 | } |
| 6118 | |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 6119 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 6122 | /// TransformFPLoadStorePair - For a given floating point load / store pair, |
| 6123 | /// if the load value isn't used by any other operations, then consider |
| 6124 | /// transforming the pair to integer load / store operations if the target |
| 6125 | /// deems the transformation profitable. |
| 6126 | SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { |
| 6127 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 6128 | SDValue Chain = ST->getChain(); |
| 6129 | SDValue Value = ST->getValue(); |
| 6130 | if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && |
| 6131 | Value.hasOneUse() && |
| 6132 | Chain == SDValue(Value.getNode(), 1)) { |
| 6133 | LoadSDNode *LD = cast<LoadSDNode>(Value); |
| 6134 | EVT VT = LD->getMemoryVT(); |
| 6135 | if (!VT.isFloatingPoint() || |
| 6136 | VT != ST->getMemoryVT() || |
| 6137 | LD->isNonTemporal() || |
| 6138 | ST->isNonTemporal() || |
| 6139 | LD->getPointerInfo().getAddrSpace() != 0 || |
| 6140 | ST->getPointerInfo().getAddrSpace() != 0) |
| 6141 | return SDValue(); |
| 6142 | |
| 6143 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); |
| 6144 | if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || |
| 6145 | !TLI.isOperationLegal(ISD::STORE, IntVT) || |
| 6146 | !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || |
| 6147 | !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) |
| 6148 | return SDValue(); |
| 6149 | |
| 6150 | unsigned LDAlign = LD->getAlignment(); |
| 6151 | unsigned STAlign = ST->getAlignment(); |
| 6152 | const Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); |
| 6153 | unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy); |
| 6154 | if (LDAlign < ABIAlign || STAlign < ABIAlign) |
| 6155 | return SDValue(); |
| 6156 | |
| 6157 | SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(), |
| 6158 | LD->getChain(), LD->getBasePtr(), |
| 6159 | LD->getPointerInfo(), |
| 6160 | false, false, LDAlign); |
| 6161 | |
| 6162 | SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(), |
| 6163 | NewLD, ST->getBasePtr(), |
| 6164 | ST->getPointerInfo(), |
| 6165 | false, false, STAlign); |
| 6166 | |
| 6167 | AddToWorkList(NewLD.getNode()); |
| 6168 | AddToWorkList(NewST.getNode()); |
| 6169 | WorkListRemover DeadNodes(*this); |
| 6170 | DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1), |
| 6171 | &DeadNodes); |
| 6172 | ++LdStFP2Int; |
| 6173 | return NewST; |
| 6174 | } |
| 6175 | |
| 6176 | return SDValue(); |
| 6177 | } |
| 6178 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6179 | SDValue DAGCombiner::visitSTORE(SDNode *N) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 6180 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6181 | SDValue Chain = ST->getChain(); |
| 6182 | SDValue Value = ST->getValue(); |
| 6183 | SDValue Ptr = ST->getBasePtr(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6184 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 6185 | // 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] | 6186 | // resultant store does not need a higher alignment than the original. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6187 | if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6188 | ST->isUnindexed()) { |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 6189 | unsigned OrigAlign = ST->getAlignment(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6190 | EVT SVT = Value.getOperand(0).getValueType(); |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 6191 | unsigned Align = TLI.getTargetData()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6192 | getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext())); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6193 | if (Align <= OrigAlign && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6194 | ((!LegalOperations && !ST->isVolatile()) || |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6195 | TLI.isOperationLegalOrCustom(ISD::STORE, SVT))) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6196 | return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6197 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6198 | ST->isNonTemporal(), OrigAlign); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6199 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6200 | |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 6201 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 6202 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6203 | // NOTE: If the original store is volatile, this transform must not increase |
| 6204 | // the number of stores. For example, on x86-32 an f64 can be stored in one |
| 6205 | // processor operation but an i64 (which is not legal) requires two. So the |
| 6206 | // transform should not be done in this case. |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 6207 | if (Value.getOpcode() != ISD::TargetConstantFP) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6208 | SDValue Tmp; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6209 | switch (CFP->getValueType(0).getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6210 | default: llvm_unreachable("Unknown FP type"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6211 | case MVT::f80: // We don't do this for these yet. |
| 6212 | case MVT::f128: |
| 6213 | case MVT::ppcf128: |
Dale Johannesen | c7b21d5 | 2007-09-18 18:36:59 +0000 | [diff] [blame] | 6214 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6215 | case MVT::f32: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6216 | if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6217 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 6218 | Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6219 | bitcastToAPInt().getZExtValue(), MVT::i32); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6220 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6221 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6222 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 6223 | } |
| 6224 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6225 | case MVT::f64: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6226 | if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6227 | !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6228 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 6229 | Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6230 | getZExtValue(), MVT::i64); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6231 | return DAG.getStore(Chain, N->getDebugLoc(), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6232 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6233 | ST->isNonTemporal(), ST->getAlignment()); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6234 | } else if (!ST->isVolatile() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6235 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 6236 | // 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] | 6237 | // argument passing. Since this is so common, custom legalize the |
| 6238 | // 64-bit integer store into two 32-bit stores. |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 6239 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6240 | SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); |
| 6241 | SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 6242 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 6243 | |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 6244 | unsigned Alignment = ST->getAlignment(); |
| 6245 | bool isVolatile = ST->isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6246 | bool isNonTemporal = ST->isNonTemporal(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 6247 | |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6248 | SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6249 | Ptr, ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6250 | isVolatile, isNonTemporal, |
| 6251 | ST->getAlignment()); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6252 | Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr, |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 6253 | DAG.getConstant(4, Ptr.getValueType())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 6254 | Alignment = MinAlign(Alignment, 4U); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6255 | SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6256 | Ptr, ST->getPointerInfo().getWithOffset(4), |
| 6257 | isVolatile, isNonTemporal, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6258 | Alignment); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6259 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6260 | St0, St1); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 6261 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6262 | |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 6263 | break; |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 6264 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 6265 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 6268 | // Try to infer better alignment information than the store already has. |
| 6269 | if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { |
| 6270 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 6271 | if (Align > ST->getAlignment()) |
| 6272 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 6273 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 6274 | ST->isVolatile(), ST->isNonTemporal(), Align); |
| 6275 | } |
| 6276 | } |
| 6277 | |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 6278 | // Try transforming a pair floating point load / store ops to integer |
| 6279 | // load / store ops. |
| 6280 | SDValue NewST = TransformFPLoadStorePair(N); |
| 6281 | if (NewST.getNode()) |
| 6282 | return NewST; |
| 6283 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6284 | if (CombinerAA) { |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6285 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6286 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6287 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 6288 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6289 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6290 | SDValue ReplStore; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 6291 | |
| 6292 | // Replace the chain to avoid dependency. |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 6293 | if (ST->isTruncatingStore()) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6294 | ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 6295 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6296 | ST->getMemoryVT(), ST->isVolatile(), |
| 6297 | ST->isNonTemporal(), ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 6298 | } else { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6299 | ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 6300 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6301 | ST->isVolatile(), ST->isNonTemporal(), |
| 6302 | ST->getAlignment()); |
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 6303 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6304 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6305 | // Create token to keep both nodes around. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6306 | SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6307 | MVT::Other, Chain, ReplStore); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6308 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 6309 | // Make sure the new and old chains are cleaned up. |
| 6310 | AddToWorkList(Token.getNode()); |
| 6311 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 6312 | // Don't add users to work list. |
| 6313 | return CombineTo(N, Token, false); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 6314 | } |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 6315 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6316 | |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 6317 | // Try transforming N to an indexed store. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 6318 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6319 | return SDValue(N, 0); |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 6320 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 6321 | // FIXME: is there such a thing as a truncating indexed store? |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6322 | if (ST->isTruncatingStore() && ST->isUnindexed() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6323 | Value.getValueType().isInteger()) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 6324 | // See if we can simplify the input to this truncstore with knowledge that |
| 6325 | // only the low bits are being used. For example: |
| 6326 | // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6327 | SDValue Shorter = |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 6328 | GetDemandedBits(Value, |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6329 | APInt::getLowBitsSet(Value.getValueSizeInBits(), |
| 6330 | ST->getMemoryVT().getSizeInBits())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6331 | AddToWorkList(Value.getNode()); |
| 6332 | if (Shorter.getNode()) |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6333 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 6334 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6335 | ST->isVolatile(), ST->isNonTemporal(), |
| 6336 | ST->getAlignment()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6337 | |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 6338 | // Otherwise, see if we can simplify the operation with |
| 6339 | // SimplifyDemandedBits, which only works if the value has a single use. |
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 6340 | if (SimplifyDemandedBits(Value, |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 6341 | APInt::getLowBitsSet( |
| 6342 | Value.getValueType().getScalarType().getSizeInBits(), |
| 6343 | ST->getMemoryVT().getScalarType().getSizeInBits()))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6344 | return SDValue(N, 0); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 6345 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6346 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 6347 | // If this is a load followed by a store to the same location, then the store |
| 6348 | // is dead/noop. |
| 6349 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6350 | if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6351 | ST->isUnindexed() && !ST->isVolatile() && |
Chris Lattner | 07649d9 | 2008-01-08 23:08:06 +0000 | [diff] [blame] | 6352 | // There can't be any side effects between the load and store, such as |
| 6353 | // a call or store. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6354 | Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 6355 | // The store is dead, remove it. |
| 6356 | return Chain; |
| 6357 | } |
| 6358 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6359 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6360 | // If this is an FP_ROUND or TRUNC followed by a store, fold this into a |
| 6361 | // truncating store. We can do this even if this is already a truncstore. |
| 6362 | if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6363 | && Value.getNode()->hasOneUse() && ST->isUnindexed() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6364 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 6365 | ST->getMemoryVT())) { |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6366 | return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0), |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 6367 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6368 | ST->isVolatile(), ST->isNonTemporal(), |
| 6369 | ST->getAlignment()); |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 6370 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6371 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 6372 | return ReduceLoadOpStoreWidth(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6375 | SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 6376 | SDValue InVec = N->getOperand(0); |
| 6377 | SDValue InVal = N->getOperand(1); |
| 6378 | SDValue EltNo = N->getOperand(2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6379 | |
Bob Wilson | 492fd45 | 2010-05-19 23:42:58 +0000 | [diff] [blame] | 6380 | // If the inserted element is an UNDEF, just use the input vector. |
| 6381 | if (InVal.getOpcode() == ISD::UNDEF) |
| 6382 | return InVec; |
| 6383 | |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 6384 | EVT VT = InVec.getValueType(); |
| 6385 | |
| 6386 | // If we can't generate a legal BUILD_VECTOR, exit |
| 6387 | if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
| 6388 | return SDValue(); |
| 6389 | |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 6390 | // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new |
| 6391 | // vector with the inserted element. |
| 6392 | if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6393 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 6394 | SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(), |
| 6395 | InVec.getNode()->op_end()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 6396 | if (Elt < Ops.size()) |
| 6397 | Ops[Elt] = InVal; |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6398 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 6399 | VT, &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 6400 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6401 | // If the invec is an UNDEF and if EltNo is a constant, create a new |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6402 | // BUILD_VECTOR with undef elements and the inserted element. |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 6403 | if (InVec.getOpcode() == ISD::UNDEF && |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6404 | isa<ConstantSDNode>(EltNo)) { |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6405 | EVT EltVT = VT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6406 | unsigned NElts = VT.getVectorNumElements(); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6407 | SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6408 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6409 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| 6410 | if (Elt < Ops.size()) |
| 6411 | Ops[Elt] = InVal; |
| 6412 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 6413 | VT, &Ops[0], Ops.size()); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6414 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6415 | return SDValue(); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6418 | SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 6419 | // (vextract (scalar_to_vector val, 0) -> val |
| 6420 | SDValue InVec = N->getOperand(0); |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 6421 | |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 6422 | if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Mon P Wang | c6654ec4 | 2010-02-01 19:03:18 +0000 | [diff] [blame] | 6423 | // Check if the result type doesn't match the inserted element type. A |
| 6424 | // SCALAR_TO_VECTOR may truncate the inserted element and the |
| 6425 | // EXTRACT_VECTOR_ELT may widen the extracted vector. |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 6426 | SDValue InOp = InVec.getOperand(0); |
Mon P Wang | c6654ec4 | 2010-02-01 19:03:18 +0000 | [diff] [blame] | 6427 | EVT NVT = N->getValueType(0); |
| 6428 | if (InOp.getValueType() != NVT) { |
| 6429 | assert(InOp.getValueType().isInteger() && NVT.isInteger()); |
Mon P Wang | 87c46d8 | 2010-02-01 22:15:09 +0000 | [diff] [blame] | 6430 | return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT); |
Mon P Wang | c6654ec4 | 2010-02-01 19:03:18 +0000 | [diff] [blame] | 6431 | } |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 6432 | return InOp; |
| 6433 | } |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6434 | |
| 6435 | // Perform only after legalization to ensure build_vector / vector_shuffle |
| 6436 | // optimizations have already been done. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6437 | if (!LegalOperations) return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6438 | |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 6439 | // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) |
| 6440 | // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) |
| 6441 | // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr) |
Mon P Wang | e3bc6ae | 2009-01-18 06:43:40 +0000 | [diff] [blame] | 6442 | SDValue EltNo = N->getOperand(1); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6443 | |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6444 | if (isa<ConstantSDNode>(EltNo)) { |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 6445 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6446 | bool NewLoad = false; |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 6447 | bool BCNumEltsChanged = false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6448 | EVT VT = InVec.getValueType(); |
| 6449 | EVT ExtVT = VT.getVectorElementType(); |
| 6450 | EVT LVT = ExtVT; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6451 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6452 | if (InVec.getOpcode() == ISD::BITCAST) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6453 | EVT BCVT = InVec.getOperand(0).getValueType(); |
| 6454 | if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6455 | return SDValue(); |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 6456 | if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) |
| 6457 | BCNumEltsChanged = true; |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6458 | InVec = InVec.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6459 | ExtVT = BCVT.getVectorElementType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6460 | NewLoad = true; |
| 6461 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6462 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6463 | LoadSDNode *LN0 = NULL; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6464 | const ShuffleVectorSDNode *SVN = NULL; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6465 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6466 | LN0 = cast<LoadSDNode>(InVec); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6467 | } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6468 | InVec.getOperand(0).getValueType() == ExtVT && |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6469 | ISD::isNormalLoad(InVec.getOperand(0).getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6470 | LN0 = cast<LoadSDNode>(InVec.getOperand(0)); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6471 | } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6472 | // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) |
| 6473 | // => |
| 6474 | // (load $addr+1*size) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6475 | |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 6476 | // If the bit convert changed the number of elements, it is unsafe |
| 6477 | // to examine the mask. |
| 6478 | if (BCNumEltsChanged) |
| 6479 | return SDValue(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6480 | |
| 6481 | // Select the input vector, guarding against out of range extract vector. |
| 6482 | unsigned NumElems = VT.getVectorNumElements(); |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 6483 | int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6484 | InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); |
| 6485 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6486 | if (InVec.getOpcode() == ISD::BITCAST) |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6487 | InVec = InVec.getOperand(0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6488 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6489 | LN0 = cast<LoadSDNode>(InVec); |
Ted Kremenek | d0e88f3 | 2010-04-08 18:49:30 +0000 | [diff] [blame] | 6490 | Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6491 | } |
| 6492 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6493 | |
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 6494 | if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6495 | return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6496 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 6497 | // If Idx was -1 above, Elt is going to be -1, so just return undef. |
| 6498 | if (Elt == -1) |
| 6499 | return DAG.getUNDEF(LN0->getBasePtr().getValueType()); |
| 6500 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6501 | unsigned Align = LN0->getAlignment(); |
| 6502 | if (NewLoad) { |
| 6503 | // Check the resultant load doesn't need a higher alignment than the |
| 6504 | // original load. |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6505 | unsigned NewAlign = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 6506 | TLI.getTargetData() |
| 6507 | ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6508 | |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6509 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6510 | return SDValue(); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6511 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6512 | Align = NewAlign; |
| 6513 | } |
| 6514 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6515 | SDValue NewPtr = LN0->getBasePtr(); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6516 | unsigned PtrOff = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6517 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 6518 | if (Elt) { |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6519 | PtrOff = LVT.getSizeInBits() * Elt / 8; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6520 | EVT PtrType = NewPtr.getValueType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6521 | if (TLI.isBigEndian()) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6522 | PtrOff = VT.getSizeInBits() / 8 - PtrOff; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6523 | NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr, |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 6524 | DAG.getConstant(PtrOff, PtrType)); |
| 6525 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6526 | |
| 6527 | return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6528 | LN0->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6529 | LN0->isVolatile(), LN0->isNonTemporal(), Align); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6530 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 6531 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6532 | return SDValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6533 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6534 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6535 | SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6536 | unsigned NumInScalars = N->getNumOperands(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6537 | EVT VT = N->getValueType(0); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 6538 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6539 | // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT |
| 6540 | // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from |
| 6541 | // at most two distinct vectors, turn this into a shuffle node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6542 | SDValue VecIn1, VecIn2; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6543 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 6544 | // Ignore undef inputs. |
| 6545 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6546 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6547 | // 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] | 6548 | // constant index, bail out. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6549 | if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6550 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6551 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6552 | break; |
| 6553 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6554 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6555 | // If the input vector type disagrees with the result of the build_vector, |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6556 | // we can't make a shuffle. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6557 | SDValue ExtractedFromVec = N->getOperand(i).getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6558 | if (ExtractedFromVec.getValueType() != VT) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6559 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6560 | break; |
| 6561 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6562 | |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6563 | // Otherwise, remember this. We allow up to two distinct input vectors. |
| 6564 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 6565 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6566 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6567 | if (VecIn1.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6568 | VecIn1 = ExtractedFromVec; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6569 | } else if (VecIn2.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6570 | VecIn2 = ExtractedFromVec; |
| 6571 | } else { |
| 6572 | // Too many inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6573 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6574 | break; |
| 6575 | } |
| 6576 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6577 | |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6578 | // If everything is good, we can make a shuffle operation. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6579 | if (VecIn1.getNode()) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6580 | SmallVector<int, 8> Mask; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6581 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 6582 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6583 | Mask.push_back(-1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6584 | continue; |
| 6585 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6586 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 6587 | // If extracting from the first vector, just use the index directly. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6588 | SDValue Extract = N->getOperand(i); |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 6589 | SDValue ExtVal = Extract.getOperand(1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6590 | if (Extract.getOperand(0) == VecIn1) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6591 | unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
| 6592 | if (ExtIndex > VT.getVectorNumElements()) |
| 6593 | return SDValue(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6594 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6595 | Mask.push_back(ExtIndex); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6596 | continue; |
| 6597 | } |
| 6598 | |
| 6599 | // Otherwise, use InIdx + VecSize |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 6600 | unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6601 | Mask.push_back(Idx+NumInScalars); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6602 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6603 | |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6604 | // Add count and size info. |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6605 | if (!isTypeLegal(VT)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6606 | return SDValue(); |
| 6607 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6608 | // Return the new VECTOR_SHUFFLE node. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6609 | SDValue Ops[2]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6610 | Ops[0] = VecIn1; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6611 | Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); |
| 6612 | return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6613 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6614 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6615 | return SDValue(); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6618 | SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6619 | // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of |
| 6620 | // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector |
| 6621 | // inputs come from at most two distinct vectors, turn this into a shuffle |
| 6622 | // node. |
| 6623 | |
| 6624 | // 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] | 6625 | if (N->getNumOperands() == 1) |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6626 | return N->getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6627 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6628 | return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6629 | } |
| 6630 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6631 | SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6632 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6633 | unsigned NumElts = VT.getVectorNumElements(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 6634 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 6635 | SDValue N0 = N->getOperand(0); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 6636 | |
| 6637 | assert(N0.getValueType().getVectorNumElements() == NumElts && |
| 6638 | "Vector shuffle must be normalized in DAG"); |
| 6639 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6640 | // FIXME: implement canonicalizations from DAG.getVectorShuffle() |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 6641 | |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 6642 | // If it is a splat, check if the argument vector is another splat or a |
| 6643 | // build_vector with all scalar elements the same. |
| 6644 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 6645 | if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6646 | SDNode *V = N0.getNode(); |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 6647 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6648 | // 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] | 6649 | // not the number of vector elements, look through it. Be careful not to |
| 6650 | // look though conversions that change things like v4f32 to v2f64. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6651 | if (V->getOpcode() == ISD::BITCAST) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6652 | SDValue ConvInput = V->getOperand(0); |
Evan Cheng | 2925786 | 2008-07-22 20:42:56 +0000 | [diff] [blame] | 6653 | if (ConvInput.getValueType().isVector() && |
| 6654 | ConvInput.getValueType().getVectorNumElements() == NumElts) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6655 | V = ConvInput.getNode(); |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 6656 | } |
| 6657 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6658 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 6659 | assert(V->getNumOperands() == NumElts && |
| 6660 | "BUILD_VECTOR has wrong number of operands"); |
| 6661 | SDValue Base; |
| 6662 | bool AllSame = true; |
| 6663 | for (unsigned i = 0; i != NumElts; ++i) { |
| 6664 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 6665 | Base = V->getOperand(i); |
| 6666 | break; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 6667 | } |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 6668 | } |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 6669 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 6670 | if (!Base.getNode()) |
| 6671 | return N0; |
| 6672 | for (unsigned i = 0; i != NumElts; ++i) { |
| 6673 | if (V->getOperand(i) != Base) { |
| 6674 | AllSame = false; |
| 6675 | break; |
| 6676 | } |
| 6677 | } |
| 6678 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 6679 | if (AllSame) |
| 6680 | return N0; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 6681 | } |
| 6682 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6683 | return SDValue(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
Jim Grosbach | 9a52649 | 2010-06-23 16:07:42 +0000 | [diff] [blame] | 6686 | SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) { |
| 6687 | if (!TLI.getShouldFoldAtomicFences()) |
| 6688 | return SDValue(); |
| 6689 | |
| 6690 | SDValue atomic = N->getOperand(0); |
| 6691 | switch (atomic.getOpcode()) { |
| 6692 | case ISD::ATOMIC_CMP_SWAP: |
| 6693 | case ISD::ATOMIC_SWAP: |
| 6694 | case ISD::ATOMIC_LOAD_ADD: |
| 6695 | case ISD::ATOMIC_LOAD_SUB: |
| 6696 | case ISD::ATOMIC_LOAD_AND: |
| 6697 | case ISD::ATOMIC_LOAD_OR: |
| 6698 | case ISD::ATOMIC_LOAD_XOR: |
| 6699 | case ISD::ATOMIC_LOAD_NAND: |
| 6700 | case ISD::ATOMIC_LOAD_MIN: |
| 6701 | case ISD::ATOMIC_LOAD_MAX: |
| 6702 | case ISD::ATOMIC_LOAD_UMIN: |
| 6703 | case ISD::ATOMIC_LOAD_UMAX: |
| 6704 | break; |
| 6705 | default: |
| 6706 | return SDValue(); |
| 6707 | } |
| 6708 | |
| 6709 | SDValue fence = atomic.getOperand(0); |
| 6710 | if (fence.getOpcode() != ISD::MEMBARRIER) |
| 6711 | return SDValue(); |
| 6712 | |
| 6713 | switch (atomic.getOpcode()) { |
| 6714 | case ISD::ATOMIC_CMP_SWAP: |
| 6715 | return SDValue(DAG.UpdateNodeOperands(atomic.getNode(), |
| 6716 | fence.getOperand(0), |
| 6717 | atomic.getOperand(1), atomic.getOperand(2), |
| 6718 | atomic.getOperand(3)), atomic.getResNo()); |
| 6719 | case ISD::ATOMIC_SWAP: |
| 6720 | case ISD::ATOMIC_LOAD_ADD: |
| 6721 | case ISD::ATOMIC_LOAD_SUB: |
| 6722 | case ISD::ATOMIC_LOAD_AND: |
| 6723 | case ISD::ATOMIC_LOAD_OR: |
| 6724 | case ISD::ATOMIC_LOAD_XOR: |
| 6725 | case ISD::ATOMIC_LOAD_NAND: |
| 6726 | case ISD::ATOMIC_LOAD_MIN: |
| 6727 | case ISD::ATOMIC_LOAD_MAX: |
| 6728 | case ISD::ATOMIC_LOAD_UMIN: |
| 6729 | case ISD::ATOMIC_LOAD_UMAX: |
| 6730 | return SDValue(DAG.UpdateNodeOperands(atomic.getNode(), |
| 6731 | fence.getOperand(0), |
| 6732 | atomic.getOperand(1), atomic.getOperand(2)), |
| 6733 | atomic.getResNo()); |
| 6734 | default: |
| 6735 | return SDValue(); |
| 6736 | } |
| 6737 | } |
| 6738 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6739 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6740 | /// an AND to a vector_shuffle with the destination vector and a zero vector. |
| 6741 | /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6742 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6743 | SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6744 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6745 | DebugLoc dl = N->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6746 | SDValue LHS = N->getOperand(0); |
| 6747 | SDValue RHS = N->getOperand(1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6748 | if (N->getOpcode() == ISD::AND) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6749 | if (RHS.getOpcode() == ISD::BITCAST) |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6750 | RHS = RHS.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6751 | if (RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6752 | SmallVector<int, 8> Indices; |
| 6753 | unsigned NumElts = RHS.getNumOperands(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6754 | for (unsigned i = 0; i != NumElts; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6755 | SDValue Elt = RHS.getOperand(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6756 | if (!isa<ConstantSDNode>(Elt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6757 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6758 | else if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6759 | Indices.push_back(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6760 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6761 | Indices.push_back(NumElts); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6762 | else |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6763 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | // Let's see if the target supports this vector_shuffle. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6767 | EVT RVT = RHS.getValueType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6768 | if (!TLI.isVectorClearMaskLegal(Indices, RVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6769 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6770 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6771 | // Return the new VECTOR_SHUFFLE node. |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6772 | EVT EltVT = RVT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6773 | SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(), |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6774 | DAG.getConstant(0, EltVT)); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6775 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 6776 | RVT, &ZeroOps[0], ZeroOps.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6777 | LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6778 | SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6779 | return DAG.getNode(ISD::BITCAST, dl, VT, Shuf); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6780 | } |
| 6781 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6782 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6783 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6784 | } |
| 6785 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6786 | /// SimplifyVBinOp - Visit a binary vector operation, like ADD. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6787 | SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6788 | // After legalize, the target may be depending on adds and other |
| 6789 | // binary ops to provide legal ways to construct constants or other |
| 6790 | // things. Simplifying them may result in a loss of legality. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6791 | if (LegalOperations) return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6792 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 6793 | assert(N->getValueType(0).isVector() && |
| 6794 | "SimplifyVBinOp only works on vectors!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6795 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6796 | SDValue LHS = N->getOperand(0); |
| 6797 | SDValue RHS = N->getOperand(1); |
| 6798 | SDValue Shuffle = XformToShuffleWithZero(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6799 | if (Shuffle.getNode()) return Shuffle; |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 6800 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6801 | // 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] | 6802 | // this operation. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6803 | if (LHS.getOpcode() == ISD::BUILD_VECTOR && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6804 | RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6805 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6806 | for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6807 | SDValue LHSOp = LHS.getOperand(i); |
| 6808 | SDValue RHSOp = RHS.getOperand(i); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 6809 | // If these two elements can't be folded, bail out. |
| 6810 | if ((LHSOp.getOpcode() != ISD::UNDEF && |
| 6811 | LHSOp.getOpcode() != ISD::Constant && |
| 6812 | LHSOp.getOpcode() != ISD::ConstantFP) || |
| 6813 | (RHSOp.getOpcode() != ISD::UNDEF && |
| 6814 | RHSOp.getOpcode() != ISD::Constant && |
| 6815 | RHSOp.getOpcode() != ISD::ConstantFP)) |
| 6816 | break; |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6817 | |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 6818 | // Can't fold divide by zero. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6819 | if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV || |
| 6820 | N->getOpcode() == ISD::FDIV) { |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 6821 | if ((RHSOp.getOpcode() == ISD::Constant && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6822 | cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) || |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 6823 | (RHSOp.getOpcode() == ISD::ConstantFP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6824 | cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero())) |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 6825 | break; |
| 6826 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6827 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 6828 | EVT VT = LHSOp.getValueType(); |
| 6829 | assert(RHSOp.getValueType() == VT && |
| 6830 | "SimplifyVBinOp with different BUILD_VECTOR element types"); |
| 6831 | SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT, |
Evan Cheng | a083988 | 2010-05-18 00:03:40 +0000 | [diff] [blame] | 6832 | LHSOp, RHSOp); |
| 6833 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 6834 | FoldOp.getOpcode() != ISD::Constant && |
| 6835 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 6836 | break; |
| 6837 | Ops.push_back(FoldOp); |
| 6838 | AddToWorkList(FoldOp.getNode()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 6839 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6840 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 6841 | if (Ops.size() == LHS.getNumOperands()) |
| 6842 | return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), |
| 6843 | LHS.getValueType(), &Ops[0], Ops.size()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 6844 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6845 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6846 | return SDValue(); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6849 | SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0, |
| 6850 | SDValue N1, SDValue N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6851 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6852 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6853 | SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6854 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6855 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6856 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 6857 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 6858 | // the SELECT node, since we were called with a SELECT node. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6859 | if (SCC.getNode()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6860 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 6861 | // Otherwise, just return whatever node we got back, like fabs. |
| 6862 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6863 | SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(), |
| 6864 | N0.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6865 | SCC.getOperand(0), SCC.getOperand(1), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6866 | SCC.getOperand(4)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6867 | AddToWorkList(SETCC.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6868 | return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(), |
| 6869 | SCC.getOperand(2), SCC.getOperand(3), SETCC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6870 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 6871 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6872 | return SCC; |
| 6873 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6874 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 6877 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 6878 | /// 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] | 6879 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 6880 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 6881 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 6882 | /// looked at. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6883 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6884 | SDValue RHS) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6885 | |
Nadav Rotem | f94fdb6 | 2011-02-11 19:57:47 +0000 | [diff] [blame] | 6886 | // Cannot simplify select with vector condition |
| 6887 | if (TheSelect->getOperand(0).getValueType().isVector()) return false; |
| 6888 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 6889 | // If this is a select from two identical things, try to pull the operation |
| 6890 | // through the select. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6891 | if (LHS.getOpcode() != RHS.getOpcode() || |
| 6892 | !LHS.hasOneUse() || !RHS.hasOneUse()) |
| 6893 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6894 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6895 | // If this is a load and the token chain is identical, replace the select |
| 6896 | // of two loads with a load through a select of the address to load from. |
| 6897 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 6898 | // constants have been dropped into the constant pool. |
| 6899 | if (LHS.getOpcode() == ISD::LOAD) { |
| 6900 | LoadSDNode *LLD = cast<LoadSDNode>(LHS); |
| 6901 | LoadSDNode *RLD = cast<LoadSDNode>(RHS); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6902 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6903 | // Token chains must be identical. |
| 6904 | if (LHS.getOperand(0) != RHS.getOperand(0) || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6905 | // Do not let this transformation reduce the number of volatile loads. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6906 | LLD->isVolatile() || RLD->isVolatile() || |
| 6907 | // If this is an EXTLOAD, the VT's must match. |
| 6908 | LLD->getMemoryVT() != RLD->getMemoryVT() || |
Duncan Sands | dcfd3a7 | 2010-11-18 20:05:18 +0000 | [diff] [blame] | 6909 | // If this is an EXTLOAD, the kind of extension must match. |
| 6910 | (LLD->getExtensionType() != RLD->getExtensionType() && |
| 6911 | // The only exception is if one of the extensions is anyext. |
| 6912 | LLD->getExtensionType() != ISD::EXTLOAD && |
| 6913 | RLD->getExtensionType() != ISD::EXTLOAD) || |
Dan Gohman | 75832d7 | 2009-10-31 14:14:04 +0000 | [diff] [blame] | 6914 | // FIXME: this discards src value information. This is |
| 6915 | // over-conservative. It would be beneficial to be able to remember |
Mon P Wang | fe240b1 | 2010-01-11 20:12:49 +0000 | [diff] [blame] | 6916 | // both potential memory locations. Since we are discarding |
| 6917 | // src value info, don't do the transformation if the memory |
| 6918 | // locations are not in the default address space. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6919 | LLD->getPointerInfo().getAddrSpace() != 0 || |
| 6920 | RLD->getPointerInfo().getAddrSpace() != 0) |
| 6921 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6922 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6923 | // Check that the select condition doesn't reach either load. If so, |
| 6924 | // folding this will induce a cycle into the DAG. If not, this is safe to |
| 6925 | // xform, so create a select of the addresses. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6926 | SDValue Addr; |
| 6927 | if (TheSelect->getOpcode() == ISD::SELECT) { |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6928 | SDNode *CondNode = TheSelect->getOperand(0).getNode(); |
| 6929 | if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || |
| 6930 | (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) |
| 6931 | return false; |
| 6932 | Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(), |
| 6933 | LLD->getBasePtr().getValueType(), |
| 6934 | TheSelect->getOperand(0), LLD->getBasePtr(), |
| 6935 | RLD->getBasePtr()); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6936 | } else { // Otherwise SELECT_CC |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6937 | SDNode *CondLHS = TheSelect->getOperand(0).getNode(); |
| 6938 | SDNode *CondRHS = TheSelect->getOperand(1).getNode(); |
| 6939 | |
| 6940 | if ((LLD->hasAnyUseOfValue(1) && |
| 6941 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || |
| 6942 | (LLD->hasAnyUseOfValue(1) && |
| 6943 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS)))) |
| 6944 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6945 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6946 | Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(), |
| 6947 | LLD->getBasePtr().getValueType(), |
| 6948 | TheSelect->getOperand(0), |
| 6949 | TheSelect->getOperand(1), |
| 6950 | LLD->getBasePtr(), RLD->getBasePtr(), |
| 6951 | TheSelect->getOperand(4)); |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6954 | SDValue Load; |
| 6955 | if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { |
| 6956 | Load = DAG.getLoad(TheSelect->getValueType(0), |
| 6957 | TheSelect->getDebugLoc(), |
| 6958 | // FIXME: Discards pointer info. |
| 6959 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 6960 | LLD->isVolatile(), LLD->isNonTemporal(), |
| 6961 | LLD->getAlignment()); |
| 6962 | } else { |
Duncan Sands | b9064bb | 2010-11-18 21:16:28 +0000 | [diff] [blame] | 6963 | Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ? |
| 6964 | RLD->getExtensionType() : LLD->getExtensionType(), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6965 | TheSelect->getValueType(0), |
| 6966 | TheSelect->getDebugLoc(), |
| 6967 | // FIXME: Discards pointer info. |
| 6968 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 6969 | LLD->getMemoryVT(), LLD->isVolatile(), |
| 6970 | LLD->isNonTemporal(), LLD->getAlignment()); |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 6971 | } |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 6972 | |
| 6973 | // Users of the select now use the result of the load. |
| 6974 | CombineTo(TheSelect, Load); |
| 6975 | |
| 6976 | // Users of the old loads now use the new load's chain. We know the |
| 6977 | // old-load value is dead now. |
| 6978 | CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 6979 | CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 6980 | return true; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 6981 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6982 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 6983 | return false; |
| 6984 | } |
| 6985 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 6986 | /// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3 |
| 6987 | /// where 'cond' is the comparison specified by CC. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6988 | SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6989 | SDValue N2, SDValue N3, |
| 6990 | ISD::CondCode CC, bool NotExtCompare) { |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 6991 | // (x ? y : y) -> y. |
| 6992 | if (N2 == N3) return N2; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6993 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6994 | EVT VT = N2.getValueType(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6995 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| 6996 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); |
| 6997 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 6998 | |
| 6999 | // Determine if the condition we're dealing with is constant |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 7000 | SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 7001 | N0, N1, CC, DL, false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7002 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
| 7003 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7004 | |
| 7005 | // fold select_cc true, x, y -> x |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7006 | if (SCCC && !SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7007 | return N2; |
| 7008 | // fold select_cc false, x, y -> y |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7009 | if (SCCC && SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7010 | return N3; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7011 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7012 | // Check to see if we can simplify the select into an fabs node |
| 7013 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 7014 | // Allow either -0.0 or 0.0 |
Dale Johannesen | 87503a6 | 2007-08-25 22:10:57 +0000 | [diff] [blame] | 7015 | if (CFP->getValueAPF().isZero()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7016 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 7017 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 7018 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 7019 | N2 == N3.getOperand(0)) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7020 | return DAG.getNode(ISD::FABS, DL, VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7021 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7022 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 7023 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 7024 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 7025 | N2.getOperand(0) == N3) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7026 | return DAG.getNode(ISD::FABS, DL, VT, N3); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7027 | } |
| 7028 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7029 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7030 | // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" |
| 7031 | // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 |
| 7032 | // in it. This is a win when the constant is not otherwise available because |
| 7033 | // it replaces two constant pool loads with one. We only do this if the FP |
| 7034 | // type is known to be legal, because if it isn't, then we are before legalize |
| 7035 | // 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] | 7036 | // messing with soft float) and if the ConstantFP is not legal, because if |
| 7037 | // 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] | 7038 | if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) |
| 7039 | if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { |
| 7040 | if (TLI.isTypeLegal(N2.getValueType()) && |
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 7041 | (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != |
| 7042 | TargetLowering::Legal) && |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7043 | // If both constants have multiple uses, then we won't need to do an |
| 7044 | // extra load, they are likely around in registers for other users. |
| 7045 | (TV->hasOneUse() || FV->hasOneUse())) { |
| 7046 | Constant *Elts[] = { |
| 7047 | const_cast<ConstantFP*>(FV->getConstantFPValue()), |
| 7048 | const_cast<ConstantFP*>(TV->getConstantFPValue()) |
| 7049 | }; |
| 7050 | const Type *FPTy = Elts[0]->getType(); |
| 7051 | const TargetData &TD = *TLI.getTargetData(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7052 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7053 | // Create a ConstantArray of the two constants. |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 7054 | Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7055 | SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(), |
| 7056 | TD.getPrefTypeAlignment(FPTy)); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 7057 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7058 | |
| 7059 | // Get the offsets to the 0 and 1 element of the array so that we can |
| 7060 | // select between them. |
| 7061 | SDValue Zero = DAG.getIntPtrConstant(0); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 7062 | unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7063 | SDValue One = DAG.getIntPtrConstant(EltSize); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7064 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7065 | SDValue Cond = DAG.getSetCC(DL, |
| 7066 | TLI.getSetCCResultType(N0.getValueType()), |
| 7067 | N0, N1, CC); |
| 7068 | SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(), |
| 7069 | Cond, One, Zero); |
| 7070 | CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx, |
| 7071 | CstOffset); |
| 7072 | return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 7073 | MachinePointerInfo::getConstantPool(), false, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7074 | false, Alignment); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 7075 | |
| 7076 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7077 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7078 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7079 | // Check to see if we can perform the "gzip trick", transforming |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7080 | // (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] | 7081 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7082 | N0.getValueType().isInteger() && |
| 7083 | N2.getValueType().isInteger() && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7084 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
| 7085 | (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7086 | EVT XType = N0.getValueType(); |
| 7087 | EVT AType = N2.getValueType(); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 7088 | if (XType.bitsGE(AType)) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7089 | // 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] | 7090 | // single-bit constant. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7091 | if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) { |
| 7092 | unsigned ShCtV = N2C->getAPIntValue().logBase2(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7093 | ShCtV = XType.getSizeInBits()-ShCtV-1; |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7094 | SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy()); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7095 | SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7096 | XType, N0, ShCt); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7097 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7098 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 7099 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7100 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7101 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7102 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7103 | |
| 7104 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7105 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7106 | |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7107 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7108 | XType, N0, |
| 7109 | DAG.getConstant(XType.getSizeInBits()-1, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7110 | getShiftAmountTy())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7111 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7112 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 7113 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7114 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7115 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7116 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7117 | |
| 7118 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7119 | } |
| 7120 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7121 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 7122 | // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) |
| 7123 | // where y is has a single bit set. |
| 7124 | // A plaintext description would be, we can turn the SELECT_CC into an AND |
| 7125 | // when the condition can be materialized as an all-ones register. Any |
| 7126 | // single bit-test can be materialized as an all-ones register with |
| 7127 | // shift-left and shift-right-arith. |
| 7128 | if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && |
| 7129 | N0->getValueType(0) == VT && |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7130 | N1C && N1C->isNullValue() && |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 7131 | N2C && N2C->isNullValue()) { |
| 7132 | SDValue AndLHS = N0->getOperand(0); |
| 7133 | ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); |
| 7134 | if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { |
| 7135 | // Shift the tested bit over the sign bit. |
| 7136 | APInt AndMask = ConstAndRHS->getAPIntValue(); |
| 7137 | SDValue ShlAmt = |
| 7138 | DAG.getConstant(AndMask.countLeadingZeros(), getShiftAmountTy()); |
| 7139 | SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7140 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 7141 | // Now arithmetic right shift it all the way over, so the result is either |
| 7142 | // all-ones, or zero. |
| 7143 | SDValue ShrAmt = |
| 7144 | DAG.getConstant(AndMask.getBitWidth()-1, getShiftAmountTy()); |
| 7145 | SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7146 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 7147 | return DAG.getNode(ISD::AND, DL, VT, Shr, N3); |
| 7148 | } |
| 7149 | } |
| 7150 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 7151 | // fold select C, 16, 0 -> shl C, 4 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7152 | if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() && |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 7153 | TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7154 | |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 7155 | // If the caller doesn't want us to simplify this into a zext of a compare, |
| 7156 | // don't do it. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7157 | if (NotExtCompare && N2C->getAPIntValue() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7158 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7159 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 7160 | // Get a SetCC of the condition |
| 7161 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 7162 | // target where it isn't. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7163 | SDValue Temp, SCC; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 7164 | // cast from setcc result type to select result type |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7165 | if (LegalTypes) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7166 | SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 7167 | N0, N1, CC); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 7168 | if (N2.getValueType().bitsLT(SCC.getValueType())) |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7169 | Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType()); |
Chris Lattner | 555d8d6 | 2006-12-07 22:36:47 +0000 | [diff] [blame] | 7170 | else |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7171 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7172 | N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 7173 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7174 | SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC); |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7175 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7176 | N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 7177 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7178 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7179 | AddToWorkList(SCC.getNode()); |
| 7180 | AddToWorkList(Temp.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7181 | |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7182 | if (N2C->getAPIntValue() == 1) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 7183 | return Temp; |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7184 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 7185 | // shl setcc result by log2 n2c |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7186 | return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7187 | DAG.getConstant(N2C->getAPIntValue().logBase2(), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7188 | getShiftAmountTy())); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 7189 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7190 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7191 | // Check to see if this is the equivalent of setcc |
| 7192 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 7193 | // otherwise, go ahead with the folds. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7194 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7195 | EVT XType = N0.getValueType(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7196 | if (!LegalOperations || |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 7197 | TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7198 | SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7199 | if (Res.getValueType() != VT) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7200 | Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7201 | return Res; |
| 7202 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7203 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7204 | // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X)))) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7205 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7206 | (!LegalOperations || |
Duncan Sands | 184a876 | 2008-06-14 17:48:34 +0000 | [diff] [blame] | 7207 | TLI.isOperationLegal(ISD::CTLZ, XType))) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7208 | SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7209 | return DAG.getNode(ISD::SRL, DL, XType, Ctlz, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7210 | DAG.getConstant(Log2_32(XType.getSizeInBits()), |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7211 | getShiftAmountTy())); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7212 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7213 | // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7214 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7215 | SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(), |
| 7216 | XType, DAG.getConstant(0, XType), N0); |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 7217 | SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7218 | return DAG.getNode(ISD::SRL, DL, XType, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 7219 | DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0), |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7220 | DAG.getConstant(XType.getSizeInBits()-1, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7221 | getShiftAmountTy())); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7222 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7223 | // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1)) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7224 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 7225 | SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7226 | DAG.getConstant(XType.getSizeInBits()-1, |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 7227 | getShiftAmountTy())); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7228 | return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType)); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7229 | } |
| 7230 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7231 | |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 7232 | // Check to see if this is an integer abs. |
| 7233 | // select_cc setg[te] X, 0, X, -X -> |
| 7234 | // select_cc setgt X, -1, X, -X -> |
| 7235 | // select_cc setl[te] X, 0, -X, X -> |
| 7236 | // select_cc setlt X, 1, -X, X -> |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7237 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 7238 | if (N1C) { |
| 7239 | ConstantSDNode *SubC = NULL; |
| 7240 | if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 7241 | (N1C->isAllOnesValue() && CC == ISD::SETGT)) && |
| 7242 | N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) |
| 7243 | SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); |
| 7244 | else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || |
| 7245 | (N1C->isOne() && CC == ISD::SETLT)) && |
| 7246 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) |
| 7247 | SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); |
| 7248 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7249 | EVT XType = N0.getValueType(); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 7250 | if (SubC && SubC->isNullValue() && XType.isInteger()) { |
| 7251 | SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, |
| 7252 | N0, |
| 7253 | DAG.getConstant(XType.getSizeInBits()-1, |
| 7254 | getShiftAmountTy())); |
| 7255 | SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), |
| 7256 | XType, N0, Shift); |
| 7257 | AddToWorkList(Shift.getNode()); |
| 7258 | AddToWorkList(Add.getNode()); |
| 7259 | return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 7260 | } |
| 7261 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7262 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7263 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 7266 | /// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7267 | SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7268 | SDValue N1, ISD::CondCode Cond, |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 7269 | DebugLoc DL, bool foldBooleans) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7270 | TargetLowering::DAGCombinerInfo |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 7271 | DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this); |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 7272 | return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 7275 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 7276 | /// return a DAG expression to select that will generate the same value by |
| 7277 | /// multiplying by a magic number. See: |
| 7278 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7279 | SDValue DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 7280 | std::vector<SDNode*> Built; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7281 | SDValue S = TLI.BuildSDIV(N, DAG, &Built); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 7282 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 7283 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 7284 | ii != ee; ++ii) |
| 7285 | AddToWorkList(*ii); |
| 7286 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 7290 | /// return a DAG expression to select that will generate the same value by |
| 7291 | /// multiplying by a magic number. See: |
| 7292 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7293 | SDValue DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 7294 | std::vector<SDNode*> Built; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7295 | SDValue S = TLI.BuildUDIV(N, DAG, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 7296 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 7297 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 7298 | ii != ee; ++ii) |
| 7299 | AddToWorkList(*ii); |
| 7300 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7303 | /// 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] | 7304 | // to alias with anything but itself. Provides base object and offset as |
| 7305 | // results. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7306 | static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7307 | const GlobalValue *&GV, void *&CV) { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7308 | // Assume it is a primitive operation. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7309 | Base = Ptr; Offset = 0; GV = 0; CV = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7310 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7311 | // If it's an adding a simple constant then integrate the offset. |
| 7312 | if (Base.getOpcode() == ISD::ADD) { |
| 7313 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { |
| 7314 | Base = Base.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7315 | Offset += C->getZExtValue(); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7316 | } |
| 7317 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7318 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7319 | // Return the underlying GlobalValue, and update the Offset. Return false |
| 7320 | // for GlobalAddressSDNode since the same GlobalAddress may be represented |
| 7321 | // by multiple nodes with different offsets. |
| 7322 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { |
| 7323 | GV = G->getGlobal(); |
| 7324 | Offset += G->getOffset(); |
| 7325 | return false; |
| 7326 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7327 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7328 | // Return the underlying Constant value, and update the Offset. Return false |
| 7329 | // for ConstantSDNodes since the same constant pool entry may be represented |
| 7330 | // by multiple nodes with different offsets. |
| 7331 | if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { |
| 7332 | CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal() |
| 7333 | : (void *)C->getConstVal(); |
| 7334 | Offset += C->getOffset(); |
| 7335 | return false; |
| 7336 | } |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7337 | // 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] | 7338 | return isa<FrameIndexSDNode>(Base); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | /// isAlias - Return true if there is any possibility that the two addresses |
| 7342 | /// overlap. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7343 | bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 7344 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7345 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7346 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7347 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7348 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7349 | unsigned SrcValueAlign2, |
| 7350 | const MDNode *TBAAInfo2) const { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7351 | // If they are the same then they must be aliases. |
| 7352 | if (Ptr1 == Ptr2) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7353 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7354 | // Gather base node and offset information. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7355 | SDValue Base1, Base2; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7356 | int64_t Offset1, Offset2; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7357 | const GlobalValue *GV1, *GV2; |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7358 | void *CV1, *CV2; |
| 7359 | bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1); |
| 7360 | bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7361 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7362 | // If they have a same base address then check to see if they overlap. |
| 7363 | if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 7364 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7365 | |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 7366 | // It is possible for different frame indices to alias each other, mostly |
| 7367 | // when tail call optimization reuses return address slots for arguments. |
| 7368 | // To catch this case, look up the actual index of frame indices to compute |
| 7369 | // the real alias relationship. |
| 7370 | if (isFrameIndex1 && isFrameIndex2) { |
| 7371 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 7372 | Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); |
| 7373 | Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); |
| 7374 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
| 7375 | } |
| 7376 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7377 | // 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] | 7378 | // we know they cannot alias. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 7379 | if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) |
| 7380 | return false; |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 7381 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7382 | // If we know required SrcValue1 and SrcValue2 have relatively large alignment |
| 7383 | // compared to the size and offset of the access, we may be able to prove they |
| 7384 | // do not alias. This check is conservative for now to catch cases created by |
| 7385 | // splitting vector types. |
| 7386 | if ((SrcValueAlign1 == SrcValueAlign2) && |
| 7387 | (SrcValueOffset1 != SrcValueOffset2) && |
| 7388 | (Size1 == Size2) && (SrcValueAlign1 > Size1)) { |
| 7389 | int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1; |
| 7390 | int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7391 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7392 | // There is no overlap between these relatively aligned accesses of similar |
| 7393 | // size, return no alias. |
| 7394 | if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1) |
| 7395 | return false; |
| 7396 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7397 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 7398 | if (CombinerGlobalAA) { |
| 7399 | // Use alias analysis information. |
Dan Gohman | e9c8fa0 | 2007-08-27 16:32:11 +0000 | [diff] [blame] | 7400 | int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2); |
| 7401 | int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset; |
| 7402 | int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7403 | AliasAnalysis::AliasResult AAResult = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7404 | AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1), |
| 7405 | AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2)); |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 7406 | if (AAResult == AliasAnalysis::NoAlias) |
| 7407 | return false; |
| 7408 | } |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 7409 | |
| 7410 | // Otherwise we have to assume they alias. |
| 7411 | return true; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7412 | } |
| 7413 | |
| 7414 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 7415 | /// node. Returns true if the operand was a load. |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7416 | bool DAGCombiner::FindAliasInfo(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7417 | SDValue &Ptr, int64_t &Size, |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7418 | const Value *&SrcValue, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7419 | int &SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7420 | unsigned &SrcValueAlign, |
| 7421 | const MDNode *&TBAAInfo) const { |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7422 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 7423 | Ptr = LD->getBasePtr(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7424 | Size = LD->getMemoryVT().getSizeInBits() >> 3; |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7425 | SrcValue = LD->getSrcValue(); |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 7426 | SrcValueOffset = LD->getSrcValueOffset(); |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7427 | SrcValueAlign = LD->getOriginalAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7428 | TBAAInfo = LD->getTBAAInfo(); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7429 | return true; |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7430 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7431 | Ptr = ST->getBasePtr(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7432 | Size = ST->getMemoryVT().getSizeInBits() >> 3; |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7433 | SrcValue = ST->getSrcValue(); |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 7434 | SrcValueOffset = ST->getSrcValueOffset(); |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7435 | SrcValueAlign = ST->getOriginalAlignment(); |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7436 | TBAAInfo = ST->getTBAAInfo(); |
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 7437 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 7438 | llvm_unreachable("FindAliasInfo expected a memory operand"); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7439 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7440 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 7441 | return false; |
| 7442 | } |
| 7443 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7444 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 7445 | /// looking for aliasing nodes and adding them to the Aliases vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7446 | void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| 7447 | SmallVector<SDValue, 8> &Aliases) { |
| 7448 | SmallVector<SDValue, 8> Chains; // List of chains to visit. |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7449 | SmallPtrSet<SDNode *, 16> Visited; // Visited node set. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7450 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7451 | // Get alias information for node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7452 | SDValue Ptr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7453 | int64_t Size; |
| 7454 | const Value *SrcValue; |
| 7455 | int SrcValueOffset; |
| 7456 | unsigned SrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7457 | const MDNode *SrcTBAAInfo; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7458 | bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7459 | SrcValueAlign, SrcTBAAInfo); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7460 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7461 | // Starting off. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7462 | Chains.push_back(OriginalChain); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 7463 | unsigned Depth = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7464 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7465 | // Look at each chain and determine if it is an alias. If so, add it to the |
| 7466 | // 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] | 7467 | // candidate. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7468 | while (!Chains.empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7469 | SDValue Chain = Chains.back(); |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7470 | Chains.pop_back(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7471 | |
| 7472 | // For TokenFactor nodes, look at each operand and only continue up the |
| 7473 | // 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] | 7474 | // find more and revert to original chain since the xform is unlikely to be |
| 7475 | // profitable. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7476 | // |
| 7477 | // 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] | 7478 | // chain we found before we hit a tokenfactor rather than the original |
| 7479 | // chain. |
| 7480 | if (Depth > 6 || Aliases.size() == 2) { |
| 7481 | Aliases.clear(); |
| 7482 | Aliases.push_back(OriginalChain); |
| 7483 | break; |
| 7484 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7485 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7486 | // Don't bother if we've been before. |
| 7487 | if (!Visited.insert(Chain.getNode())) |
| 7488 | continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7489 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7490 | switch (Chain.getOpcode()) { |
| 7491 | case ISD::EntryToken: |
| 7492 | // Entry token is ideal chain operand, but handled in FindBetterChain. |
| 7493 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7494 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7495 | case ISD::LOAD: |
| 7496 | case ISD::STORE: { |
| 7497 | // Get alias information for Chain. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7498 | SDValue OpPtr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7499 | int64_t OpSize; |
| 7500 | const Value *OpSrcValue; |
| 7501 | int OpSrcValueOffset; |
| 7502 | unsigned OpSrcValueAlign; |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7503 | const MDNode *OpSrcTBAAInfo; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7504 | bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7505 | OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7506 | OpSrcValueAlign, |
| 7507 | OpSrcTBAAInfo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7508 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7509 | // If chain is alias then stop here. |
| 7510 | if (!(IsLoad && IsOpLoad) && |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7511 | isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7512 | SrcTBAAInfo, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7513 | OpPtr, OpSize, OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 7514 | OpSrcValueAlign, OpSrcTBAAInfo)) { |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7515 | Aliases.push_back(Chain); |
| 7516 | } else { |
| 7517 | // Look further up the chain. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7518 | Chains.push_back(Chain.getOperand(0)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 7519 | ++Depth; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7520 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7521 | break; |
| 7522 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7523 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7524 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7525 | // We have to check each of the operands of the token factor for "small" |
| 7526 | // token factors, so we queue them up. Adding the operands to the queue |
| 7527 | // (stack) in reverse order maintains the original order and increases the |
| 7528 | // likelihood that getNode will find a matching token factor (CSE.) |
| 7529 | if (Chain.getNumOperands() > 16) { |
| 7530 | Aliases.push_back(Chain); |
| 7531 | break; |
| 7532 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7533 | for (unsigned n = Chain.getNumOperands(); n;) |
| 7534 | Chains.push_back(Chain.getOperand(--n)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 7535 | ++Depth; |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7536 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7537 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 7538 | default: |
| 7539 | // For all other instructions we will just have to take what we can get. |
| 7540 | Aliases.push_back(Chain); |
| 7541 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7542 | } |
| 7543 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
| 7546 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking |
| 7547 | /// for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7548 | SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { |
| 7549 | SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7550 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7551 | // Accumulate all the aliases to this node. |
| 7552 | GatherAllAliases(N, OldChain, Aliases); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7553 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7554 | if (Aliases.size() == 0) { |
| 7555 | // If no operands then chain to entry token. |
| 7556 | return DAG.getEntryNode(); |
| 7557 | } else if (Aliases.size() == 1) { |
| 7558 | // If a single operand then chain to it. We don't need to revisit it. |
| 7559 | return Aliases[0]; |
| 7560 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7561 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7562 | // Construct a custom tailored token factor. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7563 | return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7564 | &Aliases[0], Aliases.size()); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7567 | // SelectionDAG::Combine - This is the entry point for the file. |
| 7568 | // |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 7569 | void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 7570 | CodeGenOpt::Level OptLevel) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7571 | /// run - This is the main entry point to this class. |
| 7572 | /// |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 7573 | DAGCombiner(*this, AA, OptLevel).Run(Level); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7574 | } |