Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
| 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 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 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, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 293 | DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 294 | } else { |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 295 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 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. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 431 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 432 | if (Tmp1 != Node->getOperand(0) || |
| 433 | Tmp2 != Node->getOperand(1)) |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 434 | Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 435 | else |
| 436 | Result = SDOperand(Node, 0); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 438 | // Since loads produce two values, make sure to remember that we legalized |
| 439 | // both of them. |
| 440 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 441 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 442 | return Result.getValue(Op.ResNo); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 444 | case ISD::EXTLOAD: |
| 445 | case ISD::SEXTLOAD: |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 446 | case ISD::ZEXTLOAD: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 447 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 448 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 450 | MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType(); |
| 451 | switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 452 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 1c51c6a | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 453 | case TargetLowering::Promote: |
| 454 | assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); |
| 455 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 456 | Tmp1, Tmp2, Node->getOperand(2), MVT::i8); |
Chris Lattner | 1c51c6a | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 457 | // Since loads produce two values, make sure to remember that we legalized |
| 458 | // both of them. |
| 459 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 460 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 461 | return Result.getValue(Op.ResNo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 463 | case TargetLowering::Legal: |
| 464 | if (Tmp1 != Node->getOperand(0) || |
| 465 | Tmp2 != Node->getOperand(1)) |
| 466 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 467 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 468 | else |
| 469 | Result = SDOperand(Node, 0); |
| 470 | |
| 471 | // Since loads produce two values, make sure to remember that we legalized |
| 472 | // both of them. |
| 473 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 474 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 475 | return Result.getValue(Op.ResNo); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 476 | case TargetLowering::Expand: |
| 477 | assert(Node->getOpcode() != ISD::EXTLOAD && |
| 478 | "EXTLOAD should always be supported!"); |
| 479 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 480 | // zero/sign extend inreg. |
| 481 | Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 482 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 483 | SDOperand ValRes; |
| 484 | if (Node->getOpcode() == ISD::SEXTLOAD) |
| 485 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 486 | Result, SrcVT); |
| 487 | else |
| 488 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 489 | AddLegalizedOperand(SDOperand(Node, 0), ValRes); |
| 490 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 491 | if (Op.ResNo) |
| 492 | return Result.getValue(1); |
| 493 | return ValRes; |
| 494 | } |
| 495 | assert(0 && "Unreachable"); |
| 496 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 497 | case ISD::EXTRACT_ELEMENT: |
| 498 | // Get both the low and high parts. |
| 499 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 500 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 501 | Result = Tmp2; // 1 -> Hi |
| 502 | else |
| 503 | Result = Tmp1; // 0 -> Lo |
| 504 | break; |
| 505 | |
| 506 | case ISD::CopyToReg: |
| 507 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 509 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 510 | case Legal: |
| 511 | // Legalize the incoming value (must be legal). |
| 512 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 513 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 514 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 515 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 516 | case Promote: |
| 517 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 518 | Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg()); |
| 519 | break; |
| 520 | case Expand: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 521 | SDOperand Lo, Hi; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 522 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 523 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 524 | Lo = DAG.getCopyToReg(Tmp1, Lo, Reg); |
| 525 | Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1); |
| 526 | // Note that the copytoreg nodes are independent of each other. |
| 527 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 528 | assert(isTypeLegal(Result.getValueType()) && |
| 529 | "Cannot expand multiple times yet (i64 -> i16)"); |
| 530 | break; |
| 531 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | case ISD::RET: |
| 535 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 536 | switch (Node->getNumOperands()) { |
| 537 | case 2: // ret val |
| 538 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 539 | case Legal: |
| 540 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 541 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 542 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 543 | break; |
| 544 | case Expand: { |
| 545 | SDOperand Lo, Hi; |
| 546 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 547 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 548 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 549 | } |
| 550 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 551 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 552 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); |
| 553 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 554 | } |
| 555 | break; |
| 556 | case 1: // ret void |
| 557 | if (Tmp1 != Node->getOperand(0)) |
| 558 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); |
| 559 | break; |
| 560 | default: { // ret <values> |
| 561 | std::vector<SDOperand> NewValues; |
| 562 | NewValues.push_back(Tmp1); |
| 563 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 564 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 565 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 566 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 567 | break; |
| 568 | case Expand: { |
| 569 | SDOperand Lo, Hi; |
| 570 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 571 | NewValues.push_back(Lo); |
| 572 | NewValues.push_back(Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 573 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 574 | } |
| 575 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 576 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 577 | } |
| 578 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | break; |
| 583 | case ISD::STORE: |
| 584 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 585 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 586 | |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 587 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 588 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 589 | if (CFP->getValueType(0) == MVT::f32) { |
| 590 | union { |
| 591 | unsigned I; |
| 592 | float F; |
| 593 | } V; |
| 594 | V.F = CFP->getValue(); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 595 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
| 596 | DAG.getConstant(V.I, MVT::i32), Tmp2, Node->getOperand(3)); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 597 | } else { |
| 598 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 599 | union { |
| 600 | uint64_t I; |
| 601 | double F; |
| 602 | } V; |
| 603 | V.F = CFP->getValue(); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 604 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, |
| 605 | DAG.getConstant(V.I, MVT::i64), Tmp2, Node->getOperand(3)); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 606 | } |
Chris Lattner | 84734ce | 2005-02-22 07:23:39 +0000 | [diff] [blame] | 607 | Node = Result.Val; |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 610 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 611 | case Legal: { |
| 612 | SDOperand Val = LegalizeOp(Node->getOperand(1)); |
| 613 | if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || |
| 614 | Tmp2 != Node->getOperand(2)) |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 615 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2, Node->getOperand(3)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 619 | // Truncate the value and store the result. |
| 620 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 621 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 622 | Node->getOperand(3), |
| 623 | Node->getOperand(1).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 624 | break; |
| 625 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 626 | case Expand: |
| 627 | SDOperand Lo, Hi; |
| 628 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 629 | |
| 630 | if (!TLI.isLittleEndian()) |
| 631 | std::swap(Lo, Hi); |
| 632 | |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 633 | Lo = DAG.getNode(ISD::STORE, MVT::Other,Tmp1, Lo, Tmp2,Node->getOperand(3)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 634 | |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 635 | unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 636 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 637 | getIntPtrConstant(IncrementSize)); |
| 638 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 639 | "Pointers must be legal!"); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 640 | //Again, claiming both parts of the store came form the same Instr |
| 641 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, Node->getOperand(3)); |
| 642 | |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 643 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 644 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 645 | } |
| 646 | break; |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 647 | case ISD::PCMARKER: |
| 648 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 649 | if (Tmp1 != Node->getOperand(0)) |
| 650 | Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 651 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 652 | case ISD::TRUNCSTORE: |
| 653 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 654 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 655 | |
| 656 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 657 | case Legal: |
| 658 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 659 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 660 | Tmp3 != Node->getOperand(2)) |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 661 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 662 | Node->getOperand(3), |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 663 | cast<MVTSDNode>(Node)->getExtraValueType()); |
| 664 | break; |
| 665 | case Promote: |
| 666 | case Expand: |
| 667 | assert(0 && "Cannot handle illegal TRUNCSTORE yet!"); |
| 668 | } |
| 669 | break; |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 670 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 671 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 672 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 673 | case Legal: |
| 674 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 675 | break; |
| 676 | case Promote: |
| 677 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 678 | break; |
| 679 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 680 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 681 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 682 | |
| 683 | switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) { |
| 684 | default: assert(0 && "This action is not supported yet!"); |
| 685 | case TargetLowering::Legal: |
| 686 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 687 | Tmp3 != Node->getOperand(2)) |
| 688 | Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), |
| 689 | Tmp1, Tmp2, Tmp3); |
| 690 | break; |
| 691 | case TargetLowering::Promote: { |
| 692 | MVT::ValueType NVT = |
| 693 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 694 | unsigned ExtOp, TruncOp; |
| 695 | if (MVT::isInteger(Tmp2.getValueType())) { |
| 696 | ExtOp = ISD::ZERO_EXTEND; |
| 697 | TruncOp = ISD::TRUNCATE; |
| 698 | } else { |
| 699 | ExtOp = ISD::FP_EXTEND; |
| 700 | TruncOp = ISD::FP_ROUND; |
| 701 | } |
| 702 | // Promote each of the values to the new type. |
| 703 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 704 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 705 | // Perform the larger operation, then round down. |
| 706 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 707 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 708 | break; |
| 709 | } |
| 710 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 711 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 712 | case ISD::SETCC: |
| 713 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 714 | case Legal: |
| 715 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 716 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 717 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 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 Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 722 | Tmp1 = PromoteOp(Node->getOperand(0)); // LHS |
| 723 | Tmp2 = PromoteOp(Node->getOperand(1)); // RHS |
| 724 | |
| 725 | // If this is an FP compare, the operands have already been extended. |
| 726 | if (MVT::isInteger(Node->getOperand(0).getValueType())) { |
| 727 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 728 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 729 | |
| 730 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 731 | // that we could insert sign extends for ALL conditions, but zero extend |
| 732 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 733 | // it. |
| 734 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 735 | default: assert(0 && "Unknown integer comparison!"); |
| 736 | case ISD::SETEQ: |
| 737 | case ISD::SETNE: |
| 738 | case ISD::SETUGE: |
| 739 | case ISD::SETUGT: |
| 740 | case ISD::SETULE: |
| 741 | case ISD::SETULT: |
| 742 | // ALL of these operations will work if we either sign or zero extend |
| 743 | // the operands (including the unsigned comparisons!). Zero extend is |
| 744 | // usually a simpler/cheaper operation, so prefer it. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 745 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 746 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 747 | break; |
| 748 | case ISD::SETGE: |
| 749 | case ISD::SETGT: |
| 750 | case ISD::SETLT: |
| 751 | case ISD::SETLE: |
| 752 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
| 753 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | } |
| 758 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 759 | Node->getValueType(0), Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 760 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 761 | case Expand: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 762 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 763 | ExpandOp(Node->getOperand(0), LHSLo, LHSHi); |
| 764 | ExpandOp(Node->getOperand(1), RHSLo, RHSHi); |
| 765 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 766 | case ISD::SETEQ: |
| 767 | case ISD::SETNE: |
Chris Lattner | 08b698e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 768 | if (RHSLo == RHSHi) |
| 769 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 770 | if (RHSCST->isAllOnesValue()) { |
| 771 | // Comparison to -1. |
| 772 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 773 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | 08b698e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 774 | Node->getValueType(0), Tmp1, RHSLo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 775 | break; |
Chris Lattner | 08b698e | 2005-04-12 01:46:05 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 778 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 779 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 780 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 781 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 782 | Node->getValueType(0), Tmp1, |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 783 | DAG.getConstant(0, Tmp1.getValueType())); |
| 784 | break; |
| 785 | default: |
Chris Lattner | 5b95ed6 | 2005-04-12 02:19:10 +0000 | [diff] [blame] | 786 | // If this is a comparison of the sign bit, just look at the top part. |
| 787 | // X > -1, x < 0 |
| 788 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1))) |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 789 | if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT && |
Chris Lattner | 5b95ed6 | 2005-04-12 02:19:10 +0000 | [diff] [blame] | 790 | CST->getValue() == 0) || // X < 0 |
| 791 | (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT && |
| 792 | (CST->isAllOnesValue()))) // X > -1 |
| 793 | return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 794 | Node->getValueType(0), LHSHi, RHSHi); |
| 795 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 796 | // FIXME: This generated code sucks. |
| 797 | ISD::CondCode LowCC; |
| 798 | switch (cast<SetCCSDNode>(Node)->getCondition()) { |
| 799 | default: assert(0 && "Unknown integer setcc!"); |
| 800 | case ISD::SETLT: |
| 801 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 802 | case ISD::SETGT: |
| 803 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 804 | case ISD::SETLE: |
| 805 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 806 | case ISD::SETGE: |
| 807 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 808 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 810 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 811 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 812 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 813 | |
| 814 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 815 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 816 | Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 817 | Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 818 | Node->getValueType(0), LHSHi, RHSHi); |
| 819 | Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi); |
| 820 | Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 821 | Result, Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | } |
| 825 | break; |
| 826 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 827 | case ISD::MEMSET: |
| 828 | case ISD::MEMCPY: |
| 829 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 830 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 831 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 832 | |
| 833 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 834 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 835 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 836 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 837 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 838 | break; |
| 839 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 840 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 841 | break; |
| 842 | } |
| 843 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 844 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 845 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 846 | |
| 847 | SDOperand Tmp4; |
| 848 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 849 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 850 | case Legal: |
| 851 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 852 | break; |
| 853 | case Promote: |
| 854 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 855 | break; |
| 856 | } |
| 857 | |
| 858 | SDOperand Tmp5; |
| 859 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 860 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 861 | case Legal: |
| 862 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 863 | break; |
| 864 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 865 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 866 | break; |
| 867 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 868 | |
| 869 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 870 | default: assert(0 && "This action not implemented for this operation!"); |
| 871 | case TargetLowering::Legal: |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 872 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 873 | Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || |
| 874 | Tmp5 != Node->getOperand(4)) { |
| 875 | std::vector<SDOperand> Ops; |
| 876 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 877 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 878 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
| 879 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 880 | break; |
| 881 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 882 | // Otherwise, the target does not support this operation. Lower the |
| 883 | // operation to an explicit libcall as appropriate. |
| 884 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 885 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 886 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 887 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 888 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 889 | if (Node->getOpcode() == ISD::MEMSET) { |
| 890 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 891 | // Extend the ubyte argument to be an int value for the call. |
| 892 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
| 893 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 894 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 895 | |
| 896 | FnName = "memset"; |
| 897 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 898 | Node->getOpcode() == ISD::MEMMOVE) { |
| 899 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 900 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 901 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 902 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 903 | } else { |
| 904 | assert(0 && "Unknown op!"); |
| 905 | } |
| 906 | std::pair<SDOperand,SDOperand> CallResult = |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 907 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 908 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
| 909 | Result = LegalizeOp(CallResult.second); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | case TargetLowering::Custom: |
| 913 | std::vector<SDOperand> Ops; |
| 914 | Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); |
| 915 | Ops.push_back(Tmp4); Ops.push_back(Tmp5); |
| 916 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); |
| 917 | Result = TLI.LowerOperation(Result); |
| 918 | Result = LegalizeOp(Result); |
| 919 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 920 | } |
| 921 | break; |
| 922 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 923 | case ISD::ADD_PARTS: |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 924 | case ISD::SUB_PARTS: |
| 925 | case ISD::SHL_PARTS: |
| 926 | case ISD::SRA_PARTS: |
| 927 | case ISD::SRL_PARTS: { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 928 | std::vector<SDOperand> Ops; |
| 929 | bool Changed = false; |
| 930 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 931 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 932 | Changed |= Ops.back() != Node->getOperand(i); |
| 933 | } |
| 934 | if (Changed) |
| 935 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 936 | |
| 937 | // Since these produce multiple values, make sure to remember that we |
| 938 | // legalized all of them. |
| 939 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 940 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 941 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 942 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 943 | |
| 944 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 945 | case ISD::ADD: |
| 946 | case ISD::SUB: |
| 947 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 948 | case ISD::MULHS: |
| 949 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 950 | case ISD::UDIV: |
| 951 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 952 | case ISD::AND: |
| 953 | case ISD::OR: |
| 954 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 955 | case ISD::SHL: |
| 956 | case ISD::SRL: |
| 957 | case ISD::SRA: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 958 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 959 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 960 | if (Tmp1 != Node->getOperand(0) || |
| 961 | Tmp2 != Node->getOperand(1)) |
| 962 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); |
| 963 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 964 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 965 | case ISD::UREM: |
| 966 | case ISD::SREM: |
| 967 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 968 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 969 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 970 | case TargetLowering::Legal: |
| 971 | if (Tmp1 != Node->getOperand(0) || |
| 972 | Tmp2 != Node->getOperand(1)) |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 973 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 974 | Tmp2); |
| 975 | break; |
| 976 | case TargetLowering::Promote: |
| 977 | case TargetLowering::Custom: |
| 978 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 979 | case TargetLowering::Expand: { |
| 980 | MVT::ValueType VT = Node->getValueType(0); |
| 981 | unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
| 982 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 983 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 984 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 985 | } |
| 986 | break; |
| 987 | } |
| 988 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 989 | |
| 990 | // Unary operators |
| 991 | case ISD::FABS: |
| 992 | case ISD::FNEG: |
| 993 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 994 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 995 | case TargetLowering::Legal: |
| 996 | if (Tmp1 != Node->getOperand(0)) |
| 997 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 998 | break; |
| 999 | case TargetLowering::Promote: |
| 1000 | case TargetLowering::Custom: |
| 1001 | assert(0 && "Cannot promote/custom handle this yet!"); |
| 1002 | case TargetLowering::Expand: |
| 1003 | if (Node->getOpcode() == ISD::FNEG) { |
| 1004 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 1005 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
| 1006 | Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0), |
| 1007 | Tmp2, Tmp1)); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 1008 | } else if (Node->getOpcode() == ISD::FABS) { |
| 1009 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 1010 | MVT::ValueType VT = Node->getValueType(0); |
| 1011 | Tmp2 = DAG.getConstantFP(0.0, VT); |
| 1012 | Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2); |
| 1013 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 1014 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
| 1015 | Result = LegalizeOp(Result); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1016 | } else { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 1017 | assert(0 && "Unreachable!"); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1018 | } |
| 1019 | break; |
| 1020 | } |
| 1021 | break; |
| 1022 | |
| 1023 | // Conversion operators. The source and destination have different types. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1024 | case ISD::ZERO_EXTEND: |
| 1025 | case ISD::SIGN_EXTEND: |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 1026 | case ISD::TRUNCATE: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 1027 | case ISD::FP_EXTEND: |
| 1028 | case ISD::FP_ROUND: |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 1029 | case ISD::FP_TO_SINT: |
| 1030 | case ISD::FP_TO_UINT: |
| 1031 | case ISD::SINT_TO_FP: |
| 1032 | case ISD::UINT_TO_FP: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1033 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1034 | case Legal: |
| 1035 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1036 | if (Tmp1 != Node->getOperand(0)) |
| 1037 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
| 1038 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 1039 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1040 | if (Node->getOpcode() == ISD::SINT_TO_FP || |
| 1041 | Node->getOpcode() == ISD::UINT_TO_FP) { |
| 1042 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 1043 | Node->getValueType(0), Node->getOperand(0)); |
| 1044 | Result = LegalizeOp(Result); |
| 1045 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1046 | } else if (Node->getOpcode() == ISD::TRUNCATE) { |
| 1047 | // In the expand case, we must be dealing with a truncate, because |
| 1048 | // otherwise the result would be larger than the source. |
| 1049 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1051 | // Since the result is legal, we should just be able to truncate the low |
| 1052 | // part of the source. |
| 1053 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 1054 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1056 | assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1058 | case Promote: |
| 1059 | switch (Node->getOpcode()) { |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1060 | case ISD::ZERO_EXTEND: |
| 1061 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1062 | // NOTE: Any extend would work here... |
| 1063 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1064 | Result = DAG.getZeroExtendInReg(Result, |
| 1065 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1066 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1067 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1068 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1069 | // NOTE: Any extend would work here... |
Chris Lattner | d5d5682 | 2005-01-18 21:57:59 +0000 | [diff] [blame] | 1070 | Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1071 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1072 | Result, Node->getOperand(0).getValueType()); |
| 1073 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1074 | case ISD::TRUNCATE: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1075 | Result = PromoteOp(Node->getOperand(0)); |
| 1076 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 1077 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1078 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 1079 | Result = PromoteOp(Node->getOperand(0)); |
| 1080 | if (Result.getValueType() != Op.getValueType()) |
| 1081 | // Dynamically dead while we have only 2 FP types. |
| 1082 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 1083 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1084 | case ISD::FP_ROUND: |
| 1085 | case ISD::FP_TO_SINT: |
| 1086 | case ISD::FP_TO_UINT: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1087 | Result = PromoteOp(Node->getOperand(0)); |
| 1088 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 1089 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1090 | case ISD::SINT_TO_FP: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1091 | Result = PromoteOp(Node->getOperand(0)); |
| 1092 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1093 | Result, Node->getOperand(0).getValueType()); |
| 1094 | Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); |
| 1095 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1096 | case ISD::UINT_TO_FP: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1097 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1098 | Result = DAG.getZeroExtendInReg(Result, |
| 1099 | Node->getOperand(0).getValueType()); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 1100 | Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); |
| 1101 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1102 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1103 | } |
| 1104 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1105 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1106 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1107 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1108 | MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType(); |
| 1109 | |
| 1110 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 1111 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1112 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 1113 | default: assert(0 && "This action not supported for this op yet!"); |
| 1114 | case TargetLowering::Legal: |
| 1115 | if (Tmp1 != Node->getOperand(0)) |
| 1116 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, |
| 1117 | ExtraVT); |
| 1118 | break; |
| 1119 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1120 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1121 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1122 | // NOTE: we could fall back on load/store here too for targets without |
| 1123 | // SAR. However, it is doubtful that any exist. |
| 1124 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 1125 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 1126 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1127 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 1128 | Node->getOperand(0), ShiftCst); |
| 1129 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 1130 | Result, ShiftCst); |
| 1131 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 1132 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 1133 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 1134 | |
| 1135 | // NOTE: there is a choice here between constantly creating new stack |
| 1136 | // slots and always reusing the same one. We currently always create |
| 1137 | // new ones, as reuse may inhibit scheduling. |
| 1138 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 1139 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 1140 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 1141 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1142 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1143 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 1144 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 1145 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1146 | Node->getOperand(0), StackSlot, DAG.getSrcValue(NULL), ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1147 | Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1148 | Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1149 | } else { |
| 1150 | assert(0 && "Unknown op"); |
| 1151 | } |
| 1152 | Result = LegalizeOp(Result); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1153 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1155 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1156 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 1157 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1158 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 1159 | if (!Op.Val->hasOneUse()) |
| 1160 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1161 | |
| 1162 | return Result; |
| 1163 | } |
| 1164 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1165 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 1166 | /// promote it to compute the value into a larger type. The produced value will |
| 1167 | /// have the correct bits for the low portion of the register, but no guarantee |
| 1168 | /// 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] | 1169 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 1170 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 1171 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1172 | assert(getTypeAction(VT) == Promote && |
| 1173 | "Caller should expand or legalize operands that are not promotable!"); |
| 1174 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 1175 | "Cannot promote to smaller type!"); |
| 1176 | |
| 1177 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 1178 | if (I != PromotedNodes.end()) return I->second; |
| 1179 | |
| 1180 | SDOperand Tmp1, Tmp2, Tmp3; |
| 1181 | |
| 1182 | SDOperand Result; |
| 1183 | SDNode *Node = Op.Val; |
| 1184 | |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1185 | // Promotion needs an optimization step to clean up after it, and is not |
| 1186 | // careful to avoid operations the target does not support. Make sure that |
| 1187 | // all generated operations are legalized in the next iteration. |
| 1188 | NeedsAnotherIteration = true; |
| 1189 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1190 | switch (Node->getOpcode()) { |
| 1191 | default: |
| 1192 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 1193 | assert(0 && "Do not know how to promote this operator!"); |
| 1194 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1195 | case ISD::UNDEF: |
| 1196 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 1197 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1198 | case ISD::Constant: |
| 1199 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
| 1200 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 1201 | break; |
| 1202 | case ISD::ConstantFP: |
| 1203 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 1204 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 1205 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 1206 | case ISD::CopyFromReg: |
| 1207 | Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT, |
| 1208 | Node->getOperand(0)); |
| 1209 | // Remember that we legalized the chain. |
| 1210 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1211 | break; |
| 1212 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 1213 | case ISD::SETCC: |
| 1214 | assert(getTypeAction(TLI.getSetCCResultTy()) == Legal && |
| 1215 | "SetCC type is not legal??"); |
| 1216 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), |
| 1217 | TLI.getSetCCResultTy(), Node->getOperand(0), |
| 1218 | Node->getOperand(1)); |
| 1219 | Result = LegalizeOp(Result); |
| 1220 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1221 | |
| 1222 | case ISD::TRUNCATE: |
| 1223 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1224 | case Legal: |
| 1225 | Result = LegalizeOp(Node->getOperand(0)); |
| 1226 | assert(Result.getValueType() >= NVT && |
| 1227 | "This truncation doesn't make sense!"); |
| 1228 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 1229 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 1230 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 1231 | case Promote: |
| 1232 | // The truncation is not required, because we don't guarantee anything |
| 1233 | // about high bits anyway. |
| 1234 | Result = PromoteOp(Node->getOperand(0)); |
| 1235 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1236 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 1237 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1238 | // Truncate the low part of the expanded value to the result type |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1239 | Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1240 | } |
| 1241 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1242 | case ISD::SIGN_EXTEND: |
| 1243 | case ISD::ZERO_EXTEND: |
| 1244 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1245 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 1246 | case Legal: |
| 1247 | // Input is legal? Just do extend all the way to the larger type. |
| 1248 | Result = LegalizeOp(Node->getOperand(0)); |
| 1249 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
| 1250 | break; |
| 1251 | case Promote: |
| 1252 | // Promote the reg if it's smaller. |
| 1253 | Result = PromoteOp(Node->getOperand(0)); |
| 1254 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 1255 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 1256 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 1257 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1258 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1259 | Result = DAG.getZeroExtendInReg(Result, |
| 1260 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | break; |
| 1264 | |
| 1265 | case ISD::FP_EXTEND: |
| 1266 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 1267 | case ISD::FP_ROUND: |
| 1268 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1269 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 1270 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 1271 | case Legal: |
| 1272 | // Input is legal? Do an FP_ROUND_INREG. |
| 1273 | Result = LegalizeOp(Node->getOperand(0)); |
| 1274 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1275 | break; |
| 1276 | } |
| 1277 | break; |
| 1278 | |
| 1279 | case ISD::SINT_TO_FP: |
| 1280 | case ISD::UINT_TO_FP: |
| 1281 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1282 | case Legal: |
| 1283 | Result = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1284 | // No extra round required here. |
| 1285 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | |
| 1288 | case Promote: |
| 1289 | Result = PromoteOp(Node->getOperand(0)); |
| 1290 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 1291 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1292 | Result, Node->getOperand(0).getValueType()); |
| 1293 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1294 | Result = DAG.getZeroExtendInReg(Result, |
| 1295 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1296 | // No extra round required here. |
| 1297 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1300 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 1301 | Node->getOperand(0)); |
| 1302 | Result = LegalizeOp(Result); |
| 1303 | |
| 1304 | // Round if we cannot tolerate excess precision. |
| 1305 | if (NoExcessFPPrecision) |
| 1306 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1307 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1308 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1309 | break; |
| 1310 | |
| 1311 | case ISD::FP_TO_SINT: |
| 1312 | case ISD::FP_TO_UINT: |
| 1313 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1314 | case Legal: |
| 1315 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1316 | break; |
| 1317 | case Promote: |
| 1318 | // The input result is prerounded, so we don't have to do anything |
| 1319 | // special. |
| 1320 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1321 | break; |
| 1322 | case Expand: |
| 1323 | assert(0 && "not implemented"); |
| 1324 | } |
| 1325 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1326 | break; |
| 1327 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1328 | case ISD::FABS: |
| 1329 | case ISD::FNEG: |
| 1330 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1331 | assert(Tmp1.getValueType() == NVT); |
| 1332 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 1333 | // NOTE: we do not have to do any extra rounding here for |
| 1334 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 1335 | // precision, and these operations don't modify precision at all. |
| 1336 | break; |
| 1337 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1338 | case ISD::AND: |
| 1339 | case ISD::OR: |
| 1340 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1341 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1342 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1343 | case ISD::MUL: |
| 1344 | // The input may have strange things in the top bits of the registers, but |
| 1345 | // these operations don't care. They may have wierd bits going out, but |
| 1346 | // that too is okay if they are integer operations. |
| 1347 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1348 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1349 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 1350 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1351 | |
| 1352 | // However, if this is a floating point operation, they will give excess |
| 1353 | // precision that we may not be able to tolerate. If we DO allow excess |
| 1354 | // precision, just leave it, otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1355 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 1356 | // 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] | 1357 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
| 1358 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1359 | break; |
| 1360 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1361 | case ISD::SDIV: |
| 1362 | case ISD::SREM: |
| 1363 | // These operators require that their input be sign extended. |
| 1364 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1365 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1366 | if (MVT::isInteger(NVT)) { |
| 1367 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
Chris Lattner | ff3e50c | 2005-01-16 00:17:42 +0000 | [diff] [blame] | 1368 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1369 | } |
| 1370 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1371 | |
| 1372 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 1373 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
| 1374 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT); |
| 1375 | break; |
| 1376 | |
| 1377 | case ISD::UDIV: |
| 1378 | case ISD::UREM: |
| 1379 | // These operators require that their input be zero extended. |
| 1380 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1381 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 1382 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1383 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 1384 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1385 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 1386 | break; |
| 1387 | |
| 1388 | case ISD::SHL: |
| 1389 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1390 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1391 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2); |
| 1392 | break; |
| 1393 | case ISD::SRA: |
| 1394 | // The input value must be properly sign extended. |
| 1395 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 1396 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT); |
| 1397 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1398 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2); |
| 1399 | break; |
| 1400 | case ISD::SRL: |
| 1401 | // The input value must be properly zero extended. |
| 1402 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1403 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1404 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1405 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2); |
| 1406 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1407 | case ISD::LOAD: |
| 1408 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1409 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | 232ee95 | 2005-04-10 04:33:47 +0000 | [diff] [blame] | 1410 | // FIXME: When the DAG combiner exists, change this to use EXTLOAD! |
Chris Lattner | 6841dec | 2005-04-10 17:40:35 +0000 | [diff] [blame] | 1411 | if (MVT::isInteger(NVT)) |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1412 | Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT); |
Chris Lattner | 6841dec | 2005-04-10 17:40:35 +0000 | [diff] [blame] | 1413 | else |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1414 | Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Remember that we legalized the chain. |
| 1417 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1418 | break; |
| 1419 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1420 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1421 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1422 | case Legal: |
| 1423 | Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition. |
| 1424 | break; |
| 1425 | case Promote: |
| 1426 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1427 | break; |
| 1428 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1429 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 1430 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
| 1431 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3); |
| 1432 | break; |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1433 | case ISD::CALL: { |
| 1434 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1435 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 1436 | |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1437 | std::vector<SDOperand> Ops; |
| 1438 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) |
| 1439 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1440 | |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1441 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 1442 | "Can only promote single result calls"); |
| 1443 | std::vector<MVT::ValueType> RetTyVTs; |
| 1444 | RetTyVTs.reserve(2); |
| 1445 | RetTyVTs.push_back(NVT); |
| 1446 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1447 | SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops); |
Chris Lattner | 8ac532c | 2005-01-16 19:46:48 +0000 | [diff] [blame] | 1448 | Result = SDOperand(NC, 0); |
| 1449 | |
| 1450 | // Insert the new chain mapping. |
| 1451 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1452 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1453 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | assert(Result.Val && "Didn't set a result!"); |
| 1457 | AddPromotedOperand(Op, Result); |
| 1458 | return Result; |
| 1459 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1460 | |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1461 | /// ExpandAddSub - Find a clever way to expand this add operation into |
| 1462 | /// subcomponents. |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1463 | void SelectionDAGLegalize:: |
| 1464 | ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS, |
| 1465 | SDOperand &Lo, SDOperand &Hi) { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1466 | // Expand the subcomponents. |
| 1467 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 1468 | ExpandOp(LHS, LHSL, LHSH); |
| 1469 | ExpandOp(RHS, RHSL, RHSH); |
| 1470 | |
Chris Lattner | bd0781e | 2005-04-11 20:29:59 +0000 | [diff] [blame] | 1471 | // FIXME: this should be moved to the dag combiner someday. |
| 1472 | if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS) |
| 1473 | if (LHSL.getValueType() == MVT::i32) { |
| 1474 | SDOperand LowEl; |
| 1475 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL)) |
| 1476 | if (C->getValue() == 0) |
| 1477 | LowEl = RHSL; |
| 1478 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL)) |
| 1479 | if (C->getValue() == 0) |
| 1480 | LowEl = LHSL; |
| 1481 | if (LowEl.Val) { |
| 1482 | // Turn this into an add/sub of the high part only. |
| 1483 | SDOperand HiEl = |
| 1484 | DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB, |
| 1485 | LowEl.getValueType(), LHSH, RHSH); |
| 1486 | Lo = LowEl; |
| 1487 | Hi = HiEl; |
| 1488 | return; |
| 1489 | } |
| 1490 | } |
| 1491 | |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1492 | std::vector<SDOperand> Ops; |
| 1493 | Ops.push_back(LHSL); |
| 1494 | Ops.push_back(LHSH); |
| 1495 | Ops.push_back(RHSL); |
| 1496 | Ops.push_back(RHSH); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 1497 | Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1498 | Hi = Lo.getValue(1); |
| 1499 | } |
| 1500 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1501 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 1502 | SDOperand Op, SDOperand Amt, |
| 1503 | SDOperand &Lo, SDOperand &Hi) { |
| 1504 | // Expand the subcomponents. |
| 1505 | SDOperand LHSL, LHSH; |
| 1506 | ExpandOp(Op, LHSL, LHSH); |
| 1507 | |
| 1508 | std::vector<SDOperand> Ops; |
| 1509 | Ops.push_back(LHSL); |
| 1510 | Ops.push_back(LHSH); |
| 1511 | Ops.push_back(Amt); |
| 1512 | Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops); |
| 1513 | Hi = Lo.getValue(1); |
| 1514 | } |
| 1515 | |
| 1516 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1517 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 1518 | /// smaller elements. If we can't find a way that is more efficient than a |
| 1519 | /// libcall on this target, return false. Otherwise, return true with the |
| 1520 | /// low-parts expanded into Lo and Hi. |
| 1521 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 1522 | SDOperand &Lo, SDOperand &Hi) { |
| 1523 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 1524 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1526 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1527 | SDOperand ShAmt = LegalizeOp(Amt); |
| 1528 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 1529 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 1530 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 1531 | |
| 1532 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 1533 | // and are disabled. |
| 1534 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 1535 | unsigned Cst = CN->getValue(); |
| 1536 | // Expand the incoming operand to be shifted, so that we have its parts |
| 1537 | SDOperand InL, InH; |
| 1538 | ExpandOp(Op, InL, InH); |
| 1539 | switch(Opc) { |
| 1540 | case ISD::SHL: |
| 1541 | if (Cst > VTBits) { |
| 1542 | Lo = DAG.getConstant(0, NVT); |
| 1543 | Hi = DAG.getConstant(0, NVT); |
| 1544 | } else if (Cst > NVTBits) { |
| 1545 | Lo = DAG.getConstant(0, NVT); |
| 1546 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 1547 | } else if (Cst == NVTBits) { |
| 1548 | Lo = DAG.getConstant(0, NVT); |
| 1549 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1550 | } else { |
| 1551 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 1552 | Hi = DAG.getNode(ISD::OR, NVT, |
| 1553 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 1554 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1555 | } |
| 1556 | return true; |
| 1557 | case ISD::SRL: |
| 1558 | if (Cst > VTBits) { |
| 1559 | Lo = DAG.getConstant(0, NVT); |
| 1560 | Hi = DAG.getConstant(0, NVT); |
| 1561 | } else if (Cst > NVTBits) { |
| 1562 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 1563 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 1564 | } else if (Cst == NVTBits) { |
| 1565 | Lo = InH; |
| 1566 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1567 | } else { |
| 1568 | Lo = DAG.getNode(ISD::OR, NVT, |
| 1569 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 1570 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1571 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 1572 | } |
| 1573 | return true; |
| 1574 | case ISD::SRA: |
| 1575 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1576 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1577 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1578 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1579 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1580 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1581 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1582 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 1583 | } else if (Cst == NVTBits) { |
| 1584 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1585 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 1586 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 1587 | } else { |
| 1588 | Lo = DAG.getNode(ISD::OR, NVT, |
| 1589 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 1590 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 1591 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 1592 | } |
| 1593 | return true; |
| 1594 | } |
| 1595 | } |
| 1596 | // FIXME: The following code for expanding shifts using ISD::SELECT is buggy, |
| 1597 | // so disable it for now. Currently targets are handling this via SHL_PARTS |
| 1598 | // and friends. |
| 1599 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1600 | |
| 1601 | // If we have an efficient select operation (or if the selects will all fold |
| 1602 | // away), lower to some complex code, otherwise just emit the libcall. |
| 1603 | if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal && |
| 1604 | !isa<ConstantSDNode>(Amt)) |
| 1605 | return false; |
| 1606 | |
| 1607 | SDOperand InL, InH; |
| 1608 | ExpandOp(Op, InL, InH); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1609 | SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt |
| 1610 | DAG.getConstant(NVTBits, ShTy), ShAmt); |
| 1611 | |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1612 | // Compare the unmasked shift amount against 32. |
| 1613 | SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt, |
| 1614 | DAG.getConstant(NVTBits, ShTy)); |
| 1615 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1616 | if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) { |
| 1617 | ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31 |
| 1618 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1619 | NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31 |
| 1620 | DAG.getConstant(NVTBits-1, ShTy)); |
| 1621 | } |
| 1622 | |
| 1623 | if (Opc == ISD::SHL) { |
| 1624 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt) |
| 1625 | DAG.getNode(ISD::SHL, NVT, InH, ShAmt), |
| 1626 | DAG.getNode(ISD::SRL, NVT, InL, NAmt)); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1627 | SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31 |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1628 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1629 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
| 1630 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2); |
| 1631 | } else { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1632 | SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT, |
| 1633 | DAG.getSetCC(ISD::SETEQ, |
| 1634 | TLI.getSetCCResultTy(), NAmt, |
| 1635 | DAG.getConstant(32, ShTy)), |
| 1636 | DAG.getConstant(0, NVT), |
| 1637 | DAG.getNode(ISD::SHL, NVT, InH, NAmt)); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1638 | SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1639 | HiLoPart, |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1640 | DAG.getNode(ISD::SRL, NVT, InL, ShAmt)); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1641 | SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31 |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1642 | |
| 1643 | SDOperand HiPart; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1644 | if (Opc == ISD::SRA) |
| 1645 | HiPart = DAG.getNode(ISD::SRA, NVT, InH, |
| 1646 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1647 | else |
| 1648 | HiPart = DAG.getConstant(0, NVT); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1649 | Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1); |
Chris Lattner | e5544f8 | 2005-01-20 20:29:23 +0000 | [diff] [blame] | 1650 | Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1651 | } |
| 1652 | return true; |
| 1653 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1655 | /// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest |
| 1656 | /// NodeDepth) node that is an AdjCallStackDown operation and occurs later than |
| 1657 | /// Found. |
| 1658 | static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) { |
| 1659 | if (Node->getNodeDepth() <= Found->getNodeDepth()) return; |
| 1660 | |
| 1661 | // If we found an ADJCALLSTACKDOWN, we already know this node occurs later |
| 1662 | // than the Found node. Just remember this node and return. |
| 1663 | if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) { |
| 1664 | Found = Node; |
| 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 1669 | assert(Node->getNumOperands() != 0 && |
| 1670 | "All leaves should have depth equal to the entry node!"); |
| 1671 | for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i) |
| 1672 | FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found); |
| 1673 | |
| 1674 | // Tail recurse for the last iteration. |
| 1675 | FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val, |
| 1676 | Found); |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | /// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest |
| 1681 | /// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent |
| 1682 | /// than Found. |
| 1683 | static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) { |
| 1684 | if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return; |
| 1685 | |
| 1686 | // If we found an ADJCALLSTACKUP, we already know this node occurs earlier |
| 1687 | // than the Found node. Just remember this node and return. |
| 1688 | if (Node->getOpcode() == ISD::ADJCALLSTACKUP) { |
| 1689 | Found = Node; |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | // Otherwise, scan the operands of Node to see if any of them is a call. |
| 1694 | SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 1695 | if (UI == E) return; |
| 1696 | for (--E; UI != E; ++UI) |
| 1697 | FindEarliestAdjCallStackUp(*UI, Found); |
| 1698 | |
| 1699 | // Tail recurse for the last iteration. |
| 1700 | FindEarliestAdjCallStackUp(*UI, Found); |
| 1701 | } |
| 1702 | |
| 1703 | /// FindAdjCallStackUp - Given a chained node that is part of a call sequence, |
| 1704 | /// find the ADJCALLSTACKUP node that terminates the call sequence. |
| 1705 | static SDNode *FindAdjCallStackUp(SDNode *Node) { |
| 1706 | if (Node->getOpcode() == ISD::ADJCALLSTACKUP) |
| 1707 | return Node; |
Chris Lattner | f4b4579 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 1708 | if (Node->use_empty()) |
| 1709 | return 0; // No adjcallstackup |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1710 | |
| 1711 | if (Node->hasOneUse()) // Simple case, only has one user to check. |
| 1712 | return FindAdjCallStackUp(*Node->use_begin()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1714 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 1715 | assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1716 | |
| 1717 | for (SDNode::use_iterator UI = Node->use_begin(), |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1718 | E = Node->use_end(); ; ++UI) { |
| 1719 | assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1721 | // Make sure to only follow users of our token chain. |
| 1722 | SDNode *User = *UI; |
| 1723 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 1724 | if (User->getOperand(i) == TheChain) |
| 1725 | return FindAdjCallStackUp(User); |
| 1726 | } |
| 1727 | assert(0 && "Unreachable"); |
| 1728 | abort(); |
| 1729 | } |
| 1730 | |
| 1731 | /// FindInputOutputChains - If we are replacing an operation with a call we need |
| 1732 | /// to find the call that occurs before and the call that occurs after it to |
| 1733 | /// properly serialize the calls in the block. |
| 1734 | static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain, |
| 1735 | SDOperand Entry) { |
| 1736 | SDNode *LatestAdjCallStackDown = Entry.Val; |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 1737 | SDNode *LatestAdjCallStackUp = 0; |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1738 | FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown); |
| 1739 | //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1740 | |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 1741 | // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no |
| 1742 | // previous call in the function. LatestCallStackDown may in that case be |
| 1743 | // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP |
| 1744 | // unless LatestCallStackDown is an ADJCALLSTACKDOWN. |
| 1745 | if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN) |
| 1746 | LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown); |
| 1747 | else |
| 1748 | LatestAdjCallStackUp = Entry.Val; |
| 1749 | assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1751 | SDNode *EarliestAdjCallStackUp = 0; |
| 1752 | FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp); |
| 1753 | |
| 1754 | if (EarliestAdjCallStackUp) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1755 | //std::cerr << "Found node: "; |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1756 | //EarliestAdjCallStackUp->dump(); std::cerr <<"\n"; |
| 1757 | } |
| 1758 | |
| 1759 | return SDOperand(LatestAdjCallStackUp, 0); |
| 1760 | } |
| 1761 | |
| 1762 | |
| 1763 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1764 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 1765 | // does not fit into a register, return the lo part and set the hi part to the |
| 1766 | // by-reg argument. If it does fit into a single register, return the result |
| 1767 | // and leave the Hi part unset. |
| 1768 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 1769 | SDOperand &Hi) { |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1770 | SDNode *OutChain; |
| 1771 | SDOperand InChain = FindInputOutputChains(Node, OutChain, |
| 1772 | DAG.getEntryNode()); |
Chris Lattner | f4b4579 | 2005-04-02 03:22:40 +0000 | [diff] [blame] | 1773 | if (InChain.Val == 0) |
| 1774 | InChain = DAG.getEntryNode(); |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1776 | TargetLowering::ArgListTy Args; |
| 1777 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1778 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 1779 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 1780 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 1781 | } |
| 1782 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1784 | // We don't care about token chains for libcalls. We just use the entry |
| 1785 | // node as our input and ignore the output chain. This allows us to place |
| 1786 | // calls wherever we need them to satisfy data dependences. |
| 1787 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1788 | SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1789 | Args, DAG).first; |
| 1790 | switch (getTypeAction(Result.getValueType())) { |
| 1791 | default: assert(0 && "Unknown thing"); |
| 1792 | case Legal: |
| 1793 | return Result; |
| 1794 | case Promote: |
| 1795 | assert(0 && "Cannot promote this yet!"); |
| 1796 | case Expand: |
| 1797 | SDOperand Lo; |
| 1798 | ExpandOp(Result, Lo, Hi); |
| 1799 | return Lo; |
| 1800 | } |
| 1801 | } |
| 1802 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1804 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 1805 | /// destination type is legal. |
| 1806 | SDOperand SelectionDAGLegalize:: |
| 1807 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
| 1808 | assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!"); |
| 1809 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 1810 | "This is not an expansion!"); |
| 1811 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 1812 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 1813 | SDNode *OutChain; |
| 1814 | SDOperand InChain = FindInputOutputChains(Source.Val, OutChain, |
| 1815 | DAG.getEntryNode()); |
| 1816 | |
Chris Lattner | fed5577 | 2005-01-23 23:19:44 +0000 | [diff] [blame] | 1817 | const char *FnName = 0; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1818 | if (isSigned) { |
| 1819 | if (DestTy == MVT::f32) |
| 1820 | FnName = "__floatdisf"; |
| 1821 | else { |
| 1822 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 1823 | FnName = "__floatdidf"; |
| 1824 | } |
| 1825 | } else { |
| 1826 | // If this is unsigned, and not supported, first perform the conversion to |
| 1827 | // signed, then adjust the result if the sign bit is set. |
Chris Lattner | ffe284c | 2005-04-13 03:42:14 +0000 | [diff] [blame] | 1828 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1829 | |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 1830 | assert(Source.getValueType() == MVT::i64 && |
| 1831 | "This only works for 64-bit -> FP"); |
| 1832 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 1833 | // incoming integer is set. To handle this, we dynamically test to see if |
| 1834 | // it is set, and, if so, add a fudge factor. |
| 1835 | SDOperand Lo, Hi; |
| 1836 | ExpandOp(Source, Lo, Hi); |
| 1837 | |
| 1838 | SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi, |
| 1839 | DAG.getConstant(0, Hi.getValueType())); |
| 1840 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 1841 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 1842 | SignSet, Four, Zero); |
| 1843 | // FIXME: This is almost certainly broken for big-endian systems. Should |
| 1844 | // this just put the fudge factor in the low bits of the uint64 constant or? |
| 1845 | static Constant *FudgeFactor = |
| 1846 | ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32); |
| 1847 | |
| 1848 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); |
| 1849 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor), |
| 1850 | TLI.getPointerTy()); |
| 1851 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 1852 | SDOperand FudgeInReg; |
| 1853 | if (DestTy == MVT::f32) |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1854 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 1855 | else { |
| 1856 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
| 1857 | FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1858 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 1859 | } |
| 1860 | return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1861 | } |
| 1862 | SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy()); |
| 1863 | |
| 1864 | TargetLowering::ArgListTy Args; |
| 1865 | const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType()); |
| 1866 | Args.push_back(std::make_pair(Source, ArgTy)); |
| 1867 | |
| 1868 | // We don't care about token chains for libcalls. We just use the entry |
| 1869 | // node as our input and ignore the output chain. This allows us to place |
| 1870 | // calls wherever we need them to satisfy data dependences. |
| 1871 | const Type *RetTy = MVT::getTypeForValueType(DestTy); |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1872 | return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 1873 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 1875 | |
| 1876 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1877 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 1878 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 1879 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 1880 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 1881 | /// Lo/Hi values are returned. |
| 1882 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 1883 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 1884 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1885 | SDNode *Node = Op.Val; |
| 1886 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
| 1887 | assert(MVT::isInteger(VT) && "Cannot expand FP values!"); |
| 1888 | assert(MVT::isInteger(NVT) && NVT < VT && |
| 1889 | "Cannot expand to FP value or to larger int value!"); |
| 1890 | |
| 1891 | // If there is more than one use of this, see if we already expanded it. |
| 1892 | // There is no use remembering values that only have a single use, as the map |
| 1893 | // entries will never be reused. |
| 1894 | if (!Node->hasOneUse()) { |
| 1895 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 1896 | = ExpandedNodes.find(Op); |
| 1897 | if (I != ExpandedNodes.end()) { |
| 1898 | Lo = I->second.first; |
| 1899 | Hi = I->second.second; |
| 1900 | return; |
| 1901 | } |
| 1902 | } |
| 1903 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1904 | // Expanding to multiple registers needs to perform an optimization step, and |
| 1905 | // is not careful to avoid operations the target does not support. Make sure |
| 1906 | // that all generated operations are legalized in the next iteration. |
| 1907 | NeedsAnotherIteration = true; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1908 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1909 | switch (Node->getOpcode()) { |
| 1910 | default: |
| 1911 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 1912 | assert(0 && "Do not know how to expand this operator!"); |
| 1913 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1914 | case ISD::UNDEF: |
| 1915 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 1916 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 1917 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1918 | case ISD::Constant: { |
| 1919 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 1920 | Lo = DAG.getConstant(Cst, NVT); |
| 1921 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 1922 | break; |
| 1923 | } |
| 1924 | |
| 1925 | case ISD::CopyFromReg: { |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1926 | unsigned Reg = cast<RegSDNode>(Node)->getReg(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1927 | // Aggregate register values are always in consequtive pairs. |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 1928 | Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0)); |
| 1929 | Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 1931 | // Remember that we legalized the chain. |
| 1932 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 1933 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1934 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 1935 | break; |
| 1936 | } |
| 1937 | |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 1938 | case ISD::BUILD_PAIR: |
| 1939 | // Legalize both operands. FIXME: in the future we should handle the case |
| 1940 | // where the two elements are not legal. |
| 1941 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); |
| 1942 | Lo = LegalizeOp(Node->getOperand(0)); |
| 1943 | Hi = LegalizeOp(Node->getOperand(1)); |
| 1944 | break; |
| 1945 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1946 | case ISD::LOAD: { |
| 1947 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1948 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1949 | Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1950 | |
| 1951 | // Increment the pointer to the other half. |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 1952 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1953 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 1954 | getIntPtrConstant(IncrementSize)); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame^] | 1955 | //Is this safe? declaring that the two parts of the split load |
| 1956 | //are from the same instruction? |
| 1957 | Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1958 | |
| 1959 | // Build a factor node to remember that this load is independent of the |
| 1960 | // other one. |
| 1961 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 1962 | Hi.getValue(1)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1964 | // Remember that we legalized the chain. |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1965 | AddLegalizedOperand(Op.getValue(1), TF); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1966 | if (!TLI.isLittleEndian()) |
| 1967 | std::swap(Lo, Hi); |
| 1968 | break; |
| 1969 | } |
| 1970 | case ISD::CALL: { |
| 1971 | SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1972 | SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee. |
| 1973 | |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1974 | bool Changed = false; |
| 1975 | std::vector<SDOperand> Ops; |
| 1976 | for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) { |
| 1977 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1978 | Changed |= Ops.back() != Node->getOperand(i); |
| 1979 | } |
| 1980 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1981 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && |
| 1982 | "Can only expand a call once so far, not i64 -> i16!"); |
| 1983 | |
| 1984 | std::vector<MVT::ValueType> RetTyVTs; |
| 1985 | RetTyVTs.reserve(3); |
| 1986 | RetTyVTs.push_back(NVT); |
| 1987 | RetTyVTs.push_back(NVT); |
| 1988 | RetTyVTs.push_back(MVT::Other); |
Chris Lattner | 3d9dffc | 2005-01-19 20:24:35 +0000 | [diff] [blame] | 1989 | SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1990 | Lo = SDOperand(NC, 0); |
| 1991 | Hi = SDOperand(NC, 1); |
| 1992 | |
| 1993 | // Insert the new chain mapping. |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 1994 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1995 | break; |
| 1996 | } |
| 1997 | case ISD::AND: |
| 1998 | case ISD::OR: |
| 1999 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 2000 | SDOperand LL, LH, RL, RH; |
| 2001 | ExpandOp(Node->getOperand(0), LL, LH); |
| 2002 | ExpandOp(Node->getOperand(1), RL, RH); |
| 2003 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 2004 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 2005 | break; |
| 2006 | } |
| 2007 | case ISD::SELECT: { |
| 2008 | SDOperand C, LL, LH, RL, RH; |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2009 | |
| 2010 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2011 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 2012 | case Legal: |
| 2013 | C = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2014 | break; |
| 2015 | case Promote: |
| 2016 | C = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 2017 | break; |
| 2018 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2019 | ExpandOp(Node->getOperand(1), LL, LH); |
| 2020 | ExpandOp(Node->getOperand(2), RL, RH); |
| 2021 | Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); |
| 2022 | Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); |
| 2023 | break; |
| 2024 | } |
| 2025 | case ISD::SIGN_EXTEND: { |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2026 | SDOperand In; |
| 2027 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2028 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 2029 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 2030 | case Promote: |
| 2031 | In = PromoteOp(Node->getOperand(0)); |
| 2032 | // Emit the appropriate sign_extend_inreg to get the value we want. |
| 2033 | In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In, |
| 2034 | Node->getOperand(0).getValueType()); |
| 2035 | break; |
| 2036 | } |
| 2037 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2038 | // The low part is just a sign extension of the input (which degenerates to |
| 2039 | // a copy). |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2040 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2041 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2042 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 2043 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 2044 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2045 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 2046 | TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2047 | break; |
| 2048 | } |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2049 | case ISD::ZERO_EXTEND: { |
| 2050 | SDOperand In; |
| 2051 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2052 | case Expand: assert(0 && "expand-expand not implemented yet!"); |
| 2053 | case Legal: In = LegalizeOp(Node->getOperand(0)); break; |
| 2054 | case Promote: |
| 2055 | In = PromoteOp(Node->getOperand(0)); |
| 2056 | // Emit the appropriate zero_extend_inreg to get the value we want. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2057 | In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType()); |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2058 | break; |
| 2059 | } |
| 2060 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2061 | // The low part is just a zero extension of the input (which degenerates to |
| 2062 | // a copy). |
Chris Lattner | dea29e2 | 2005-04-10 01:13:15 +0000 | [diff] [blame] | 2063 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2065 | // The high part is just a zero. |
| 2066 | Hi = DAG.getConstant(0, NVT); |
| 2067 | break; |
Chris Lattner | 06098e0 | 2005-04-03 23:41:52 +0000 | [diff] [blame] | 2068 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2069 | // These operators cannot be expanded directly, emit them as calls to |
| 2070 | // library functions. |
| 2071 | case ISD::FP_TO_SINT: |
| 2072 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2073 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2074 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2075 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | case ISD::FP_TO_UINT: |
| 2078 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2079 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2080 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2081 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2082 | break; |
| 2083 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2084 | case ISD::SHL: |
| 2085 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2086 | if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2087 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2088 | |
| 2089 | // If this target supports SHL_PARTS, use it. |
| 2090 | if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2091 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2092 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2093 | break; |
| 2094 | } |
| 2095 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2096 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2097 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | |
| 2100 | case ISD::SRA: |
| 2101 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2102 | if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2103 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2104 | |
| 2105 | // If this target supports SRA_PARTS, use it. |
| 2106 | if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2107 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2108 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2112 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2113 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2114 | break; |
| 2115 | case ISD::SRL: |
| 2116 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2117 | if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2118 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2119 | |
| 2120 | // If this target supports SRL_PARTS, use it. |
| 2121 | if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) { |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2122 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2123 | Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2124 | break; |
| 2125 | } |
| 2126 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2127 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2128 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2129 | break; |
| 2130 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2131 | case ISD::ADD: |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2132 | ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2133 | Lo, Hi); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2134 | break; |
| 2135 | case ISD::SUB: |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 2136 | ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1), |
| 2137 | Lo, Hi); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2138 | break; |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2139 | case ISD::MUL: { |
| 2140 | if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) { |
| 2141 | SDOperand LL, LH, RL, RH; |
| 2142 | ExpandOp(Node->getOperand(0), LL, LH); |
| 2143 | ExpandOp(Node->getOperand(1), RL, RH); |
| 2144 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 2145 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 2146 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 2147 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 2148 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 2149 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 2150 | } else { |
| 2151 | Lo = ExpandLibCall("__muldi3" , Node, Hi); break; |
| 2152 | } |
| 2153 | break; |
| 2154 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2155 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 2156 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 2157 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 2158 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | // Remember in a map if the values will be reused later. |
| 2162 | if (!Node->hasOneUse()) { |
| 2163 | bool isNew = ExpandedNodes.insert(std::make_pair(Op, |
| 2164 | std::make_pair(Lo, Hi))).second; |
| 2165 | assert(isNew && "Value already expanded?!?"); |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | |
| 2170 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 2171 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2172 | void SelectionDAG::Legalize() { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2173 | /// run - This is the main entry point to this class. |
| 2174 | /// |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 2175 | SelectionDAGLegalize(*this).Run(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |