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