Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 1 | //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the TargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Target/TargetLowering.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 17 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 24 | /// InitLibcallNames - Set default libcall names. |
| 25 | /// |
Evan Cheng | 79cca50 | 2007-01-12 22:51:10 +0000 | [diff] [blame] | 26 | static void InitLibcallNames(const char **Names) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 27 | Names[RTLIB::SHL_I32] = "__ashlsi3"; |
| 28 | Names[RTLIB::SHL_I64] = "__ashldi3"; |
| 29 | Names[RTLIB::SRL_I32] = "__lshrsi3"; |
| 30 | Names[RTLIB::SRL_I64] = "__lshrdi3"; |
| 31 | Names[RTLIB::SRA_I32] = "__ashrsi3"; |
| 32 | Names[RTLIB::SRA_I64] = "__ashrdi3"; |
| 33 | Names[RTLIB::MUL_I32] = "__mulsi3"; |
| 34 | Names[RTLIB::MUL_I64] = "__muldi3"; |
| 35 | Names[RTLIB::SDIV_I32] = "__divsi3"; |
| 36 | Names[RTLIB::SDIV_I64] = "__divdi3"; |
| 37 | Names[RTLIB::UDIV_I32] = "__udivsi3"; |
| 38 | Names[RTLIB::UDIV_I64] = "__udivdi3"; |
| 39 | Names[RTLIB::SREM_I32] = "__modsi3"; |
| 40 | Names[RTLIB::SREM_I64] = "__moddi3"; |
| 41 | Names[RTLIB::UREM_I32] = "__umodsi3"; |
| 42 | Names[RTLIB::UREM_I64] = "__umoddi3"; |
| 43 | Names[RTLIB::NEG_I32] = "__negsi2"; |
| 44 | Names[RTLIB::NEG_I64] = "__negdi2"; |
| 45 | Names[RTLIB::ADD_F32] = "__addsf3"; |
| 46 | Names[RTLIB::ADD_F64] = "__adddf3"; |
| 47 | Names[RTLIB::SUB_F32] = "__subsf3"; |
| 48 | Names[RTLIB::SUB_F64] = "__subdf3"; |
| 49 | Names[RTLIB::MUL_F32] = "__mulsf3"; |
| 50 | Names[RTLIB::MUL_F64] = "__muldf3"; |
| 51 | Names[RTLIB::DIV_F32] = "__divsf3"; |
| 52 | Names[RTLIB::DIV_F64] = "__divdf3"; |
| 53 | Names[RTLIB::REM_F32] = "fmodf"; |
| 54 | Names[RTLIB::REM_F64] = "fmod"; |
| 55 | Names[RTLIB::NEG_F32] = "__negsf2"; |
| 56 | Names[RTLIB::NEG_F64] = "__negdf2"; |
| 57 | Names[RTLIB::POWI_F32] = "__powisf2"; |
| 58 | Names[RTLIB::POWI_F64] = "__powidf2"; |
| 59 | Names[RTLIB::SQRT_F32] = "sqrtf"; |
| 60 | Names[RTLIB::SQRT_F64] = "sqrt"; |
| 61 | Names[RTLIB::SIN_F32] = "sinf"; |
| 62 | Names[RTLIB::SIN_F64] = "sin"; |
| 63 | Names[RTLIB::COS_F32] = "cosf"; |
| 64 | Names[RTLIB::COS_F64] = "cos"; |
| 65 | Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2"; |
| 66 | Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2"; |
| 67 | Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi"; |
| 68 | Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi"; |
| 69 | Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi"; |
| 70 | Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi"; |
| 71 | Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi"; |
| 72 | Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi"; |
| 73 | Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi"; |
| 74 | Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi"; |
| 75 | Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf"; |
| 76 | Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf"; |
| 77 | Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf"; |
| 78 | Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf"; |
| 79 | Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf"; |
| 80 | Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf"; |
| 81 | Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf"; |
| 82 | Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf"; |
| 83 | Names[RTLIB::OEQ_F32] = "__eqsf2"; |
| 84 | Names[RTLIB::OEQ_F64] = "__eqdf2"; |
| 85 | Names[RTLIB::UNE_F32] = "__nesf2"; |
| 86 | Names[RTLIB::UNE_F64] = "__nedf2"; |
| 87 | Names[RTLIB::OGE_F32] = "__gesf2"; |
| 88 | Names[RTLIB::OGE_F64] = "__gedf2"; |
| 89 | Names[RTLIB::OLT_F32] = "__ltsf2"; |
| 90 | Names[RTLIB::OLT_F64] = "__ltdf2"; |
| 91 | Names[RTLIB::OLE_F32] = "__lesf2"; |
| 92 | Names[RTLIB::OLE_F64] = "__ledf2"; |
| 93 | Names[RTLIB::OGT_F32] = "__gtsf2"; |
| 94 | Names[RTLIB::OGT_F64] = "__gtdf2"; |
| 95 | Names[RTLIB::UO_F32] = "__unordsf2"; |
| 96 | Names[RTLIB::UO_F64] = "__unorddf2"; |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 97 | Names[RTLIB::O_F32] = "__unordsf2"; |
| 98 | Names[RTLIB::O_F64] = "__unorddf2"; |
| 99 | } |
| 100 | |
| 101 | /// InitCmpLibcallCCs - Set default comparison libcall CC. |
| 102 | /// |
| 103 | static void InitCmpLibcallCCs(ISD::CondCode *CCs) { |
| 104 | memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL); |
| 105 | CCs[RTLIB::OEQ_F32] = ISD::SETEQ; |
| 106 | CCs[RTLIB::OEQ_F64] = ISD::SETEQ; |
| 107 | CCs[RTLIB::UNE_F32] = ISD::SETNE; |
| 108 | CCs[RTLIB::UNE_F64] = ISD::SETNE; |
| 109 | CCs[RTLIB::OGE_F32] = ISD::SETGE; |
| 110 | CCs[RTLIB::OGE_F64] = ISD::SETGE; |
| 111 | CCs[RTLIB::OLT_F32] = ISD::SETLT; |
| 112 | CCs[RTLIB::OLT_F64] = ISD::SETLT; |
| 113 | CCs[RTLIB::OLE_F32] = ISD::SETLE; |
| 114 | CCs[RTLIB::OLE_F64] = ISD::SETLE; |
| 115 | CCs[RTLIB::OGT_F32] = ISD::SETGT; |
| 116 | CCs[RTLIB::OGT_F64] = ISD::SETGT; |
| 117 | CCs[RTLIB::UO_F32] = ISD::SETNE; |
| 118 | CCs[RTLIB::UO_F64] = ISD::SETNE; |
| 119 | CCs[RTLIB::O_F32] = ISD::SETEQ; |
| 120 | CCs[RTLIB::O_F64] = ISD::SETEQ; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 123 | TargetLowering::TargetLowering(TargetMachine &tm) |
Chris Lattner | 3e6e8cc | 2006-01-29 08:41:12 +0000 | [diff] [blame] | 124 | : TM(tm), TD(TM.getTargetData()) { |
Evan Cheng | 33143dc | 2006-03-03 06:58:59 +0000 | [diff] [blame] | 125 | assert(ISD::BUILTIN_OP_END <= 156 && |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 126 | "Fixed size array in TargetLowering is not large enough!"); |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 127 | // All operations default to being supported. |
| 128 | memset(OpActions, 0, sizeof(OpActions)); |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 129 | memset(LoadXActions, 0, sizeof(LoadXActions)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 130 | memset(&StoreXActions, 0, sizeof(StoreXActions)); |
Evan Cheng | 5ff839f | 2006-11-09 18:56:43 +0000 | [diff] [blame] | 131 | // Initialize all indexed load / store to expand. |
| 132 | for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) { |
| 133 | for (unsigned IM = (unsigned)ISD::PRE_INC; |
| 134 | IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) { |
| 135 | setIndexedLoadAction(IM, (MVT::ValueType)VT, Expand); |
| 136 | setIndexedStoreAction(IM, (MVT::ValueType)VT, Expand); |
| 137 | } |
| 138 | } |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 139 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 140 | IsLittleEndian = TD->isLittleEndian(); |
Chris Lattner | cf9668f | 2006-10-06 22:52:08 +0000 | [diff] [blame] | 141 | UsesGlobalOffsetTable = false; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 142 | ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD->getIntPtrType()); |
Chris Lattner | d6e4967 | 2005-01-19 03:36:14 +0000 | [diff] [blame] | 143 | ShiftAmtHandling = Undefined; |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 144 | memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*)); |
Chris Lattner | 00ffed0 | 2006-03-01 04:52:55 +0000 | [diff] [blame] | 145 | memset(TargetDAGCombineArray, 0, |
| 146 | sizeof(TargetDAGCombineArray)/sizeof(TargetDAGCombineArray[0])); |
Evan Cheng | a03a5dc | 2006-02-14 08:38:30 +0000 | [diff] [blame] | 147 | maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8; |
Reid Spencer | 0f9beca | 2005-08-27 19:09:02 +0000 | [diff] [blame] | 148 | allowUnalignedMemoryAccesses = false; |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 149 | UseUnderscoreSetJmp = false; |
| 150 | UseUnderscoreLongJmp = false; |
Chris Lattner | 6618039 | 2007-02-25 01:28:05 +0000 | [diff] [blame] | 151 | SelectIsExpensive = false; |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 152 | IntDivIsCheap = false; |
| 153 | Pow2DivIsCheap = false; |
Chris Lattner | ee4a765 | 2006-01-25 18:57:15 +0000 | [diff] [blame] | 154 | StackPointerRegisterToSaveRestore = 0; |
Jim Laskey | 9bb3c93 | 2007-02-22 18:04:49 +0000 | [diff] [blame] | 155 | ExceptionPointerRegister = 0; |
| 156 | ExceptionSelectorRegister = 0; |
Evan Cheng | 0577a22 | 2006-01-25 18:52:42 +0000 | [diff] [blame] | 157 | SchedPreferenceInfo = SchedulingForLatency; |
Chris Lattner | 7acf5f3 | 2006-09-05 17:39:15 +0000 | [diff] [blame] | 158 | JumpBufSize = 0; |
Duraid Madina | 0c9e0ff | 2006-09-04 07:44:11 +0000 | [diff] [blame] | 159 | JumpBufAlignment = 0; |
Evan Cheng | d60483e | 2007-05-16 23:45:53 +0000 | [diff] [blame] | 160 | IfCvtBlockSizeLimit = 2; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 161 | |
| 162 | InitLibcallNames(LibcallRoutineNames); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 163 | InitCmpLibcallCCs(CmpLibcallCCs); |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 166 | TargetLowering::~TargetLowering() {} |
| 167 | |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 168 | /// setValueTypeAction - Set the action for a particular value type. This |
| 169 | /// assumes an action has not already been set for this value type. |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 170 | static void SetValueTypeAction(MVT::ValueType VT, |
| 171 | TargetLowering::LegalizeAction Action, |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 172 | TargetLowering &TLI, |
| 173 | MVT::ValueType *TransformToType, |
Chris Lattner | 3e6e8cc | 2006-01-29 08:41:12 +0000 | [diff] [blame] | 174 | TargetLowering::ValueTypeActionImpl &ValueTypeActions) { |
| 175 | ValueTypeActions.setTypeAction(VT, Action); |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 176 | if (Action == TargetLowering::Promote) { |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 177 | MVT::ValueType PromoteTo; |
| 178 | if (VT == MVT::f32) |
| 179 | PromoteTo = MVT::f64; |
| 180 | else { |
| 181 | unsigned LargerReg = VT+1; |
Chris Lattner | 9ed62c1 | 2005-08-24 16:34:12 +0000 | [diff] [blame] | 182 | while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) { |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 183 | ++LargerReg; |
| 184 | assert(MVT::isInteger((MVT::ValueType)LargerReg) && |
| 185 | "Nothing to promote to??"); |
| 186 | } |
| 187 | PromoteTo = (MVT::ValueType)LargerReg; |
| 188 | } |
| 189 | |
| 190 | assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) && |
| 191 | MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) && |
| 192 | "Can only promote from int->int or fp->fp!"); |
| 193 | assert(VT < PromoteTo && "Must promote to a larger type!"); |
| 194 | TransformToType[VT] = PromoteTo; |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 195 | } else if (Action == TargetLowering::Expand) { |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 196 | // f32 and f64 is each expanded to corresponding integer type of same size. |
| 197 | if (VT == MVT::f32) |
| 198 | TransformToType[VT] = MVT::i32; |
| 199 | else if (VT == MVT::f64) |
| 200 | TransformToType[VT] = MVT::i64; |
| 201 | else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 202 | assert((MVT::isVector(VT) || MVT::isInteger(VT)) && VT > MVT::i8 && |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 203 | "Cannot expand this type: target must support SOME integer reg!"); |
| 204 | // Expand to the next smaller integer type! |
| 205 | TransformToType[VT] = (MVT::ValueType)(VT-1); |
| 206 | } |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 211 | /// computeRegisterProperties - Once all of the register classes are added, |
| 212 | /// this allows us to compute derived properties we expose. |
| 213 | void TargetLowering::computeRegisterProperties() { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 214 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 215 | "Too many value types for ValueTypeActions to hold!"); |
| 216 | |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 217 | // Everything defaults to one. |
| 218 | for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 219 | NumRegistersForVT[i] = 1; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 221 | // Find the largest integer register class. |
| 222 | unsigned LargestIntReg = MVT::i128; |
| 223 | for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg) |
| 224 | assert(LargestIntReg != MVT::i1 && "No integer registers defined!"); |
| 225 | |
| 226 | // Every integer value type larger than this largest register takes twice as |
| 227 | // many registers to represent as the previous ValueType. |
| 228 | unsigned ExpandedReg = LargestIntReg; ++LargestIntReg; |
| 229 | for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg) |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 230 | NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1]; |
Chris Lattner | 310968c | 2005-01-07 07:44:53 +0000 | [diff] [blame] | 231 | |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 232 | // Inspect all of the ValueType's possible, deciding how to process them. |
| 233 | for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg) |
| 234 | // If we are expanding this type, expand it! |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 235 | if (getNumRegisters((MVT::ValueType)IntReg) != 1) |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 236 | SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType, |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 237 | ValueTypeActions); |
Chris Lattner | 9ed62c1 | 2005-08-24 16:34:12 +0000 | [diff] [blame] | 238 | else if (!isTypeLegal((MVT::ValueType)IntReg)) |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 239 | // Otherwise, if we don't have native support, we must promote to a |
| 240 | // larger type. |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 241 | SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this, |
| 242 | TransformToType, ValueTypeActions); |
Chris Lattner | cfdfe4c | 2005-01-16 01:20:18 +0000 | [diff] [blame] | 243 | else |
| 244 | TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 245 | |
Dan Gohman | 2d74a31 | 2007-06-21 14:48:26 +0000 | [diff] [blame] | 246 | // If the target does not have native f64 support, expand it to i64. We will |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 247 | // be generating soft float library calls. If the target does not have native |
Dan Gohman | 2d74a31 | 2007-06-21 14:48:26 +0000 | [diff] [blame] | 248 | // support for f32, promote it to f64 if it is legal. Otherwise, expand it to |
| 249 | // i32. |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 250 | if (isTypeLegal(MVT::f64)) |
| 251 | TransformToType[MVT::f64] = MVT::f64; |
| 252 | else { |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 253 | NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64]; |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 254 | SetValueTypeAction(MVT::f64, Expand, *this, TransformToType, |
| 255 | ValueTypeActions); |
| 256 | } |
| 257 | if (isTypeLegal(MVT::f32)) |
Chris Lattner | cfdfe4c | 2005-01-16 01:20:18 +0000 | [diff] [blame] | 258 | TransformToType[MVT::f32] = MVT::f32; |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 259 | else if (isTypeLegal(MVT::f64)) |
| 260 | SetValueTypeAction(MVT::f32, Promote, *this, TransformToType, |
| 261 | ValueTypeActions); |
| 262 | else { |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 263 | NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32]; |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 264 | SetValueTypeAction(MVT::f32, Expand, *this, TransformToType, |
| 265 | ValueTypeActions); |
| 266 | } |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 3a593584 | 2006-03-16 19:50:01 +0000 | [diff] [blame] | 268 | // Loop over all of the legal vector value types, specifying an identity type |
| 269 | // transformation. |
| 270 | for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE; |
Evan Cheng | 677274b | 2006-03-23 23:24:51 +0000 | [diff] [blame] | 271 | i <= MVT::LAST_VECTOR_VALUETYPE; ++i) { |
Chris Lattner | 3a593584 | 2006-03-16 19:50:01 +0000 | [diff] [blame] | 272 | if (isTypeLegal((MVT::ValueType)i)) |
| 273 | TransformToType[i] = (MVT::ValueType)i; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 274 | else { |
| 275 | MVT::ValueType VT1, VT2; |
| 276 | NumRegistersForVT[i] = getVectorTypeBreakdown(i, VT1, VT2); |
| 277 | SetValueTypeAction(i, Expand, *this, TransformToType, |
| 278 | ValueTypeActions); |
| 279 | } |
Chris Lattner | 3a593584 | 2006-03-16 19:50:01 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | bb97d81 | 2005-01-16 01:10:58 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | cba82f9 | 2005-01-16 07:28:11 +0000 | [diff] [blame] | 282 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 283 | const char *TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 284 | return NULL; |
| 285 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 286 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 287 | /// getVectorTypeBreakdown - Vector types are broken down into some number of |
| 288 | /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32 |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 289 | /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 290 | /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86. |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 291 | /// |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 292 | /// This method returns the number of registers needed, and the VT for each |
| 293 | /// register. It also returns the VT of the VectorType elements before they |
| 294 | /// are promoted/expanded. |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 295 | /// |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 296 | unsigned TargetLowering::getVectorTypeBreakdown(MVT::ValueType VT, |
| 297 | MVT::ValueType &ElementVT, |
| 298 | MVT::ValueType &LegalElementVT) const { |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 299 | // Figure out the right, legal destination reg to copy into. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 300 | unsigned NumElts = MVT::getVectorNumElements(VT); |
| 301 | MVT::ValueType EltTy = MVT::getVectorElementType(VT); |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 302 | |
| 303 | unsigned NumVectorRegs = 1; |
| 304 | |
| 305 | // Divide the input until we get to a supported size. This will always |
| 306 | // end with a scalar if the target doesn't support vectors. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 307 | while (NumElts > 1 && |
| 308 | !isTypeLegal(MVT::getVectorType(EltTy, NumElts))) { |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 309 | NumElts >>= 1; |
| 310 | NumVectorRegs <<= 1; |
| 311 | } |
| 312 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 313 | MVT::ValueType NewVT = MVT::getVectorType(EltTy, NumElts); |
| 314 | if (!isTypeLegal(NewVT)) |
| 315 | NewVT = EltTy; |
| 316 | ElementVT = NewVT; |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 317 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 318 | MVT::ValueType DestVT = getTypeToTransformTo(NewVT); |
| 319 | LegalElementVT = DestVT; |
| 320 | if (DestVT < NewVT) { |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 321 | // Value is expanded, e.g. i64 -> i16. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame^] | 322 | return NumVectorRegs*(MVT::getSizeInBits(NewVT)/MVT::getSizeInBits(DestVT)); |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 323 | } else { |
| 324 | // Otherwise, promotion or legal types use the same number of registers as |
| 325 | // the vector decimated to the appropriate level. |
Chris Lattner | 79227e2 | 2006-03-31 00:46:36 +0000 | [diff] [blame] | 326 | return NumVectorRegs; |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Evan Cheng | e9b3da1 | 2006-05-17 18:10:06 +0000 | [diff] [blame] | 329 | return 1; |
Chris Lattner | dc87929 | 2006-03-31 00:28:56 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 332 | //===----------------------------------------------------------------------===// |
| 333 | // Optimization Methods |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 336 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 337 | /// specified instruction is a constant integer. If so, check to see if there |
| 338 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 339 | /// constant and return true. |
| 340 | bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDOperand Op, |
| 341 | uint64_t Demanded) { |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 342 | // FIXME: ISD::SELECT, ISD::SELECT_CC |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 343 | switch(Op.getOpcode()) { |
| 344 | default: break; |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 345 | case ISD::AND: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 346 | case ISD::OR: |
| 347 | case ISD::XOR: |
| 348 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
| 349 | if ((~Demanded & C->getValue()) != 0) { |
| 350 | MVT::ValueType VT = Op.getValueType(); |
| 351 | SDOperand New = DAG.getNode(Op.getOpcode(), VT, Op.getOperand(0), |
| 352 | DAG.getConstant(Demanded & C->getValue(), |
| 353 | VT)); |
| 354 | return CombineTo(Op, New); |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 355 | } |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | return false; |
| 359 | } |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 360 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 361 | /// SimplifyDemandedBits - Look at Op. At this point, we know that only the |
| 362 | /// DemandedMask bits of the result of Op are ever used downstream. If we can |
| 363 | /// use this information to simplify Op, create a new simplified DAG node and |
| 364 | /// return true, returning the original and new nodes in Old and New. Otherwise, |
| 365 | /// analyze the expression and return a mask of KnownOne and KnownZero bits for |
| 366 | /// the expression (used to simplify the caller). The KnownZero/One bits may |
| 367 | /// only be accurate for those bits in the DemandedMask. |
| 368 | bool TargetLowering::SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask, |
| 369 | uint64_t &KnownZero, |
| 370 | uint64_t &KnownOne, |
| 371 | TargetLoweringOpt &TLO, |
| 372 | unsigned Depth) const { |
| 373 | KnownZero = KnownOne = 0; // Don't know anything. |
Chris Lattner | 3fc5b01 | 2007-05-17 18:19:23 +0000 | [diff] [blame] | 374 | |
| 375 | // The masks are not wide enough to represent this type! Should use APInt. |
| 376 | if (Op.getValueType() == MVT::i128) |
| 377 | return false; |
| 378 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 379 | // Other users may use these bits. |
| 380 | if (!Op.Val->hasOneUse()) { |
| 381 | if (Depth != 0) { |
| 382 | // If not at the root, Just compute the KnownZero/KnownOne bits to |
| 383 | // simplify things downstream. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 384 | TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | // If this is the root being simplified, allow it to have multiple uses, |
| 388 | // just set the DemandedMask to all bits. |
| 389 | DemandedMask = MVT::getIntVTBitMask(Op.getValueType()); |
| 390 | } else if (DemandedMask == 0) { |
| 391 | // Not demanding any bits from Op. |
| 392 | if (Op.getOpcode() != ISD::UNDEF) |
| 393 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::UNDEF, Op.getValueType())); |
| 394 | return false; |
| 395 | } else if (Depth == 6) { // Limit search depth. |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | uint64_t KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut; |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 400 | switch (Op.getOpcode()) { |
| 401 | case ISD::Constant: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 402 | // We know all of the bits for a constant! |
| 403 | KnownOne = cast<ConstantSDNode>(Op)->getValue() & DemandedMask; |
| 404 | KnownZero = ~KnownOne & DemandedMask; |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 405 | return false; // Don't fall through, will infinitely loop. |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 406 | case ISD::AND: |
Chris Lattner | 81cd355 | 2006-02-27 00:36:27 +0000 | [diff] [blame] | 407 | // If the RHS is a constant, check to see if the LHS would be zero without |
| 408 | // using the bits from the RHS. Below, we use knowledge about the RHS to |
| 409 | // simplify the LHS, here we're using information from the LHS to simplify |
| 410 | // the RHS. |
| 411 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 412 | uint64_t LHSZero, LHSOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 413 | TLO.DAG.ComputeMaskedBits(Op.getOperand(0), DemandedMask, |
| 414 | LHSZero, LHSOne, Depth+1); |
Chris Lattner | 81cd355 | 2006-02-27 00:36:27 +0000 | [diff] [blame] | 415 | // If the LHS already has zeros where RHSC does, this and is dead. |
| 416 | if ((LHSZero & DemandedMask) == (~RHSC->getValue() & DemandedMask)) |
| 417 | return TLO.CombineTo(Op, Op.getOperand(0)); |
| 418 | // If any of the set bits in the RHS are known zero on the LHS, shrink |
| 419 | // the constant. |
| 420 | if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & DemandedMask)) |
| 421 | return true; |
| 422 | } |
| 423 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 424 | if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero, |
| 425 | KnownOne, TLO, Depth+1)) |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 426 | return true; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 427 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 428 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownZero, |
| 429 | KnownZero2, KnownOne2, TLO, Depth+1)) |
| 430 | return true; |
| 431 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 432 | |
| 433 | // If all of the demanded bits are known one on one side, return the other. |
| 434 | // These bits cannot contribute to the result of the 'and'. |
| 435 | if ((DemandedMask & ~KnownZero2 & KnownOne)==(DemandedMask & ~KnownZero2)) |
| 436 | return TLO.CombineTo(Op, Op.getOperand(0)); |
| 437 | if ((DemandedMask & ~KnownZero & KnownOne2)==(DemandedMask & ~KnownZero)) |
| 438 | return TLO.CombineTo(Op, Op.getOperand(1)); |
| 439 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 440 | if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask) |
| 441 | return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType())); |
| 442 | // If the RHS is a constant, see if we can simplify it. |
| 443 | if (TLO.ShrinkDemandedConstant(Op, DemandedMask & ~KnownZero2)) |
| 444 | return true; |
Chris Lattner | 5f0c658 | 2006-02-27 00:22:28 +0000 | [diff] [blame] | 445 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 446 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 447 | KnownOne &= KnownOne2; |
| 448 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 449 | KnownZero |= KnownZero2; |
| 450 | break; |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 451 | case ISD::OR: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 452 | if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero, |
| 453 | KnownOne, TLO, Depth+1)) |
| 454 | return true; |
| 455 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 456 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & ~KnownOne, |
| 457 | KnownZero2, KnownOne2, TLO, Depth+1)) |
| 458 | return true; |
| 459 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 460 | |
| 461 | // If all of the demanded bits are known zero on one side, return the other. |
| 462 | // These bits cannot contribute to the result of the 'or'. |
Jeff Cohen | 5755b17 | 2006-02-17 02:12:18 +0000 | [diff] [blame] | 463 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 464 | return TLO.CombineTo(Op, Op.getOperand(0)); |
Jeff Cohen | 5755b17 | 2006-02-17 02:12:18 +0000 | [diff] [blame] | 465 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 466 | return TLO.CombineTo(Op, Op.getOperand(1)); |
| 467 | // If all of the potentially set bits on one side are known to be set on |
| 468 | // the other side, just use the 'other' side. |
| 469 | if ((DemandedMask & (~KnownZero) & KnownOne2) == |
| 470 | (DemandedMask & (~KnownZero))) |
| 471 | return TLO.CombineTo(Op, Op.getOperand(0)); |
| 472 | if ((DemandedMask & (~KnownZero2) & KnownOne) == |
| 473 | (DemandedMask & (~KnownZero2))) |
| 474 | return TLO.CombineTo(Op, Op.getOperand(1)); |
| 475 | // If the RHS is a constant, see if we can simplify it. |
| 476 | if (TLO.ShrinkDemandedConstant(Op, DemandedMask)) |
| 477 | return true; |
| 478 | |
| 479 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 480 | KnownZero &= KnownZero2; |
| 481 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 482 | KnownOne |= KnownOne2; |
| 483 | break; |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 484 | case ISD::XOR: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 485 | if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero, |
| 486 | KnownOne, TLO, Depth+1)) |
| 487 | return true; |
| 488 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 489 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, KnownZero2, |
| 490 | KnownOne2, TLO, Depth+1)) |
| 491 | return true; |
| 492 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 493 | |
| 494 | // If all of the demanded bits are known zero on one side, return the other. |
| 495 | // These bits cannot contribute to the result of the 'xor'. |
| 496 | if ((DemandedMask & KnownZero) == DemandedMask) |
| 497 | return TLO.CombineTo(Op, Op.getOperand(0)); |
| 498 | if ((DemandedMask & KnownZero2) == DemandedMask) |
| 499 | return TLO.CombineTo(Op, Op.getOperand(1)); |
Chris Lattner | 3687c1a | 2006-11-27 21:50:02 +0000 | [diff] [blame] | 500 | |
| 501 | // If all of the unknown bits are known to be zero on one side or the other |
| 502 | // (but not both) turn this into an *inclusive* or. |
| 503 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 504 | if ((DemandedMask & ~KnownZero & ~KnownZero2) == 0) |
| 505 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, Op.getValueType(), |
| 506 | Op.getOperand(0), |
| 507 | Op.getOperand(1))); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 508 | |
| 509 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 510 | KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 511 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 512 | KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 513 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 514 | // If all of the demanded bits on one side are known, and all of the set |
| 515 | // bits on that side are also known to be set on the other side, turn this |
| 516 | // into an AND, as we know the bits will be cleared. |
| 517 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 518 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known |
| 519 | if ((KnownOne & KnownOne2) == KnownOne) { |
| 520 | MVT::ValueType VT = Op.getValueType(); |
| 521 | SDOperand ANDC = TLO.DAG.getConstant(~KnownOne & DemandedMask, VT); |
| 522 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, VT, Op.getOperand(0), |
| 523 | ANDC)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // If the RHS is a constant, see if we can simplify it. |
| 528 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 529 | if (TLO.ShrinkDemandedConstant(Op, DemandedMask)) |
| 530 | return true; |
| 531 | |
| 532 | KnownZero = KnownZeroOut; |
| 533 | KnownOne = KnownOneOut; |
| 534 | break; |
| 535 | case ISD::SETCC: |
| 536 | // If we know the result of a setcc has the top bits zero, use this info. |
| 537 | if (getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) |
| 538 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 539 | break; |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 540 | case ISD::SELECT: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 541 | if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero, |
| 542 | KnownOne, TLO, Depth+1)) |
| 543 | return true; |
| 544 | if (SimplifyDemandedBits(Op.getOperand(1), DemandedMask, KnownZero2, |
| 545 | KnownOne2, TLO, Depth+1)) |
| 546 | return true; |
| 547 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 548 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 549 | |
| 550 | // If the operands are constants, see if we can simplify them. |
| 551 | if (TLO.ShrinkDemandedConstant(Op, DemandedMask)) |
| 552 | return true; |
| 553 | |
| 554 | // Only known if known in both the LHS and RHS. |
| 555 | KnownOne &= KnownOne2; |
| 556 | KnownZero &= KnownZero2; |
| 557 | break; |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 558 | case ISD::SELECT_CC: |
| 559 | if (SimplifyDemandedBits(Op.getOperand(3), DemandedMask, KnownZero, |
| 560 | KnownOne, TLO, Depth+1)) |
| 561 | return true; |
| 562 | if (SimplifyDemandedBits(Op.getOperand(2), DemandedMask, KnownZero2, |
| 563 | KnownOne2, TLO, Depth+1)) |
| 564 | return true; |
| 565 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 566 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 567 | |
| 568 | // If the operands are constants, see if we can simplify them. |
| 569 | if (TLO.ShrinkDemandedConstant(Op, DemandedMask)) |
| 570 | return true; |
| 571 | |
| 572 | // Only known if known in both the LHS and RHS. |
| 573 | KnownOne &= KnownOne2; |
| 574 | KnownZero &= KnownZero2; |
| 575 | break; |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 576 | case ISD::SHL: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 577 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 578 | unsigned ShAmt = SA->getValue(); |
| 579 | SDOperand InOp = Op.getOperand(0); |
| 580 | |
| 581 | // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a |
| 582 | // single shift. We can do this if the bottom bits (which are shifted |
| 583 | // out) are never demanded. |
| 584 | if (InOp.getOpcode() == ISD::SRL && |
| 585 | isa<ConstantSDNode>(InOp.getOperand(1))) { |
| 586 | if (ShAmt && (DemandedMask & ((1ULL << ShAmt)-1)) == 0) { |
| 587 | unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue(); |
| 588 | unsigned Opc = ISD::SHL; |
| 589 | int Diff = ShAmt-C1; |
| 590 | if (Diff < 0) { |
| 591 | Diff = -Diff; |
| 592 | Opc = ISD::SRL; |
| 593 | } |
| 594 | |
| 595 | SDOperand NewSA = |
Chris Lattner | 4e7e6cd | 2007-05-30 16:30:06 +0000 | [diff] [blame] | 596 | TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType()); |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 597 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 0a16a1f | 2007-04-18 03:01:40 +0000 | [diff] [blame] | 598 | return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT, |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 599 | InOp.getOperand(0), NewSA)); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask >> ShAmt, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 604 | KnownZero, KnownOne, TLO, Depth+1)) |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 605 | return true; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 606 | KnownZero <<= SA->getValue(); |
| 607 | KnownOne <<= SA->getValue(); |
| 608 | KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero. |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 609 | } |
| 610 | break; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 611 | case ISD::SRL: |
| 612 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 613 | MVT::ValueType VT = Op.getValueType(); |
| 614 | unsigned ShAmt = SA->getValue(); |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 615 | uint64_t TypeMask = MVT::getIntVTBitMask(VT); |
| 616 | unsigned VTSize = MVT::getSizeInBits(VT); |
| 617 | SDOperand InOp = Op.getOperand(0); |
| 618 | |
| 619 | // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a |
| 620 | // single shift. We can do this if the top bits (which are shifted out) |
| 621 | // are never demanded. |
| 622 | if (InOp.getOpcode() == ISD::SHL && |
| 623 | isa<ConstantSDNode>(InOp.getOperand(1))) { |
| 624 | if (ShAmt && (DemandedMask & (~0ULL << (VTSize-ShAmt))) == 0) { |
| 625 | unsigned C1 = cast<ConstantSDNode>(InOp.getOperand(1))->getValue(); |
| 626 | unsigned Opc = ISD::SRL; |
| 627 | int Diff = ShAmt-C1; |
| 628 | if (Diff < 0) { |
| 629 | Diff = -Diff; |
| 630 | Opc = ISD::SHL; |
| 631 | } |
| 632 | |
| 633 | SDOperand NewSA = |
Chris Lattner | 8c7d2d5 | 2007-04-17 22:53:02 +0000 | [diff] [blame] | 634 | TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType()); |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 635 | return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, VT, |
| 636 | InOp.getOperand(0), NewSA)); |
| 637 | } |
| 638 | } |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 639 | |
| 640 | // Compute the new bits that are at the top now. |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 641 | if (SimplifyDemandedBits(InOp, (DemandedMask << ShAmt) & TypeMask, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 642 | KnownZero, KnownOne, TLO, Depth+1)) |
| 643 | return true; |
| 644 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 645 | KnownZero &= TypeMask; |
| 646 | KnownOne &= TypeMask; |
| 647 | KnownZero >>= ShAmt; |
| 648 | KnownOne >>= ShAmt; |
Chris Lattner | c4fa603 | 2006-06-13 16:52:37 +0000 | [diff] [blame] | 649 | |
| 650 | uint64_t HighBits = (1ULL << ShAmt)-1; |
Chris Lattner | 895c4ab | 2007-04-17 21:14:16 +0000 | [diff] [blame] | 651 | HighBits <<= VTSize - ShAmt; |
Chris Lattner | c4fa603 | 2006-06-13 16:52:37 +0000 | [diff] [blame] | 652 | KnownZero |= HighBits; // High bits known zero. |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 653 | } |
| 654 | break; |
| 655 | case ISD::SRA: |
| 656 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 657 | MVT::ValueType VT = Op.getValueType(); |
| 658 | unsigned ShAmt = SA->getValue(); |
| 659 | |
| 660 | // Compute the new bits that are at the top now. |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 661 | uint64_t TypeMask = MVT::getIntVTBitMask(VT); |
| 662 | |
Chris Lattner | 1b73713 | 2006-05-08 17:22:53 +0000 | [diff] [blame] | 663 | uint64_t InDemandedMask = (DemandedMask << ShAmt) & TypeMask; |
| 664 | |
| 665 | // If any of the demanded bits are produced by the sign extension, we also |
| 666 | // demand the input sign bit. |
Chris Lattner | c4fa603 | 2006-06-13 16:52:37 +0000 | [diff] [blame] | 667 | uint64_t HighBits = (1ULL << ShAmt)-1; |
| 668 | HighBits <<= MVT::getSizeInBits(VT) - ShAmt; |
Chris Lattner | 1b73713 | 2006-05-08 17:22:53 +0000 | [diff] [blame] | 669 | if (HighBits & DemandedMask) |
| 670 | InDemandedMask |= MVT::getIntVTSignBit(VT); |
| 671 | |
| 672 | if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 673 | KnownZero, KnownOne, TLO, Depth+1)) |
| 674 | return true; |
| 675 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 676 | KnownZero &= TypeMask; |
| 677 | KnownOne &= TypeMask; |
Chris Lattner | c4fa603 | 2006-06-13 16:52:37 +0000 | [diff] [blame] | 678 | KnownZero >>= ShAmt; |
| 679 | KnownOne >>= ShAmt; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 680 | |
| 681 | // Handle the sign bits. |
| 682 | uint64_t SignBit = MVT::getIntVTSignBit(VT); |
Chris Lattner | c4fa603 | 2006-06-13 16:52:37 +0000 | [diff] [blame] | 683 | SignBit >>= ShAmt; // Adjust to where it is now in the mask. |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 684 | |
| 685 | // If the input sign bit is known to be zero, or if none of the top bits |
| 686 | // are demanded, turn this into an unsigned shift right. |
| 687 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 688 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, VT, Op.getOperand(0), |
| 689 | Op.getOperand(1))); |
| 690 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 691 | KnownOne |= HighBits; |
| 692 | } |
| 693 | } |
| 694 | break; |
| 695 | case ISD::SIGN_EXTEND_INREG: { |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 696 | MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
| 697 | |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 698 | // Sign extension. Compute the demanded bits in the result that are not |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 699 | // present in the input. |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 700 | uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & DemandedMask; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 701 | |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 702 | // If none of the extended bits are demanded, eliminate the sextinreg. |
| 703 | if (NewBits == 0) |
| 704 | return TLO.CombineTo(Op, Op.getOperand(0)); |
| 705 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 706 | uint64_t InSignBit = MVT::getIntVTSignBit(EVT); |
| 707 | int64_t InputDemandedBits = DemandedMask & MVT::getIntVTBitMask(EVT); |
| 708 | |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 709 | // Since the sign extended bits are demanded, we know that the sign |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 710 | // bit is demanded. |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 711 | InputDemandedBits |= InSignBit; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 712 | |
| 713 | if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits, |
| 714 | KnownZero, KnownOne, TLO, Depth+1)) |
| 715 | return true; |
| 716 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 717 | |
| 718 | // If the sign bit of the input is known set or clear, then we know the |
| 719 | // top bits of the result. |
| 720 | |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 721 | // If the input sign bit is known zero, convert this into a zero extension. |
| 722 | if (KnownZero & InSignBit) |
| 723 | return TLO.CombineTo(Op, |
| 724 | TLO.DAG.getZeroExtendInReg(Op.getOperand(0), EVT)); |
| 725 | |
| 726 | if (KnownOne & InSignBit) { // Input sign bit known set |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 727 | KnownOne |= NewBits; |
| 728 | KnownZero &= ~NewBits; |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 729 | } else { // Input sign bit unknown |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 730 | KnownZero &= ~NewBits; |
| 731 | KnownOne &= ~NewBits; |
| 732 | } |
| 733 | break; |
| 734 | } |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 735 | case ISD::CTTZ: |
| 736 | case ISD::CTLZ: |
| 737 | case ISD::CTPOP: { |
| 738 | MVT::ValueType VT = Op.getValueType(); |
| 739 | unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1; |
| 740 | KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT); |
| 741 | KnownOne = 0; |
| 742 | break; |
| 743 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 744 | case ISD::LOAD: { |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 745 | if (ISD::isZEXTLoad(Op.Val)) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 746 | LoadSDNode *LD = cast<LoadSDNode>(Op); |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 747 | MVT::ValueType VT = LD->getLoadedVT(); |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 748 | KnownZero |= ~MVT::getIntVTBitMask(VT) & DemandedMask; |
| 749 | } |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | case ISD::ZERO_EXTEND: { |
| 753 | uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType()); |
| 754 | |
| 755 | // If none of the top bits are demanded, convert this into an any_extend. |
| 756 | uint64_t NewBits = (~InMask) & DemandedMask; |
| 757 | if (NewBits == 0) |
| 758 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, |
| 759 | Op.getValueType(), |
| 760 | Op.getOperand(0))); |
| 761 | |
| 762 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask, |
| 763 | KnownZero, KnownOne, TLO, Depth+1)) |
| 764 | return true; |
| 765 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 766 | KnownZero |= NewBits; |
| 767 | break; |
| 768 | } |
| 769 | case ISD::SIGN_EXTEND: { |
| 770 | MVT::ValueType InVT = Op.getOperand(0).getValueType(); |
| 771 | uint64_t InMask = MVT::getIntVTBitMask(InVT); |
| 772 | uint64_t InSignBit = MVT::getIntVTSignBit(InVT); |
| 773 | uint64_t NewBits = (~InMask) & DemandedMask; |
| 774 | |
| 775 | // If none of the top bits are demanded, convert this into an any_extend. |
| 776 | if (NewBits == 0) |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 777 | return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND,Op.getValueType(), |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 778 | Op.getOperand(0))); |
| 779 | |
| 780 | // Since some of the sign extended bits are demanded, we know that the sign |
| 781 | // bit is demanded. |
| 782 | uint64_t InDemandedBits = DemandedMask & InMask; |
| 783 | InDemandedBits |= InSignBit; |
| 784 | |
| 785 | if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero, |
| 786 | KnownOne, TLO, Depth+1)) |
| 787 | return true; |
| 788 | |
| 789 | // If the sign bit is known zero, convert this to a zero extend. |
| 790 | if (KnownZero & InSignBit) |
| 791 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, |
| 792 | Op.getValueType(), |
| 793 | Op.getOperand(0))); |
| 794 | |
| 795 | // If the sign bit is known one, the top bits match. |
| 796 | if (KnownOne & InSignBit) { |
| 797 | KnownOne |= NewBits; |
| 798 | KnownZero &= ~NewBits; |
| 799 | } else { // Otherwise, top bits aren't known. |
| 800 | KnownOne &= ~NewBits; |
| 801 | KnownZero &= ~NewBits; |
| 802 | } |
| 803 | break; |
| 804 | } |
| 805 | case ISD::ANY_EXTEND: { |
| 806 | uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType()); |
| 807 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask, |
| 808 | KnownZero, KnownOne, TLO, Depth+1)) |
| 809 | return true; |
| 810 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 811 | break; |
| 812 | } |
Chris Lattner | fe8babf | 2006-05-05 22:32:12 +0000 | [diff] [blame] | 813 | case ISD::TRUNCATE: { |
Chris Lattner | c93dfda | 2006-05-06 00:11:52 +0000 | [diff] [blame] | 814 | // Simplify the input, using demanded bit information, and compute the known |
| 815 | // zero/one bits live out. |
Chris Lattner | fe8babf | 2006-05-05 22:32:12 +0000 | [diff] [blame] | 816 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, |
| 817 | KnownZero, KnownOne, TLO, Depth+1)) |
| 818 | return true; |
Chris Lattner | c93dfda | 2006-05-06 00:11:52 +0000 | [diff] [blame] | 819 | |
| 820 | // If the input is only used by this truncate, see if we can shrink it based |
| 821 | // on the known demanded bits. |
| 822 | if (Op.getOperand(0).Val->hasOneUse()) { |
| 823 | SDOperand In = Op.getOperand(0); |
| 824 | switch (In.getOpcode()) { |
| 825 | default: break; |
| 826 | case ISD::SRL: |
| 827 | // Shrink SRL by a constant if none of the high bits shifted in are |
| 828 | // demanded. |
| 829 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1))){ |
| 830 | uint64_t HighBits = MVT::getIntVTBitMask(In.getValueType()); |
| 831 | HighBits &= ~MVT::getIntVTBitMask(Op.getValueType()); |
| 832 | HighBits >>= ShAmt->getValue(); |
| 833 | |
| 834 | if (ShAmt->getValue() < MVT::getSizeInBits(Op.getValueType()) && |
| 835 | (DemandedMask & HighBits) == 0) { |
| 836 | // None of the shifted in bits are needed. Add a truncate of the |
| 837 | // shift input, then shift it. |
| 838 | SDOperand NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, |
| 839 | Op.getValueType(), |
| 840 | In.getOperand(0)); |
| 841 | return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL,Op.getValueType(), |
| 842 | NewTrunc, In.getOperand(1))); |
| 843 | } |
| 844 | } |
| 845 | break; |
| 846 | } |
| 847 | } |
| 848 | |
Chris Lattner | fe8babf | 2006-05-05 22:32:12 +0000 | [diff] [blame] | 849 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 850 | uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType()); |
| 851 | KnownZero &= OutMask; |
| 852 | KnownOne &= OutMask; |
| 853 | break; |
| 854 | } |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 855 | case ISD::AssertZext: { |
| 856 | MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
| 857 | uint64_t InMask = MVT::getIntVTBitMask(VT); |
| 858 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask & InMask, |
| 859 | KnownZero, KnownOne, TLO, Depth+1)) |
| 860 | return true; |
| 861 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 862 | KnownZero |= ~InMask & DemandedMask; |
| 863 | break; |
| 864 | } |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 865 | case ISD::ADD: |
Chris Lattner | a6bc5a4 | 2006-02-27 01:00:42 +0000 | [diff] [blame] | 866 | case ISD::SUB: |
Chris Lattner | 1482b5f | 2006-04-02 06:15:09 +0000 | [diff] [blame] | 867 | case ISD::INTRINSIC_WO_CHAIN: |
| 868 | case ISD::INTRINSIC_W_CHAIN: |
| 869 | case ISD::INTRINSIC_VOID: |
| 870 | // Just use ComputeMaskedBits to compute output bits. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 871 | TLO.DAG.ComputeMaskedBits(Op, DemandedMask, KnownZero, KnownOne, Depth); |
Chris Lattner | a6bc5a4 | 2006-02-27 01:00:42 +0000 | [diff] [blame] | 872 | break; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 873 | } |
Chris Lattner | ec66515 | 2006-02-26 23:36:02 +0000 | [diff] [blame] | 874 | |
| 875 | // If we know the value of all of the demanded bits, return this as a |
| 876 | // constant. |
| 877 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
| 878 | return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType())); |
| 879 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 880 | return false; |
| 881 | } |
| 882 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 883 | /// computeMaskedBitsForTargetNode - Determine which of the bits specified |
| 884 | /// in Mask are known to be either zero or one and return them in the |
| 885 | /// KnownZero/KnownOne bitsets. |
| 886 | void TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 887 | uint64_t Mask, |
| 888 | uint64_t &KnownZero, |
| 889 | uint64_t &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 890 | const SelectionDAG &DAG, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 891 | unsigned Depth) const { |
Chris Lattner | 1b5232a | 2006-04-02 06:19:46 +0000 | [diff] [blame] | 892 | assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || |
| 893 | Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || |
| 894 | Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || |
| 895 | Op.getOpcode() == ISD::INTRINSIC_VOID) && |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 896 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 897 | " is a target node!"); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 898 | KnownZero = 0; |
| 899 | KnownOne = 0; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 900 | } |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 901 | |
Chris Lattner | 5c3e21d | 2006-05-06 09:27:13 +0000 | [diff] [blame] | 902 | /// ComputeNumSignBitsForTargetNode - This method can be implemented by |
| 903 | /// targets that want to expose additional information about sign bits to the |
| 904 | /// DAG Combiner. |
| 905 | unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDOperand Op, |
| 906 | unsigned Depth) const { |
| 907 | assert((Op.getOpcode() >= ISD::BUILTIN_OP_END || |
| 908 | Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || |
| 909 | Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || |
| 910 | Op.getOpcode() == ISD::INTRINSIC_VOID) && |
| 911 | "Should use ComputeNumSignBits if you don't know whether Op" |
| 912 | " is a target node!"); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 917 | /// SimplifySetCC - Try to simplify a setcc built with the specified operands |
| 918 | /// and cc. If it is unable to simplify it, return a null SDOperand. |
| 919 | SDOperand |
| 920 | TargetLowering::SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1, |
| 921 | ISD::CondCode Cond, bool foldBooleans, |
| 922 | DAGCombinerInfo &DCI) const { |
| 923 | SelectionDAG &DAG = DCI.DAG; |
| 924 | |
| 925 | // These setcc operations always fold. |
| 926 | switch (Cond) { |
| 927 | default: break; |
| 928 | case ISD::SETFALSE: |
| 929 | case ISD::SETFALSE2: return DAG.getConstant(0, VT); |
| 930 | case ISD::SETTRUE: |
| 931 | case ISD::SETTRUE2: return DAG.getConstant(1, VT); |
| 932 | } |
| 933 | |
| 934 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 935 | uint64_t C1 = N1C->getValue(); |
| 936 | if (isa<ConstantSDNode>(N0.Val)) { |
| 937 | return DAG.FoldSetCC(VT, N0, N1, Cond); |
| 938 | } else { |
| 939 | // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an |
| 940 | // equality comparison, then we're just comparing whether X itself is |
| 941 | // zero. |
| 942 | if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) && |
| 943 | N0.getOperand(0).getOpcode() == ISD::CTLZ && |
| 944 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 945 | unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 946 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 947 | ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) { |
| 948 | if ((C1 == 0) == (Cond == ISD::SETEQ)) { |
| 949 | // (srl (ctlz x), 5) == 0 -> X != 0 |
| 950 | // (srl (ctlz x), 5) != 1 -> X != 0 |
| 951 | Cond = ISD::SETNE; |
| 952 | } else { |
| 953 | // (srl (ctlz x), 5) != 0 -> X == 0 |
| 954 | // (srl (ctlz x), 5) == 1 -> X == 0 |
| 955 | Cond = ISD::SETEQ; |
| 956 | } |
| 957 | SDOperand Zero = DAG.getConstant(0, N0.getValueType()); |
| 958 | return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0), |
| 959 | Zero, Cond); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
| 964 | if (N0.getOpcode() == ISD::ZERO_EXTEND) { |
| 965 | unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType()); |
| 966 | |
| 967 | // If the comparison constant has bits in the upper part, the |
| 968 | // zero-extended value could never match. |
| 969 | if (C1 & (~0ULL << InSize)) { |
| 970 | unsigned VSize = MVT::getSizeInBits(N0.getValueType()); |
| 971 | switch (Cond) { |
| 972 | case ISD::SETUGT: |
| 973 | case ISD::SETUGE: |
| 974 | case ISD::SETEQ: return DAG.getConstant(0, VT); |
| 975 | case ISD::SETULT: |
| 976 | case ISD::SETULE: |
| 977 | case ISD::SETNE: return DAG.getConstant(1, VT); |
| 978 | case ISD::SETGT: |
| 979 | case ISD::SETGE: |
| 980 | // True if the sign bit of C1 is set. |
Chris Lattner | 01ca65b | 2007-02-24 02:09:29 +0000 | [diff] [blame] | 981 | return DAG.getConstant((C1 & (1ULL << (VSize-1))) != 0, VT); |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 982 | case ISD::SETLT: |
| 983 | case ISD::SETLE: |
| 984 | // True if the sign bit of C1 isn't set. |
Chris Lattner | 01ca65b | 2007-02-24 02:09:29 +0000 | [diff] [blame] | 985 | return DAG.getConstant((C1 & (1ULL << (VSize-1))) == 0, VT); |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 986 | default: |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // Otherwise, we can perform the comparison with the low bits. |
| 992 | switch (Cond) { |
| 993 | case ISD::SETEQ: |
| 994 | case ISD::SETNE: |
| 995 | case ISD::SETUGT: |
| 996 | case ISD::SETUGE: |
| 997 | case ISD::SETULT: |
| 998 | case ISD::SETULE: |
| 999 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1000 | DAG.getConstant(C1, N0.getOperand(0).getValueType()), |
| 1001 | Cond); |
| 1002 | default: |
| 1003 | break; // todo, be more careful with signed comparisons |
| 1004 | } |
| 1005 | } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1006 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 1007 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); |
| 1008 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 1009 | MVT::ValueType ExtDstTy = N0.getValueType(); |
| 1010 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
| 1011 | |
| 1012 | // If the extended part has any inconsistent bits, it cannot ever |
| 1013 | // compare equal. In other words, they have to be all ones or all |
| 1014 | // zeros. |
| 1015 | uint64_t ExtBits = |
| 1016 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
| 1017 | if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) |
| 1018 | return DAG.getConstant(Cond == ISD::SETNE, VT); |
| 1019 | |
| 1020 | SDOperand ZextOp; |
| 1021 | MVT::ValueType Op0Ty = N0.getOperand(0).getValueType(); |
| 1022 | if (Op0Ty == ExtSrcTy) { |
| 1023 | ZextOp = N0.getOperand(0); |
| 1024 | } else { |
| 1025 | int64_t Imm = ~0ULL >> (64-ExtSrcTyBits); |
| 1026 | ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0), |
| 1027 | DAG.getConstant(Imm, Op0Ty)); |
| 1028 | } |
| 1029 | if (!DCI.isCalledByLegalizer()) |
| 1030 | DCI.AddToWorklist(ZextOp.Val); |
| 1031 | // Otherwise, make this a use of a zext. |
| 1032 | return DAG.getSetCC(VT, ZextOp, |
| 1033 | DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)), |
| 1034 | ExtDstTy), |
| 1035 | Cond); |
| 1036 | } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) && |
| 1037 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 1038 | |
| 1039 | // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC |
| 1040 | if (N0.getOpcode() == ISD::SETCC) { |
| 1041 | bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1); |
| 1042 | if (TrueWhenTrue) |
| 1043 | return N0; |
| 1044 | |
| 1045 | // Invert the condition. |
| 1046 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); |
| 1047 | CC = ISD::getSetCCInverse(CC, |
| 1048 | MVT::isInteger(N0.getOperand(0).getValueType())); |
| 1049 | return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC); |
| 1050 | } |
| 1051 | |
| 1052 | if ((N0.getOpcode() == ISD::XOR || |
| 1053 | (N0.getOpcode() == ISD::AND && |
| 1054 | N0.getOperand(0).getOpcode() == ISD::XOR && |
| 1055 | N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && |
| 1056 | isa<ConstantSDNode>(N0.getOperand(1)) && |
| 1057 | cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) { |
| 1058 | // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We |
| 1059 | // can only do this if the top bits are known zero. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1060 | if (DAG.MaskedValueIsZero(N0, |
| 1061 | MVT::getIntVTBitMask(N0.getValueType())-1)){ |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 1062 | // Okay, get the un-inverted input value. |
| 1063 | SDOperand Val; |
| 1064 | if (N0.getOpcode() == ISD::XOR) |
| 1065 | Val = N0.getOperand(0); |
| 1066 | else { |
| 1067 | assert(N0.getOpcode() == ISD::AND && |
| 1068 | N0.getOperand(0).getOpcode() == ISD::XOR); |
| 1069 | // ((X^1)&1)^1 -> X & 1 |
| 1070 | Val = DAG.getNode(ISD::AND, N0.getValueType(), |
| 1071 | N0.getOperand(0).getOperand(0), |
| 1072 | N0.getOperand(1)); |
| 1073 | } |
| 1074 | return DAG.getSetCC(VT, Val, N1, |
| 1075 | Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | uint64_t MinVal, MaxVal; |
| 1081 | unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0)); |
| 1082 | if (ISD::isSignedIntSetCC(Cond)) { |
| 1083 | MinVal = 1ULL << (OperandBitSize-1); |
| 1084 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 1085 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 1086 | else |
| 1087 | MaxVal = 0; |
| 1088 | } else { |
| 1089 | MinVal = 0; |
| 1090 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 1091 | } |
| 1092 | |
| 1093 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 1094 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 1095 | if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true |
| 1096 | --C1; // X >= C0 --> X > (C0-1) |
| 1097 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 1098 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
| 1099 | } |
| 1100 | |
| 1101 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 1102 | if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true |
| 1103 | ++C1; // X <= C0 --> X < (C0+1) |
| 1104 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 1105 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
| 1106 | } |
| 1107 | |
| 1108 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) |
| 1109 | return DAG.getConstant(0, VT); // X < MIN --> false |
| 1110 | if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal) |
| 1111 | return DAG.getConstant(1, VT); // X >= MIN --> true |
| 1112 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal) |
| 1113 | return DAG.getConstant(0, VT); // X > MAX --> false |
| 1114 | if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal) |
| 1115 | return DAG.getConstant(1, VT); // X <= MAX --> true |
| 1116 | |
| 1117 | // Canonicalize setgt X, Min --> setne X, Min |
| 1118 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) |
| 1119 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
| 1120 | // Canonicalize setlt X, Max --> setne X, Max |
| 1121 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) |
| 1122 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
| 1123 | |
| 1124 | // If we have setult X, 1, turn it into seteq X, 0 |
| 1125 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) |
| 1126 | return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()), |
| 1127 | ISD::SETEQ); |
| 1128 | // If we have setugt X, Max-1, turn it into seteq X, Max |
| 1129 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) |
| 1130 | return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()), |
| 1131 | ISD::SETEQ); |
| 1132 | |
| 1133 | // If we have "setcc X, C0", check to see if we can shrink the immediate |
| 1134 | // by changing cc. |
| 1135 | |
| 1136 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 1137 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 1138 | C1 == (~0ULL >> (65-OperandBitSize))) |
| 1139 | return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()), |
| 1140 | ISD::SETLT); |
| 1141 | |
| 1142 | // FIXME: Implement the rest of these. |
| 1143 | |
| 1144 | // Fold bit comparisons when we can. |
| 1145 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 1146 | VT == N0.getValueType() && N0.getOpcode() == ISD::AND) |
| 1147 | if (ConstantSDNode *AndRHS = |
| 1148 | dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1149 | if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 1150 | // Perform the xform if the AND RHS is a single bit. |
| 1151 | if (isPowerOf2_64(AndRHS->getValue())) { |
| 1152 | return DAG.getNode(ISD::SRL, VT, N0, |
| 1153 | DAG.getConstant(Log2_64(AndRHS->getValue()), |
| 1154 | getShiftAmountTy())); |
| 1155 | } |
| 1156 | } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) { |
| 1157 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 1158 | // Perform the xform if C1 is a single bit. |
| 1159 | if (isPowerOf2_64(C1)) { |
| 1160 | return DAG.getNode(ISD::SRL, VT, N0, |
| 1161 | DAG.getConstant(Log2_64(C1), getShiftAmountTy())); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | } else if (isa<ConstantSDNode>(N0.Val)) { |
| 1167 | // Ensure that the constant occurs on the RHS. |
| 1168 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 1169 | } |
| 1170 | |
| 1171 | if (isa<ConstantFPSDNode>(N0.Val)) { |
| 1172 | // Constant fold or commute setcc. |
| 1173 | SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond); |
| 1174 | if (O.Val) return O; |
| 1175 | } |
| 1176 | |
| 1177 | if (N0 == N1) { |
| 1178 | // We can always fold X == X for integer setcc's. |
| 1179 | if (MVT::isInteger(N0.getValueType())) |
| 1180 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 1181 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 1182 | if (UOF == 2) // FP operators that are undefined on NaNs. |
| 1183 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 1184 | if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) |
| 1185 | return DAG.getConstant(UOF, VT); |
| 1186 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 1187 | // if it is not already. |
| 1188 | ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; |
| 1189 | if (NewCond != Cond) |
| 1190 | return DAG.getSetCC(VT, N0, N1, NewCond); |
| 1191 | } |
| 1192 | |
| 1193 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 1194 | MVT::isInteger(N0.getValueType())) { |
| 1195 | if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || |
| 1196 | N0.getOpcode() == ISD::XOR) { |
| 1197 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 1198 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1199 | if (N0.getOperand(0) == N1.getOperand(0)) |
| 1200 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 1201 | if (N0.getOperand(1) == N1.getOperand(1)) |
| 1202 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond); |
| 1203 | if (DAG.isCommutativeBinOp(N0.getOpcode())) { |
| 1204 | // If X op Y == Y op X, try other combinations. |
| 1205 | if (N0.getOperand(0) == N1.getOperand(1)) |
| 1206 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond); |
| 1207 | if (N0.getOperand(1) == N1.getOperand(0)) |
| 1208 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { |
| 1213 | if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1214 | // Turn (X+C1) == C2 --> X == C2-C1 |
| 1215 | if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) { |
| 1216 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1217 | DAG.getConstant(RHSC->getValue()-LHSR->getValue(), |
| 1218 | N0.getValueType()), Cond); |
| 1219 | } |
| 1220 | |
| 1221 | // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. |
| 1222 | if (N0.getOpcode() == ISD::XOR) |
| 1223 | // If we know that all of the inverted bits are zero, don't bother |
| 1224 | // performing the inversion. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1225 | if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue())) |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 1226 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1227 | DAG.getConstant(LHSR->getValue()^RHSC->getValue(), |
| 1228 | N0.getValueType()), Cond); |
| 1229 | } |
| 1230 | |
| 1231 | // Turn (C1-X) == C2 --> X == C1-C2 |
| 1232 | if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { |
| 1233 | if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) { |
| 1234 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 1235 | DAG.getConstant(SUBC->getValue()-RHSC->getValue(), |
| 1236 | N0.getValueType()), Cond); |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | // Simplify (X+Z) == X --> Z == 0 |
| 1242 | if (N0.getOperand(0) == N1) |
| 1243 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 1244 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 1245 | if (N0.getOperand(1) == N1) { |
| 1246 | if (DAG.isCommutativeBinOp(N0.getOpcode())) |
| 1247 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 1248 | DAG.getConstant(0, N0.getValueType()), Cond); |
Chris Lattner | 2ad913b | 2007-05-19 00:43:44 +0000 | [diff] [blame] | 1249 | else if (N0.Val->hasOneUse()) { |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 1250 | assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 1251 | // (Z-X) == X --> Z == X<<1 |
| 1252 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), |
| 1253 | N1, |
| 1254 | DAG.getConstant(1, getShiftAmountTy())); |
| 1255 | if (!DCI.isCalledByLegalizer()) |
| 1256 | DCI.AddToWorklist(SH.Val); |
| 1257 | return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond); |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 1263 | N1.getOpcode() == ISD::XOR) { |
| 1264 | // Simplify X == (X+Z) --> Z == 0 |
| 1265 | if (N1.getOperand(0) == N0) { |
| 1266 | return DAG.getSetCC(VT, N1.getOperand(1), |
| 1267 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 1268 | } else if (N1.getOperand(1) == N0) { |
| 1269 | if (DAG.isCommutativeBinOp(N1.getOpcode())) { |
| 1270 | return DAG.getSetCC(VT, N1.getOperand(0), |
| 1271 | DAG.getConstant(0, N1.getValueType()), Cond); |
Chris Lattner | 7667c0b | 2007-05-19 00:46:51 +0000 | [diff] [blame] | 1272 | } else if (N1.Val->hasOneUse()) { |
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 1273 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 1274 | // X == (Z-X) --> X<<1 == Z |
| 1275 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0, |
| 1276 | DAG.getConstant(1, getShiftAmountTy())); |
| 1277 | if (!DCI.isCalledByLegalizer()) |
| 1278 | DCI.AddToWorklist(SH.Val); |
| 1279 | return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | // Fold away ALL boolean setcc's. |
| 1286 | SDOperand Temp; |
| 1287 | if (N0.getValueType() == MVT::i1 && foldBooleans) { |
| 1288 | switch (Cond) { |
| 1289 | default: assert(0 && "Unknown integer setcc!"); |
| 1290 | case ISD::SETEQ: // X == Y -> (X^Y)^1 |
| 1291 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 1292 | N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1)); |
| 1293 | if (!DCI.isCalledByLegalizer()) |
| 1294 | DCI.AddToWorklist(Temp.Val); |
| 1295 | break; |
| 1296 | case ISD::SETNE: // X != Y --> (X^Y) |
| 1297 | N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 1298 | break; |
| 1299 | case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y |
| 1300 | case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y |
| 1301 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 1302 | N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); |
| 1303 | if (!DCI.isCalledByLegalizer()) |
| 1304 | DCI.AddToWorklist(Temp.Val); |
| 1305 | break; |
| 1306 | case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X |
| 1307 | case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X |
| 1308 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 1309 | N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp); |
| 1310 | if (!DCI.isCalledByLegalizer()) |
| 1311 | DCI.AddToWorklist(Temp.Val); |
| 1312 | break; |
| 1313 | case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y |
| 1314 | case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y |
| 1315 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 1316 | N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp); |
| 1317 | if (!DCI.isCalledByLegalizer()) |
| 1318 | DCI.AddToWorklist(Temp.Val); |
| 1319 | break; |
| 1320 | case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X |
| 1321 | case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X |
| 1322 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 1323 | N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp); |
| 1324 | break; |
| 1325 | } |
| 1326 | if (VT != MVT::i1) { |
| 1327 | if (!DCI.isCalledByLegalizer()) |
| 1328 | DCI.AddToWorklist(N0.Val); |
| 1329 | // FIXME: If running after legalize, we probably can't do this. |
| 1330 | N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
| 1331 | } |
| 1332 | return N0; |
| 1333 | } |
| 1334 | |
| 1335 | // Could not fold it. |
| 1336 | return SDOperand(); |
| 1337 | } |
| 1338 | |
Chris Lattner | 00ffed0 | 2006-03-01 04:52:55 +0000 | [diff] [blame] | 1339 | SDOperand TargetLowering:: |
| 1340 | PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const { |
| 1341 | // Default implementation: no optimization. |
| 1342 | return SDOperand(); |
| 1343 | } |
| 1344 | |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1345 | //===----------------------------------------------------------------------===// |
| 1346 | // Inline Assembler Implementation Methods |
| 1347 | //===----------------------------------------------------------------------===// |
| 1348 | |
| 1349 | TargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1350 | TargetLowering::getConstraintType(const std::string &Constraint) const { |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1351 | // FIXME: lots more standard ones to handle. |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1352 | if (Constraint.size() == 1) { |
| 1353 | switch (Constraint[0]) { |
| 1354 | default: break; |
| 1355 | case 'r': return C_RegisterClass; |
| 1356 | case 'm': // memory |
| 1357 | case 'o': // offsetable |
| 1358 | case 'V': // not offsetable |
| 1359 | return C_Memory; |
| 1360 | case 'i': // Simple Integer or Relocatable Constant |
| 1361 | case 'n': // Simple Integer |
| 1362 | case 's': // Relocatable Constant |
Chris Lattner | c13dd1c | 2007-03-25 04:35:41 +0000 | [diff] [blame] | 1363 | case 'X': // Allow ANY value. |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1364 | case 'I': // Target registers. |
| 1365 | case 'J': |
| 1366 | case 'K': |
| 1367 | case 'L': |
| 1368 | case 'M': |
| 1369 | case 'N': |
| 1370 | case 'O': |
| 1371 | case 'P': |
| 1372 | return C_Other; |
| 1373 | } |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1374 | } |
Chris Lattner | 065421f | 2007-03-25 02:18:14 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (Constraint.size() > 1 && Constraint[0] == '{' && |
| 1377 | Constraint[Constraint.size()-1] == '}') |
| 1378 | return C_Register; |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1379 | return C_Unknown; |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 1382 | /// isOperandValidForConstraint - Return the specified operand (possibly |
| 1383 | /// modified) if the specified SDOperand is valid for the specified target |
| 1384 | /// constraint letter, otherwise return null. |
| 1385 | SDOperand TargetLowering::isOperandValidForConstraint(SDOperand Op, |
| 1386 | char ConstraintLetter, |
| 1387 | SelectionDAG &DAG) { |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1388 | switch (ConstraintLetter) { |
Chris Lattner | 9ff6ee8 | 2007-02-17 06:00:35 +0000 | [diff] [blame] | 1389 | default: break; |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1390 | case 'i': // Simple Integer or Relocatable Constant |
| 1391 | case 'n': // Simple Integer |
| 1392 | case 's': // Relocatable Constant |
Chris Lattner | 75c7d2b | 2007-05-03 16:54:34 +0000 | [diff] [blame] | 1393 | case 'X': { // Allows any operand. |
| 1394 | // These operands are interested in values of the form (GV+C), where C may |
| 1395 | // be folded in as an offset of GV, or it may be explicitly added. Also, it |
| 1396 | // is possible and fine if either GV or C are missing. |
| 1397 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); |
| 1398 | GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); |
| 1399 | |
| 1400 | // If we have "(add GV, C)", pull out GV/C |
| 1401 | if (Op.getOpcode() == ISD::ADD) { |
| 1402 | C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 1403 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 1404 | if (C == 0 || GA == 0) { |
| 1405 | C = dyn_cast<ConstantSDNode>(Op.getOperand(0)); |
| 1406 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1)); |
| 1407 | } |
| 1408 | if (C == 0 || GA == 0) |
| 1409 | C = 0, GA = 0; |
| 1410 | } |
| 1411 | |
| 1412 | // If we find a valid operand, map to the TargetXXX version so that the |
| 1413 | // value itself doesn't get selected. |
| 1414 | if (GA) { // Either &GV or &GV+C |
| 1415 | if (ConstraintLetter != 'n') { |
| 1416 | int64_t Offs = GA->getOffset(); |
| 1417 | if (C) Offs += C->getValue(); |
| 1418 | return DAG.getTargetGlobalAddress(GA->getGlobal(), Op.getValueType(), |
| 1419 | Offs); |
| 1420 | } |
| 1421 | } |
| 1422 | if (C) { // just C, no GV. |
Chris Lattner | 9ff6ee8 | 2007-02-17 06:00:35 +0000 | [diff] [blame] | 1423 | // Simple constants are not allowed for 's'. |
| 1424 | if (ConstraintLetter != 's') |
| 1425 | return DAG.getTargetConstant(C->getValue(), Op.getValueType()); |
| 1426 | } |
Chris Lattner | 9ff6ee8 | 2007-02-17 06:00:35 +0000 | [diff] [blame] | 1427 | break; |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1428 | } |
Chris Lattner | 75c7d2b | 2007-05-03 16:54:34 +0000 | [diff] [blame] | 1429 | } |
Chris Lattner | 9ff6ee8 | 2007-02-17 06:00:35 +0000 | [diff] [blame] | 1430 | return SDOperand(0,0); |
Chris Lattner | eb8146b | 2006-02-04 02:13:02 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 1433 | std::vector<unsigned> TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1434 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 1435 | MVT::ValueType VT) const { |
| 1436 | return std::vector<unsigned>(); |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | std::pair<unsigned, const TargetRegisterClass*> TargetLowering:: |
Chris Lattner | 4217ca8dc | 2006-02-21 23:11:00 +0000 | [diff] [blame] | 1441 | getRegForInlineAsmConstraint(const std::string &Constraint, |
| 1442 | MVT::ValueType VT) const { |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1443 | if (Constraint[0] != '{') |
| 1444 | return std::pair<unsigned, const TargetRegisterClass*>(0, 0); |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 1445 | assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?"); |
| 1446 | |
| 1447 | // Remove the braces from around the name. |
| 1448 | std::string RegName(Constraint.begin()+1, Constraint.end()-1); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Figure out which register class contains this reg. |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 1451 | const MRegisterInfo *RI = TM.getRegisterInfo(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1452 | for (MRegisterInfo::regclass_iterator RCI = RI->regclass_begin(), |
| 1453 | E = RI->regclass_end(); RCI != E; ++RCI) { |
| 1454 | const TargetRegisterClass *RC = *RCI; |
Chris Lattner | b3befd4 | 2006-02-22 23:00:51 +0000 | [diff] [blame] | 1455 | |
| 1456 | // If none of the the value types for this register class are valid, we |
| 1457 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 1458 | bool isLegal = false; |
| 1459 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 1460 | I != E; ++I) { |
| 1461 | if (isTypeLegal(*I)) { |
| 1462 | isLegal = true; |
| 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | if (!isLegal) continue; |
| 1468 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1469 | for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); |
| 1470 | I != E; ++I) { |
Chris Lattner | b3befd4 | 2006-02-22 23:00:51 +0000 | [diff] [blame] | 1471 | if (StringsEqualNoCase(RegName, RI->get(*I).Name)) |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1472 | return std::make_pair(*I, RC); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1473 | } |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 1474 | } |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1476 | return std::pair<unsigned, const TargetRegisterClass*>(0, 0); |
Chris Lattner | 4ccb070 | 2006-01-26 20:37:03 +0000 | [diff] [blame] | 1477 | } |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 1478 | |
| 1479 | //===----------------------------------------------------------------------===// |
| 1480 | // Loop Strength Reduction hooks |
| 1481 | //===----------------------------------------------------------------------===// |
| 1482 | |
Chris Lattner | 1436bb6 | 2007-03-30 23:14:50 +0000 | [diff] [blame] | 1483 | /// isLegalAddressingMode - Return true if the addressing mode represented |
| 1484 | /// by AM is legal for this target, for a load/store of the specified type. |
| 1485 | bool TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 1486 | const Type *Ty) const { |
| 1487 | // The default implementation of this implements a conservative RISCy, r+r and |
| 1488 | // r+i addr mode. |
| 1489 | |
| 1490 | // Allows a sign-extended 16-bit immediate field. |
| 1491 | if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) |
| 1492 | return false; |
| 1493 | |
| 1494 | // No global is ever allowed as a base. |
| 1495 | if (AM.BaseGV) |
| 1496 | return false; |
| 1497 | |
| 1498 | // Only support r+r, |
| 1499 | switch (AM.Scale) { |
| 1500 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 1501 | break; |
| 1502 | case 1: |
| 1503 | if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. |
| 1504 | return false; |
| 1505 | // Otherwise we have r+r or r+i. |
| 1506 | break; |
| 1507 | case 2: |
| 1508 | if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. |
| 1509 | return false; |
| 1510 | // Allow 2*r as r+r. |
| 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 1517 | // Magic for divide replacement |
| 1518 | |
| 1519 | struct ms { |
| 1520 | int64_t m; // magic number |
| 1521 | int64_t s; // shift amount |
| 1522 | }; |
| 1523 | |
| 1524 | struct mu { |
| 1525 | uint64_t m; // magic number |
| 1526 | int64_t a; // add indicator |
| 1527 | int64_t s; // shift amount |
| 1528 | }; |
| 1529 | |
| 1530 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 1531 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 1532 | /// or -1. |
| 1533 | static ms magic32(int32_t d) { |
| 1534 | int32_t p; |
| 1535 | uint32_t ad, anc, delta, q1, r1, q2, r2, t; |
| 1536 | const uint32_t two31 = 0x80000000U; |
| 1537 | struct ms mag; |
| 1538 | |
| 1539 | ad = abs(d); |
| 1540 | t = two31 + ((uint32_t)d >> 31); |
| 1541 | anc = t - 1 - t%ad; // absolute value of nc |
| 1542 | p = 31; // initialize p |
| 1543 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 1544 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 1545 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 1546 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 1547 | do { |
| 1548 | p = p + 1; |
| 1549 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 1550 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 1551 | if (r1 >= anc) { // must be unsigned comparison |
| 1552 | q1 = q1 + 1; |
| 1553 | r1 = r1 - anc; |
| 1554 | } |
| 1555 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 1556 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 1557 | if (r2 >= ad) { // must be unsigned comparison |
| 1558 | q2 = q2 + 1; |
| 1559 | r2 = r2 - ad; |
| 1560 | } |
| 1561 | delta = ad - r2; |
| 1562 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 1563 | |
| 1564 | mag.m = (int32_t)(q2 + 1); // make sure to sign extend |
| 1565 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 1566 | mag.s = p - 32; // resulting shift |
| 1567 | return mag; |
| 1568 | } |
| 1569 | |
| 1570 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 1571 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 1572 | static mu magicu32(uint32_t d) { |
| 1573 | int32_t p; |
| 1574 | uint32_t nc, delta, q1, r1, q2, r2; |
| 1575 | struct mu magu; |
| 1576 | magu.a = 0; // initialize "add" indicator |
| 1577 | nc = - 1 - (-d)%d; |
| 1578 | p = 31; // initialize p |
| 1579 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 1580 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 1581 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 1582 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 1583 | do { |
| 1584 | p = p + 1; |
| 1585 | if (r1 >= nc - r1 ) { |
| 1586 | q1 = 2*q1 + 1; // update q1 |
| 1587 | r1 = 2*r1 - nc; // update r1 |
| 1588 | } |
| 1589 | else { |
| 1590 | q1 = 2*q1; // update q1 |
| 1591 | r1 = 2*r1; // update r1 |
| 1592 | } |
| 1593 | if (r2 + 1 >= d - r2) { |
| 1594 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 1595 | q2 = 2*q2 + 1; // update q2 |
| 1596 | r2 = 2*r2 + 1 - d; // update r2 |
| 1597 | } |
| 1598 | else { |
| 1599 | if (q2 >= 0x80000000) magu.a = 1; |
| 1600 | q2 = 2*q2; // update q2 |
| 1601 | r2 = 2*r2 + 1; // update r2 |
| 1602 | } |
| 1603 | delta = d - 1 - r2; |
| 1604 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 1605 | magu.m = q2 + 1; // resulting magic number |
| 1606 | magu.s = p - 32; // resulting shift |
| 1607 | return magu; |
| 1608 | } |
| 1609 | |
| 1610 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 1611 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 1612 | /// or -1. |
| 1613 | static ms magic64(int64_t d) { |
| 1614 | int64_t p; |
| 1615 | uint64_t ad, anc, delta, q1, r1, q2, r2, t; |
| 1616 | const uint64_t two63 = 9223372036854775808ULL; // 2^63 |
| 1617 | struct ms mag; |
| 1618 | |
| 1619 | ad = d >= 0 ? d : -d; |
| 1620 | t = two63 + ((uint64_t)d >> 63); |
| 1621 | anc = t - 1 - t%ad; // absolute value of nc |
| 1622 | p = 63; // initialize p |
| 1623 | q1 = two63/anc; // initialize q1 = 2p/abs(nc) |
| 1624 | r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 1625 | q2 = two63/ad; // initialize q2 = 2p/abs(d) |
| 1626 | r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 1627 | do { |
| 1628 | p = p + 1; |
| 1629 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 1630 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 1631 | if (r1 >= anc) { // must be unsigned comparison |
| 1632 | q1 = q1 + 1; |
| 1633 | r1 = r1 - anc; |
| 1634 | } |
| 1635 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 1636 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 1637 | if (r2 >= ad) { // must be unsigned comparison |
| 1638 | q2 = q2 + 1; |
| 1639 | r2 = r2 - ad; |
| 1640 | } |
| 1641 | delta = ad - r2; |
| 1642 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 1643 | |
| 1644 | mag.m = q2 + 1; |
| 1645 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 1646 | mag.s = p - 64; // resulting shift |
| 1647 | return mag; |
| 1648 | } |
| 1649 | |
| 1650 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 1651 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 1652 | static mu magicu64(uint64_t d) |
| 1653 | { |
| 1654 | int64_t p; |
| 1655 | uint64_t nc, delta, q1, r1, q2, r2; |
| 1656 | struct mu magu; |
| 1657 | magu.a = 0; // initialize "add" indicator |
| 1658 | nc = - 1 - (-d)%d; |
| 1659 | p = 63; // initialize p |
| 1660 | q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc |
| 1661 | r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc) |
| 1662 | q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d |
| 1663 | r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d) |
| 1664 | do { |
| 1665 | p = p + 1; |
| 1666 | if (r1 >= nc - r1 ) { |
| 1667 | q1 = 2*q1 + 1; // update q1 |
| 1668 | r1 = 2*r1 - nc; // update r1 |
| 1669 | } |
| 1670 | else { |
| 1671 | q1 = 2*q1; // update q1 |
| 1672 | r1 = 2*r1; // update r1 |
| 1673 | } |
| 1674 | if (r2 + 1 >= d - r2) { |
| 1675 | if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1; |
| 1676 | q2 = 2*q2 + 1; // update q2 |
| 1677 | r2 = 2*r2 + 1 - d; // update r2 |
| 1678 | } |
| 1679 | else { |
| 1680 | if (q2 >= 0x8000000000000000ull) magu.a = 1; |
| 1681 | q2 = 2*q2; // update q2 |
| 1682 | r2 = 2*r2 + 1; // update r2 |
| 1683 | } |
| 1684 | delta = d - 1 - r2; |
Andrew Lenharth | 3e34849 | 2006-05-16 17:45:23 +0000 | [diff] [blame] | 1685 | } while (p < 128 && (q1 < delta || (q1 == delta && r1 == 0))); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 1686 | magu.m = q2 + 1; // resulting magic number |
| 1687 | magu.s = p - 64; // resulting shift |
| 1688 | return magu; |
| 1689 | } |
| 1690 | |
| 1691 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 1692 | /// return a DAG expression to select that will generate the same value by |
| 1693 | /// multiplying by a magic number. See: |
| 1694 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 1695 | SDOperand TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1696 | std::vector<SDNode*>* Created) const { |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 1697 | MVT::ValueType VT = N->getValueType(0); |
| 1698 | |
| 1699 | // Check to see if we can do this. |
| 1700 | if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64)) |
| 1701 | return SDOperand(); // BuildSDIV only operates on i32 or i64 |
| 1702 | if (!isOperationLegal(ISD::MULHS, VT)) |
| 1703 | return SDOperand(); // Make sure the target supports MULHS. |
| 1704 | |
| 1705 | int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended(); |
| 1706 | ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d); |
| 1707 | |
| 1708 | // Multiply the numerator (operand 0) by the magic value |
| 1709 | SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0), |
| 1710 | DAG.getConstant(magics.m, VT)); |
| 1711 | // If d > 0 and m < 0, add the numerator |
| 1712 | if (d > 0 && magics.m < 0) { |
| 1713 | Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0)); |
| 1714 | if (Created) |
| 1715 | Created->push_back(Q.Val); |
| 1716 | } |
| 1717 | // If d < 0 and m > 0, subtract the numerator. |
| 1718 | if (d < 0 && magics.m > 0) { |
| 1719 | Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0)); |
| 1720 | if (Created) |
| 1721 | Created->push_back(Q.Val); |
| 1722 | } |
| 1723 | // Shift right algebraic if shift value is nonzero |
| 1724 | if (magics.s > 0) { |
| 1725 | Q = DAG.getNode(ISD::SRA, VT, Q, |
| 1726 | DAG.getConstant(magics.s, getShiftAmountTy())); |
| 1727 | if (Created) |
| 1728 | Created->push_back(Q.Val); |
| 1729 | } |
| 1730 | // Extract the sign bit and add it to the quotient |
| 1731 | SDOperand T = |
| 1732 | DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1, |
| 1733 | getShiftAmountTy())); |
| 1734 | if (Created) |
| 1735 | Created->push_back(T.Val); |
| 1736 | return DAG.getNode(ISD::ADD, VT, Q, T); |
| 1737 | } |
| 1738 | |
| 1739 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 1740 | /// return a DAG expression to select that will generate the same value by |
| 1741 | /// multiplying by a magic number. See: |
| 1742 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 1743 | SDOperand TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1744 | std::vector<SDNode*>* Created) const { |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 1745 | MVT::ValueType VT = N->getValueType(0); |
| 1746 | |
| 1747 | // Check to see if we can do this. |
| 1748 | if (!isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64)) |
| 1749 | return SDOperand(); // BuildUDIV only operates on i32 or i64 |
| 1750 | if (!isOperationLegal(ISD::MULHU, VT)) |
| 1751 | return SDOperand(); // Make sure the target supports MULHU. |
| 1752 | |
| 1753 | uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 1754 | mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d); |
| 1755 | |
| 1756 | // Multiply the numerator (operand 0) by the magic value |
| 1757 | SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0), |
| 1758 | DAG.getConstant(magics.m, VT)); |
| 1759 | if (Created) |
| 1760 | Created->push_back(Q.Val); |
| 1761 | |
| 1762 | if (magics.a == 0) { |
| 1763 | return DAG.getNode(ISD::SRL, VT, Q, |
| 1764 | DAG.getConstant(magics.s, getShiftAmountTy())); |
| 1765 | } else { |
| 1766 | SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q); |
| 1767 | if (Created) |
| 1768 | Created->push_back(NPQ.Val); |
| 1769 | NPQ = DAG.getNode(ISD::SRL, VT, NPQ, |
| 1770 | DAG.getConstant(1, getShiftAmountTy())); |
| 1771 | if (Created) |
| 1772 | Created->push_back(NPQ.Val); |
| 1773 | NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q); |
| 1774 | if (Created) |
| 1775 | Created->push_back(NPQ.Val); |
| 1776 | return DAG.getNode(ISD::SRL, VT, NPQ, |
| 1777 | DAG.getConstant(magics.s-1, getShiftAmountTy())); |
| 1778 | } |
| 1779 | } |