Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
| 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
| 15 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.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 | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include <iostream> |
| 23 | using namespace llvm; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 27 | /// hacks on it until the target machine can handle it. This involves |
| 28 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 29 | /// large sizes or splitting up large values into small values) as well as |
| 30 | /// eliminating operations the machine cannot handle. |
| 31 | /// |
| 32 | /// This code also does a small amount of optimization and recognition of idioms |
| 33 | /// as part of its processing. For example, if a target does not support a |
| 34 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 35 | /// will attempt merge setcc and brc instructions into brcc's. |
| 36 | /// |
| 37 | namespace { |
| 38 | class SelectionDAGLegalize { |
| 39 | TargetLowering &TLI; |
| 40 | SelectionDAG &DAG; |
| 41 | |
| 42 | /// LegalizeAction - This enum indicates what action we should take for each |
| 43 | /// value type the can occur in the program. |
| 44 | enum LegalizeAction { |
| 45 | Legal, // The target natively supports this value type. |
| 46 | Promote, // This should be promoted to the next larger type. |
| 47 | Expand, // This integer type should be broken into smaller pieces. |
| 48 | }; |
| 49 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 50 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 51 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 52 | /// This can be queried with "getTypeAction(VT)". |
| 53 | unsigned ValueTypeActions; |
| 54 | |
| 55 | /// NeedsAnotherIteration - This is set when we expand a large integer |
| 56 | /// operation into smaller integer operations, but the smaller operations are |
| 57 | /// not set. This occurs only rarely in practice, for targets that don't have |
| 58 | /// 32-bit or larger integer registers. |
| 59 | bool NeedsAnotherIteration; |
| 60 | |
| 61 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 62 | /// than one use, this map indicates what regularized operand to use. This |
| 63 | /// allows us to avoid legalizing the same thing more than once. |
| 64 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 65 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 66 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 67 | /// than one use, this map indicates what promoted value to use. This allows |
| 68 | /// us to avoid promoting the same thing more than once. |
| 69 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 70 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 71 | /// ExpandedNodes - For nodes that need to be expanded, and which have more |
| 72 | /// than one use, this map indicates which which operands are the expanded |
| 73 | /// version of the input. This allows us to avoid expanding the same node |
| 74 | /// more than once. |
| 75 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 76 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 77 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
| 78 | bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second; |
| 79 | assert(isNew && "Got into the map somehow?"); |
| 80 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 81 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 82 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 83 | assert(isNew && "Got into the map somehow?"); |
| 84 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 86 | public: |
| 87 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 88 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 89 | |
| 90 | /// Run - While there is still lowering to do, perform a pass over the DAG. |
| 91 | /// Most regularization can be done in a single pass, but targets that require |
| 92 | /// large values to be split into registers multiple times (e.g. i64 -> 4x |
| 93 | /// i16) require iteration for these values (the first iteration will demote |
| 94 | /// to i32, the second will demote to i16). |
| 95 | void Run() { |
| 96 | do { |
| 97 | NeedsAnotherIteration = false; |
| 98 | LegalizeDAG(); |
| 99 | } while (NeedsAnotherIteration); |
| 100 | } |
| 101 | |
| 102 | /// getTypeAction - Return how we should legalize values of this type, either |
| 103 | /// it is already legal or we need to expand it into multiple registers of |
| 104 | /// smaller integer type, or we need to promote it to a larger type. |
| 105 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
| 106 | return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); |
| 107 | } |
| 108 | |
| 109 | /// isTypeLegal - Return true if this type is legal on this target. |
| 110 | /// |
| 111 | bool isTypeLegal(MVT::ValueType VT) const { |
| 112 | return getTypeAction(VT) == Legal; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | void LegalizeDAG(); |
| 117 | |
| 118 | SDOperand LegalizeOp(SDOperand O); |
| 119 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 120 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 122 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, |
| 123 | SDOperand &Hi); |
| 124 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 125 | SDOperand Source); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 126 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 127 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 128 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 129 | SDOperand &Lo, SDOperand &Hi); |
| 130 | void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 131 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 133 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 134 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 135 | } |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 140 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 141 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 142 | ValueTypeActions(TLI.getValueTypeActions()) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 143 | assert(MVT::LAST_VALUETYPE <= 16 && |
| 144 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 147 | void SelectionDAGLegalize::LegalizeDAG() { |
| 148 | SDOperand OldRoot = DAG.getRoot(); |
| 149 | SDOperand NewRoot = LegalizeOp(OldRoot); |
| 150 | DAG.setRoot(NewRoot); |
| 151 | |
| 152 | ExpandedNodes.clear(); |
| 153 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 154 | PromotedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 155 | |
| 156 | // Remove dead nodes now. |
Chris Lattner | 62fd269 | 2005-01-07 21:09:37 +0000 | [diff] [blame] | 157 | DAG.RemoveDeadNodes(OldRoot.Val); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 161 | assert(getTypeAction(Op.getValueType()) == Legal && |
| 162 | "Caller should expand or promote operands that are not legal!"); |
| 163 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 164 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 165 | // register on this target, make sure to expand or promote them. |
| 166 | if (Op.Val->getNumValues() > 1) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 167 | for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i) |
| 168 | switch (getTypeAction(Op.Val->getValueType(i))) { |
| 169 | case Legal: break; // Nothing to do. |
| 170 | case Expand: { |
| 171 | SDOperand T1, T2; |
| 172 | ExpandOp(Op.getValue(i), T1, T2); |
| 173 | assert(LegalizedNodes.count(Op) && |
| 174 | "Expansion didn't add legal operands!"); |
| 175 | return LegalizedNodes[Op]; |
| 176 | } |
| 177 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 178 | PromoteOp(Op.getValue(i)); |
| 179 | assert(LegalizedNodes.count(Op) && |
| 180 | "Expansion didn't add legal operands!"); |
| 181 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 185 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 186 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 187 | |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 188 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 189 | |
| 190 | SDOperand Result = Op; |
| 191 | SDNode *Node = Op.Val; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 192 | |
| 193 | switch (Node->getOpcode()) { |
| 194 | default: |
| 195 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 196 | assert(0 && "Do not know how to legalize this operator!"); |
| 197 | abort(); |
| 198 | case ISD::EntryToken: |
| 199 | case ISD::FrameIndex: |
| 200 | case ISD::GlobalAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 201 | case ISD::ExternalSymbol: |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 202 | case ISD::ConstantPool: // Nothing to do. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 203 | assert(getTypeAction(Node->getValueType(0)) == Legal && |
| 204 | "This must be legal!"); |
| 205 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 206 | case ISD::CopyFromReg: |
| 207 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 208 | if (Tmp1 != Node->getOperand(0)) |
| 209 | Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), |
| 210 | Node->getValueType(0), Tmp1); |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 211 | else |
| 212 | Result = Op.getValue(0); |
| 213 | |
| 214 | // Since CopyFromReg produces two values, make sure to remember that we |
| 215 | // legalized both of them. |
| 216 | AddLegalizedOperand(Op.getValue(0), Result); |
| 217 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 218 | return Result.getValue(Op.ResNo); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 219 | case ISD::ImplicitDef: |
| 220 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 221 | if (Tmp1 != Node->getOperand(0)) |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 222 | Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg()); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 223 | break; |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 224 | case ISD::UNDEF: { |
| 225 | MVT::ValueType VT = Op.getValueType(); |
| 226 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 227 | default: assert(0 && "This action is not supported yet!"); |
| 228 | case TargetLowering::Expand: |
| 229 | case TargetLowering::Promote: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 230 | if (MVT::isInteger(VT)) |
| 231 | Result = DAG.getConstant(0, VT); |
| 232 | else if (MVT::isFloatingPoint(VT)) |
| 233 | Result = DAG.getConstantFP(0, VT); |
| 234 | else |
| 235 | assert(0 && "Unknown value type!"); |
| 236 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 237 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | break; |
| 241 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 242 | case ISD::Constant: |
| 243 | // We know we don't need to expand constants here, constants only have one |
| 244 | // value and we check that it is fine above. |
| 245 | |
| 246 | // FIXME: Maybe we should handle things like targets that don't support full |
| 247 | // 32-bit immediates? |
| 248 | break; |
| 249 | case ISD::ConstantFP: { |
| 250 | // Spill FP immediates to the constant pool if the target cannot directly |
| 251 | // codegen them. Targets often have some immediate values that can be |
| 252 | // efficiently generated into an FP register without a load. We explicitly |
| 253 | // leave these constants as ConstantFP nodes for the target to deal with. |
| 254 | |
| 255 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 256 | |
| 257 | // Check to see if this FP immediate is already legal. |
| 258 | bool isLegal = false; |
| 259 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 260 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 261 | if (CFP->isExactlyValue(*I)) { |
| 262 | isLegal = true; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | if (!isLegal) { |
| 267 | // Otherwise we need to spill the constant to memory. |
| 268 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); |
| 269 | |
| 270 | bool Extend = false; |
| 271 | |
| 272 | // If a FP immediate is precise when represented as a float, we put it |
| 273 | // into the constant pool as a float, even if it's is statically typed |
| 274 | // as a double. |
| 275 | MVT::ValueType VT = CFP->getValueType(0); |
| 276 | bool isDouble = VT == MVT::f64; |
| 277 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 278 | Type::FloatTy, CFP->getValue()); |
Chris Lattner | 99939d3 | 2005-01-28 22:58:25 +0000 | [diff] [blame] | 279 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 280 | // Only do this if the target has a native EXTLOAD instruction from |
| 281 | // f32. |
| 282 | TLI.getOperationAction(ISD::EXTLOAD, |
| 283 | MVT::f32) == TargetLowering::Legal) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 284 | LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); |
| 285 | VT = MVT::f32; |
| 286 | Extend = true; |
| 287 | } |
| 288 | |
| 289 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), |
| 290 | TLI.getPointerTy()); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 291 | if (Extend) { |
| 292 | Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx, |
| 293 | MVT::f32); |
| 294 | } else { |
| 295 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx); |
| 296 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 297 | } |
| 298 | break; |
| 299 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 300 | case ISD::TokenFactor: { |
| 301 | std::vector<SDOperand> Ops; |
| 302 | bool Changed = false; |
| 303 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
Chris Lattner | 1e81b9e | 2005-01-19 19:10:54 +0000 | [diff] [blame] | 304 | SDOperand Op = Node->getOperand(i); |
| 305 | // Fold single-use TokenFactor nodes into this token factor as we go. |
| 306 | if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { |
| 307 | Changed = true; |
| 308 | for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) |
| 309 | Ops.push_back(LegalizeOp(Op.getOperand(j))); |
| 310 | } else { |
| 311 | Ops.push_back(LegalizeOp(Op)); // Legalize the operands |
| 312 | Changed |= Ops[i] != Op; |
| 313 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 314 | } |
| 315 | if (Changed) |
| 316 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 317 | break; |
| 318 | } |
| 319 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 320 | case ISD::ADJCALLSTACKDOWN: |
| 321 | case ISD::ADJCALLSTACKUP: |
| 322 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 323 | // There is no need to legalize the size argument (Operand #1) |
| 324 | if (Tmp1 != Node->getOperand(0)) |
| 325 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, |
| 326 | Node->getOperand(1)); |
| 327 | break; |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 328 | case ISD::DYNAMIC_STACKALLOC: |
| 329 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 330 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 331 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
| 332 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 333 | Tmp3 != Node->getOperand(2)) |
| 334 | Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0), |
| 335 | Tmp1, Tmp2, Tmp3); |
Chris Lattner | 513e52e | 2005-01-09 19:07:54 +0000 | [diff] [blame] | 336 | else |
| 337 | Result = Op.getValue(0); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 338 | |
| 339 | // Since this op produces two values, make sure to remember that we |
| 340 | // legalized both of them. |
| 341 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 342 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 343 | return Result.getValue(Op.ResNo); |
| 344 | |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 345 | case ISD::CALL: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 346 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 347 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 348 | |
| 349 | bool Changed = false; |
| 350 | std::vector<SDOperand> Ops; |
| 351 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { |
| 352 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 353 | Changed |= Ops.back() != Node->getOperand(i); |
| 354 | } |
| 355 | |
| 356 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 357 | std::vector<MVT::ValueType> RetTyVTs; |
| 358 | RetTyVTs.reserve(Node->getNumValues()); |
| 359 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | ebda942 | 2005-01-07 21:34:13 +0000 | [diff] [blame] | 360 | RetTyVTs.push_back(Node->getValueType(i)); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 361 | Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0); |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 362 | } else { |
| 363 | Result = Result.getValue(0); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 365 | // Since calls produce multiple values, make sure to remember that we |
| 366 | // legalized all of them. |
| 367 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 368 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 369 | return Result.getValue(Op.ResNo); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 371 | case ISD::BR: |
| 372 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 373 | if (Tmp1 != Node->getOperand(0)) |
| 374 | Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); |
| 375 | break; |
| 376 | |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 377 | case ISD::BRCOND: |
| 378 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 379 | |
| 380 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 381 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 382 | case Legal: |
| 383 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 384 | break; |
| 385 | case Promote: |
| 386 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 387 | break; |
| 388 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 389 | // Basic block destination (Op#2) is always legal. |
| 390 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 391 | Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, |
| 392 | Node->getOperand(2)); |
| 393 | break; |
Chris Lattner | 411e888 | 2005-04-09 03:30:19 +0000 | [diff] [blame] | 394 | case ISD::BRCONDTWOWAY: |
| 395 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 396 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 397 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 398 | case Legal: |
| 399 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 400 | break; |
| 401 | case Promote: |
| 402 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 403 | break; |
| 404 | } |
| 405 | // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR |
| 406 | // pair. |
| 407 | switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) { |
| 408 | case TargetLowering::Promote: |
| 409 | default: assert(0 && "This action is not supported yet!"); |
| 410 | case TargetLowering::Legal: |
| 411 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { |
| 412 | std::vector<SDOperand> Ops; |
| 413 | Ops.push_back(Tmp1); |
| 414 | Ops.push_back(Tmp2); |
| 415 | Ops.push_back(Node->getOperand(2)); |
| 416 | Ops.push_back(Node->getOperand(3)); |
| 417 | Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops); |
| 418 | } |
| 419 | break; |
| 420 | case TargetLowering::Expand: |
| 421 | Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, |
| 422 | Node->getOperand(2)); |
| 423 | Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3)); |
| 424 | break; |
| 425 | } |
| 426 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 428 | case ISD::LOAD: |
| 429 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 430 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 431 | if (Tmp1 != Node->getOperand(0) || |
| 432 | Tmp2 != Node->getOperand(1)) |
| 433 | Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 434 | else |
| 435 | Result = SDOperand(Node, 0); |
| 436 | |
| 437 | // Since loads produce two values, make sure to remember that we legalized |
| 438 | // both of them. |
| 439 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 440 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 441 | return Result.getValue(Op.ResNo); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 443 | case ISD::EXTLOAD: |
| 444 | case ISD::SEXTLOAD: |
| 445 | case ISD::ZEXTLOAD: |
| 446 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 447 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 448 | if (Tmp1 != Node->getOperand(0) || |
| 449 | Tmp2 != Node->getOperand(1)) |
| 450 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2, |
| 451 | cast<MVTSDNode>(Node)->getExtraValueType()); |
| 452 | else |
| 453 | Result = SDOperand(Node, 0); |
| 454 | |
| 455 | // Since loads produce two values, make sure to remember that we legalized |
| 456 | // both of them. |
| 457 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 458 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 459 | return Result.getValue(Op.ResNo); |
| 460 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 461 | case ISD::EXTRACT_ELEMENT: |
| 462 | // Get both the low and high parts. |
| 463 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 464 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 465 | Result = Tmp2; // 1 -> Hi |
| 466 | else |
| 467 | Result = Tmp1; // 0 -> Lo |
| 468 | break; |
| 469 | |
| 470 | case ISD::CopyToReg: |
| 471 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 472 | |
| 473 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 474 | case Legal: |
| 475 | // Legalize the incoming value (must be legal). |
| 476 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 477 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 478 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 479 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 480 | case Promote: |
| 481 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 482 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
| 483 | break; |
| 484 | case Expand: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 485 | SDOperand Lo, Hi; |
| 486 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 487 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 488 | Lo = DAG.getCopyToReg(Tmp1, Lo, Reg); |
| 489 | Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1); |
| 490 | // Note that the copytoreg nodes are independent of each other. |
| 491 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 492 | assert(isTypeLegal(Result.getValueType()) && |
| 493 | "Cannot expand multiple times yet (i64 -> i16)"); |
| 494 | break; |
| 495 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | case ISD::RET: |
| 499 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 500 | switch (Node->getNumOperands()) { |
| 501 | case 2: // ret val |
| 502 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 503 | case Legal: |
| 504 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 505 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 506 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 507 | break; |
| 508 | case Expand: { |
| 509 | SDOperand Lo, Hi; |
| 510 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 511 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
| 512 | break; |
| 513 | } |
| 514 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 515 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 516 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 517 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 518 | } |
| 519 | break; |
| 520 | case 1: // ret void |
| 521 | if (Tmp1 != Node->getOperand(0)) |
| 522 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); |
| 523 | break; |
| 524 | default: { // ret <values> |
| 525 | std::vector<SDOperand> NewValues; |
| 526 | NewValues.push_back(Tmp1); |
| 527 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 528 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 529 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 530 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 531 | break; |
| 532 | case Expand: { |
| 533 | SDOperand Lo, Hi; |
| 534 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 535 | NewValues.push_back(Lo); |
| 536 | NewValues.push_back(Hi); |
| 537 | break; |
| 538 | } |
| 539 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 540 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 541 | } |
| 542 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | break; |
| 547 | case ISD::STORE: |
| 548 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 549 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 550 | |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 551 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 552 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 553 | if (CFP->getValueType(0) == MVT::f32) { |
| 554 | union { |
| 555 | unsigned I; |
| 556 | float F; |
| 557 | } V; |
| 558 | V.F = CFP->getValue(); |
| 559 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
| 560 | DAG.getConstant(V.I, MVT::i32), Tmp2); |
| 561 | } else { |
| 562 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 563 | union { |
| 564 | uint64_t I; |
| 565 | double F; |
| 566 | } V; |
| 567 | V.F = CFP->getValue(); |
| 568 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
| 569 | DAG.getConstant(V.I, MVT::i64), Tmp2); |
| 570 | } |
Chris Lattner | 84734ce | 2005-02-22 07:23:39 +0000 | [diff] [blame] | 571 | Node = Result.Val; |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 574 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 575 | case Legal: { |
| 576 | SDOperand Val = LegalizeOp(Node->getOperand(1)); |
| 577 | if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || |
| 578 | Tmp2 != Node->getOperand(2)) |
| 579 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2); |
| 580 | break; |
| 581 | } |
| 582 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 583 | // Truncate the value and store the result. |
| 584 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 585 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
| 586 | Node->getOperand(1).getValueType()); |
| 587 | break; |
| 588 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 589 | case Expand: |
| 590 | SDOperand Lo, Hi; |
| 591 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 592 | |
| 593 | if (!TLI.isLittleEndian()) |
| 594 | std::swap(Lo, Hi); |
| 595 | |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 596 | Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 597 | |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 598 | unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 599 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 600 | getIntPtrConstant(IncrementSize)); |
| 601 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 602 | "Pointers must be legal!"); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 603 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2); |
| 604 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 605 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 606 | } |
| 607 | break; |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 608 | case ISD::PCMARKER: |
| 609 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 610 | if (Tmp1 != Node->getOperand(0)) |
| 611 | Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 612 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 613 | case ISD::TRUNCSTORE: |
| 614 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 615 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 616 | |
| 617 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 618 | case Legal: |
| 619 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 620 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 621 | Tmp3 != Node->getOperand(2)) |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 622 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 623 | cast<MVTSDNode>(Node)->getExtraValueType()); |
| 624 | break; |
| 625 | case Promote: |
| 626 | case Expand: |
| 627 | assert(0 && "Cannot handle illegal TRUNCSTORE yet!"); |
| 628 | } |
| 629 | break; |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 630 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 631 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 632 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 633 | case Legal: |
| 634 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 635 | break; |
| 636 | case Promote: |
| 637 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 638 | break; |
| 639 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 640 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 641 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 642 | |
| 643 | switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) { |
| 644 | default: assert(0 && "This action is not supported yet!"); |
| 645 | case TargetLowering::Legal: |
| 646 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 647 | Tmp3 != Node->getOperand(2)) |
| 648 | Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), |
| 649 | Tmp1, Tmp2, Tmp3); |
| 650 | break; |
| 651 | case TargetLowering::Promote: { |
| 652 | MVT::ValueType NVT = |
| 653 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 654 | unsigned ExtOp, TruncOp; |
| 655 | if (MVT::isInteger(Tmp2.getValueType())) { |
| 656 | ExtOp = ISD::ZERO_EXTEND; |
| 657 | TruncOp = ISD::TRUNCATE; |
| 658 | } else { |
| 659 | ExtOp = ISD::FP_EXTEND; |
| 660 | TruncOp = ISD::FP_ROUND; |
| 661 | } |
| 662 | // Promote each of the values to the new type. |
| 663 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 664 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 665 | // Perform the larger operation, then round down. |
| 666 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 667 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 668 | break; |
| 669 | } |
| 670 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 671 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 672 | case ISD::SETCC: |
| 673 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 674 | case Legal: |
| 675 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 676 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 677 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 678 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 679 | Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 680 | break; |
| 681 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 682 | Tmp1 = PromoteOp(Node->getOperand(0)); // LHS |
| 683 | Tmp2 = PromoteOp(Node->getOperand(1)); // RHS |
| 684 | |
| 685 | // If this is an FP compare, the operands have already been extended. |
| 686 | if (MVT::isInteger(Node->getOperand(0).getValueType())) { |
| 687 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 688 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 689 | |
| 690 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 691 | // that we could insert sign extends for ALL conditions, but zero extend |
| 692 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 693 | // it. |
| 694 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 695 | default: assert(0 && "Unknown integer comparison!"); |
| 696 | case ISD::SETEQ: |
| 697 | case ISD::SETNE: |
| 698 | case ISD::SETUGE: |
| 699 | case ISD::SETUGT: |
| 700 | case ISD::SETULE: |
| 701 | case ISD::SETULT: |
| 702 | // ALL of these operations will work if we either sign or zero extend |
| 703 | // the operands (including the unsigned comparisons!). Zero extend is |
| 704 | // usually a simpler/cheaper operation, so prefer it. |
| 705 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); |
| 706 | Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); |
| 707 | break; |
| 708 | case ISD::SETGE: |
| 709 | case ISD::SETGT: |
| 710 | case ISD::SETLT: |
| 711 | case ISD::SETLE: |
| 712 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
| 713 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | } |
| 718 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 719 | Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 720 | break; |
| 721 | case Expand: |
| 722 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 723 | ExpandOp(Node->getOperand(0), LHSLo, LHSHi); |
| 724 | ExpandOp(Node->getOperand(1), RHSLo, RHSHi); |
| 725 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 726 | case ISD::SETEQ: |
| 727 | case ISD::SETNE: |
| 728 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 729 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 730 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 731 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 732 | Node->getValueType(0), Tmp1, |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 733 | DAG.getConstant(0, Tmp1.getValueType())); |
| 734 | break; |
| 735 | default: |
| 736 | // FIXME: This generated code sucks. |
| 737 | ISD::CondCode LowCC; |
| 738 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 739 | default: assert(0 && "Unknown integer setcc!"); |
| 740 | case ISD::SETLT: |
| 741 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 742 | case ISD::SETGT: |
| 743 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 744 | case ISD::SETLE: |
| 745 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 746 | case ISD::SETGE: |
| 747 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 748 | } |
| 749 | |
| 750 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 751 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 752 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 753 | |
| 754 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 755 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 756 | Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 757 | Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 758 | Node->getValueType(0), LHSHi, RHSHi); |
| 759 | Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi); |
| 760 | Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 761 | Result, Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | } |
| 765 | break; |
| 766 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 767 | case ISD::MEMSET: |
| 768 | case ISD::MEMCPY: |
| 769 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 770 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 771 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 772 | |
| 773 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 774 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 775 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 776 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 777 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 778 | break; |
| 779 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 780 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | } else { |
| 784 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
| 785 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 786 | |
| 787 | SDOperand Tmp4; |
| 788 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 789 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 790 | case Legal: |
| 791 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 792 | break; |
| 793 | case Promote: |
| 794 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | |
| 798 | SDOperand Tmp5; |
| 799 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 800 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 801 | case Legal: |
| 802 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 803 | break; |
| 804 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 805 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 806 | break; |
| 807 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 808 | |
| 809 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 810 | default: assert(0 && "This action not implemented for this operation!"); |
| 811 | case TargetLowering::Legal: |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 812 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 813 | Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || |
| 814 | Tmp5 != Node->getOperand(4)) { |
| 815 | std::vector<SDOperand> Ops; |
| 816 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 817 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 818 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
| 819 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 820 | break; |
| 821 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 822 | // Otherwise, the target does not support this operation. Lower the |
| 823 | // operation to an explicit libcall as appropriate. |
| 824 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 825 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 826 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 827 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 828 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 829 | if (Node->getOpcode() == ISD::MEMSET) { |
| 830 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 831 | // Extend the ubyte argument to be an int value for the call. |
| 832 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
| 833 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 834 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 835 | |
| 836 | FnName = "memset"; |
| 837 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 838 | Node->getOpcode() == ISD::MEMMOVE) { |
| 839 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 840 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 841 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 842 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 843 | } else { |
| 844 | assert(0 && "Unknown op!"); |
| 845 | } |
| 846 | std::pair<SDOperand,SDOperand> CallResult = |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 847 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 848 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
| 849 | Result = LegalizeOp(CallResult.second); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 850 | break; |
| 851 | } |
| 852 | case TargetLowering::Custom: |
| 853 | std::vector<SDOperand> Ops; |
| 854 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 855 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 856 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
| 857 | Result = TLI.LowerOperation(Result); |
| 858 | Result = LegalizeOp(Result); |
| 859 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 860 | } |
| 861 | break; |
| 862 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 863 | case ISD::ADD_PARTS: |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 864 | case ISD::SUB_PARTS: |
| 865 | case ISD::SHL_PARTS: |
| 866 | case ISD::SRA_PARTS: |
| 867 | case ISD::SRL_PARTS: { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 868 | std::vector<SDOperand> Ops; |
| 869 | bool Changed = false; |
| 870 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 871 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 872 | Changed |= Ops.back() != Node->getOperand(i); |
| 873 | } |
| 874 | if (Changed) |
| 875 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 876 | |
| 877 | // Since these produce multiple values, make sure to remember that we |
| 878 | // legalized all of them. |
| 879 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 880 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 881 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 882 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 883 | |
| 884 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 885 | case ISD::ADD: |
| 886 | case ISD::SUB: |
| 887 | case ISD::MUL: |
| 888 | case ISD::UDIV: |
| 889 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 890 | case ISD::AND: |
| 891 | case ISD::OR: |
| 892 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 893 | case ISD::SHL: |
| 894 | case ISD::SRL: |
| 895 | case ISD::SRA: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 896 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 897 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 898 | if (Tmp1 != Node->getOperand(0) || |
| 899 | Tmp2 != Node->getOperand(1)) |
| 900 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); |
| 901 | break; |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 902 | |
| 903 | case ISD::UREM: |
| 904 | case ISD::SREM: |
| 905 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 906 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 907 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 908 | case TargetLowering::Legal: |
| 909 | if (Tmp1 != Node->getOperand(0) || |
| 910 | Tmp2 != Node->getOperand(1)) |
| 911 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
| 912 | Tmp2); |
| 913 | break; |
| 914 | case TargetLowering::Promote: |
| 915 | case TargetLowering::Custom: |
| 916 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 917 | case TargetLowering::Expand: { |
| 918 | MVT::ValueType VT = Node->getValueType(0); |
| 919 | unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
| 920 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 921 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 922 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 923 | } |
| 924 | break; |
| 925 | } |
| 926 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 927 | |
| 928 | // Unary operators |
| 929 | case ISD::FABS: |
| 930 | case ISD::FNEG: |
| 931 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 932 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 933 | case TargetLowering::Legal: |
| 934 | if (Tmp1 != Node->getOperand(0)) |
| 935 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 936 | break; |
| 937 | case TargetLowering::Promote: |
| 938 | case TargetLowering::Custom: |
| 939 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 940 | case TargetLowering::Expand: |
| 941 | if (Node->getOpcode() == ISD::FNEG) { |
| 942 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 943 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
| 944 | Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0), |
| 945 | Tmp2, Tmp1)); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 946 | } else if (Node->getOpcode() == ISD::FABS) { |
| 947 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 948 | MVT::ValueType VT = Node->getValueType(0); |
| 949 | Tmp2 = DAG.getConstantFP(0.0, VT); |
| 950 | Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2); |
| 951 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 952 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
| 953 | Result = LegalizeOp(Result); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 954 | } else { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 955 | assert(0 && "Unreachable!"); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 956 | } |
| 957 | break; |
| 958 | } |
| 959 | break; |
| 960 | |
| 961 | // Conversion operators. The source and destination have different types. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 962 | case ISD::ZERO_EXTEND: |
| 963 | case ISD::SIGN_EXTEND: |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 964 | case ISD::TRUNCATE: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 965 | case ISD::FP_EXTEND: |
| 966 | case ISD::FP_ROUND: |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 967 | case ISD::FP_TO_SINT: |
| 968 | case ISD::FP_TO_UINT: |
| 969 | case ISD::SINT_TO_FP: |
| 970 | case ISD::UINT_TO_FP: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 971 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 972 | case Legal: |
| 973 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 974 | if (Tmp1 != Node->getOperand(0)) |
| 975 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 976 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 977 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 978 | if (Node->getOpcode() == ISD::SINT_TO_FP || |
| 979 | Node->getOpcode() == ISD::UINT_TO_FP) { |
| 980 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 981 | Node->getValueType(0), Node->getOperand(0)); |
| 982 | Result = LegalizeOp(Result); |
| 983 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 984 | } else if (Node->getOpcode() == ISD::TRUNCATE) { |
| 985 | // In the expand case, we must be dealing with a truncate, because |
| 986 | // otherwise the result would be larger than the source. |
| 987 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 988 | |
| 989 | // Since the result is legal, we should just be able to truncate the low |
| 990 | // part of the source. |
| 991 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 992 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 994 | assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 996 | case Promote: |
| 997 | switch (Node->getOpcode()) { |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 998 | case ISD::ZERO_EXTEND: |
| 999 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1000 | // NOTE: Any extend would work here... |
| 1001 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
| 1002 | Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(), |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1003 | Result, Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1004 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1005 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1006 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1007 | // NOTE: Any extend would work here... |
Chris Lattner | d5d5682 | 2005-01-18 21:57:59 +0000 | [diff] [blame] | 1008 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1009 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1010 | Result, Node->getOperand(0).getValueType()); |
| 1011 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1012 | case ISD::TRUNCATE: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1013 | Result = PromoteOp(Node->getOperand(0)); |
| 1014 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 1015 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1016 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1017 | Result = PromoteOp(Node->getOperand(0)); |
| 1018 | if (Result.getValueType() != Op.getValueType()) |
| 1019 | // Dynamically dead while we have only 2 FP types. |
| 1020 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 1021 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1022 | case ISD::FP_ROUND: |
| 1023 | case ISD::FP_TO_SINT: |
| 1024 | case ISD::FP_TO_UINT: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1025 | Result = PromoteOp(Node->getOperand(0)); |
| 1026 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 1027 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1028 | case ISD::SINT_TO_FP: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1029 | Result = PromoteOp(Node->getOperand(0)); |
| 1030 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1031 | Result, Node->getOperand(0).getValueType()); |
| 1032 | Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); |
| 1033 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1034 | case ISD::UINT_TO_FP: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1035 | Result = PromoteOp(Node->getOperand(0)); |
| 1036 | Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), |
| 1037 | Result, Node->getOperand(0).getValueType()); |
| 1038 | Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); |
| 1039 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1041 | } |
| 1042 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1043 | case ISD::FP_ROUND_INREG: |
| 1044 | case ISD::SIGN_EXTEND_INREG: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1045 | case ISD::ZERO_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1046 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1047 | MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType(); |
| 1048 | |
| 1049 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 1050 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1051 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 1052 | default: assert(0 && "This action not supported for this op yet!"); |
| 1053 | case TargetLowering::Legal: |
| 1054 | if (Tmp1 != Node->getOperand(0)) |
| 1055 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
| 1056 | ExtraVT); |
| 1057 | break; |
| 1058 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1059 | // If this is an integer extend and shifts are supported, do that. |
| 1060 | if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) { |
| 1061 | // NOTE: we could fall back on load/store here too for targets without |
| 1062 | // AND. However, it is doubtful that any exist. |
| 1063 | // AND out the appropriate bits. |
| 1064 | SDOperand Mask = |
| 1065 | DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1, |
| 1066 | Node->getValueType(0)); |
| 1067 | Result = DAG.getNode(ISD::AND, Node->getValueType(0), |
| 1068 | Node->getOperand(0), Mask); |
| 1069 | } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
| 1070 | // NOTE: we could fall back on load/store here too for targets without |
| 1071 | // SAR. However, it is doubtful that any exist. |
| 1072 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 1073 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 1074 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1075 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 1076 | Node->getOperand(0), ShiftCst); |
| 1077 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 1078 | Result, ShiftCst); |
| 1079 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 1080 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 1081 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 1082 | |
| 1083 | // NOTE: there is a choice here between constantly creating new stack |
| 1084 | // slots and always reusing the same one. We currently always create |
| 1085 | // new ones, as reuse may inhibit scheduling. |
| 1086 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 1087 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 1088 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 1089 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1090 | int SSFI = |
| 1091 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 1092 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 1093 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
| 1094 | Node->getOperand(0), StackSlot, ExtraVT); |
| 1095 | Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), |
| 1096 | Result, StackSlot, ExtraVT); |
| 1097 | } else { |
| 1098 | assert(0 && "Unknown op"); |
| 1099 | } |
| 1100 | Result = LegalizeOp(Result); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1101 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1102 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1103 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1104 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1105 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1106 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 1107 | if (!Op.Val->hasOneUse()) |
| 1108 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1109 | |
| 1110 | return Result; |
| 1111 | } |
| 1112 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1113 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 1114 | /// promote it to compute the value into a larger type. The produced value will |
| 1115 | /// have the correct bits for the low portion of the register, but no guarantee |
| 1116 | /// 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] | 1117 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 1118 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 1119 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1120 | assert(getTypeAction(VT) == Promote && |
| 1121 | "Caller should expand or legalize operands that are not promotable!"); |
| 1122 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 1123 | "Cannot promote to smaller type!"); |
| 1124 | |
| 1125 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 1126 | if (I != PromotedNodes.end()) return I->second; |
| 1127 | |
| 1128 | SDOperand Tmp1, Tmp2, Tmp3; |
| 1129 | |
| 1130 | SDOperand Result; |
| 1131 | SDNode *Node = Op.Val; |
| 1132 | |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1133 | // Promotion needs an optimization step to clean up after it, and is not |
| 1134 | // careful to avoid operations the target does not support. Make sure that |
| 1135 | // all generated operations are legalized in the next iteration. |
| 1136 | NeedsAnotherIteration = true; |
| 1137 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1138 | switch (Node->getOpcode()) { |
| 1139 | default: |
| 1140 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 1141 | assert(0 && "Do not know how to promote this operator!"); |
| 1142 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1143 | case ISD::UNDEF: |
| 1144 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 1145 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1146 | case ISD::Constant: |
| 1147 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
| 1148 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 1149 | break; |
| 1150 | case ISD::ConstantFP: |
| 1151 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 1152 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 1153 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 1154 | case ISD::CopyFromReg: |
| 1155 | Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT, |
| 1156 | Node->getOperand(0)); |
| 1157 | // Remember that we legalized the chain. |
| 1158 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1159 | break; |
| 1160 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 1161 | case ISD::SETCC: |
| 1162 | assert(getTypeAction(TLI.getSetCCResultTy()) == Legal && |
| 1163 | "SetCC type is not legal??"); |
| 1164 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 1165 | TLI.getSetCCResultTy(), Node->getOperand(0), |
| 1166 | Node->getOperand(1)); |
| 1167 | Result = LegalizeOp(Result); |
| 1168 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1169 | |
| 1170 | case ISD::TRUNCATE: |
| 1171 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1172 | case Legal: |
| 1173 | Result = LegalizeOp(Node->getOperand(0)); |
| 1174 | assert(Result.getValueType() >= NVT && |
| 1175 | "This truncation doesn't make sense!"); |
| 1176 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 1177 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 1178 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 1179 | case Promote: |
| 1180 | // The truncation is not required, because we don't guarantee anything |
| 1181 | // about high bits anyway. |
| 1182 | Result = PromoteOp(Node->getOperand(0)); |
| 1183 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1184 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 1185 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1186 | // Truncate the low part of the expanded value to the result type |
| 1187 | Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1188 | } |
| 1189 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1190 | case ISD::SIGN_EXTEND: |
| 1191 | case ISD::ZERO_EXTEND: |
| 1192 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1193 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 1194 | case Legal: |
| 1195 | // Input is legal? Just do extend all the way to the larger type. |
| 1196 | Result = LegalizeOp(Node->getOperand(0)); |
| 1197 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
| 1198 | break; |
| 1199 | case Promote: |
| 1200 | // Promote the reg if it's smaller. |
| 1201 | Result = PromoteOp(Node->getOperand(0)); |
| 1202 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 1203 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 1204 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 1205 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1206 | else |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 1207 | Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, |
| 1208 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | } |
| 1211 | break; |
| 1212 | |
| 1213 | case ISD::FP_EXTEND: |
| 1214 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 1215 | case ISD::FP_ROUND: |
| 1216 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1217 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 1218 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 1219 | case Legal: |
| 1220 | // Input is legal? Do an FP_ROUND_INREG. |
| 1221 | Result = LegalizeOp(Node->getOperand(0)); |
| 1222 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1223 | break; |
| 1224 | } |
| 1225 | break; |
| 1226 | |
| 1227 | case ISD::SINT_TO_FP: |
| 1228 | case ISD::UINT_TO_FP: |
| 1229 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1230 | case Legal: |
| 1231 | Result = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1232 | // No extra round required here. |
| 1233 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | |
| 1236 | case Promote: |
| 1237 | Result = PromoteOp(Node->getOperand(0)); |
| 1238 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 1239 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1240 | Result, Node->getOperand(0).getValueType()); |
| 1241 | else |
| 1242 | Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(), |
| 1243 | Result, Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1244 | // No extra round required here. |
| 1245 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1248 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 1249 | Node->getOperand(0)); |
| 1250 | Result = LegalizeOp(Result); |
| 1251 | |
| 1252 | // Round if we cannot tolerate excess precision. |
| 1253 | if (NoExcessFPPrecision) |
| 1254 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1255 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1256 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | |
| 1259 | case ISD::FP_TO_SINT: |
| 1260 | case ISD::FP_TO_UINT: |
| 1261 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1262 | case Legal: |
| 1263 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1264 | break; |
| 1265 | case Promote: |
| 1266 | // The input result is prerounded, so we don't have to do anything |
| 1267 | // special. |
| 1268 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1269 | break; |
| 1270 | case Expand: |
| 1271 | assert(0 && "not implemented"); |
| 1272 | } |
| 1273 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1274 | break; |
| 1275 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1276 | case ISD::FABS: |
| 1277 | case ISD::FNEG: |
| 1278 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1279 | assert(Tmp1.getValueType() == NVT); |
| 1280 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1281 | // NOTE: we do not have to do any extra rounding here for |
| 1282 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 1283 | // precision, and these operations don't modify precision at all. |
| 1284 | break; |
| 1285 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1286 | case ISD::AND: |
| 1287 | case ISD::OR: |
| 1288 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1289 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1290 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1291 | case ISD::MUL: |
| 1292 | // The input may have strange things in the top bits of the registers, but |
| 1293 | // these operations don't care. They may have wierd bits going out, but |
| 1294 | // that too is okay if they are integer operations. |
| 1295 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1296 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1297 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 1298 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1299 | |
| 1300 | // However, if this is a floating point operation, they will give excess |
| 1301 | // precision that we may not be able to tolerate. If we DO allow excess |
| 1302 | // precision, just leave it, otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1303 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 1304 | // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1305 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
| 1306 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1307 | break; |
| 1308 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1309 | case ISD::SDIV: |
| 1310 | case ISD::SREM: |
| 1311 | // These operators require that their input be sign extended. |
| 1312 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1313 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1314 | if (MVT::isInteger(NVT)) { |
| 1315 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
Chris Lattner | ff3e50c | 2005-01-16 00:17:42 +0000 | [diff] [blame] | 1316 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1317 | } |
| 1318 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1319 | |
| 1320 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 1321 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
| 1322 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1323 | break; |
| 1324 | |
| 1325 | case ISD::UDIV: |
| 1326 | case ISD::UREM: |
| 1327 | // These operators require that their input be zero extended. |
| 1328 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1329 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1330 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
| 1331 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); |
Chris Lattner | ff3e50c | 2005-01-16 00:17:42 +0000 | [diff] [blame] | 1332 | Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1333 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1334 | break; |
| 1335 | |
| 1336 | case ISD::SHL: |
| 1337 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1338 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1339 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2); |
| 1340 | break; |
| 1341 | case ISD::SRA: |
| 1342 | // The input value must be properly sign extended. |
| 1343 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1344 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
| 1345 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1346 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2); |
| 1347 | break; |
| 1348 | case ISD::SRL: |
| 1349 | // The input value must be properly zero extended. |
| 1350 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1351 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT); |
| 1352 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1353 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2); |
| 1354 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1355 | case ISD::LOAD: |
| 1356 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1357 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 1358 | Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT); |
| 1359 | |
| 1360 | // Remember that we legalized the chain. |
| 1361 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1362 | break; |
| 1363 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1364 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1365 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1366 | case Legal: |
| 1367 | Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition. |
| 1368 | break; |
| 1369 | case Promote: |
| 1370 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1371 | break; |
| 1372 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1373 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 1374 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
| 1375 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3); |
| 1376 | break; |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1377 | case ISD::CALL: { |
| 1378 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1379 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 1380 | |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1381 | std::vector<SDOperand> Ops; |
| 1382 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) |
| 1383 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1384 | |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1385 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 1386 | "Can only promote single result calls"); |
| 1387 | std::vector<MVT::ValueType> RetTyVTs; |
| 1388 | RetTyVTs.reserve(2); |
| 1389 | RetTyVTs.push_back(NVT); |
| 1390 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1391 | SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops); |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1392 | Result = SDOperand(NC, 0); |
| 1393 | |
| 1394 | // Insert the new chain mapping. |
| 1395 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1396 | break; |
| 1397 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | assert(Result.Val && "Didn't set a result!"); |
| 1401 | AddPromotedOperand(Op, Result); |
| 1402 | return Result; |
| 1403 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1405 | /// ExpandAddSub - Find a clever way to expand this add operation into |
| 1406 | /// subcomponents. |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1407 | void SelectionDAGLegalize:: |
| 1408 | ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, |
| 1409 | SDOperand &Lo, SDOperand &Hi) { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1410 | // Expand the subcomponents. |
| 1411 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 1412 | ExpandOp(LHS, LHSL, LHSH); |
| 1413 | ExpandOp(RHS, RHSL, RHSH); |
| 1414 | |
| 1415 | // Convert this add to the appropriate ADDC pair. The low part has no carry |
| 1416 | // in. |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1417 | std::vector<SDOperand> Ops; |
| 1418 | Ops.push_back(LHSL); |
| 1419 | Ops.push_back(LHSH); |
| 1420 | Ops.push_back(RHSL); |
| 1421 | Ops.push_back(RHSH); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1422 | Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1423 | Hi = Lo.getValue(1); |
| 1424 | } |
| 1425 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1426 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 1427 | SDOperand Op, SDOperand Amt, |
| 1428 | SDOperand &Lo, SDOperand &Hi) { |
| 1429 | // Expand the subcomponents. |
| 1430 | SDOperand LHSL, LHSH; |
| 1431 | ExpandOp(Op, LHSL, LHSH); |
| 1432 | |
| 1433 | std::vector<SDOperand> Ops; |
| 1434 | Ops.push_back(LHSL); |
| 1435 | Ops.push_back(LHSH); |
| 1436 | Ops.push_back(Amt); |
| 1437 | Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); |
| 1438 | Hi = Lo.getValue(1); |
| 1439 | } |
| 1440 | |
| 1441 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1442 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 1443 | /// smaller elements. If we can't find a way that is more efficient than a |
| 1444 | /// libcall on this target, return false. Otherwise, return true with the |
| 1445 | /// low-parts expanded into Lo and Hi. |
| 1446 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 1447 | SDOperand &Lo, SDOperand &Hi) { |
| 1448 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 1449 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1451 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1452 | SDOperand ShAmt = LegalizeOp(Amt); |
| 1453 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 1454 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 1455 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 1456 | |
| 1457 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 1458 | // and are disabled. |
| 1459 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 1460 | unsigned Cst = CN->getValue(); |
| 1461 | // Expand the incoming operand to be shifted, so that we have its parts |
| 1462 | SDOperand InL, InH; |
| 1463 | ExpandOp(Op, InL, InH); |
| 1464 | switch(Opc) { |
| 1465 | case ISD::SHL: |
| 1466 | if (Cst > VTBits) { |
| 1467 | Lo = DAG.getConstant(0, NVT); |
| 1468 | Hi = DAG.getConstant(0, NVT); |
| 1469 | } else if (Cst > NVTBits) { |
| 1470 | Lo = DAG.getConstant(0, NVT); |
| 1471 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 1472 | } else { |
| 1473 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 1474 | Hi = DAG.getNode(ISD::OR, NVT, |
| 1475 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 1476 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1477 | } |
| 1478 | return true; |
| 1479 | case ISD::SRL: |
| 1480 | if (Cst > VTBits) { |
| 1481 | Lo = DAG.getConstant(0, NVT); |
| 1482 | Hi = DAG.getConstant(0, NVT); |
| 1483 | } else if (Cst > NVTBits) { |
| 1484 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 1485 | Hi = DAG.getConstant(0, NVT); |
| 1486 | } else { |
| 1487 | Lo = DAG.getNode(ISD::OR, NVT, |
| 1488 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 1489 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1490 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 1491 | } |
| 1492 | return true; |
| 1493 | case ISD::SRA: |
| 1494 | if (Cst > VTBits) { |
| 1495 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
| 1496 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1497 | } else if (Cst > NVTBits) { |
| 1498 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
| 1499 | DAG.getConstant(Cst-NVTBits, ShTy)); |
| 1500 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
| 1501 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1502 | } else { |
| 1503 | Lo = DAG.getNode(ISD::OR, NVT, |
| 1504 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 1505 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1506 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 1507 | } |
| 1508 | return true; |
| 1509 | } |
| 1510 | } |
| 1511 | // FIXME: The following code for expanding shifts using ISD::SELECT is buggy, |
| 1512 | // so disable it for now. Currently targets are handling this via SHL_PARTS |
| 1513 | // and friends. |
| 1514 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1515 | |
| 1516 | // If we have an efficient select operation (or if the selects will all fold |
| 1517 | // away), lower to some complex code, otherwise just emit the libcall. |
| 1518 | if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal && |
| 1519 | !isa<ConstantSDNode>(Amt)) |
| 1520 | return false; |
| 1521 | |
| 1522 | SDOperand InL, InH; |
| 1523 | ExpandOp(Op, InL, InH); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1524 | SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt |
| 1525 | DAG.getConstant(NVTBits, ShTy), ShAmt); |
| 1526 | |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1527 | // Compare the unmasked shift amount against 32. |
| 1528 | SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt, |
| 1529 | DAG.getConstant(NVTBits, ShTy)); |
| 1530 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1531 | if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) { |
| 1532 | ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31 |
| 1533 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1534 | NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31 |
| 1535 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1536 | } |
| 1537 | |
| 1538 | if (Opc == ISD::SHL) { |
| 1539 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt) |
| 1540 | DAG.getNode(ISD::SHL, NVT, InH, ShAmt), |
| 1541 | DAG.getNode(ISD::SRL, NVT, InL, NAmt)); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1542 | SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31 |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1544 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
| 1545 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2); |
| 1546 | } else { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1547 | SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT, |
| 1548 | DAG.getSetCC(ISD::SETEQ, |
| 1549 | TLI.getSetCCResultTy(), NAmt, |
| 1550 | DAG.getConstant(32, ShTy)), |
| 1551 | DAG.getConstant(0, NVT), |
| 1552 | DAG.getNode(ISD::SHL, NVT, InH, NAmt)); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1553 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1554 | HiLoPart, |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1555 | DAG.getNode(ISD::SRL, NVT, InL, ShAmt)); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1556 | SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31 |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1557 | |
| 1558 | SDOperand HiPart; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1559 | if (Opc == ISD::SRA) |
| 1560 | HiPart = DAG.getNode(ISD::SRA, NVT, InH, |
| 1561 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1562 | else |
| 1563 | HiPart = DAG.getConstant(0, NVT); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1564 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1565 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1566 | } |
| 1567 | return true; |
| 1568 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1570 | /// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest |
| 1571 | /// NodeDepth) node that is an AdjCallStackDown operation and occurs later than |
| 1572 | /// Found. |
| 1573 | static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) { |
| 1574 | if (Node->getNodeDepth() <= Found->getNodeDepth()) return; |
| 1575 | |
| 1576 | // If we found an ADJCALLSTACKDOWN, we already know this node occurs later |
| 1577 | // than the Found node. Just remember this node and return. |
| 1578 | if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) { |
| 1579 | Found = Node; |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 1584 | assert(Node->getNumOperands() != 0 && |
| 1585 | "All leaves should have depth equal to the entry node!"); |
| 1586 | for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i) |
| 1587 | FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found); |
| 1588 | |
| 1589 | // Tail recurse for the last iteration. |
| 1590 | FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val, |
| 1591 | Found); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | /// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest |
| 1596 | /// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent |
| 1597 | /// than Found. |
| 1598 | static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) { |
| 1599 | if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return; |
| 1600 | |
| 1601 | // If we found an ADJCALLSTACKUP, we already know this node occurs earlier |
| 1602 | // than the Found node. Just remember this node and return. |
| 1603 | if (Node->getOpcode() == ISD::ADJCALLSTACKUP) { |
| 1604 | Found = Node; |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 1609 | SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 1610 | if (UI == E) return; |
| 1611 | for (--E; UI != E; ++UI) |
| 1612 | FindEarliestAdjCallStackUp(*UI, Found); |
| 1613 | |
| 1614 | // Tail recurse for the last iteration. |
| 1615 | FindEarliestAdjCallStackUp(*UI, Found); |
| 1616 | } |
| 1617 | |
| 1618 | /// FindAdjCallStackUp - Given a chained node that is part of a call sequence, |
| 1619 | /// find the ADJCALLSTACKUP node that terminates the call sequence. |
| 1620 | static SDNode *FindAdjCallStackUp(SDNode *Node) { |
| 1621 | if (Node->getOpcode() == ISD::ADJCALLSTACKUP) |
| 1622 | return Node; |
Chris Lattner | f4b4579 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 1623 | if (Node->use_empty()) |
| 1624 | return 0; // No adjcallstackup |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (Node->hasOneUse()) // Simple case, only has one user to check. |
| 1627 | return FindAdjCallStackUp(*Node->use_begin()); |
| 1628 | |
| 1629 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 1630 | assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!"); |
| 1631 | |
| 1632 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 1633 | E = Node->use_end(); ; ++UI) { |
| 1634 | assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!"); |
| 1635 | |
| 1636 | // Make sure to only follow users of our token chain. |
| 1637 | SDNode *User = *UI; |
| 1638 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 1639 | if (User->getOperand(i) == TheChain) |
| 1640 | return FindAdjCallStackUp(User); |
| 1641 | } |
| 1642 | assert(0 && "Unreachable"); |
| 1643 | abort(); |
| 1644 | } |
| 1645 | |
| 1646 | /// FindInputOutputChains - If we are replacing an operation with a call we need |
| 1647 | /// to find the call that occurs before and the call that occurs after it to |
| 1648 | /// properly serialize the calls in the block. |
| 1649 | static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain, |
| 1650 | SDOperand Entry) { |
| 1651 | SDNode *LatestAdjCallStackDown = Entry.Val; |
| 1652 | FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown); |
| 1653 | //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n"; |
| 1654 | |
| 1655 | SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown); |
| 1656 | |
| 1657 | |
| 1658 | SDNode *EarliestAdjCallStackUp = 0; |
| 1659 | FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp); |
| 1660 | |
| 1661 | if (EarliestAdjCallStackUp) { |
| 1662 | //std::cerr << "Found node: "; |
| 1663 | //EarliestAdjCallStackUp->dump(); std::cerr <<"\n"; |
| 1664 | } |
| 1665 | |
| 1666 | return SDOperand(LatestAdjCallStackUp, 0); |
| 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1671 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 1672 | // does not fit into a register, return the lo part and set the hi part to the |
| 1673 | // by-reg argument. If it does fit into a single register, return the result |
| 1674 | // and leave the Hi part unset. |
| 1675 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 1676 | SDOperand &Hi) { |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1677 | SDNode *OutChain; |
| 1678 | SDOperand InChain = FindInputOutputChains(Node, OutChain, |
| 1679 | DAG.getEntryNode()); |
Chris Lattner | f4b4579 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 1680 | if (InChain.Val == 0) |
| 1681 | InChain = DAG.getEntryNode(); |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1682 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1683 | TargetLowering::ArgListTy Args; |
| 1684 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1685 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 1686 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 1687 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 1688 | } |
| 1689 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
| 1690 | |
| 1691 | // We don't care about token chains for libcalls. We just use the entry |
| 1692 | // node as our input and ignore the output chain. This allows us to place |
| 1693 | // calls wherever we need them to satisfy data dependences. |
| 1694 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1695 | SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1696 | Args, DAG).first; |
| 1697 | switch (getTypeAction(Result.getValueType())) { |
| 1698 | default: assert(0 && "Unknown thing"); |
| 1699 | case Legal: |
| 1700 | return Result; |
| 1701 | case Promote: |
| 1702 | assert(0 && "Cannot promote this yet!"); |
| 1703 | case Expand: |
| 1704 | SDOperand Lo; |
| 1705 | ExpandOp(Result, Lo, Hi); |
| 1706 | return Lo; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1711 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 1712 | /// destination type is legal. |
| 1713 | SDOperand SelectionDAGLegalize:: |
| 1714 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
| 1715 | assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!"); |
| 1716 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 1717 | "This is not an expansion!"); |
| 1718 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 1719 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1720 | SDNode *OutChain; |
| 1721 | SDOperand InChain = FindInputOutputChains(Source.Val, OutChain, |
| 1722 | DAG.getEntryNode()); |
| 1723 | |
Chris Lattner | fed5577 | 2005-01-23 23:19:44 +0000 | [diff] [blame] | 1724 | const char *FnName = 0; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1725 | if (isSigned) { |
| 1726 | if (DestTy == MVT::f32) |
| 1727 | FnName = "__floatdisf"; |
| 1728 | else { |
| 1729 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 1730 | FnName = "__floatdidf"; |
| 1731 | } |
| 1732 | } else { |
| 1733 | // If this is unsigned, and not supported, first perform the conversion to |
| 1734 | // signed, then adjust the result if the sign bit is set. |
| 1735 | SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source); |
| 1736 | |
| 1737 | assert(0 && "Unsigned casts not supported yet!"); |
| 1738 | } |
| 1739 | SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy()); |
| 1740 | |
| 1741 | TargetLowering::ArgListTy Args; |
| 1742 | const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType()); |
| 1743 | Args.push_back(std::make_pair(Source, ArgTy)); |
| 1744 | |
| 1745 | // We don't care about token chains for libcalls. We just use the entry |
| 1746 | // node as our input and ignore the output chain. This allows us to place |
| 1747 | // calls wherever we need them to satisfy data dependences. |
| 1748 | const Type *RetTy = MVT::getTypeForValueType(DestTy); |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1749 | return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first; |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1753 | |
| 1754 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1755 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 1756 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 1757 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 1758 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 1759 | /// Lo/Hi values are returned. |
| 1760 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 1761 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 1762 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1763 | SDNode *Node = Op.Val; |
| 1764 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
| 1765 | assert(MVT::isInteger(VT) && "Cannot expand FP values!"); |
| 1766 | assert(MVT::isInteger(NVT) && NVT < VT && |
| 1767 | "Cannot expand to FP value or to larger int value!"); |
| 1768 | |
| 1769 | // If there is more than one use of this, see if we already expanded it. |
| 1770 | // There is no use remembering values that only have a single use, as the map |
| 1771 | // entries will never be reused. |
| 1772 | if (!Node->hasOneUse()) { |
| 1773 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 1774 | = ExpandedNodes.find(Op); |
| 1775 | if (I != ExpandedNodes.end()) { |
| 1776 | Lo = I->second.first; |
| 1777 | Hi = I->second.second; |
| 1778 | return; |
| 1779 | } |
| 1780 | } |
| 1781 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1782 | // Expanding to multiple registers needs to perform an optimization step, and |
| 1783 | // is not careful to avoid operations the target does not support. Make sure |
| 1784 | // that all generated operations are legalized in the next iteration. |
| 1785 | NeedsAnotherIteration = true; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1786 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1787 | switch (Node->getOpcode()) { |
| 1788 | default: |
| 1789 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 1790 | assert(0 && "Do not know how to expand this operator!"); |
| 1791 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1792 | case ISD::UNDEF: |
| 1793 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 1794 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 1795 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1796 | case ISD::Constant: { |
| 1797 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 1798 | Lo = DAG.getConstant(Cst, NVT); |
| 1799 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 1800 | break; |
| 1801 | } |
| 1802 | |
| 1803 | case ISD::CopyFromReg: { |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1804 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1805 | // Aggregate register values are always in consequtive pairs. |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 1806 | Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0)); |
| 1807 | Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1)); |
| 1808 | |
| 1809 | // Remember that we legalized the chain. |
| 1810 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 1811 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1812 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 1813 | break; |
| 1814 | } |
| 1815 | |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 1816 | case ISD::BUILD_PAIR: |
| 1817 | // Legalize both operands. FIXME: in the future we should handle the case |
| 1818 | // where the two elements are not legal. |
| 1819 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 1820 | Lo = LegalizeOp(Node->getOperand(0)); |
| 1821 | Hi = LegalizeOp(Node->getOperand(1)); |
| 1822 | break; |
| 1823 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1824 | case ISD::LOAD: { |
| 1825 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1826 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 1827 | Lo = DAG.getLoad(NVT, Ch, Ptr); |
| 1828 | |
| 1829 | // Increment the pointer to the other half. |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 1830 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1831 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 1832 | getIntPtrConstant(IncrementSize)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1833 | Hi = DAG.getLoad(NVT, Ch, Ptr); |
| 1834 | |
| 1835 | // Build a factor node to remember that this load is independent of the |
| 1836 | // other one. |
| 1837 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 1838 | Hi.getValue(1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1839 | |
| 1840 | // Remember that we legalized the chain. |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1841 | AddLegalizedOperand(Op.getValue(1), TF); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1842 | if (!TLI.isLittleEndian()) |
| 1843 | std::swap(Lo, Hi); |
| 1844 | break; |
| 1845 | } |
| 1846 | case ISD::CALL: { |
| 1847 | SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1848 | SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 1849 | |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1850 | bool Changed = false; |
| 1851 | std::vector<SDOperand> Ops; |
| 1852 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { |
| 1853 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1854 | Changed |= Ops.back() != Node->getOperand(i); |
| 1855 | } |
| 1856 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1857 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 1858 | "Can only expand a call once so far, not i64 -> i16!"); |
| 1859 | |
| 1860 | std::vector<MVT::ValueType> RetTyVTs; |
| 1861 | RetTyVTs.reserve(3); |
| 1862 | RetTyVTs.push_back(NVT); |
| 1863 | RetTyVTs.push_back(NVT); |
| 1864 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1865 | SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1866 | Lo = SDOperand(NC, 0); |
| 1867 | Hi = SDOperand(NC, 1); |
| 1868 | |
| 1869 | // Insert the new chain mapping. |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 1870 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
| 1873 | case ISD::AND: |
| 1874 | case ISD::OR: |
| 1875 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 1876 | SDOperand LL, LH, RL, RH; |
| 1877 | ExpandOp(Node->getOperand(0), LL, LH); |
| 1878 | ExpandOp(Node->getOperand(1), RL, RH); |
| 1879 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 1880 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 1881 | break; |
| 1882 | } |
| 1883 | case ISD::SELECT: { |
| 1884 | SDOperand C, LL, LH, RL, RH; |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1885 | |
| 1886 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1887 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1888 | case Legal: |
| 1889 | C = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1890 | break; |
| 1891 | case Promote: |
| 1892 | C = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1893 | break; |
| 1894 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1895 | ExpandOp(Node->getOperand(1), LL, LH); |
| 1896 | ExpandOp(Node->getOperand(2), RL, RH); |
| 1897 | Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); |
| 1898 | Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); |
| 1899 | break; |
| 1900 | } |
| 1901 | case ISD::SIGN_EXTEND: { |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 1902 | SDOperand In; |
| 1903 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1904 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 1905 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 1906 | case Promote: |
| 1907 | In = PromoteOp(Node->getOperand(0)); |
| 1908 | // Emit the appropriate sign_extend_inreg to get the value we want. |
| 1909 | In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In, |
| 1910 | Node->getOperand(0).getValueType()); |
| 1911 | break; |
| 1912 | } |
| 1913 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1914 | // The low part is just a sign extension of the input (which degenerates to |
| 1915 | // a copy). |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 1916 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1917 | |
| 1918 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 1919 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 1920 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 1921 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 1922 | TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | } |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 1925 | case ISD::ZERO_EXTEND: { |
| 1926 | SDOperand In; |
| 1927 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1928 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 1929 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 1930 | case Promote: |
| 1931 | In = PromoteOp(Node->getOperand(0)); |
| 1932 | // Emit the appropriate zero_extend_inreg to get the value we want. |
| 1933 | In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In, |
| 1934 | Node->getOperand(0).getValueType()); |
| 1935 | break; |
| 1936 | } |
| 1937 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1938 | // The low part is just a zero extension of the input (which degenerates to |
| 1939 | // a copy). |
Chris Lattner | dea29e2 | 2005-04-10 01:13:15 +0000 | [diff] [blame^] | 1940 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1941 | |
| 1942 | // The high part is just a zero. |
| 1943 | Hi = DAG.getConstant(0, NVT); |
| 1944 | break; |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 1945 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1946 | // These operators cannot be expanded directly, emit them as calls to |
| 1947 | // library functions. |
| 1948 | case ISD::FP_TO_SINT: |
| 1949 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1950 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1951 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1952 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | case ISD::FP_TO_UINT: |
| 1955 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1956 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1957 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1958 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1961 | case ISD::SHL: |
| 1962 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1963 | if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1964 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1965 | |
| 1966 | // If this target supports SHL_PARTS, use it. |
| 1967 | if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1968 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 1969 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1970 | break; |
| 1971 | } |
| 1972 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1973 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1974 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1975 | break; |
| 1976 | |
| 1977 | case ISD::SRA: |
| 1978 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1979 | if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1980 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1981 | |
| 1982 | // If this target supports SRA_PARTS, use it. |
| 1983 | if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1984 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 1985 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1986 | break; |
| 1987 | } |
| 1988 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1989 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1990 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1991 | break; |
| 1992 | case ISD::SRL: |
| 1993 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1994 | if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1995 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1996 | |
| 1997 | // If this target supports SRL_PARTS, use it. |
| 1998 | if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1999 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2000 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | } |
| 2003 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2004 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2005 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2006 | break; |
| 2007 | |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2008 | case ISD::ADD: |
| 2009 | ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2010 | Lo, Hi); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2011 | break; |
| 2012 | case ISD::SUB: |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2013 | ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2014 | Lo, Hi); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2015 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2016 | case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break; |
| 2017 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 2018 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 2019 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 2020 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | // Remember in a map if the values will be reused later. |
| 2024 | if (!Node->hasOneUse()) { |
| 2025 | bool isNew = ExpandedNodes.insert(std::make_pair(Op, |
| 2026 | std::make_pair(Lo, Hi))).second; |
| 2027 | assert(isNew && "Value already expanded?!?"); |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 2033 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2034 | void SelectionDAG::Legalize() { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2035 | /// run - This is the main entry point to this class. |
| 2036 | /// |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2037 | SelectionDAGLegalize(*this).Run(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |