Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +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 | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
| 15 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include <iostream> |
| 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 28 | /// hacks on it until the target machine can handle it. This involves |
| 29 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 30 | /// large sizes or splitting up large values into small values) as well as |
| 31 | /// eliminating operations the machine cannot handle. |
| 32 | /// |
| 33 | /// This code also does a small amount of optimization and recognition of idioms |
| 34 | /// as part of its processing. For example, if a target does not support a |
| 35 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 36 | /// will attempt merge setcc and brc instructions into brcc's. |
| 37 | /// |
| 38 | namespace { |
| 39 | class SelectionDAGLegalize { |
| 40 | TargetLowering &TLI; |
| 41 | SelectionDAG &DAG; |
| 42 | |
| 43 | /// LegalizeAction - This enum indicates what action we should take for each |
| 44 | /// value type the can occur in the program. |
| 45 | enum LegalizeAction { |
| 46 | Legal, // The target natively supports this value type. |
| 47 | Promote, // This should be promoted to the next larger type. |
| 48 | Expand, // This integer type should be broken into smaller pieces. |
| 49 | }; |
| 50 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 51 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 52 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 53 | /// This can be queried with "getTypeAction(VT)". |
| 54 | unsigned ValueTypeActions; |
| 55 | |
| 56 | /// NeedsAnotherIteration - This is set when we expand a large integer |
| 57 | /// operation into smaller integer operations, but the smaller operations are |
| 58 | /// not set. This occurs only rarely in practice, for targets that don't have |
| 59 | /// 32-bit or larger integer registers. |
| 60 | bool NeedsAnotherIteration; |
| 61 | |
| 62 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 63 | /// than one use, this map indicates what regularized operand to use. This |
| 64 | /// allows us to avoid legalizing the same thing more than once. |
| 65 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 66 | |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 67 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 68 | /// than one use, this map indicates what promoted value to use. This allows |
| 69 | /// us to avoid promoting the same thing more than once. |
| 70 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 71 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 72 | /// ExpandedNodes - For nodes that need to be expanded, and which have more |
| 73 | /// than one use, this map indicates which which operands are the expanded |
| 74 | /// version of the input. This allows us to avoid expanding the same node |
| 75 | /// more than once. |
| 76 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 77 | |
Chris Lattner | ea4ca94 | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 78 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
| 79 | bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second; |
| 80 | assert(isNew && "Got into the map somehow?"); |
| 81 | } |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 82 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 83 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 84 | assert(isNew && "Got into the map somehow?"); |
| 85 | } |
Chris Lattner | ea4ca94 | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 86 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 87 | public: |
| 88 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 89 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 90 | |
| 91 | /// Run - While there is still lowering to do, perform a pass over the DAG. |
| 92 | /// Most regularization can be done in a single pass, but targets that require |
| 93 | /// large values to be split into registers multiple times (e.g. i64 -> 4x |
| 94 | /// i16) require iteration for these values (the first iteration will demote |
| 95 | /// to i32, the second will demote to i16). |
| 96 | void Run() { |
| 97 | do { |
| 98 | NeedsAnotherIteration = false; |
| 99 | LegalizeDAG(); |
| 100 | } while (NeedsAnotherIteration); |
| 101 | } |
| 102 | |
| 103 | /// getTypeAction - Return how we should legalize values of this type, either |
| 104 | /// it is already legal or we need to expand it into multiple registers of |
| 105 | /// smaller integer type, or we need to promote it to a larger type. |
| 106 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
| 107 | return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); |
| 108 | } |
| 109 | |
| 110 | /// isTypeLegal - Return true if this type is legal on this target. |
| 111 | /// |
| 112 | bool isTypeLegal(MVT::ValueType VT) const { |
| 113 | return getTypeAction(VT) == Legal; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | void LegalizeDAG(); |
| 118 | |
| 119 | SDOperand LegalizeOp(SDOperand O); |
| 120 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 121 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 122 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 123 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, |
| 124 | SDOperand &Hi); |
| 125 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 126 | SDOperand Source); |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 127 | |
| 128 | SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT); |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 129 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 130 | bool isSigned); |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 131 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 132 | bool isSigned); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 134 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 135 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 136 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 137 | SDOperand &Lo, SDOperand &Hi); |
| 138 | void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 139 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 140 | |
Chris Lattner | a5bf103 | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 141 | void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain); |
| 142 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 143 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 144 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 145 | } |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 150 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 151 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 152 | ValueTypeActions(TLI.getValueTypeActions()) { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 153 | assert(MVT::LAST_VALUETYPE <= 16 && |
| 154 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 157 | /// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 158 | /// UINT_TO_FP operation of the specified operand when the target requests that |
| 159 | /// we expand it. At this point, we know that the result and operand types are |
| 160 | /// legal for the target. |
| 161 | SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0, |
| 162 | MVT::ValueType DestVT) { |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 163 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 164 | |
| 165 | SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 166 | Op0, |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 167 | DAG.getConstant(0, |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 168 | Op0.getValueType())); |
| 169 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 170 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 171 | SignSet, Four, Zero); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 172 | |
Chris Lattner | b35912e | 2005-07-18 04:31:14 +0000 | [diff] [blame] | 173 | // If the sign bit of the integer is set, the large number will be treated as |
| 174 | // a negative number. To counteract this, the dynamic code adds an offset |
| 175 | // depending on the data type. |
| 176 | uint64_t FF; |
| 177 | switch (Op0.getValueType()) { |
| 178 | default: assert(0 && "Unsupported integer type!"); |
| 179 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 180 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 181 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 182 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 183 | } |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 184 | if (TLI.isLittleEndian()) FF <<= 32; |
| 185 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 186 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 187 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); |
| 188 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor), |
| 189 | TLI.getPointerTy()); |
| 190 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 191 | SDOperand FudgeInReg; |
| 192 | if (DestVT == MVT::f32) |
| 193 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 194 | DAG.getSrcValue(NULL)); |
| 195 | else { |
| 196 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 197 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 198 | DAG.getEntryNode(), CPIdx, |
| 199 | DAG.getSrcValue(NULL), MVT::f32)); |
| 200 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 201 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 202 | NeedsAnotherIteration = true; |
| 203 | return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg); |
| 204 | } |
| 205 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 206 | /// PromoteLegalUINT_TO_FP - This function is responsible for legalizing a |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 207 | /// *INT_TO_FP operation of the specified operand when the target requests that |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 208 | /// we promote it. At this point, we know that the result and operand types are |
| 209 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 210 | /// operation that takes a larger input. |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 211 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 212 | MVT::ValueType DestVT, |
| 213 | bool isSigned) { |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 214 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 215 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 216 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 217 | unsigned OpToUse = 0; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 218 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 219 | // Scan for the appropriate larger type to use. |
| 220 | while (1) { |
| 221 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 222 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 223 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 224 | // If the target supports SINT_TO_FP of this type, use it. |
| 225 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 226 | default: break; |
| 227 | case TargetLowering::Legal: |
| 228 | if (!TLI.hasNativeSupportFor(NewInTy)) |
| 229 | break; // Can't use this datatype. |
| 230 | // FALL THROUGH. |
| 231 | case TargetLowering::Custom: |
| 232 | OpToUse = ISD::SINT_TO_FP; |
| 233 | break; |
| 234 | } |
| 235 | if (OpToUse) break; |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 236 | if (isSigned) continue; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 237 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 238 | // If the target supports UINT_TO_FP of this type, use it. |
| 239 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 240 | default: break; |
| 241 | case TargetLowering::Legal: |
| 242 | if (!TLI.hasNativeSupportFor(NewInTy)) |
| 243 | break; // Can't use this datatype. |
| 244 | // FALL THROUGH. |
| 245 | case TargetLowering::Custom: |
| 246 | OpToUse = ISD::UINT_TO_FP; |
| 247 | break; |
| 248 | } |
| 249 | if (OpToUse) break; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 250 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 251 | // Otherwise, try a larger type. |
| 252 | } |
| 253 | |
| 254 | // Make sure to legalize any nodes we create here in the next pass. |
| 255 | NeedsAnotherIteration = true; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 256 | |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 257 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 258 | // desired type then run the operation on it. |
| 259 | return DAG.getNode(OpToUse, DestVT, |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 260 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 261 | NewInTy, LegalOp)); |
Chris Lattner | e3e847b | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 264 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 265 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 266 | /// we promote it. At this point, we know that the result and operand types are |
| 267 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 268 | /// operation that returns a larger result. |
| 269 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 270 | MVT::ValueType DestVT, |
| 271 | bool isSigned) { |
| 272 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 273 | MVT::ValueType NewOutTy = DestVT; |
| 274 | |
| 275 | unsigned OpToUse = 0; |
| 276 | |
| 277 | // Scan for the appropriate larger type to use. |
| 278 | while (1) { |
| 279 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 280 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 281 | |
| 282 | // If the target supports FP_TO_SINT returning this type, use it. |
| 283 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 284 | default: break; |
| 285 | case TargetLowering::Legal: |
| 286 | if (!TLI.hasNativeSupportFor(NewOutTy)) |
| 287 | break; // Can't use this datatype. |
| 288 | // FALL THROUGH. |
| 289 | case TargetLowering::Custom: |
| 290 | OpToUse = ISD::FP_TO_SINT; |
| 291 | break; |
| 292 | } |
| 293 | if (OpToUse) break; |
| 294 | |
| 295 | // If the target supports FP_TO_UINT of this type, use it. |
| 296 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 297 | default: break; |
| 298 | case TargetLowering::Legal: |
| 299 | if (!TLI.hasNativeSupportFor(NewOutTy)) |
| 300 | break; // Can't use this datatype. |
| 301 | // FALL THROUGH. |
| 302 | case TargetLowering::Custom: |
| 303 | OpToUse = ISD::FP_TO_UINT; |
| 304 | break; |
| 305 | } |
| 306 | if (OpToUse) break; |
| 307 | |
| 308 | // Otherwise, try a larger type. |
| 309 | } |
| 310 | |
| 311 | // Make sure to legalize any nodes we create here in the next pass. |
| 312 | NeedsAnotherIteration = true; |
| 313 | |
| 314 | // Okay, we found the operation and type to use. Truncate the result of the |
| 315 | // extended FP_TO_*INT operation to the desired size. |
| 316 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 317 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 318 | } |
| 319 | |
| 320 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 321 | void SelectionDAGLegalize::LegalizeDAG() { |
| 322 | SDOperand OldRoot = DAG.getRoot(); |
| 323 | SDOperand NewRoot = LegalizeOp(OldRoot); |
| 324 | DAG.setRoot(NewRoot); |
| 325 | |
| 326 | ExpandedNodes.clear(); |
| 327 | LegalizedNodes.clear(); |
Chris Lattner | 87a769c | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 328 | PromotedNodes.clear(); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 329 | |
| 330 | // Remove dead nodes now. |
Chris Lattner | 473825c | 2005-01-07 21:09:37 +0000 | [diff] [blame] | 331 | DAG.RemoveDeadNodes(OldRoot.Val); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c0f31c5 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 335 | assert(getTypeAction(Op.getValueType()) == Legal && |
| 336 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 337 | SDNode *Node = Op.Val; |
Chris Lattner | c0f31c5 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 338 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 339 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | c0f31c5 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 340 | // register on this target, make sure to expand or promote them. |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 341 | if (Node->getNumValues() > 1) { |
| 342 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 343 | switch (getTypeAction(Node->getValueType(i))) { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 344 | case Legal: break; // Nothing to do. |
| 345 | case Expand: { |
| 346 | SDOperand T1, T2; |
| 347 | ExpandOp(Op.getValue(i), T1, T2); |
| 348 | assert(LegalizedNodes.count(Op) && |
| 349 | "Expansion didn't add legal operands!"); |
| 350 | return LegalizedNodes[Op]; |
| 351 | } |
| 352 | case Promote: |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 353 | PromoteOp(Op.getValue(i)); |
| 354 | assert(LegalizedNodes.count(Op) && |
| 355 | "Expansion didn't add legal operands!"); |
| 356 | return LegalizedNodes[Op]; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 360 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 361 | // means that we always must cache transformed nodes. |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 362 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 363 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 364 | |
Chris Lattner | ec26b48 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 365 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 366 | |
| 367 | SDOperand Result = Op; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 368 | |
| 369 | switch (Node->getOpcode()) { |
| 370 | default: |
Chris Lattner | 3eb8693 | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 371 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 372 | // If this is a target node, legalize it by legalizing the operands then |
| 373 | // passing it through. |
| 374 | std::vector<SDOperand> Ops; |
| 375 | bool Changed = false; |
| 376 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 377 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 378 | Changed = Changed || Node->getOperand(i) != Ops.back(); |
| 379 | } |
| 380 | if (Changed) |
| 381 | if (Node->getNumValues() == 1) |
| 382 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); |
| 383 | else { |
| 384 | std::vector<MVT::ValueType> VTs(Node->value_begin(), |
| 385 | Node->value_end()); |
| 386 | Result = DAG.getNode(Node->getOpcode(), VTs, Ops); |
| 387 | } |
| 388 | |
| 389 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 390 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 391 | return Result.getValue(Op.ResNo); |
| 392 | } |
| 393 | // Otherwise this is an unhandled builtin node. splat. |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 394 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 395 | assert(0 && "Do not know how to legalize this operator!"); |
| 396 | abort(); |
| 397 | case ISD::EntryToken: |
| 398 | case ISD::FrameIndex: |
| 399 | case ISD::GlobalAddress: |
Chris Lattner | 32f20bf | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 400 | case ISD::ExternalSymbol: |
Chris Lattner | 3b8e719 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 401 | case ISD::ConstantPool: // Nothing to do. |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 402 | assert(getTypeAction(Node->getValueType(0)) == Legal && |
| 403 | "This must be legal!"); |
| 404 | break; |
Chris Lattner | 3b8e719 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 405 | case ISD::CopyFromReg: |
| 406 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 407 | if (Tmp1 != Node->getOperand(0)) |
| 408 | Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), |
| 409 | Node->getValueType(0), Tmp1); |
Chris Lattner | eb6614d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 410 | else |
| 411 | Result = Op.getValue(0); |
| 412 | |
| 413 | // Since CopyFromReg produces two values, make sure to remember that we |
| 414 | // legalized both of them. |
| 415 | AddLegalizedOperand(Op.getValue(0), Result); |
| 416 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 417 | return Result.getValue(Op.ResNo); |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 418 | case ISD::ImplicitDef: |
| 419 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 420 | if (Tmp1 != Node->getOperand(0)) |
Chris Lattner | 39c6744 | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 421 | Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg()); |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 422 | break; |
Nate Begeman | cda9aa7 | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 423 | case ISD::UNDEF: { |
| 424 | MVT::ValueType VT = Op.getValueType(); |
| 425 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | 69d3943 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 426 | default: assert(0 && "This action is not supported yet!"); |
| 427 | case TargetLowering::Expand: |
| 428 | case TargetLowering::Promote: |
Nate Begeman | cda9aa7 | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 429 | if (MVT::isInteger(VT)) |
| 430 | Result = DAG.getConstant(0, VT); |
| 431 | else if (MVT::isFloatingPoint(VT)) |
| 432 | Result = DAG.getConstantFP(0, VT); |
| 433 | else |
| 434 | assert(0 && "Unknown value type!"); |
| 435 | break; |
Nate Begeman | 69d3943 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 436 | case TargetLowering::Legal: |
Nate Begeman | cda9aa7 | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | break; |
| 440 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 441 | case ISD::Constant: |
| 442 | // We know we don't need to expand constants here, constants only have one |
| 443 | // value and we check that it is fine above. |
| 444 | |
| 445 | // FIXME: Maybe we should handle things like targets that don't support full |
| 446 | // 32-bit immediates? |
| 447 | break; |
| 448 | case ISD::ConstantFP: { |
| 449 | // Spill FP immediates to the constant pool if the target cannot directly |
| 450 | // codegen them. Targets often have some immediate values that can be |
| 451 | // efficiently generated into an FP register without a load. We explicitly |
| 452 | // leave these constants as ConstantFP nodes for the target to deal with. |
| 453 | |
| 454 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 455 | |
| 456 | // Check to see if this FP immediate is already legal. |
| 457 | bool isLegal = false; |
| 458 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 459 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 460 | if (CFP->isExactlyValue(*I)) { |
| 461 | isLegal = true; |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | if (!isLegal) { |
| 466 | // Otherwise we need to spill the constant to memory. |
| 467 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); |
| 468 | |
| 469 | bool Extend = false; |
| 470 | |
| 471 | // If a FP immediate is precise when represented as a float, we put it |
| 472 | // into the constant pool as a float, even if it's is statically typed |
| 473 | // as a double. |
| 474 | MVT::ValueType VT = CFP->getValueType(0); |
| 475 | bool isDouble = VT == MVT::f64; |
| 476 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 477 | Type::FloatTy, CFP->getValue()); |
Chris Lattner | bc7497d | 2005-01-28 22:58:25 +0000 | [diff] [blame] | 478 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 479 | // Only do this if the target has a native EXTLOAD instruction from |
| 480 | // f32. |
| 481 | TLI.getOperationAction(ISD::EXTLOAD, |
| 482 | MVT::f32) == TargetLowering::Legal) { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 483 | LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); |
| 484 | VT = MVT::f32; |
| 485 | Extend = true; |
| 486 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 487 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 488 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), |
| 489 | TLI.getPointerTy()); |
Chris Lattner | 3ba56b3 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 490 | if (Extend) { |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 491 | Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 492 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | 3ba56b3 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 493 | } else { |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 494 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 495 | DAG.getSrcValue(NULL)); |
Chris Lattner | 3ba56b3 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 496 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 497 | } |
| 498 | break; |
| 499 | } |
Chris Lattner | 05b4e37 | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 500 | case ISD::TokenFactor: { |
| 501 | std::vector<SDOperand> Ops; |
| 502 | bool Changed = false; |
| 503 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
Chris Lattner | 55562fa | 2005-01-19 19:10:54 +0000 | [diff] [blame] | 504 | SDOperand Op = Node->getOperand(i); |
| 505 | // Fold single-use TokenFactor nodes into this token factor as we go. |
Chris Lattner | f09c0b4 | 2005-05-12 06:04:14 +0000 | [diff] [blame] | 506 | // FIXME: This is something that the DAGCombiner should do!! |
Chris Lattner | 55562fa | 2005-01-19 19:10:54 +0000 | [diff] [blame] | 507 | if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { |
| 508 | Changed = true; |
| 509 | for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) |
| 510 | Ops.push_back(LegalizeOp(Op.getOperand(j))); |
| 511 | } else { |
| 512 | Ops.push_back(LegalizeOp(Op)); // Legalize the operands |
| 513 | Changed |= Ops[i] != Op; |
| 514 | } |
Chris Lattner | 05b4e37 | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 515 | } |
| 516 | if (Changed) |
| 517 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 518 | break; |
| 519 | } |
| 520 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 521 | case ISD::CALLSEQ_START: |
| 522 | case ISD::CALLSEQ_END: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 523 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | d34cd28 | 2005-05-12 23:24:44 +0000 | [diff] [blame] | 524 | // Do not try to legalize the target-specific arguments (#1+) |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 525 | Tmp2 = Node->getOperand(0); |
| 526 | if (Tmp1 != Tmp2) { |
Chris Lattner | 8005e91 | 2005-05-12 00:17:04 +0000 | [diff] [blame] | 527 | Node->setAdjCallChain(Tmp1); |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 528 | |
| 529 | // If moving the operand from pointing to Tmp2 dropped its use count to 1, |
| 530 | // this will cause the maps used to memoize results to get confused. |
| 531 | // Create and add a dummy use, just to increase its use count. This will |
| 532 | // be removed at the end of legalize when dead nodes are removed. |
| 533 | if (Tmp2.Val->hasOneUse()) |
| 534 | DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2, |
| 535 | DAG.getConstant(0, MVT::i32)); |
| 536 | } |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 537 | // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These |
Chris Lattner | 8005e91 | 2005-05-12 00:17:04 +0000 | [diff] [blame] | 538 | // nodes are treated specially and are mutated in place. This makes the dag |
| 539 | // legalization process more efficient and also makes libcall insertion |
| 540 | // easier. |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 541 | break; |
Chris Lattner | ec26b48 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 542 | case ISD::DYNAMIC_STACKALLOC: |
| 543 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 544 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 545 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
| 546 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
Chris Lattner | 96c262e | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 547 | Tmp3 != Node->getOperand(2)) { |
| 548 | std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); |
| 549 | std::vector<SDOperand> Ops; |
| 550 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 551 | Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
| 552 | } else |
Chris Lattner | 02f5ce2 | 2005-01-09 19:07:54 +0000 | [diff] [blame] | 553 | Result = Op.getValue(0); |
Chris Lattner | ec26b48 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 554 | |
| 555 | // Since this op produces two values, make sure to remember that we |
| 556 | // legalized both of them. |
| 557 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 558 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 559 | return Result.getValue(Op.ResNo); |
| 560 | |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 561 | case ISD::TAILCALL: |
Chris Lattner | 3d95c14 | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 562 | case ISD::CALL: { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 563 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 564 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
Chris Lattner | 3d95c14 | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 565 | |
| 566 | bool Changed = false; |
| 567 | std::vector<SDOperand> Ops; |
| 568 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { |
| 569 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 570 | Changed |= Ops.back() != Node->getOperand(i); |
| 571 | } |
| 572 | |
| 573 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 574 | std::vector<MVT::ValueType> RetTyVTs; |
| 575 | RetTyVTs.reserve(Node->getNumValues()); |
| 576 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | f025d67 | 2005-01-07 21:34:13 +0000 | [diff] [blame] | 577 | RetTyVTs.push_back(Node->getValueType(i)); |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 578 | Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops, |
| 579 | Node->getOpcode() == ISD::TAILCALL), 0); |
Chris Lattner | 9242c50 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 580 | } else { |
| 581 | Result = Result.getValue(0); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | 9242c50 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 583 | // Since calls produce multiple values, make sure to remember that we |
| 584 | // legalized all of them. |
| 585 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 586 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 587 | return Result.getValue(Op.ResNo); |
Chris Lattner | 3d95c14 | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 588 | } |
Chris Lattner | 68a1214 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 589 | case ISD::BR: |
| 590 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 591 | if (Tmp1 != Node->getOperand(0)) |
| 592 | Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); |
| 593 | break; |
| 594 | |
Chris Lattner | ec3fe7c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 595 | case ISD::BRCOND: |
| 596 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 597 | |
| 598 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 599 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 600 | case Legal: |
| 601 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 602 | break; |
| 603 | case Promote: |
| 604 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 605 | break; |
| 606 | } |
Chris Lattner | ec3fe7c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 607 | // Basic block destination (Op#2) is always legal. |
| 608 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 609 | Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, |
| 610 | Node->getOperand(2)); |
| 611 | break; |
Chris Lattner | fd98678 | 2005-04-09 03:30:19 +0000 | [diff] [blame] | 612 | case ISD::BRCONDTWOWAY: |
| 613 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 614 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 615 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 616 | case Legal: |
| 617 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 618 | break; |
| 619 | case Promote: |
| 620 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 621 | break; |
| 622 | } |
| 623 | // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR |
| 624 | // pair. |
| 625 | switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) { |
| 626 | case TargetLowering::Promote: |
| 627 | default: assert(0 && "This action is not supported yet!"); |
| 628 | case TargetLowering::Legal: |
| 629 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { |
| 630 | std::vector<SDOperand> Ops; |
| 631 | Ops.push_back(Tmp1); |
| 632 | Ops.push_back(Tmp2); |
| 633 | Ops.push_back(Node->getOperand(2)); |
| 634 | Ops.push_back(Node->getOperand(3)); |
| 635 | Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops); |
| 636 | } |
| 637 | break; |
| 638 | case TargetLowering::Expand: |
| 639 | Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, |
| 640 | Node->getOperand(2)); |
| 641 | Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3)); |
| 642 | break; |
| 643 | } |
| 644 | break; |
Chris Lattner | ec3fe7c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 645 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 646 | case ISD::LOAD: |
| 647 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 648 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 4a73c2c | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 649 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 650 | if (Tmp1 != Node->getOperand(0) || |
| 651 | Tmp2 != Node->getOperand(1)) |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 652 | Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2, |
| 653 | Node->getOperand(2)); |
Chris Lattner | ea4ca94 | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 654 | else |
| 655 | Result = SDOperand(Node, 0); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 656 | |
Chris Lattner | ea4ca94 | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 657 | // Since loads produce two values, make sure to remember that we legalized |
| 658 | // both of them. |
| 659 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 660 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 661 | return Result.getValue(Op.ResNo); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 662 | |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 663 | case ISD::EXTLOAD: |
| 664 | case ISD::SEXTLOAD: |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 665 | case ISD::ZEXTLOAD: { |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 666 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 667 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 668 | |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 669 | MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 670 | switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 671 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 0b73a6d | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 672 | case TargetLowering::Promote: |
| 673 | assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 674 | Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0), |
| 675 | Tmp1, Tmp2, Node->getOperand(2), MVT::i8); |
Chris Lattner | 0b73a6d | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 676 | // Since loads produce two values, make sure to remember that we legalized |
| 677 | // both of them. |
| 678 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 679 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 680 | return Result.getValue(Op.ResNo); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 681 | |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 682 | case TargetLowering::Legal: |
| 683 | if (Tmp1 != Node->getOperand(0) || |
| 684 | Tmp2 != Node->getOperand(1)) |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 685 | Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0), |
| 686 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 687 | else |
| 688 | Result = SDOperand(Node, 0); |
| 689 | |
| 690 | // Since loads produce two values, make sure to remember that we legalized |
| 691 | // both of them. |
| 692 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 693 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 694 | return Result.getValue(Op.ResNo); |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 695 | case TargetLowering::Expand: |
Andrew Lenharth | b5597e3 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 696 | //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 697 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 698 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); |
Andrew Lenharth | 0a370f4 | 2005-06-30 19:32:57 +0000 | [diff] [blame] | 699 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
Andrew Lenharth | b5597e3 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 700 | if (Op.ResNo) |
| 701 | return Load.getValue(1); |
| 702 | return Result; |
| 703 | } |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 704 | assert(Node->getOpcode() != ISD::EXTLOAD && |
| 705 | "EXTLOAD should always be supported!"); |
| 706 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 707 | // zero/sign extend inreg. |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 708 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 709 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 710 | SDOperand ValRes; |
| 711 | if (Node->getOpcode() == ISD::SEXTLOAD) |
| 712 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 713 | Result, DAG.getValueType(SrcVT)); |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 714 | else |
| 715 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
Chris Lattner | a3b7ef0 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 716 | AddLegalizedOperand(SDOperand(Node, 0), ValRes); |
| 717 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 718 | if (Op.ResNo) |
| 719 | return Result.getValue(1); |
| 720 | return ValRes; |
| 721 | } |
| 722 | assert(0 && "Unreachable"); |
| 723 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 724 | case ISD::EXTRACT_ELEMENT: |
| 725 | // Get both the low and high parts. |
| 726 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 727 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 728 | Result = Tmp2; // 1 -> Hi |
| 729 | else |
| 730 | Result = Tmp1; // 0 -> Lo |
| 731 | break; |
| 732 | |
| 733 | case ISD::CopyToReg: |
| 734 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 735 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 736 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 737 | case Legal: |
| 738 | // Legalize the incoming value (must be legal). |
| 739 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 740 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 741 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 742 | break; |
Chris Lattner | 9f2c4a5 | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 743 | case Promote: |
| 744 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 745 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
| 746 | break; |
| 747 | case Expand: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 748 | SDOperand Lo, Hi; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 749 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 750 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | 0d03eb4 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 751 | Lo = DAG.getCopyToReg(Tmp1, Lo, Reg); |
| 752 | Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1); |
| 753 | // Note that the copytoreg nodes are independent of each other. |
| 754 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 755 | assert(isTypeLegal(Result.getValueType()) && |
| 756 | "Cannot expand multiple times yet (i64 -> i16)"); |
| 757 | break; |
| 758 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | case ISD::RET: |
| 762 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 763 | switch (Node->getNumOperands()) { |
| 764 | case 2: // ret val |
| 765 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 766 | case Legal: |
| 767 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | ea4ca94 | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 768 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 769 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 770 | break; |
| 771 | case Expand: { |
| 772 | SDOperand Lo, Hi; |
| 773 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 774 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 775 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 776 | } |
| 777 | case Promote: |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 778 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 779 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 780 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 781 | } |
| 782 | break; |
| 783 | case 1: // ret void |
| 784 | if (Tmp1 != Node->getOperand(0)) |
| 785 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); |
| 786 | break; |
| 787 | default: { // ret <values> |
| 788 | std::vector<SDOperand> NewValues; |
| 789 | NewValues.push_back(Tmp1); |
| 790 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 791 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 792 | case Legal: |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 793 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 794 | break; |
| 795 | case Expand: { |
| 796 | SDOperand Lo, Hi; |
| 797 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 798 | NewValues.push_back(Lo); |
| 799 | NewValues.push_back(Hi); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 800 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 801 | } |
| 802 | case Promote: |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 803 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 804 | } |
| 805 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | break; |
| 810 | case ISD::STORE: |
| 811 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 812 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 813 | |
Chris Lattner | e69daaf | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 814 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 815 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | e69daaf | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 816 | if (CFP->getValueType(0) == MVT::f32) { |
| 817 | union { |
| 818 | unsigned I; |
| 819 | float F; |
| 820 | } V; |
| 821 | V.F = CFP->getValue(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 822 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
Chris Lattner | ba45e6c | 2005-05-09 20:36:57 +0000 | [diff] [blame] | 823 | DAG.getConstant(V.I, MVT::i32), Tmp2, |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 824 | Node->getOperand(3)); |
Chris Lattner | e69daaf | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 825 | } else { |
| 826 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 827 | union { |
| 828 | uint64_t I; |
| 829 | double F; |
| 830 | } V; |
| 831 | V.F = CFP->getValue(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 832 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 833 | DAG.getConstant(V.I, MVT::i64), Tmp2, |
| 834 | Node->getOperand(3)); |
Chris Lattner | e69daaf | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | a474313 | 2005-02-22 07:23:39 +0000 | [diff] [blame] | 836 | Node = Result.Val; |
Chris Lattner | e69daaf | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 839 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 840 | case Legal: { |
| 841 | SDOperand Val = LegalizeOp(Node->getOperand(1)); |
| 842 | if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || |
| 843 | Tmp2 != Node->getOperand(2)) |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 844 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2, |
| 845 | Node->getOperand(3)); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | case Promote: |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 849 | // Truncate the value and store the result. |
| 850 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 851 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
Andrew Lenharth | 4a73c2c | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 852 | Node->getOperand(3), |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 853 | DAG.getValueType(Node->getOperand(1).getValueType())); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 854 | break; |
| 855 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 856 | case Expand: |
| 857 | SDOperand Lo, Hi; |
| 858 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 859 | |
| 860 | if (!TLI.isLittleEndian()) |
| 861 | std::swap(Lo, Hi); |
| 862 | |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 863 | Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, |
| 864 | Node->getOperand(3)); |
Chris Lattner | 0d03eb4 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 865 | unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 866 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 867 | getIntPtrConstant(IncrementSize)); |
| 868 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 869 | "Pointers must be legal!"); |
Andrew Lenharth | 4a73c2c | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 870 | //Again, claiming both parts of the store came form the same Instr |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 871 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, |
| 872 | Node->getOperand(3)); |
Chris Lattner | 0d03eb4 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 873 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 874 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 875 | } |
| 876 | break; |
Andrew Lenharth | dec5392 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 877 | case ISD::PCMARKER: |
| 878 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 879 | if (Tmp1 != Node->getOperand(0)) |
| 880 | Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1)); |
Andrew Lenharth | dec5392 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 881 | break; |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 882 | case ISD::TRUNCSTORE: |
| 883 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 884 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 885 | |
| 886 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 887 | case Legal: |
| 888 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 889 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 890 | Tmp3 != Node->getOperand(2)) |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 891 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 892 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 893 | break; |
| 894 | case Promote: |
| 895 | case Expand: |
| 896 | assert(0 && "Cannot handle illegal TRUNCSTORE yet!"); |
| 897 | } |
| 898 | break; |
Chris Lattner | 39c6744 | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 899 | case ISD::SELECT: |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 900 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 901 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 902 | case Legal: |
| 903 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 904 | break; |
| 905 | case Promote: |
| 906 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 907 | break; |
| 908 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 909 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 39c6744 | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 910 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 911 | |
| 912 | switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) { |
| 913 | default: assert(0 && "This action is not supported yet!"); |
| 914 | case TargetLowering::Legal: |
| 915 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 916 | Tmp3 != Node->getOperand(2)) |
| 917 | Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), |
| 918 | Tmp1, Tmp2, Tmp3); |
| 919 | break; |
| 920 | case TargetLowering::Promote: { |
| 921 | MVT::ValueType NVT = |
| 922 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 923 | unsigned ExtOp, TruncOp; |
| 924 | if (MVT::isInteger(Tmp2.getValueType())) { |
| 925 | ExtOp = ISD::ZERO_EXTEND; |
| 926 | TruncOp = ISD::TRUNCATE; |
| 927 | } else { |
| 928 | ExtOp = ISD::FP_EXTEND; |
| 929 | TruncOp = ISD::FP_ROUND; |
| 930 | } |
| 931 | // Promote each of the values to the new type. |
| 932 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 933 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 934 | // Perform the larger operation, then round down. |
| 935 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 936 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 937 | break; |
| 938 | } |
| 939 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 940 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 941 | case ISD::SETCC: |
| 942 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 943 | case Legal: |
| 944 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 945 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 946 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 947 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | b07e2d2 | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 948 | Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 949 | break; |
| 950 | case Promote: |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 951 | Tmp1 = PromoteOp(Node->getOperand(0)); // LHS |
| 952 | Tmp2 = PromoteOp(Node->getOperand(1)); // RHS |
| 953 | |
| 954 | // If this is an FP compare, the operands have already been extended. |
| 955 | if (MVT::isInteger(Node->getOperand(0).getValueType())) { |
| 956 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
Chris Lattner | 87a769c | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 957 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 958 | |
| 959 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 960 | // that we could insert sign extends for ALL conditions, but zero extend |
| 961 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 962 | // it. |
| 963 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 964 | default: assert(0 && "Unknown integer comparison!"); |
| 965 | case ISD::SETEQ: |
| 966 | case ISD::SETNE: |
| 967 | case ISD::SETUGE: |
| 968 | case ISD::SETUGT: |
| 969 | case ISD::SETULE: |
| 970 | case ISD::SETULT: |
| 971 | // ALL of these operations will work if we either sign or zero extend |
| 972 | // the operands (including the unsigned comparisons!). Zero extend is |
| 973 | // usually a simpler/cheaper operation, so prefer it. |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 974 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 975 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 976 | break; |
| 977 | case ISD::SETGE: |
| 978 | case ISD::SETGT: |
| 979 | case ISD::SETLT: |
| 980 | case ISD::SETLE: |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 981 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 982 | DAG.getValueType(VT)); |
| 983 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 984 | DAG.getValueType(VT)); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
| 988 | } |
| 989 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | b07e2d2 | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 990 | Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 991 | break; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 992 | case Expand: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 993 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 994 | ExpandOp(Node->getOperand(0), LHSLo, LHSHi); |
| 995 | ExpandOp(Node->getOperand(1), RHSLo, RHSHi); |
| 996 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 997 | case ISD::SETEQ: |
| 998 | case ISD::SETNE: |
Chris Lattner | 71ff44e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 999 | if (RHSLo == RHSHi) |
| 1000 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 1001 | if (RHSCST->isAllOnesValue()) { |
| 1002 | // Comparison to -1. |
| 1003 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1004 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | 71ff44e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 1005 | Node->getValueType(0), Tmp1, RHSLo); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1006 | break; |
Chris Lattner | 71ff44e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1009 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 1010 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 1011 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1012 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | b07e2d2 | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1013 | Node->getValueType(0), Tmp1, |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1014 | DAG.getConstant(0, Tmp1.getValueType())); |
| 1015 | break; |
| 1016 | default: |
Chris Lattner | aedcabe | 2005-04-12 02:19:10 +0000 | [diff] [blame] | 1017 | // If this is a comparison of the sign bit, just look at the top part. |
| 1018 | // X > -1, x < 0 |
| 1019 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1))) |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1020 | if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT && |
Chris Lattner | aedcabe | 2005-04-12 02:19:10 +0000 | [diff] [blame] | 1021 | CST->getValue() == 0) || // X < 0 |
| 1022 | (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT && |
| 1023 | (CST->isAllOnesValue()))) // X > -1 |
| 1024 | return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 1025 | Node->getValueType(0), LHSHi, RHSHi); |
| 1026 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1027 | // FIXME: This generated code sucks. |
| 1028 | ISD::CondCode LowCC; |
| 1029 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 1030 | default: assert(0 && "Unknown integer setcc!"); |
| 1031 | case ISD::SETLT: |
| 1032 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 1033 | case ISD::SETGT: |
| 1034 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 1035 | case ISD::SETLE: |
| 1036 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 1037 | case ISD::SETGE: |
| 1038 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 1039 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1041 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 1042 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 1043 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 1044 | |
| 1045 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 1046 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Chris Lattner | b07e2d2 | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1047 | Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1048 | Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | b07e2d2 | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1049 | Node->getValueType(0), LHSHi, RHSHi); |
| 1050 | Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi); |
| 1051 | Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 1052 | Result, Tmp1, Tmp2); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | break; |
| 1057 | |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1058 | case ISD::MEMSET: |
| 1059 | case ISD::MEMCPY: |
| 1060 | case ISD::MEMMOVE: { |
Chris Lattner | 4487b2e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1061 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1062 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 1063 | |
| 1064 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 1065 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 1066 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 1067 | case Legal: |
Chris Lattner | 4487b2e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1068 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | case Promote: |
Chris Lattner | 4487b2e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1071 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | } else { |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1075 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1076 | } |
Chris Lattner | 5aa75e4 | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1077 | |
| 1078 | SDOperand Tmp4; |
| 1079 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | ba08a33 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 1080 | case Expand: { |
| 1081 | // Length is too big, just take the lo-part of the length. |
| 1082 | SDOperand HiPart; |
| 1083 | ExpandOp(Node->getOperand(3), HiPart, Tmp4); |
| 1084 | break; |
| 1085 | } |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1086 | case Legal: |
| 1087 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | case Promote: |
| 1090 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 5aa75e4 | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | |
| 1094 | SDOperand Tmp5; |
| 1095 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 1096 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 1097 | case Legal: |
| 1098 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 1099 | break; |
| 1100 | case Promote: |
Chris Lattner | a4cfafe | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1101 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 1102 | break; |
| 1103 | } |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1104 | |
| 1105 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 1106 | default: assert(0 && "This action not implemented for this operation!"); |
| 1107 | case TargetLowering::Legal: |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1108 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1109 | Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || |
| 1110 | Tmp5 != Node->getOperand(4)) { |
| 1111 | std::vector<SDOperand> Ops; |
| 1112 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 1113 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 1114 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
| 1115 | } |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | case TargetLowering::Expand: { |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1118 | // Otherwise, the target does not support this operation. Lower the |
| 1119 | // operation to an explicit libcall as appropriate. |
| 1120 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1121 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 1122 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1123 | |
Reid Spencer | 6dced92 | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 1124 | const char *FnName = 0; |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1125 | if (Node->getOpcode() == ISD::MEMSET) { |
| 1126 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 1127 | // Extend the ubyte argument to be an int value for the call. |
| 1128 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
| 1129 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 1130 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1131 | |
| 1132 | FnName = "memset"; |
| 1133 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 1134 | Node->getOpcode() == ISD::MEMMOVE) { |
| 1135 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 1136 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 1137 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1138 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 1139 | } else { |
| 1140 | assert(0 && "Unknown op!"); |
| 1141 | } |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1143 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1144 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1145 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | f9ddfef | 2005-07-13 02:00:04 +0000 | [diff] [blame] | 1146 | Result = CallResult.second; |
| 1147 | NeedsAnotherIteration = true; |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | case TargetLowering::Custom: |
| 1151 | std::vector<SDOperand> Ops; |
| 1152 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 1153 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 1154 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
Chris Lattner | 29dcc71 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 1155 | Result = TLI.LowerOperation(Result, DAG); |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1156 | Result = LegalizeOp(Result); |
| 1157 | break; |
Chris Lattner | 85d70c6 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1158 | } |
| 1159 | break; |
| 1160 | } |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1161 | |
| 1162 | case ISD::READPORT: |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1163 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1164 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | ba45e6c | 2005-05-09 20:36:57 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | 8653599 | 2005-05-14 07:45:46 +0000 | [diff] [blame] | 1166 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { |
| 1167 | std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); |
| 1168 | std::vector<SDOperand> Ops; |
| 1169 | Ops.push_back(Tmp1); |
| 1170 | Ops.push_back(Tmp2); |
| 1171 | Result = DAG.getNode(ISD::READPORT, VTs, Ops); |
| 1172 | } else |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1173 | Result = SDOperand(Node, 0); |
| 1174 | // Since these produce two values, make sure to remember that we legalized |
| 1175 | // both of them. |
| 1176 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 1177 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1178 | return Result.getValue(Op.ResNo); |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1179 | case ISD::WRITEPORT: |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1180 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1181 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1182 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1183 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1184 | Tmp3 != Node->getOperand(2)) |
| 1185 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3); |
| 1186 | break; |
| 1187 | |
Chris Lattner | ba45e6c | 2005-05-09 20:36:57 +0000 | [diff] [blame] | 1188 | case ISD::READIO: |
| 1189 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1190 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1191 | |
| 1192 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 1193 | case TargetLowering::Custom: |
| 1194 | default: assert(0 && "This action not implemented for this operation!"); |
| 1195 | case TargetLowering::Legal: |
Chris Lattner | 8653599 | 2005-05-14 07:45:46 +0000 | [diff] [blame] | 1196 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { |
| 1197 | std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); |
| 1198 | std::vector<SDOperand> Ops; |
| 1199 | Ops.push_back(Tmp1); |
| 1200 | Ops.push_back(Tmp2); |
| 1201 | Result = DAG.getNode(ISD::READPORT, VTs, Ops); |
| 1202 | } else |
Chris Lattner | ba45e6c | 2005-05-09 20:36:57 +0000 | [diff] [blame] | 1203 | Result = SDOperand(Node, 0); |
| 1204 | break; |
| 1205 | case TargetLowering::Expand: |
| 1206 | // Replace this with a load from memory. |
| 1207 | Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0), |
| 1208 | Node->getOperand(1), DAG.getSrcValue(NULL)); |
| 1209 | Result = LegalizeOp(Result); |
| 1210 | break; |
| 1211 | } |
| 1212 | |
| 1213 | // Since these produce two values, make sure to remember that we legalized |
| 1214 | // both of them. |
| 1215 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 1216 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1217 | return Result.getValue(Op.ResNo); |
| 1218 | |
| 1219 | case ISD::WRITEIO: |
| 1220 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1221 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1222 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1223 | |
| 1224 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1225 | Node->getOperand(1).getValueType())) { |
| 1226 | case TargetLowering::Custom: |
| 1227 | default: assert(0 && "This action not implemented for this operation!"); |
| 1228 | case TargetLowering::Legal: |
| 1229 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1230 | Tmp3 != Node->getOperand(2)) |
| 1231 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3); |
| 1232 | break; |
| 1233 | case TargetLowering::Expand: |
| 1234 | // Replace this with a store to memory. |
| 1235 | Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0), |
| 1236 | Node->getOperand(1), Node->getOperand(2), |
| 1237 | DAG.getSrcValue(NULL)); |
| 1238 | Result = LegalizeOp(Result); |
| 1239 | break; |
| 1240 | } |
| 1241 | break; |
| 1242 | |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1243 | case ISD::ADD_PARTS: |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1244 | case ISD::SUB_PARTS: |
| 1245 | case ISD::SHL_PARTS: |
| 1246 | case ISD::SRA_PARTS: |
| 1247 | case ISD::SRL_PARTS: { |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1248 | std::vector<SDOperand> Ops; |
| 1249 | bool Changed = false; |
| 1250 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1251 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1252 | Changed |= Ops.back() != Node->getOperand(i); |
| 1253 | } |
Chris Lattner | 669e8c2 | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1254 | if (Changed) { |
| 1255 | std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); |
| 1256 | Result = DAG.getNode(Node->getOpcode(), VTs, Ops); |
| 1257 | } |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Since these produce multiple values, make sure to remember that we |
| 1260 | // legalized all of them. |
| 1261 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 1262 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 1263 | return Result.getValue(Op.ResNo); |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1264 | } |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Binary operators |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1267 | case ISD::ADD: |
| 1268 | case ISD::SUB: |
| 1269 | case ISD::MUL: |
Nate Begeman | add0c63 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 1270 | case ISD::MULHS: |
| 1271 | case ISD::MULHU: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1272 | case ISD::UDIV: |
| 1273 | case ISD::SDIV: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1274 | case ISD::AND: |
| 1275 | case ISD::OR: |
| 1276 | case ISD::XOR: |
Chris Lattner | 32f20bf | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 1277 | case ISD::SHL: |
| 1278 | case ISD::SRL: |
| 1279 | case ISD::SRA: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1280 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | 80fe411 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 1281 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1282 | case Expand: assert(0 && "Not possible"); |
| 1283 | case Legal: |
| 1284 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 1285 | break; |
| 1286 | case Promote: |
| 1287 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 1288 | break; |
| 1289 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1290 | if (Tmp1 != Node->getOperand(0) || |
| 1291 | Tmp2 != Node->getOperand(1)) |
| 1292 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); |
| 1293 | break; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1294 | |
Nate Begeman | 20b7d2a | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1295 | case ISD::UREM: |
| 1296 | case ISD::SREM: |
| 1297 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 1298 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 1299 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 1300 | case TargetLowering::Legal: |
| 1301 | if (Tmp1 != Node->getOperand(0) || |
| 1302 | Tmp2 != Node->getOperand(1)) |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1303 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
Nate Begeman | 20b7d2a | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1304 | Tmp2); |
| 1305 | break; |
| 1306 | case TargetLowering::Promote: |
| 1307 | case TargetLowering::Custom: |
| 1308 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 1309 | case TargetLowering::Expand: { |
| 1310 | MVT::ValueType VT = Node->getValueType(0); |
| 1311 | unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
| 1312 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 1313 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 1314 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 1315 | } |
| 1316 | break; |
| 1317 | } |
| 1318 | break; |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1319 | |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1320 | case ISD::CTPOP: |
| 1321 | case ISD::CTTZ: |
| 1322 | case ISD::CTLZ: |
| 1323 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 1324 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 1325 | case TargetLowering::Legal: |
| 1326 | if (Tmp1 != Node->getOperand(0)) |
| 1327 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1328 | break; |
| 1329 | case TargetLowering::Promote: { |
| 1330 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 1331 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Zero extend the argument. |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1334 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 1335 | // Perform the larger operation, then subtract if needed. |
| 1336 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1337 | switch(Node->getOpcode()) |
| 1338 | { |
| 1339 | case ISD::CTPOP: |
| 1340 | Result = Tmp1; |
| 1341 | break; |
| 1342 | case ISD::CTTZ: |
| 1343 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1344 | Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1, |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1345 | DAG.getConstant(getSizeInBits(NVT), NVT)); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1346 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1347 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 1348 | break; |
| 1349 | case ISD::CTLZ: |
| 1350 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1351 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 1352 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1353 | getSizeInBits(OVT), NVT)); |
| 1354 | break; |
| 1355 | } |
| 1356 | break; |
| 1357 | } |
| 1358 | case TargetLowering::Custom: |
| 1359 | assert(0 && "Cannot custom handle this yet!"); |
| 1360 | case TargetLowering::Expand: |
Andrew Lenharth | 2dbbb3a | 2005-05-05 15:55:21 +0000 | [diff] [blame] | 1361 | switch(Node->getOpcode()) |
| 1362 | { |
| 1363 | case ISD::CTPOP: { |
Chris Lattner | 05309bf5 | 2005-05-11 05:21:31 +0000 | [diff] [blame] | 1364 | static const uint64_t mask[6] = { |
| 1365 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 1366 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 1367 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 1368 | }; |
Andrew Lenharth | 2dbbb3a | 2005-05-05 15:55:21 +0000 | [diff] [blame] | 1369 | MVT::ValueType VT = Tmp1.getValueType(); |
Chris Lattner | 05309bf5 | 2005-05-11 05:21:31 +0000 | [diff] [blame] | 1370 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 1371 | unsigned len = getSizeInBits(VT); |
| 1372 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
Andrew Lenharth | 2dbbb3a | 2005-05-05 15:55:21 +0000 | [diff] [blame] | 1373 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
Chris Lattner | 05309bf5 | 2005-05-11 05:21:31 +0000 | [diff] [blame] | 1374 | Tmp2 = DAG.getConstant(mask[i], VT); |
| 1375 | Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1376 | Tmp1 = DAG.getNode(ISD::ADD, VT, |
Andrew Lenharth | 2dbbb3a | 2005-05-05 15:55:21 +0000 | [diff] [blame] | 1377 | DAG.getNode(ISD::AND, VT, Tmp1, Tmp2), |
| 1378 | DAG.getNode(ISD::AND, VT, |
| 1379 | DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3), |
| 1380 | Tmp2)); |
| 1381 | } |
| 1382 | Result = Tmp1; |
| 1383 | break; |
| 1384 | } |
Duraid Madina | a1ebbac | 2005-05-11 08:45:08 +0000 | [diff] [blame] | 1385 | case ISD::CTLZ: { |
| 1386 | /* for now, we do this: |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1387 | x = x | (x >> 1); |
| 1388 | x = x | (x >> 2); |
| 1389 | ... |
| 1390 | x = x | (x >>16); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1391 | x = x | (x >>32); // for 64-bit input |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1392 | return popcount(~x); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1394 | but see also: http://www.hackersdelight.org/HDcode/nlz.cc */ |
| 1395 | MVT::ValueType VT = Tmp1.getValueType(); |
Duraid Madina | a1ebbac | 2005-05-11 08:45:08 +0000 | [diff] [blame] | 1396 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 1397 | unsigned len = getSizeInBits(VT); |
| 1398 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 1399 | Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1400 | Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1, |
Duraid Madina | a1ebbac | 2005-05-11 08:45:08 +0000 | [diff] [blame] | 1401 | DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3)); |
| 1402 | } |
| 1403 | Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT)); |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1404 | Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3)); |
Chris Lattner | 7247324 | 2005-05-11 05:27:09 +0000 | [diff] [blame] | 1405 | break; |
Duraid Madina | a1ebbac | 2005-05-11 08:45:08 +0000 | [diff] [blame] | 1406 | } |
| 1407 | case ISD::CTTZ: { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1408 | // for now, we use: { return popcount(~x & (x - 1)); } |
Nate Begeman | 99fa5bc | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1409 | // unless the target has ctlz but not ctpop, in which case we use: |
| 1410 | // { return 32 - nlz(~x & (x-1)); } |
| 1411 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1412 | MVT::ValueType VT = Tmp1.getValueType(); |
| 1413 | Tmp2 = DAG.getConstant(~0ULL, VT); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1414 | Tmp3 = DAG.getNode(ISD::AND, VT, |
Chris Lattner | 56add05 | 2005-05-11 18:35:21 +0000 | [diff] [blame] | 1415 | DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2), |
| 1416 | DAG.getNode(ISD::SUB, VT, Tmp1, |
| 1417 | DAG.getConstant(1, VT))); |
Nate Begeman | 99fa5bc | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1418 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead |
| 1419 | if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal && |
| 1420 | TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1421 | Result = LegalizeOp(DAG.getNode(ISD::SUB, VT, |
Nate Begeman | 99fa5bc | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1422 | DAG.getConstant(getSizeInBits(VT), VT), |
| 1423 | DAG.getNode(ISD::CTLZ, VT, Tmp3))); |
| 1424 | } else { |
| 1425 | Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3)); |
| 1426 | } |
Chris Lattner | 7247324 | 2005-05-11 05:27:09 +0000 | [diff] [blame] | 1427 | break; |
Duraid Madina | a1ebbac | 2005-05-11 08:45:08 +0000 | [diff] [blame] | 1428 | } |
Andrew Lenharth | 2dbbb3a | 2005-05-05 15:55:21 +0000 | [diff] [blame] | 1429 | default: |
| 1430 | assert(0 && "Cannot expand this yet!"); |
| 1431 | break; |
| 1432 | } |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 1433 | break; |
| 1434 | } |
| 1435 | break; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1436 | |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1437 | // Unary operators |
| 1438 | case ISD::FABS: |
| 1439 | case ISD::FNEG: |
Chris Lattner | 9d6fa98 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 1440 | case ISD::FSQRT: |
| 1441 | case ISD::FSIN: |
| 1442 | case ISD::FCOS: |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1443 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1444 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 1445 | case TargetLowering::Legal: |
| 1446 | if (Tmp1 != Node->getOperand(0)) |
| 1447 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1448 | break; |
| 1449 | case TargetLowering::Promote: |
| 1450 | case TargetLowering::Custom: |
| 1451 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 1452 | case TargetLowering::Expand: |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1453 | switch(Node->getOpcode()) { |
| 1454 | case ISD::FNEG: { |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1455 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 1456 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
| 1457 | Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0), |
| 1458 | Tmp2, Tmp1)); |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | } |
| 1461 | case ISD::FABS: { |
Chris Lattner | a0c72cf | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 1462 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 1463 | MVT::ValueType VT = Node->getValueType(0); |
| 1464 | Tmp2 = DAG.getConstantFP(0.0, VT); |
| 1465 | Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2); |
| 1466 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 1467 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
| 1468 | Result = LegalizeOp(Result); |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1469 | break; |
| 1470 | } |
| 1471 | case ISD::FSQRT: |
| 1472 | case ISD::FSIN: |
| 1473 | case ISD::FCOS: { |
| 1474 | MVT::ValueType VT = Node->getValueType(0); |
| 1475 | Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy; |
| 1476 | const char *FnName = 0; |
| 1477 | switch(Node->getOpcode()) { |
| 1478 | case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; |
| 1479 | case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; |
| 1480 | case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; |
| 1481 | default: assert(0 && "Unreachable!"); |
| 1482 | } |
| 1483 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1484 | Args.push_back(std::make_pair(Tmp1, T)); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 1485 | // FIXME: should use ExpandLibCall! |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1486 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1487 | TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true, |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1488 | DAG.getExternalSymbol(FnName, VT), Args, DAG); |
| 1489 | Result = LegalizeOp(CallResult.first); |
| 1490 | break; |
| 1491 | } |
| 1492 | default: |
Chris Lattner | a0c72cf | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 1493 | assert(0 && "Unreachable!"); |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | break; |
| 1498 | |
| 1499 | // Conversion operators. The source and destination have different types. |
Chris Lattner | 2a6db3c | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 1500 | case ISD::SINT_TO_FP: |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1501 | case ISD::UINT_TO_FP: { |
| 1502 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1503 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1504 | case Legal: |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1505 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1506 | Node->getOperand(0).getValueType())) { |
| 1507 | default: assert(0 && "Unknown operation action!"); |
| 1508 | case TargetLowering::Expand: |
| 1509 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP yet"); |
| 1510 | Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 1511 | Node->getValueType(0)); |
| 1512 | AddLegalizedOperand(Op, Result); |
| 1513 | return Result; |
| 1514 | case TargetLowering::Promote: |
| 1515 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 1516 | Node->getValueType(0), |
| 1517 | isSigned); |
| 1518 | AddLegalizedOperand(Op, Result); |
| 1519 | return Result; |
| 1520 | case TargetLowering::Legal: |
| 1521 | break; |
Andrew Lenharth | d74877a | 2005-06-27 23:28:32 +0000 | [diff] [blame] | 1522 | } |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1524 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1525 | if (Tmp1 != Node->getOperand(0)) |
| 1526 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1527 | break; |
Chris Lattner | a65a2f0 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 1528 | case Expand: |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1529 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 1530 | Node->getValueType(0), Node->getOperand(0)); |
| 1531 | break; |
| 1532 | case Promote: |
| 1533 | if (isSigned) { |
| 1534 | Result = PromoteOp(Node->getOperand(0)); |
| 1535 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1536 | Result, DAG.getValueType(Node->getOperand(0).getValueType())); |
| 1537 | Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); |
| 1538 | } else { |
| 1539 | Result = PromoteOp(Node->getOperand(0)); |
| 1540 | Result = DAG.getZeroExtendInReg(Result, |
| 1541 | Node->getOperand(0).getValueType()); |
| 1542 | Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1543 | } |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | } |
| 1546 | break; |
| 1547 | } |
| 1548 | case ISD::TRUNCATE: |
| 1549 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1550 | case Legal: |
| 1551 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1552 | if (Tmp1 != Node->getOperand(0)) |
| 1553 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1554 | break; |
| 1555 | case Expand: |
| 1556 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1557 | |
| 1558 | // Since the result is legal, we should just be able to truncate the low |
| 1559 | // part of the source. |
| 1560 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 1561 | break; |
| 1562 | case Promote: |
| 1563 | Result = PromoteOp(Node->getOperand(0)); |
| 1564 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 1565 | break; |
| 1566 | } |
| 1567 | break; |
| 1568 | |
| 1569 | case ISD::FP_TO_SINT: |
| 1570 | case ISD::FP_TO_UINT: |
| 1571 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1572 | case Legal: |
Chris Lattner | f59b2da | 2005-07-30 00:04:12 +0000 | [diff] [blame^] | 1573 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1574 | |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 1575 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 1576 | default: assert(0 && "Unknown operation action!"); |
| 1577 | case TargetLowering::Expand: |
| 1578 | assert(0 && "Cannot expand FP_TO*INT yet"); |
| 1579 | case TargetLowering::Promote: |
Chris Lattner | f59b2da | 2005-07-30 00:04:12 +0000 | [diff] [blame^] | 1580 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 1581 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 1582 | AddLegalizedOperand(Op, Result); |
| 1583 | return Result; |
| 1584 | case TargetLowering::Legal: |
| 1585 | break; |
Chris Lattner | f59b2da | 2005-07-30 00:04:12 +0000 | [diff] [blame^] | 1586 | case TargetLowering::Custom: |
| 1587 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1588 | Result = TLI.LowerOperation(Result, DAG); |
| 1589 | AddLegalizedOperand(Op, Result); |
| 1590 | NeedsAnotherIteration = true; |
| 1591 | return Result; |
Chris Lattner | 44fe26f | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Chris Lattner | f99f8f9 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 1594 | if (Tmp1 != Node->getOperand(0)) |
| 1595 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1596 | break; |
| 1597 | case Expand: |
| 1598 | assert(0 && "Shouldn't need to expand other operators here!"); |
| 1599 | case Promote: |
| 1600 | Result = PromoteOp(Node->getOperand(0)); |
| 1601 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 1602 | break; |
| 1603 | } |
| 1604 | break; |
| 1605 | |
| 1606 | case ISD::ZERO_EXTEND: |
| 1607 | case ISD::SIGN_EXTEND: |
| 1608 | case ISD::FP_EXTEND: |
| 1609 | case ISD::FP_ROUND: |
| 1610 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1611 | case Legal: |
| 1612 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1613 | if (Tmp1 != Node->getOperand(0)) |
| 1614 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1615 | break; |
| 1616 | case Expand: |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1617 | assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | a65a2f0 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 1618 | |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1619 | case Promote: |
| 1620 | switch (Node->getOpcode()) { |
Chris Lattner | 71d7f6e | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1621 | case ISD::ZERO_EXTEND: |
| 1622 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1623 | // NOTE: Any extend would work here... |
| 1624 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1625 | Result = DAG.getZeroExtendInReg(Result, |
| 1626 | Node->getOperand(0).getValueType()); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1627 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1628 | case ISD::SIGN_EXTEND: |
Chris Lattner | 71d7f6e | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1629 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1630 | // NOTE: Any extend would work here... |
Chris Lattner | 42993e4 | 2005-01-18 21:57:59 +0000 | [diff] [blame] | 1631 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 71d7f6e | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1632 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1633 | Result, |
| 1634 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 71d7f6e | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1635 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1636 | case ISD::FP_EXTEND: |
Chris Lattner | 71d7f6e | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1637 | Result = PromoteOp(Node->getOperand(0)); |
| 1638 | if (Result.getValueType() != Op.getValueType()) |
| 1639 | // Dynamically dead while we have only 2 FP types. |
| 1640 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 1641 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1642 | case ISD::FP_ROUND: |
Chris Lattner | 3ba56b3 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1643 | Result = PromoteOp(Node->getOperand(0)); |
| 1644 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 1645 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1646 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1647 | } |
| 1648 | break; |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1649 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1650 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1651 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1652 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1653 | |
| 1654 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 1655 | // pair. |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1656 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 1657 | default: assert(0 && "This action not supported for this op yet!"); |
| 1658 | case TargetLowering::Legal: |
| 1659 | if (Tmp1 != Node->getOperand(0)) |
| 1660 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1661 | DAG.getValueType(ExtraVT)); |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | case TargetLowering::Expand: |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1664 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1665 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1666 | // NOTE: we could fall back on load/store here too for targets without |
| 1667 | // SAR. However, it is doubtful that any exist. |
| 1668 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 1669 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | ec21837 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 1670 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1671 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 1672 | Node->getOperand(0), ShiftCst); |
| 1673 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 1674 | Result, ShiftCst); |
| 1675 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 1676 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 1677 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 1678 | |
| 1679 | // NOTE: there is a choice here between constantly creating new stack |
| 1680 | // slots and always reusing the same one. We currently always create |
| 1681 | // new ones, as reuse may inhibit scheduling. |
| 1682 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 1683 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 1684 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 1685 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1686 | int SSFI = |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1687 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 1688 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 1689 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1690 | Node->getOperand(0), StackSlot, |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1691 | DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1692 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1693 | Result, StackSlot, DAG.getSrcValue(NULL), |
| 1694 | ExtraVT); |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1695 | } else { |
| 1696 | assert(0 && "Unknown op"); |
| 1697 | } |
| 1698 | Result = LegalizeOp(Result); |
Chris Lattner | 3c0dd46 | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1699 | break; |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1700 | } |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1701 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1702 | } |
Chris Lattner | 99222f7 | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1703 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1704 | |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1705 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 1706 | // means that we always must cache transformed nodes. |
| 1707 | AddLegalizedOperand(Op, Result); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1708 | return Result; |
| 1709 | } |
| 1710 | |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1711 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 1712 | /// promote it to compute the value into a larger type. The produced value will |
| 1713 | /// have the correct bits for the low portion of the register, but no guarantee |
| 1714 | /// is made about the top bits: it may be zero, sign-extended, or garbage. |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1715 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 1716 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 87a769c | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 1717 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1718 | assert(getTypeAction(VT) == Promote && |
| 1719 | "Caller should expand or legalize operands that are not promotable!"); |
| 1720 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 1721 | "Cannot promote to smaller type!"); |
| 1722 | |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1723 | SDOperand Tmp1, Tmp2, Tmp3; |
| 1724 | |
| 1725 | SDOperand Result; |
| 1726 | SDNode *Node = Op.Val; |
| 1727 | |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1728 | if (!Node->hasOneUse()) { |
| 1729 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 1730 | if (I != PromotedNodes.end()) return I->second; |
| 1731 | } else { |
| 1732 | assert(!PromotedNodes.count(Op) && "Repromoted this node??"); |
| 1733 | } |
| 1734 | |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1735 | // Promotion needs an optimization step to clean up after it, and is not |
| 1736 | // careful to avoid operations the target does not support. Make sure that |
| 1737 | // all generated operations are legalized in the next iteration. |
| 1738 | NeedsAnotherIteration = true; |
| 1739 | |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1740 | switch (Node->getOpcode()) { |
| 1741 | default: |
| 1742 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 1743 | assert(0 && "Do not know how to promote this operator!"); |
| 1744 | abort(); |
Nate Begeman | cda9aa7 | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1745 | case ISD::UNDEF: |
| 1746 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 1747 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1748 | case ISD::Constant: |
| 1749 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
| 1750 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 1751 | break; |
| 1752 | case ISD::ConstantFP: |
| 1753 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 1754 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 1755 | break; |
Chris Lattner | 9f2c4a5 | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 1756 | case ISD::CopyFromReg: |
| 1757 | Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT, |
| 1758 | Node->getOperand(0)); |
| 1759 | // Remember that we legalized the chain. |
| 1760 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1761 | break; |
| 1762 | |
Chris Lattner | 2cb338d | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 1763 | case ISD::SETCC: |
| 1764 | assert(getTypeAction(TLI.getSetCCResultTy()) == Legal && |
| 1765 | "SetCC type is not legal??"); |
| 1766 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 1767 | TLI.getSetCCResultTy(), Node->getOperand(0), |
| 1768 | Node->getOperand(1)); |
| 1769 | Result = LegalizeOp(Result); |
| 1770 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1771 | |
| 1772 | case ISD::TRUNCATE: |
| 1773 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1774 | case Legal: |
| 1775 | Result = LegalizeOp(Node->getOperand(0)); |
| 1776 | assert(Result.getValueType() >= NVT && |
| 1777 | "This truncation doesn't make sense!"); |
| 1778 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 1779 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 1780 | break; |
Chris Lattner | bf8c1ad | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 1781 | case Promote: |
| 1782 | // The truncation is not required, because we don't guarantee anything |
| 1783 | // about high bits anyway. |
| 1784 | Result = PromoteOp(Node->getOperand(0)); |
| 1785 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1786 | case Expand: |
Nate Begeman | cc00a7c | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 1787 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1788 | // Truncate the low part of the expanded value to the result type |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1789 | Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1790 | } |
| 1791 | break; |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1792 | case ISD::SIGN_EXTEND: |
| 1793 | case ISD::ZERO_EXTEND: |
| 1794 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1795 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 1796 | case Legal: |
| 1797 | // Input is legal? Just do extend all the way to the larger type. |
| 1798 | Result = LegalizeOp(Node->getOperand(0)); |
| 1799 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
| 1800 | break; |
| 1801 | case Promote: |
| 1802 | // Promote the reg if it's smaller. |
| 1803 | Result = PromoteOp(Node->getOperand(0)); |
| 1804 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 1805 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 0559691 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 1806 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1807 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1808 | else |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1809 | Result = DAG.getZeroExtendInReg(Result, |
| 1810 | Node->getOperand(0).getValueType()); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | } |
| 1813 | break; |
| 1814 | |
| 1815 | case ISD::FP_EXTEND: |
| 1816 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 1817 | case ISD::FP_ROUND: |
| 1818 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1819 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 1820 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 1821 | case Legal: |
| 1822 | // Input is legal? Do an FP_ROUND_INREG. |
| 1823 | Result = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1824 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 1825 | DAG.getValueType(VT)); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1826 | break; |
| 1827 | } |
| 1828 | break; |
| 1829 | |
| 1830 | case ISD::SINT_TO_FP: |
| 1831 | case ISD::UINT_TO_FP: |
| 1832 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1833 | case Legal: |
| 1834 | Result = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1835 | // No extra round required here. |
| 1836 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | |
| 1839 | case Promote: |
| 1840 | Result = PromoteOp(Node->getOperand(0)); |
| 1841 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 1842 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1843 | Result, |
| 1844 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1845 | else |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1846 | Result = DAG.getZeroExtendInReg(Result, |
| 1847 | Node->getOperand(0).getValueType()); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1848 | // No extra round required here. |
| 1849 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1850 | break; |
| 1851 | case Expand: |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1852 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 1853 | Node->getOperand(0)); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1854 | // Round if we cannot tolerate excess precision. |
| 1855 | if (NoExcessFPPrecision) |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1856 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 1857 | DAG.getValueType(VT)); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1858 | break; |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1859 | } |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1860 | break; |
| 1861 | |
| 1862 | case ISD::FP_TO_SINT: |
| 1863 | case ISD::FP_TO_UINT: |
| 1864 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1865 | case Legal: |
| 1866 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1867 | break; |
| 1868 | case Promote: |
| 1869 | // The input result is prerounded, so we don't have to do anything |
| 1870 | // special. |
| 1871 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1872 | break; |
| 1873 | case Expand: |
| 1874 | assert(0 && "not implemented"); |
| 1875 | } |
| 1876 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1877 | break; |
| 1878 | |
Chris Lattner | 13fe99c | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1879 | case ISD::FABS: |
| 1880 | case ISD::FNEG: |
| 1881 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1882 | assert(Tmp1.getValueType() == NVT); |
| 1883 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1884 | // NOTE: we do not have to do any extra rounding here for |
| 1885 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 1886 | // precision, and these operations don't modify precision at all. |
| 1887 | break; |
| 1888 | |
Chris Lattner | 9d6fa98 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 1889 | case ISD::FSQRT: |
| 1890 | case ISD::FSIN: |
| 1891 | case ISD::FCOS: |
| 1892 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1893 | assert(Tmp1.getValueType() == NVT); |
| 1894 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1895 | if(NoExcessFPPrecision) |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1896 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 1897 | DAG.getValueType(VT)); |
Chris Lattner | 9d6fa98 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 1898 | break; |
| 1899 | |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1900 | case ISD::AND: |
| 1901 | case ISD::OR: |
| 1902 | case ISD::XOR: |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1903 | case ISD::ADD: |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1904 | case ISD::SUB: |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1905 | case ISD::MUL: |
| 1906 | // The input may have strange things in the top bits of the registers, but |
| 1907 | // these operations don't care. They may have wierd bits going out, but |
| 1908 | // that too is okay if they are integer operations. |
| 1909 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1910 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1911 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 1912 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1913 | |
| 1914 | // However, if this is a floating point operation, they will give excess |
| 1915 | // precision that we may not be able to tolerate. If we DO allow excess |
| 1916 | // precision, just leave it, otherwise excise it. |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1917 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 1918 | // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1919 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1920 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 1921 | DAG.getValueType(VT)); |
Chris Lattner | c6c9a5b | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1924 | case ISD::SDIV: |
| 1925 | case ISD::SREM: |
| 1926 | // These operators require that their input be sign extended. |
| 1927 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1928 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1929 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1930 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 1931 | DAG.getValueType(VT)); |
| 1932 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 1933 | DAG.getValueType(VT)); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1934 | } |
| 1935 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1936 | |
| 1937 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 1938 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1939 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 1940 | DAG.getValueType(VT)); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1941 | break; |
| 1942 | |
| 1943 | case ISD::UDIV: |
| 1944 | case ISD::UREM: |
| 1945 | // These operators require that their input be zero extended. |
| 1946 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1947 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1948 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1949 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 1950 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1951 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1952 | break; |
| 1953 | |
| 1954 | case ISD::SHL: |
| 1955 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1956 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1957 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2); |
| 1958 | break; |
| 1959 | case ISD::SRA: |
| 1960 | // The input value must be properly sign extended. |
| 1961 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1962 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 1963 | DAG.getValueType(VT)); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1964 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1965 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2); |
| 1966 | break; |
| 1967 | case ISD::SRL: |
| 1968 | // The input value must be properly zero extended. |
| 1969 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1970 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 4d97864 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1971 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1972 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2); |
| 1973 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1974 | case ISD::LOAD: |
| 1975 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1976 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c53cd50 | 2005-04-10 04:33:47 +0000 | [diff] [blame] | 1977 | // FIXME: When the DAG combiner exists, change this to use EXTLOAD! |
Chris Lattner | 391a351 | 2005-04-10 17:40:35 +0000 | [diff] [blame] | 1978 | if (MVT::isInteger(NVT)) |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1979 | Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, |
| 1980 | Node->getOperand(2), VT); |
Chris Lattner | 391a351 | 2005-04-10 17:40:35 +0000 | [diff] [blame] | 1981 | else |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1982 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2, |
| 1983 | Node->getOperand(2), VT); |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1984 | |
| 1985 | // Remember that we legalized the chain. |
| 1986 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1987 | break; |
| 1988 | case ISD::SELECT: |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1989 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1990 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1991 | case Legal: |
| 1992 | Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition. |
| 1993 | break; |
| 1994 | case Promote: |
| 1995 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1996 | break; |
| 1997 | } |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1998 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 1999 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
| 2000 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3); |
| 2001 | break; |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 2002 | case ISD::TAILCALL: |
Chris Lattner | 5c8a85e | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 2003 | case ISD::CALL: { |
| 2004 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2005 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 2006 | |
Chris Lattner | 3d95c14 | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 2007 | std::vector<SDOperand> Ops; |
| 2008 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) |
| 2009 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2010 | |
Chris Lattner | 5c8a85e | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 2011 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 2012 | "Can only promote single result calls"); |
| 2013 | std::vector<MVT::ValueType> RetTyVTs; |
| 2014 | RetTyVTs.reserve(2); |
| 2015 | RetTyVTs.push_back(NVT); |
| 2016 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 2017 | SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops, |
| 2018 | Node->getOpcode() == ISD::TAILCALL); |
Chris Lattner | 5c8a85e | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 2019 | Result = SDOperand(NC, 0); |
| 2020 | |
| 2021 | // Insert the new chain mapping. |
| 2022 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 2023 | break; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2024 | } |
Andrew Lenharth | dd426dd | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2025 | case ISD::CTPOP: |
| 2026 | case ISD::CTTZ: |
| 2027 | case ISD::CTLZ: |
| 2028 | Tmp1 = Node->getOperand(0); |
| 2029 | //Zero extend the argument |
| 2030 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2031 | // Perform the larger operation, then subtract if needed. |
| 2032 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2033 | switch(Node->getOpcode()) |
| 2034 | { |
| 2035 | case ISD::CTPOP: |
| 2036 | Result = Tmp1; |
| 2037 | break; |
| 2038 | case ISD::CTTZ: |
| 2039 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2040 | Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1, |
Andrew Lenharth | dd426dd | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2041 | DAG.getConstant(getSizeInBits(NVT), NVT)); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2042 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | dd426dd | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2043 | DAG.getConstant(getSizeInBits(VT),NVT), Tmp1); |
| 2044 | break; |
| 2045 | case ISD::CTLZ: |
| 2046 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2047 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2048 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | dd426dd | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2049 | getSizeInBits(VT), NVT)); |
| 2050 | break; |
| 2051 | } |
| 2052 | break; |
Chris Lattner | 1f2c9d8 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | assert(Result.Val && "Didn't set a result!"); |
| 2056 | AddPromotedOperand(Op, Result); |
| 2057 | return Result; |
| 2058 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2060 | /// ExpandAddSub - Find a clever way to expand this add operation into |
| 2061 | /// subcomponents. |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2062 | void SelectionDAGLegalize:: |
| 2063 | ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, |
| 2064 | SDOperand &Lo, SDOperand &Hi) { |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2065 | // Expand the subcomponents. |
| 2066 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 2067 | ExpandOp(LHS, LHSL, LHSH); |
| 2068 | ExpandOp(RHS, RHSL, RHSH); |
| 2069 | |
Chris Lattner | 8ffd004 | 2005-04-11 20:29:59 +0000 | [diff] [blame] | 2070 | // FIXME: this should be moved to the dag combiner someday. |
Chris Lattner | 669e8c2 | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 2071 | assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS); |
| 2072 | if (LHSL.getValueType() == MVT::i32) { |
| 2073 | SDOperand LowEl; |
| 2074 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL)) |
| 2075 | if (C->getValue() == 0) |
| 2076 | LowEl = RHSL; |
| 2077 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL)) |
| 2078 | if (C->getValue() == 0) |
| 2079 | LowEl = LHSL; |
| 2080 | if (LowEl.Val) { |
| 2081 | // Turn this into an add/sub of the high part only. |
| 2082 | SDOperand HiEl = |
| 2083 | DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB, |
| 2084 | LowEl.getValueType(), LHSH, RHSH); |
| 2085 | Lo = LowEl; |
| 2086 | Hi = HiEl; |
| 2087 | return; |
Chris Lattner | 8ffd004 | 2005-04-11 20:29:59 +0000 | [diff] [blame] | 2088 | } |
Chris Lattner | 669e8c2 | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 2089 | } |
Chris Lattner | 8ffd004 | 2005-04-11 20:29:59 +0000 | [diff] [blame] | 2090 | |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2091 | std::vector<SDOperand> Ops; |
| 2092 | Ops.push_back(LHSL); |
| 2093 | Ops.push_back(LHSH); |
| 2094 | Ops.push_back(RHSL); |
| 2095 | Ops.push_back(RHSH); |
Chris Lattner | 669e8c2 | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 2096 | |
| 2097 | std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); |
| 2098 | Lo = DAG.getNode(NodeOp, VTs, Ops); |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2099 | Hi = Lo.getValue(1); |
| 2100 | } |
| 2101 | |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2102 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 2103 | SDOperand Op, SDOperand Amt, |
| 2104 | SDOperand &Lo, SDOperand &Hi) { |
| 2105 | // Expand the subcomponents. |
| 2106 | SDOperand LHSL, LHSH; |
| 2107 | ExpandOp(Op, LHSL, LHSH); |
| 2108 | |
| 2109 | std::vector<SDOperand> Ops; |
| 2110 | Ops.push_back(LHSL); |
| 2111 | Ops.push_back(LHSH); |
| 2112 | Ops.push_back(Amt); |
Chris Lattner | 669e8c2 | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 2113 | std::vector<MVT::ValueType> VTs; |
| 2114 | VTs.push_back(LHSL.getValueType()); |
| 2115 | VTs.push_back(LHSH.getValueType()); |
| 2116 | VTs.push_back(Amt.getValueType()); |
| 2117 | Lo = DAG.getNode(NodeOp, VTs, Ops); |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2118 | Hi = Lo.getValue(1); |
| 2119 | } |
| 2120 | |
| 2121 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2122 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 2123 | /// smaller elements. If we can't find a way that is more efficient than a |
| 2124 | /// libcall on this target, return false. Otherwise, return true with the |
| 2125 | /// low-parts expanded into Lo and Hi. |
| 2126 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 2127 | SDOperand &Lo, SDOperand &Hi) { |
| 2128 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 2129 | "This is not a shift!"); |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2131 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2132 | SDOperand ShAmt = LegalizeOp(Amt); |
| 2133 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 2134 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 2135 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 2136 | |
| 2137 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 2138 | // and are disabled. |
| 2139 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 2140 | unsigned Cst = CN->getValue(); |
| 2141 | // Expand the incoming operand to be shifted, so that we have its parts |
| 2142 | SDOperand InL, InH; |
| 2143 | ExpandOp(Op, InL, InH); |
| 2144 | switch(Opc) { |
| 2145 | case ISD::SHL: |
| 2146 | if (Cst > VTBits) { |
| 2147 | Lo = DAG.getConstant(0, NVT); |
| 2148 | Hi = DAG.getConstant(0, NVT); |
| 2149 | } else if (Cst > NVTBits) { |
| 2150 | Lo = DAG.getConstant(0, NVT); |
| 2151 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | edd1970 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2152 | } else if (Cst == NVTBits) { |
| 2153 | Lo = DAG.getConstant(0, NVT); |
| 2154 | Hi = InL; |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2155 | } else { |
| 2156 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 2157 | Hi = DAG.getNode(ISD::OR, NVT, |
| 2158 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 2159 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 2160 | } |
| 2161 | return true; |
| 2162 | case ISD::SRL: |
| 2163 | if (Cst > VTBits) { |
| 2164 | Lo = DAG.getConstant(0, NVT); |
| 2165 | Hi = DAG.getConstant(0, NVT); |
| 2166 | } else if (Cst > NVTBits) { |
| 2167 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 2168 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | edd1970 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2169 | } else if (Cst == NVTBits) { |
| 2170 | Lo = InH; |
| 2171 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2172 | } else { |
| 2173 | Lo = DAG.getNode(ISD::OR, NVT, |
| 2174 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 2175 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 2176 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 2177 | } |
| 2178 | return true; |
| 2179 | case ISD::SRA: |
| 2180 | if (Cst > VTBits) { |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2181 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2182 | DAG.getConstant(NVTBits-1, ShTy)); |
| 2183 | } else if (Cst > NVTBits) { |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2184 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2185 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2186 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2187 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | edd1970 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2188 | } else if (Cst == NVTBits) { |
| 2189 | Lo = InH; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2190 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | edd1970 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2191 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | b067492 | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2192 | } else { |
| 2193 | Lo = DAG.getNode(ISD::OR, NVT, |
| 2194 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 2195 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 2196 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 2197 | } |
| 2198 | return true; |
| 2199 | } |
| 2200 | } |
| 2201 | // FIXME: The following code for expanding shifts using ISD::SELECT is buggy, |
| 2202 | // so disable it for now. Currently targets are handling this via SHL_PARTS |
| 2203 | // and friends. |
| 2204 | return false; |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2205 | |
| 2206 | // If we have an efficient select operation (or if the selects will all fold |
| 2207 | // away), lower to some complex code, otherwise just emit the libcall. |
| 2208 | if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal && |
| 2209 | !isa<ConstantSDNode>(Amt)) |
| 2210 | return false; |
| 2211 | |
| 2212 | SDOperand InL, InH; |
| 2213 | ExpandOp(Op, InL, InH); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2214 | SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt |
| 2215 | DAG.getConstant(NVTBits, ShTy), ShAmt); |
| 2216 | |
Chris Lattner | 4d25c04 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 2217 | // Compare the unmasked shift amount against 32. |
| 2218 | SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt, |
| 2219 | DAG.getConstant(NVTBits, ShTy)); |
| 2220 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2221 | if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) { |
| 2222 | ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31 |
| 2223 | DAG.getConstant(NVTBits-1, ShTy)); |
| 2224 | NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31 |
| 2225 | DAG.getConstant(NVTBits-1, ShTy)); |
| 2226 | } |
| 2227 | |
| 2228 | if (Opc == ISD::SHL) { |
| 2229 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt) |
| 2230 | DAG.getNode(ISD::SHL, NVT, InH, ShAmt), |
| 2231 | DAG.getNode(ISD::SRL, NVT, InL, NAmt)); |
Chris Lattner | 4d25c04 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 2232 | SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31 |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2234 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
| 2235 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2); |
| 2236 | } else { |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2237 | SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT, |
| 2238 | DAG.getSetCC(ISD::SETEQ, |
| 2239 | TLI.getSetCCResultTy(), NAmt, |
| 2240 | DAG.getConstant(32, ShTy)), |
| 2241 | DAG.getConstant(0, NVT), |
| 2242 | DAG.getNode(ISD::SHL, NVT, InH, NAmt)); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2243 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt) |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2244 | HiLoPart, |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2245 | DAG.getNode(ISD::SRL, NVT, InL, ShAmt)); |
Chris Lattner | 4d25c04 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 2246 | SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31 |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2247 | |
| 2248 | SDOperand HiPart; |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2249 | if (Opc == ISD::SRA) |
| 2250 | HiPart = DAG.getNode(ISD::SRA, NVT, InH, |
| 2251 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2252 | else |
| 2253 | HiPart = DAG.getConstant(0, NVT); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2254 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
Chris Lattner | 4d25c04 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 2255 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2256 | } |
| 2257 | return true; |
| 2258 | } |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2259 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2260 | /// FindLatestCallSeqStart - Scan up the dag to find the latest (highest |
| 2261 | /// NodeDepth) node that is an CallSeqStart operation and occurs later than |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2262 | /// Found. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2263 | static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) { |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2264 | if (Node->getNodeDepth() <= Found->getNodeDepth()) return; |
| 2265 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2266 | // If we found an CALLSEQ_START, we already know this node occurs later |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2267 | // than the Found node. Just remember this node and return. |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2268 | if (Node->getOpcode() == ISD::CALLSEQ_START) { |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2269 | Found = Node; |
| 2270 | return; |
| 2271 | } |
| 2272 | |
| 2273 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 2274 | assert(Node->getNumOperands() != 0 && |
| 2275 | "All leaves should have depth equal to the entry node!"); |
| 2276 | for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2277 | FindLatestCallSeqStart(Node->getOperand(i).Val, Found); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2278 | |
| 2279 | // Tail recurse for the last iteration. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2280 | FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val, |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2281 | Found); |
| 2282 | } |
| 2283 | |
| 2284 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2285 | /// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest |
| 2286 | /// NodeDepth) node that is an CallSeqEnd operation and occurs more recent |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2287 | /// than Found. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2288 | static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) { |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2289 | if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return; |
| 2290 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2291 | // If we found an CALLSEQ_END, we already know this node occurs earlier |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2292 | // than the Found node. Just remember this node and return. |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2293 | if (Node->getOpcode() == ISD::CALLSEQ_END) { |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2294 | Found = Node; |
| 2295 | return; |
| 2296 | } |
| 2297 | |
| 2298 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 2299 | SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 2300 | if (UI == E) return; |
| 2301 | for (--E; UI != E; ++UI) |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2302 | FindEarliestCallSeqEnd(*UI, Found); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2303 | |
| 2304 | // Tail recurse for the last iteration. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2305 | FindEarliestCallSeqEnd(*UI, Found); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2308 | /// FindCallSeqEnd - Given a chained node that is part of a call sequence, |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2309 | /// find the CALLSEQ_END node that terminates the call sequence. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2310 | static SDNode *FindCallSeqEnd(SDNode *Node) { |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2311 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2312 | return Node; |
Chris Lattner | 07f97d5 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 2313 | if (Node->use_empty()) |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2314 | return 0; // No CallSeqEnd |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2315 | |
| 2316 | if (Node->hasOneUse()) // Simple case, only has one user to check. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2317 | return FindCallSeqEnd(*Node->use_begin()); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2318 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2319 | SDOperand TheChain(Node, Node->getNumValues()-1); |
Chris Lattner | 3268f24 | 2005-05-14 08:34:53 +0000 | [diff] [blame] | 2320 | if (TheChain.getValueType() != MVT::Other) |
| 2321 | TheChain = SDOperand(Node, 0); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2322 | assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!"); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2323 | |
| 2324 | for (SDNode::use_iterator UI = Node->use_begin(), |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2325 | E = Node->use_end(); ; ++UI) { |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2326 | assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!"); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2327 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2328 | // Make sure to only follow users of our token chain. |
| 2329 | SDNode *User = *UI; |
| 2330 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 2331 | if (User->getOperand(i) == TheChain) |
Chris Lattner | bb1d60d | 2005-05-13 05:17:00 +0000 | [diff] [blame] | 2332 | if (SDNode *Result = FindCallSeqEnd(User)) |
| 2333 | return Result; |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2334 | } |
| 2335 | assert(0 && "Unreachable"); |
| 2336 | abort(); |
| 2337 | } |
| 2338 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2339 | /// FindCallSeqStart - Given a chained node that is part of a call sequence, |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2340 | /// find the CALLSEQ_START node that initiates the call sequence. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2341 | static SDNode *FindCallSeqStart(SDNode *Node) { |
| 2342 | assert(Node && "Didn't find callseq_start for a call??"); |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2343 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2344 | |
| 2345 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 2346 | "Node doesn't have a token chain argument!"); |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2347 | return FindCallSeqStart(Node->getOperand(0).Val); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2351 | /// FindInputOutputChains - If we are replacing an operation with a call we need |
| 2352 | /// to find the call that occurs before and the call that occurs after it to |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2353 | /// properly serialize the calls in the block. The returned operand is the |
| 2354 | /// input chain value for the new call (e.g. the entry node or the previous |
| 2355 | /// call), and OutChain is set to be the chain node to update to point to the |
| 2356 | /// end of the call chain. |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2357 | static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain, |
| 2358 | SDOperand Entry) { |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2359 | SDNode *LatestCallSeqStart = Entry.Val; |
| 2360 | SDNode *LatestCallSeqEnd = 0; |
| 2361 | FindLatestCallSeqStart(OpNode, LatestCallSeqStart); |
| 2362 | //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n"; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2364 | // It is possible that no ISD::CALLSEQ_START was found because there is no |
Nate Begeman | add0c63 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2365 | // previous call in the function. LatestCallStackDown may in that case be |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2366 | // the entry node itself. Do not attempt to find a matching CALLSEQ_END |
| 2367 | // unless LatestCallStackDown is an CALLSEQ_START. |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2368 | if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) |
| 2369 | LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart); |
Nate Begeman | add0c63 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2370 | else |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2371 | LatestCallSeqEnd = Entry.Val; |
| 2372 | assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd"); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2373 | |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2374 | // Finally, find the first call that this must come before, first we find the |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2375 | // CallSeqEnd that ends the call. |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2376 | OutChain = 0; |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2377 | FindEarliestCallSeqEnd(OpNode, OutChain); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2378 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2379 | // If we found one, translate from the adj up to the callseq_start. |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2380 | if (OutChain) |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2381 | OutChain = FindCallSeqStart(OutChain); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2382 | |
Chris Lattner | 5a14c8a | 2005-05-13 05:09:11 +0000 | [diff] [blame] | 2383 | return SDOperand(LatestCallSeqEnd, 0); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2386 | /// SpliceCallInto - Given the result chain of a libcall (CallResult), and a |
Chris Lattner | a5bf103 | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 2387 | void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult, |
| 2388 | SDNode *OutChain) { |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2389 | // Nothing to splice it into? |
| 2390 | if (OutChain == 0) return; |
| 2391 | |
| 2392 | assert(OutChain->getOperand(0).getValueType() == MVT::Other); |
| 2393 | //OutChain->dump(); |
| 2394 | |
| 2395 | // Form a token factor node merging the old inval and the new inval. |
| 2396 | SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult, |
| 2397 | OutChain->getOperand(0)); |
| 2398 | // Change the node to refer to the new token. |
| 2399 | OutChain->setAdjCallChain(InToken); |
| 2400 | } |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2401 | |
| 2402 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2403 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 2404 | // does not fit into a register, return the lo part and set the hi part to the |
| 2405 | // by-reg argument. If it does fit into a single register, return the result |
| 2406 | // and leave the Hi part unset. |
| 2407 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 2408 | SDOperand &Hi) { |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2409 | SDNode *OutChain; |
| 2410 | SDOperand InChain = FindInputOutputChains(Node, OutChain, |
| 2411 | DAG.getEntryNode()); |
Chris Lattner | 07f97d5 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 2412 | if (InChain.Val == 0) |
| 2413 | InChain = DAG.getEntryNode(); |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2415 | TargetLowering::ArgListTy Args; |
| 2416 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2417 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 2418 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 2419 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 2420 | } |
| 2421 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2422 | |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2423 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2424 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2425 | std::pair<SDOperand,SDOperand> CallInfo = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 2426 | TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, |
| 2427 | Callee, Args, DAG); |
Chris Lattner | a5bf103 | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 2428 | SpliceCallInto(CallInfo.second, OutChain); |
| 2429 | |
| 2430 | NeedsAnotherIteration = true; |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2431 | |
| 2432 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2433 | default: assert(0 && "Unknown thing"); |
| 2434 | case Legal: |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2435 | return CallInfo.first; |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2436 | case Promote: |
| 2437 | assert(0 && "Cannot promote this yet!"); |
| 2438 | case Expand: |
| 2439 | SDOperand Lo; |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2440 | ExpandOp(CallInfo.first, Lo, Hi); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2441 | return Lo; |
| 2442 | } |
| 2443 | } |
| 2444 | |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2446 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 2447 | /// destination type is legal. |
| 2448 | SDOperand SelectionDAGLegalize:: |
| 2449 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
| 2450 | assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!"); |
| 2451 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 2452 | "This is not an expansion!"); |
| 2453 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 2454 | |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2455 | if (!isSigned) { |
Chris Lattner | e69ad5f | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 2456 | assert(Source.getValueType() == MVT::i64 && |
| 2457 | "This only works for 64-bit -> FP"); |
| 2458 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 2459 | // incoming integer is set. To handle this, we dynamically test to see if |
| 2460 | // it is set, and, if so, add a fudge factor. |
| 2461 | SDOperand Lo, Hi; |
| 2462 | ExpandOp(Source, Lo, Hi); |
| 2463 | |
Chris Lattner | 2a4f731 | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 2464 | // If this is unsigned, and not supported, first perform the conversion to |
| 2465 | // signed, then adjust the result if the sign bit is set. |
| 2466 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 2467 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 2468 | |
Chris Lattner | e69ad5f | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 2469 | SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi, |
| 2470 | DAG.getConstant(0, Hi.getValueType())); |
| 2471 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 2472 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 2473 | SignSet, Four, Zero); |
Chris Lattner | 26f0317 | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 2474 | uint64_t FF = 0x5f800000ULL; |
| 2475 | if (TLI.isLittleEndian()) FF <<= 32; |
| 2476 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
Chris Lattner | e69ad5f | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 2477 | |
| 2478 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); |
| 2479 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor), |
| 2480 | TLI.getPointerTy()); |
| 2481 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 2482 | SDOperand FudgeInReg; |
| 2483 | if (DestTy == MVT::f32) |
Chris Lattner | 5385db5 | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2484 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 2485 | DAG.getSrcValue(NULL)); |
Chris Lattner | e69ad5f | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 2486 | else { |
| 2487 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | de0a4b1 | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 2488 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 2489 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | e69ad5f | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 2490 | } |
| 2491 | return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2492 | } |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2493 | |
Chris Lattner | d3cc996 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 2494 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 2495 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 2496 | default: assert(0 && "This action not implemented for this operation!"); |
| 2497 | case TargetLowering::Legal: |
| 2498 | case TargetLowering::Expand: |
| 2499 | break; // This case is handled below. |
| 2500 | case TargetLowering::Custom: |
| 2501 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
Chris Lattner | 29dcc71 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 2502 | return LegalizeOp(TLI.LowerOperation(Source, DAG)); |
Chris Lattner | d3cc996 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Chris Lattner | 153587e | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 2505 | // Expand the source, then glue it back together for the call. We must expand |
| 2506 | // the source in case it is shared (this pass of legalize must traverse it). |
| 2507 | SDOperand SrcLo, SrcHi; |
| 2508 | ExpandOp(Source, SrcLo, SrcHi); |
| 2509 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 2510 | |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2511 | SDNode *OutChain = 0; |
| 2512 | SDOperand InChain = FindInputOutputChains(Source.Val, OutChain, |
| 2513 | DAG.getEntryNode()); |
| 2514 | const char *FnName = 0; |
| 2515 | if (DestTy == MVT::f32) |
| 2516 | FnName = "__floatdisf"; |
| 2517 | else { |
| 2518 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 2519 | FnName = "__floatdidf"; |
| 2520 | } |
| 2521 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2522 | SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy()); |
| 2523 | |
| 2524 | TargetLowering::ArgListTy Args; |
| 2525 | const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType()); |
Chris Lattner | 8a5ad84 | 2005-05-12 06:54:21 +0000 | [diff] [blame] | 2526 | |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2527 | Args.push_back(std::make_pair(Source, ArgTy)); |
| 2528 | |
| 2529 | // We don't care about token chains for libcalls. We just use the entry |
| 2530 | // node as our input and ignore the output chain. This allows us to place |
| 2531 | // calls wherever we need them to satisfy data dependences. |
| 2532 | const Type *RetTy = MVT::getTypeForValueType(DestTy); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2533 | |
| 2534 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 2535 | TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true, |
| 2536 | Callee, Args, DAG); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2537 | |
Chris Lattner | a5bf103 | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 2538 | SpliceCallInto(CallResult.second, OutChain); |
Chris Lattner | 06bbeb6 | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2539 | return CallResult.first; |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2540 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2541 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2542 | |
| 2543 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2544 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 2545 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 2546 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 2547 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 2548 | /// Lo/Hi values are returned. |
| 2549 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 2550 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 87a769c | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 2551 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2552 | SDNode *Node = Op.Val; |
| 2553 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
| 2554 | assert(MVT::isInteger(VT) && "Cannot expand FP values!"); |
| 2555 | assert(MVT::isInteger(NVT) && NVT < VT && |
| 2556 | "Cannot expand to FP value or to larger int value!"); |
| 2557 | |
| 2558 | // If there is more than one use of this, see if we already expanded it. |
| 2559 | // There is no use remembering values that only have a single use, as the map |
| 2560 | // entries will never be reused. |
| 2561 | if (!Node->hasOneUse()) { |
| 2562 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 2563 | = ExpandedNodes.find(Op); |
| 2564 | if (I != ExpandedNodes.end()) { |
| 2565 | Lo = I->second.first; |
| 2566 | Hi = I->second.second; |
| 2567 | return; |
| 2568 | } |
Chris Lattner | b5a78e0 | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2569 | } else { |
| 2570 | assert(!ExpandedNodes.count(Op) && "Re-expanding a node!"); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2573 | // Expanding to multiple registers needs to perform an optimization step, and |
| 2574 | // is not careful to avoid operations the target does not support. Make sure |
| 2575 | // that all generated operations are legalized in the next iteration. |
| 2576 | NeedsAnotherIteration = true; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2577 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2578 | switch (Node->getOpcode()) { |
| 2579 | default: |
| 2580 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 2581 | assert(0 && "Do not know how to expand this operator!"); |
| 2582 | abort(); |
Nate Begeman | cda9aa7 | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 2583 | case ISD::UNDEF: |
| 2584 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 2585 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 2586 | break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2587 | case ISD::Constant: { |
| 2588 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 2589 | Lo = DAG.getConstant(Cst, NVT); |
| 2590 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 2591 | break; |
| 2592 | } |
| 2593 | |
| 2594 | case ISD::CopyFromReg: { |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 2595 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2596 | // Aggregate register values are always in consequtive pairs. |
Chris Lattner | 3b8e719 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 2597 | Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0)); |
| 2598 | Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1)); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | 3b8e719 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 2600 | // Remember that we legalized the chain. |
| 2601 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 2602 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2603 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 2604 | break; |
| 2605 | } |
| 2606 | |
Chris Lattner | 32e08b7 | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 2607 | case ISD::BUILD_PAIR: |
| 2608 | // Legalize both operands. FIXME: in the future we should handle the case |
| 2609 | // where the two elements are not legal. |
| 2610 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 2611 | Lo = LegalizeOp(Node->getOperand(0)); |
| 2612 | Hi = LegalizeOp(Node->getOperand(1)); |
| 2613 | break; |
| 2614 | |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2615 | case ISD::CTPOP: |
| 2616 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 3740f39 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 2617 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 2618 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 2619 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2620 | Hi = DAG.getConstant(0, NVT); |
| 2621 | break; |
| 2622 | |
Chris Lattner | cf5f6b0 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 2623 | case ISD::CTLZ: { |
| 2624 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 0bfd177 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 2625 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | cf5f6b0 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 2626 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 2627 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
| 2628 | SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(), |
| 2629 | HLZ, BitsC); |
| 2630 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 2631 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 2632 | |
| 2633 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 2634 | Hi = DAG.getConstant(0, NVT); |
| 2635 | break; |
| 2636 | } |
| 2637 | |
| 2638 | case ISD::CTTZ: { |
| 2639 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 0bfd177 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 2640 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | cf5f6b0 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 2641 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 2642 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
| 2643 | SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(), |
| 2644 | LTZ, BitsC); |
| 2645 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 2646 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 2647 | |
| 2648 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 2649 | Hi = DAG.getConstant(0, NVT); |
| 2650 | break; |
| 2651 | } |
Chris Lattner | 55e9cde | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2653 | case ISD::LOAD: { |
| 2654 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2655 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 4a73c2c | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 2656 | Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2657 | |
| 2658 | // Increment the pointer to the other half. |
Chris Lattner | 9242c50 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 2659 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2660 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 2661 | getIntPtrConstant(IncrementSize)); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2662 | //Is this safe? declaring that the two parts of the split load |
Andrew Lenharth | 4a73c2c | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 2663 | //are from the same instruction? |
| 2664 | Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | 0d03eb4 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 2665 | |
| 2666 | // Build a factor node to remember that this load is independent of the |
| 2667 | // other one. |
| 2668 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 2669 | Hi.getValue(1)); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2671 | // Remember that we legalized the chain. |
Chris Lattner | 0d03eb4 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 2672 | AddLegalizedOperand(Op.getValue(1), TF); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2673 | if (!TLI.isLittleEndian()) |
| 2674 | std::swap(Lo, Hi); |
| 2675 | break; |
| 2676 | } |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 2677 | case ISD::TAILCALL: |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2678 | case ISD::CALL: { |
| 2679 | SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2680 | SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 2681 | |
Chris Lattner | 3d95c14 | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 2682 | bool Changed = false; |
| 2683 | std::vector<SDOperand> Ops; |
| 2684 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { |
| 2685 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2686 | Changed |= Ops.back() != Node->getOperand(i); |
| 2687 | } |
| 2688 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2689 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 2690 | "Can only expand a call once so far, not i64 -> i16!"); |
| 2691 | |
| 2692 | std::vector<MVT::ValueType> RetTyVTs; |
| 2693 | RetTyVTs.reserve(3); |
| 2694 | RetTyVTs.push_back(NVT); |
| 2695 | RetTyVTs.push_back(NVT); |
| 2696 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | d0feb64 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 2697 | SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops, |
| 2698 | Node->getOpcode() == ISD::TAILCALL); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2699 | Lo = SDOperand(NC, 0); |
| 2700 | Hi = SDOperand(NC, 1); |
| 2701 | |
| 2702 | // Insert the new chain mapping. |
Chris Lattner | c0f31c5 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 2703 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2704 | break; |
| 2705 | } |
| 2706 | case ISD::AND: |
| 2707 | case ISD::OR: |
| 2708 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 2709 | SDOperand LL, LH, RL, RH; |
| 2710 | ExpandOp(Node->getOperand(0), LL, LH); |
| 2711 | ExpandOp(Node->getOperand(1), RL, RH); |
| 2712 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 2713 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 2714 | break; |
| 2715 | } |
| 2716 | case ISD::SELECT: { |
| 2717 | SDOperand C, LL, LH, RL, RH; |
Chris Lattner | d65c3f3 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2718 | |
| 2719 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2720 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 2721 | case Legal: |
| 2722 | C = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2723 | break; |
| 2724 | case Promote: |
| 2725 | C = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 2726 | break; |
| 2727 | } |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2728 | ExpandOp(Node->getOperand(1), LL, LH); |
| 2729 | ExpandOp(Node->getOperand(2), RL, RH); |
| 2730 | Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); |
| 2731 | Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); |
| 2732 | break; |
| 2733 | } |
| 2734 | case ISD::SIGN_EXTEND: { |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2735 | SDOperand In; |
| 2736 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2737 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 2738 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 2739 | case Promote: |
| 2740 | In = PromoteOp(Node->getOperand(0)); |
| 2741 | // Emit the appropriate sign_extend_inreg to get the value we want. |
| 2742 | In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In, |
Chris Lattner | 0b6ba90 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2743 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2744 | break; |
| 2745 | } |
| 2746 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2747 | // The low part is just a sign extension of the input (which degenerates to |
| 2748 | // a copy). |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2749 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2751 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 2752 | // part. |
Chris Lattner | 9864b08 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 2753 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | ec21837 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2754 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 2755 | TLI.getShiftAmountTy())); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | } |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2758 | case ISD::ZERO_EXTEND: { |
| 2759 | SDOperand In; |
| 2760 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2761 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 2762 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 2763 | case Promote: |
| 2764 | In = PromoteOp(Node->getOperand(0)); |
| 2765 | // Emit the appropriate zero_extend_inreg to get the value we want. |
Chris Lattner | 0e852af | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2766 | In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType()); |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2767 | break; |
| 2768 | } |
| 2769 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2770 | // The low part is just a zero extension of the input (which degenerates to |
| 2771 | // a copy). |
Chris Lattner | d8cbfe8 | 2005-04-10 01:13:15 +0000 | [diff] [blame] | 2772 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2773 | |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2774 | // The high part is just a zero. |
| 2775 | Hi = DAG.getConstant(0, NVT); |
| 2776 | break; |
Chris Lattner | 4784489 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2777 | } |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2778 | // These operators cannot be expanded directly, emit them as calls to |
| 2779 | // library functions. |
| 2780 | case ISD::FP_TO_SINT: |
Chris Lattner | fe68d75 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 2781 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
| 2782 | SDOperand Op = DAG.getNode(ISD::FP_TO_SINT, VT, |
| 2783 | LegalizeOp(Node->getOperand(0))); |
| 2784 | // Now that the custom expander is done, expand the result, which is still |
| 2785 | // VT. |
| 2786 | ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi); |
| 2787 | break; |
| 2788 | } |
| 2789 | |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2790 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2791 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2792 | else |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2793 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2794 | break; |
| 2795 | case ISD::FP_TO_UINT: |
Chris Lattner | fe68d75 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 2796 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
| 2797 | SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT, |
| 2798 | LegalizeOp(Node->getOperand(0))); |
| 2799 | // Now that the custom expander is done, expand the result, which is still |
| 2800 | // VT. |
| 2801 | ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi); |
| 2802 | break; |
| 2803 | } |
| 2804 | |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2805 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2806 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2807 | else |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2808 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 7e6eeba | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2811 | case ISD::SHL: |
| 2812 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2813 | if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2814 | break; |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2815 | |
| 2816 | // If this target supports SHL_PARTS, use it. |
| 2817 | if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2818 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2819 | Lo, Hi); |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2820 | break; |
| 2821 | } |
| 2822 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2823 | // Otherwise, emit a libcall. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2824 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2825 | break; |
| 2826 | |
| 2827 | case ISD::SRA: |
| 2828 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2829 | if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2830 | break; |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2831 | |
| 2832 | // If this target supports SRA_PARTS, use it. |
| 2833 | if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2834 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2835 | Lo, Hi); |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2836 | break; |
| 2837 | } |
| 2838 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2839 | // Otherwise, emit a libcall. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2840 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2841 | break; |
| 2842 | case ISD::SRL: |
| 2843 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2844 | if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2845 | break; |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2846 | |
| 2847 | // If this target supports SRL_PARTS, use it. |
| 2848 | if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 4157c41 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2849 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2850 | Lo, Hi); |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2851 | break; |
| 2852 | } |
| 2853 | |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2854 | // Otherwise, emit a libcall. |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2855 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | 2a7f8a9 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2856 | break; |
| 2857 | |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2858 | case ISD::ADD: |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2859 | ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2860 | Lo, Hi); |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2861 | break; |
| 2862 | case ISD::SUB: |
Chris Lattner | 2e5872c | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2863 | ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2864 | Lo, Hi); |
Chris Lattner | b3f83b28 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2865 | break; |
Nate Begeman | add0c63 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2866 | case ISD::MUL: { |
| 2867 | if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) { |
| 2868 | SDOperand LL, LH, RL, RH; |
| 2869 | ExpandOp(Node->getOperand(0), LL, LH); |
| 2870 | ExpandOp(Node->getOperand(1), RL, RH); |
| 2871 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 2872 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 2873 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 2874 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 2875 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 2876 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 2877 | } else { |
| 2878 | Lo = ExpandLibCall("__muldi3" , Node, Hi); break; |
| 2879 | } |
| 2880 | break; |
| 2881 | } |
Chris Lattner | aac464e | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2882 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 2883 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 2884 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 2885 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | // Remember in a map if the values will be reused later. |
| 2889 | if (!Node->hasOneUse()) { |
| 2890 | bool isNew = ExpandedNodes.insert(std::make_pair(Op, |
| 2891 | std::make_pair(Lo, Hi))).second; |
| 2892 | assert(isNew && "Value already expanded?!?"); |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | |
| 2897 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 2898 | // |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2899 | void SelectionDAG::Legalize() { |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2900 | /// run - This is the main entry point to this class. |
| 2901 | /// |
Chris Lattner | 4add7e3 | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2902 | SelectionDAGLegalize(*this).Run(); |
Chris Lattner | dc75059 | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |