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