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