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