| Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file was developed by the LLVM research group and is distributed under | 
|  | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file implements the SelectionDAG::Legalize method. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | #include "llvm/CodeGen/SelectionDAG.h" | 
|  | 15 | #include "llvm/CodeGen/MachineConstantPool.h" | 
|  | 16 | #include "llvm/CodeGen/MachineFunction.h" | 
|  | 17 | #include "llvm/Target/TargetLowering.h" | 
|  | 18 | #include "llvm/Constants.h" | 
|  | 19 | #include <iostream> | 
|  | 20 | using namespace llvm; | 
|  | 21 |  | 
|  | 22 | //===----------------------------------------------------------------------===// | 
|  | 23 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and | 
|  | 24 | /// hacks on it until the target machine can handle it.  This involves | 
|  | 25 | /// eliminating value sizes the machine cannot handle (promoting small sizes to | 
|  | 26 | /// large sizes or splitting up large values into small values) as well as | 
|  | 27 | /// eliminating operations the machine cannot handle. | 
|  | 28 | /// | 
|  | 29 | /// This code also does a small amount of optimization and recognition of idioms | 
|  | 30 | /// as part of its processing.  For example, if a target does not support a | 
|  | 31 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this | 
|  | 32 | /// will attempt merge setcc and brc instructions into brcc's. | 
|  | 33 | /// | 
|  | 34 | namespace { | 
|  | 35 | class SelectionDAGLegalize { | 
|  | 36 | TargetLowering &TLI; | 
|  | 37 | SelectionDAG &DAG; | 
|  | 38 |  | 
|  | 39 | /// LegalizeAction - This enum indicates what action we should take for each | 
|  | 40 | /// value type the can occur in the program. | 
|  | 41 | enum LegalizeAction { | 
|  | 42 | Legal,            // The target natively supports this value type. | 
|  | 43 | Promote,          // This should be promoted to the next larger type. | 
|  | 44 | Expand,           // This integer type should be broken into smaller pieces. | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | /// TransformToType - For any value types we are promoting or expanding, this | 
|  | 48 | /// contains the value type that we are changing to.  For Expanded types, this | 
|  | 49 | /// contains one step of the expand (e.g. i64 -> i32), even if there are | 
|  | 50 | /// multiple steps required (e.g. i64 -> i16) | 
|  | 51 | MVT::ValueType TransformToType[MVT::LAST_VALUETYPE]; | 
|  | 52 |  | 
|  | 53 | /// ValueTypeActions - This is a bitvector that contains two bits for each | 
|  | 54 | /// value type, where the two bits correspond to the LegalizeAction enum. | 
|  | 55 | /// This can be queried with "getTypeAction(VT)". | 
|  | 56 | unsigned ValueTypeActions; | 
|  | 57 |  | 
|  | 58 | /// NeedsAnotherIteration - This is set when we expand a large integer | 
|  | 59 | /// operation into smaller integer operations, but the smaller operations are | 
|  | 60 | /// not set.  This occurs only rarely in practice, for targets that don't have | 
|  | 61 | /// 32-bit or larger integer registers. | 
|  | 62 | bool NeedsAnotherIteration; | 
|  | 63 |  | 
|  | 64 | /// LegalizedNodes - For nodes that are of legal width, and that have more | 
|  | 65 | /// than one use, this map indicates what regularized operand to use.  This | 
|  | 66 | /// allows us to avoid legalizing the same thing more than once. | 
|  | 67 | std::map<SDOperand, SDOperand> LegalizedNodes; | 
|  | 68 |  | 
|  | 69 | /// ExpandedNodes - For nodes that need to be expanded, and which have more | 
|  | 70 | /// than one use, this map indicates which which operands are the expanded | 
|  | 71 | /// version of the input.  This allows us to avoid expanding the same node | 
|  | 72 | /// more than once. | 
|  | 73 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; | 
|  | 74 |  | 
|  | 75 | /// setValueTypeAction - Set the action for a particular value type.  This | 
|  | 76 | /// assumes an action has not already been set for this value type. | 
|  | 77 | void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) { | 
|  | 78 | ValueTypeActions |= A << (VT*2); | 
|  | 79 | if (A == Promote) { | 
|  | 80 | MVT::ValueType PromoteTo; | 
|  | 81 | if (VT == MVT::f32) | 
|  | 82 | PromoteTo = MVT::f64; | 
|  | 83 | else { | 
|  | 84 | unsigned LargerReg = VT+1; | 
|  | 85 | while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) { | 
|  | 86 | ++LargerReg; | 
|  | 87 | assert(MVT::isInteger((MVT::ValueType)LargerReg) && | 
|  | 88 | "Nothing to promote to??"); | 
|  | 89 | } | 
|  | 90 | PromoteTo = (MVT::ValueType)LargerReg; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) && | 
|  | 94 | MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) && | 
|  | 95 | "Can only promote from int->int or fp->fp!"); | 
|  | 96 | assert(VT < PromoteTo && "Must promote to a larger type!"); | 
|  | 97 | TransformToType[VT] = PromoteTo; | 
|  | 98 | } else if (A == Expand) { | 
|  | 99 | assert(MVT::isInteger(VT) && VT > MVT::i8 && | 
|  | 100 | "Cannot expand this type: target must support SOME integer reg!"); | 
|  | 101 | // Expand to the next smaller integer type! | 
|  | 102 | TransformToType[VT] = (MVT::ValueType)(VT-1); | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | public: | 
|  | 107 |  | 
|  | 108 | SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG); | 
|  | 109 |  | 
|  | 110 | /// Run - While there is still lowering to do, perform a pass over the DAG. | 
|  | 111 | /// Most regularization can be done in a single pass, but targets that require | 
|  | 112 | /// large values to be split into registers multiple times (e.g. i64 -> 4x | 
|  | 113 | /// i16) require iteration for these values (the first iteration will demote | 
|  | 114 | /// to i32, the second will demote to i16). | 
|  | 115 | void Run() { | 
|  | 116 | do { | 
|  | 117 | NeedsAnotherIteration = false; | 
|  | 118 | LegalizeDAG(); | 
|  | 119 | } while (NeedsAnotherIteration); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | /// getTypeAction - Return how we should legalize values of this type, either | 
|  | 123 | /// it is already legal or we need to expand it into multiple registers of | 
|  | 124 | /// smaller integer type, or we need to promote it to a larger type. | 
|  | 125 | LegalizeAction getTypeAction(MVT::ValueType VT) const { | 
|  | 126 | return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | /// isTypeLegal - Return true if this type is legal on this target. | 
|  | 130 | /// | 
|  | 131 | bool isTypeLegal(MVT::ValueType VT) const { | 
|  | 132 | return getTypeAction(VT) == Legal; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | private: | 
|  | 136 | void LegalizeDAG(); | 
|  | 137 |  | 
|  | 138 | SDOperand LegalizeOp(SDOperand O); | 
|  | 139 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); | 
|  | 140 |  | 
|  | 141 | SDOperand getIntPtrConstant(uint64_t Val) { | 
|  | 142 | return DAG.getConstant(Val, TLI.getPointerTy()); | 
|  | 143 | } | 
|  | 144 | }; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 |  | 
|  | 148 | SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli, | 
|  | 149 | SelectionDAG &dag) | 
|  | 150 | : TLI(tli), DAG(dag), ValueTypeActions(0) { | 
|  | 151 |  | 
|  | 152 | assert(MVT::LAST_VALUETYPE <= 16 && | 
|  | 153 | "Too many value types for ValueTypeActions to hold!"); | 
|  | 154 |  | 
|  | 155 | // Inspect all of the ValueType's possible, deciding how to process them. | 
|  | 156 | for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg) | 
|  | 157 | // If TLI says we are expanding this type, expand it! | 
|  | 158 | if (TLI.getNumElements((MVT::ValueType)IntReg) != 1) | 
|  | 159 | setValueTypeAction((MVT::ValueType)IntReg, Expand); | 
|  | 160 | else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg)) | 
|  | 161 | // Otherwise, if we don't have native support, we must promote to a | 
|  | 162 | // larger type. | 
|  | 163 | setValueTypeAction((MVT::ValueType)IntReg, Promote); | 
|  | 164 |  | 
|  | 165 | // If the target does not have native support for F32, promote it to F64. | 
|  | 166 | if (!TLI.hasNativeSupportFor(MVT::f32)) | 
|  | 167 | setValueTypeAction(MVT::f32, Promote); | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 |  | 
|  | 171 | void SelectionDAGLegalize::LegalizeDAG() { | 
|  | 172 | SDOperand OldRoot = DAG.getRoot(); | 
|  | 173 | SDOperand NewRoot = LegalizeOp(OldRoot); | 
|  | 174 | DAG.setRoot(NewRoot); | 
|  | 175 |  | 
|  | 176 | ExpandedNodes.clear(); | 
|  | 177 | LegalizedNodes.clear(); | 
|  | 178 |  | 
|  | 179 | // Remove dead nodes now. | 
|  | 180 | if (OldRoot != NewRoot) | 
|  | 181 | // Delete all of these efficiently first. | 
|  | 182 | ; | 
|  | 183 |  | 
|  | 184 | // Then scan AllNodes. | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { | 
|  | 188 | // If this operation defines any values that cannot be represented in a | 
|  | 189 | // register on this target, make sure to expand it. | 
|  | 190 | if (Op.Val->getNumValues() == 1) {// Fast path == assertion only | 
|  | 191 | assert(getTypeAction(Op.Val->getValueType(0)) == Legal && | 
|  | 192 | "For a single use value, caller should check for legality!"); | 
|  | 193 | } else { | 
|  | 194 | for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i) | 
|  | 195 | switch (getTypeAction(Op.Val->getValueType(i))) { | 
|  | 196 | case Legal: break;  // Nothing to do. | 
|  | 197 | case Expand: { | 
|  | 198 | SDOperand T1, T2; | 
|  | 199 | ExpandOp(Op.getValue(i), T1, T2); | 
|  | 200 | assert(LegalizedNodes.count(Op) && | 
|  | 201 | "Expansion didn't add legal operands!"); | 
|  | 202 | return LegalizedNodes[Op]; | 
|  | 203 | } | 
|  | 204 | case Promote: | 
|  | 205 | // FIXME: Implement promotion! | 
|  | 206 | assert(0 && "Promotion not implemented at all yet!"); | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | // If there is more than one use of this, see if we already legalized it. | 
|  | 211 | // There is no use remembering values that only have a single use, as the map | 
|  | 212 | // entries will never be reused. | 
|  | 213 | if (!Op.Val->hasOneUse()) { | 
|  | 214 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); | 
|  | 215 | if (I != LegalizedNodes.end()) return I->second; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | SDOperand Tmp1, Tmp2; | 
|  | 219 |  | 
|  | 220 | SDOperand Result = Op; | 
|  | 221 | SDNode *Node = Op.Val; | 
|  | 222 | LegalizeAction Action; | 
|  | 223 |  | 
|  | 224 | switch (Node->getOpcode()) { | 
|  | 225 | default: | 
|  | 226 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; | 
|  | 227 | assert(0 && "Do not know how to legalize this operator!"); | 
|  | 228 | abort(); | 
|  | 229 | case ISD::EntryToken: | 
|  | 230 | case ISD::FrameIndex: | 
|  | 231 | case ISD::GlobalAddress: | 
|  | 232 | case ISD::ConstantPool: | 
|  | 233 | case ISD::CopyFromReg:            // Nothing to do. | 
|  | 234 | assert(getTypeAction(Node->getValueType(0)) == Legal && | 
|  | 235 | "This must be legal!"); | 
|  | 236 | break; | 
|  | 237 | case ISD::Constant: | 
|  | 238 | // We know we don't need to expand constants here, constants only have one | 
|  | 239 | // value and we check that it is fine above. | 
|  | 240 |  | 
|  | 241 | // FIXME: Maybe we should handle things like targets that don't support full | 
|  | 242 | // 32-bit immediates? | 
|  | 243 | break; | 
|  | 244 | case ISD::ConstantFP: { | 
|  | 245 | // Spill FP immediates to the constant pool if the target cannot directly | 
|  | 246 | // codegen them.  Targets often have some immediate values that can be | 
|  | 247 | // efficiently generated into an FP register without a load.  We explicitly | 
|  | 248 | // leave these constants as ConstantFP nodes for the target to deal with. | 
|  | 249 |  | 
|  | 250 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); | 
|  | 251 |  | 
|  | 252 | // Check to see if this FP immediate is already legal. | 
|  | 253 | bool isLegal = false; | 
|  | 254 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), | 
|  | 255 | E = TLI.legal_fpimm_end(); I != E; ++I) | 
|  | 256 | if (CFP->isExactlyValue(*I)) { | 
|  | 257 | isLegal = true; | 
|  | 258 | break; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | if (!isLegal) { | 
|  | 262 | // Otherwise we need to spill the constant to memory. | 
|  | 263 | MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool(); | 
|  | 264 |  | 
|  | 265 | bool Extend = false; | 
|  | 266 |  | 
|  | 267 | // If a FP immediate is precise when represented as a float, we put it | 
|  | 268 | // into the constant pool as a float, even if it's is statically typed | 
|  | 269 | // as a double. | 
|  | 270 | MVT::ValueType VT = CFP->getValueType(0); | 
|  | 271 | bool isDouble = VT == MVT::f64; | 
|  | 272 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : | 
|  | 273 | Type::FloatTy, CFP->getValue()); | 
|  | 274 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) { | 
|  | 275 | LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); | 
|  | 276 | VT = MVT::f32; | 
|  | 277 | Extend = true; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC), | 
|  | 281 | TLI.getPointerTy()); | 
|  | 282 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx); | 
|  | 283 |  | 
|  | 284 | if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result); | 
|  | 285 | } | 
|  | 286 | break; | 
|  | 287 | } | 
|  | 288 | case ISD::ADJCALLSTACKDOWN: | 
|  | 289 | case ISD::ADJCALLSTACKUP: | 
|  | 290 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 291 | // There is no need to legalize the size argument (Operand #1) | 
|  | 292 | if (Tmp1 != Node->getOperand(0)) | 
|  | 293 | Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, | 
|  | 294 | Node->getOperand(1)); | 
|  | 295 | break; | 
|  | 296 | case ISD::CALL: | 
|  | 297 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 298 | Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the callee. | 
|  | 299 | if (Tmp2 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { | 
|  | 300 | std::vector<MVT::ValueType> RetTyVTs; | 
|  | 301 | RetTyVTs.reserve(Node->getNumValues()); | 
|  | 302 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) | 
|  | 303 | RetTyVTs.push_back(Node->getValueType(0)); | 
|  | 304 | Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo); | 
|  | 305 | } | 
|  | 306 | break; | 
|  | 307 |  | 
| Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame^] | 308 | case ISD::BRCOND: | 
|  | 309 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 310 | // FIXME: booleans might not be legal! | 
|  | 311 | Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the condition. | 
|  | 312 | // Basic block destination (Op#2) is always legal. | 
|  | 313 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) | 
|  | 314 | Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, | 
|  | 315 | Node->getOperand(2)); | 
|  | 316 | break; | 
|  | 317 |  | 
| Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 318 | case ISD::LOAD: | 
|  | 319 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 320 | Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer. | 
|  | 321 | if (Tmp1 != Node->getOperand(0) || | 
|  | 322 | Tmp2 != Node->getOperand(1)) | 
|  | 323 | Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2); | 
|  | 324 | break; | 
|  | 325 |  | 
|  | 326 | case ISD::EXTRACT_ELEMENT: | 
|  | 327 | // Get both the low and high parts. | 
|  | 328 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); | 
|  | 329 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) | 
|  | 330 | Result = Tmp2;  // 1 -> Hi | 
|  | 331 | else | 
|  | 332 | Result = Tmp1;  // 0 -> Lo | 
|  | 333 | break; | 
|  | 334 |  | 
|  | 335 | case ISD::CopyToReg: | 
|  | 336 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 337 |  | 
|  | 338 | switch (getTypeAction(Node->getOperand(1).getValueType())) { | 
|  | 339 | case Legal: | 
|  | 340 | // Legalize the incoming value (must be legal). | 
|  | 341 | Tmp2 = LegalizeOp(Node->getOperand(1)); | 
|  | 342 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) | 
|  | 343 | Result = DAG.getCopyToReg(Tmp1, Tmp2, | 
|  | 344 | cast<CopyRegSDNode>(Node)->getReg()); | 
|  | 345 | break; | 
|  | 346 | case Expand: { | 
|  | 347 | SDOperand Lo, Hi; | 
|  | 348 | ExpandOp(Node->getOperand(1), Lo, Hi); | 
|  | 349 | unsigned Reg = cast<CopyRegSDNode>(Node)->getReg(); | 
|  | 350 | Result = DAG.getCopyToReg(Tmp1, Lo, Reg); | 
|  | 351 | Result = DAG.getCopyToReg(Result, Hi, Reg+1); | 
|  | 352 | assert(isTypeLegal(Result.getValueType()) && | 
|  | 353 | "Cannot expand multiple times yet (i64 -> i16)"); | 
|  | 354 | break; | 
|  | 355 | } | 
|  | 356 | case Promote: | 
|  | 357 | assert(0 && "Don't know what it means to promote this!"); | 
|  | 358 | abort(); | 
|  | 359 | } | 
|  | 360 | break; | 
|  | 361 |  | 
|  | 362 | case ISD::RET: | 
|  | 363 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 364 | switch (Node->getNumOperands()) { | 
|  | 365 | case 2:  // ret val | 
|  | 366 | switch (getTypeAction(Node->getOperand(1).getValueType())) { | 
|  | 367 | case Legal: | 
|  | 368 | Tmp2 = LegalizeOp(Node->getOperand(1)); | 
|  | 369 | if (Tmp2 != Node->getOperand(1)) | 
|  | 370 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); | 
|  | 371 | break; | 
|  | 372 | case Expand: { | 
|  | 373 | SDOperand Lo, Hi; | 
|  | 374 | ExpandOp(Node->getOperand(1), Lo, Hi); | 
|  | 375 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); | 
|  | 376 | break; | 
|  | 377 | } | 
|  | 378 | case Promote: | 
|  | 379 | assert(0 && "Can't promote return value!"); | 
|  | 380 | } | 
|  | 381 | break; | 
|  | 382 | case 1:  // ret void | 
|  | 383 | if (Tmp1 != Node->getOperand(0)) | 
|  | 384 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); | 
|  | 385 | break; | 
|  | 386 | default: { // ret <values> | 
|  | 387 | std::vector<SDOperand> NewValues; | 
|  | 388 | NewValues.push_back(Tmp1); | 
|  | 389 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) | 
|  | 390 | switch (getTypeAction(Node->getOperand(i).getValueType())) { | 
|  | 391 | case Legal: | 
|  | 392 | NewValues.push_back(LegalizeOp(Node->getOperand(1))); | 
|  | 393 | break; | 
|  | 394 | case Expand: { | 
|  | 395 | SDOperand Lo, Hi; | 
|  | 396 | ExpandOp(Node->getOperand(i), Lo, Hi); | 
|  | 397 | NewValues.push_back(Lo); | 
|  | 398 | NewValues.push_back(Hi); | 
|  | 399 | break; | 
|  | 400 | } | 
|  | 401 | case Promote: | 
|  | 402 | assert(0 && "Can't promote return value!"); | 
|  | 403 | } | 
|  | 404 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); | 
|  | 405 | break; | 
|  | 406 | } | 
|  | 407 | } | 
|  | 408 | break; | 
|  | 409 | case ISD::STORE: | 
|  | 410 | Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 411 | Tmp2 = LegalizeOp(Node->getOperand(2));  // Legalize the pointer. | 
|  | 412 |  | 
|  | 413 | switch (getTypeAction(Node->getOperand(1).getValueType())) { | 
|  | 414 | case Legal: { | 
|  | 415 | SDOperand Val = LegalizeOp(Node->getOperand(1)); | 
|  | 416 | if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) || | 
|  | 417 | Tmp2 != Node->getOperand(2)) | 
|  | 418 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2); | 
|  | 419 | break; | 
|  | 420 | } | 
|  | 421 | case Promote: | 
|  | 422 | assert(0 && "FIXME: promote for stores not implemented!"); | 
|  | 423 | case Expand: | 
|  | 424 | SDOperand Lo, Hi; | 
|  | 425 | ExpandOp(Node->getOperand(1), Lo, Hi); | 
|  | 426 |  | 
|  | 427 | if (!TLI.isLittleEndian()) | 
|  | 428 | std::swap(Lo, Hi); | 
|  | 429 |  | 
|  | 430 | // FIXME: These two stores are independent of each other! | 
|  | 431 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2); | 
|  | 432 |  | 
|  | 433 | unsigned IncrementSize; | 
|  | 434 | switch (Lo.getValueType()) { | 
|  | 435 | default: assert(0 && "Unknown ValueType to expand to!"); | 
|  | 436 | case MVT::i32: IncrementSize = 4; break; | 
|  | 437 | case MVT::i16: IncrementSize = 2; break; | 
|  | 438 | case MVT::i8:  IncrementSize = 1; break; | 
|  | 439 | } | 
|  | 440 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, | 
|  | 441 | getIntPtrConstant(IncrementSize)); | 
|  | 442 | assert(isTypeLegal(Tmp2.getValueType()) && | 
|  | 443 | "Pointers must be legal!"); | 
|  | 444 | Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2); | 
|  | 445 | } | 
|  | 446 | break; | 
|  | 447 | case ISD::SELECT: { | 
|  | 448 | // FIXME: BOOLS MAY REQUIRE PROMOTION! | 
|  | 449 | Tmp1 = LegalizeOp(Node->getOperand(0));   // Cond | 
|  | 450 | Tmp2 = LegalizeOp(Node->getOperand(1));   // TrueVal | 
|  | 451 | SDOperand Tmp3 = LegalizeOp(Node->getOperand(2));   // FalseVal | 
|  | 452 |  | 
|  | 453 | if (Tmp1 != Node->getOperand(0) || | 
|  | 454 | Tmp2 != Node->getOperand(1) || | 
|  | 455 | Tmp3 != Node->getOperand(2)) | 
|  | 456 | Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3); | 
|  | 457 | break; | 
|  | 458 | } | 
|  | 459 | case ISD::SETCC: | 
|  | 460 | switch (getTypeAction(Node->getOperand(0).getValueType())) { | 
|  | 461 | case Legal: | 
|  | 462 | Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS | 
|  | 463 | Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS | 
|  | 464 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) | 
|  | 465 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), | 
|  | 466 | Tmp1, Tmp2); | 
|  | 467 | break; | 
|  | 468 | case Promote: | 
|  | 469 | assert(0 && "Can't promote setcc operands yet!"); | 
|  | 470 | break; | 
|  | 471 | case Expand: | 
|  | 472 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; | 
|  | 473 | ExpandOp(Node->getOperand(0), LHSLo, LHSHi); | 
|  | 474 | ExpandOp(Node->getOperand(1), RHSLo, RHSHi); | 
|  | 475 | switch (cast<SetCCSDNode>(Node)->getCondition()) { | 
|  | 476 | case ISD::SETEQ: | 
|  | 477 | case ISD::SETNE: | 
|  | 478 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); | 
|  | 479 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); | 
|  | 480 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); | 
|  | 481 | Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1, | 
|  | 482 | DAG.getConstant(0, Tmp1.getValueType())); | 
|  | 483 | break; | 
|  | 484 | default: | 
|  | 485 | // FIXME: This generated code sucks. | 
|  | 486 | ISD::CondCode LowCC; | 
|  | 487 | switch (cast<SetCCSDNode>(Node)->getCondition()) { | 
|  | 488 | default: assert(0 && "Unknown integer setcc!"); | 
|  | 489 | case ISD::SETLT: | 
|  | 490 | case ISD::SETULT: LowCC = ISD::SETULT; break; | 
|  | 491 | case ISD::SETGT: | 
|  | 492 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; | 
|  | 493 | case ISD::SETLE: | 
|  | 494 | case ISD::SETULE: LowCC = ISD::SETULE; break; | 
|  | 495 | case ISD::SETGE: | 
|  | 496 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison | 
|  | 500 | // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands | 
|  | 501 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; | 
|  | 502 |  | 
|  | 503 | // NOTE: on targets without efficient SELECT of bools, we can always use | 
|  | 504 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) | 
|  | 505 | Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo); | 
|  | 506 | Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), | 
|  | 507 | LHSHi, RHSHi); | 
|  | 508 | Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi); | 
|  | 509 | Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2); | 
|  | 510 | break; | 
|  | 511 | } | 
|  | 512 | } | 
|  | 513 | break; | 
|  | 514 |  | 
|  | 515 | case ISD::ADD: | 
|  | 516 | case ISD::SUB: | 
|  | 517 | case ISD::MUL: | 
|  | 518 | case ISD::UDIV: | 
|  | 519 | case ISD::SDIV: | 
|  | 520 | case ISD::UREM: | 
|  | 521 | case ISD::SREM: | 
|  | 522 | case ISD::AND: | 
|  | 523 | case ISD::OR: | 
|  | 524 | case ISD::XOR: | 
|  | 525 | Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS | 
|  | 526 | Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS | 
|  | 527 | if (Tmp1 != Node->getOperand(0) || | 
|  | 528 | Tmp2 != Node->getOperand(1)) | 
|  | 529 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); | 
|  | 530 | break; | 
|  | 531 | case ISD::ZERO_EXTEND: | 
|  | 532 | case ISD::SIGN_EXTEND: | 
|  | 533 | switch (getTypeAction(Node->getOperand(0).getValueType())) { | 
|  | 534 | case Legal: | 
|  | 535 | Tmp1 = LegalizeOp(Node->getOperand(0)); | 
|  | 536 | if (Tmp1 != Node->getOperand(0)) | 
|  | 537 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); | 
|  | 538 | break; | 
|  | 539 | default: | 
|  | 540 | assert(0 && "Do not know how to expand or promote this yet!"); | 
|  | 541 | } | 
|  | 542 | break; | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | if (!Op.Val->hasOneUse()) { | 
|  | 546 | bool isNew = LegalizedNodes.insert(std::make_pair(Op, Result)).second; | 
|  | 547 | assert(isNew && "Got into the map somehow?"); | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | return Result; | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 |  | 
|  | 554 | /// ExpandOp - Expand the specified SDOperand into its two component pieces | 
|  | 555 | /// Lo&Hi.  Note that the Op MUST be an expanded type.  As a result of this, the | 
|  | 556 | /// LegalizeNodes map is filled in for any results that are not expanded, the | 
|  | 557 | /// ExpandedNodes map is filled in for any results that are expanded, and the | 
|  | 558 | /// Lo/Hi values are returned. | 
|  | 559 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ | 
|  | 560 | MVT::ValueType VT = Op.getValueType(); | 
|  | 561 | MVT::ValueType NVT = TransformToType[VT]; | 
|  | 562 | SDNode *Node = Op.Val; | 
|  | 563 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); | 
|  | 564 | assert(MVT::isInteger(VT) && "Cannot expand FP values!"); | 
|  | 565 | assert(MVT::isInteger(NVT) && NVT < VT && | 
|  | 566 | "Cannot expand to FP value or to larger int value!"); | 
|  | 567 |  | 
|  | 568 | // If there is more than one use of this, see if we already expanded it. | 
|  | 569 | // There is no use remembering values that only have a single use, as the map | 
|  | 570 | // entries will never be reused. | 
|  | 571 | if (!Node->hasOneUse()) { | 
|  | 572 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I | 
|  | 573 | = ExpandedNodes.find(Op); | 
|  | 574 | if (I != ExpandedNodes.end()) { | 
|  | 575 | Lo = I->second.first; | 
|  | 576 | Hi = I->second.second; | 
|  | 577 | return; | 
|  | 578 | } | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | // If we are lowering to a type that the target doesn't support, we will have | 
|  | 582 | // to iterate lowering. | 
|  | 583 | if (!isTypeLegal(NVT)) | 
|  | 584 | NeedsAnotherIteration = true; | 
|  | 585 |  | 
|  | 586 | LegalizeAction Action; | 
|  | 587 | switch (Node->getOpcode()) { | 
|  | 588 | default: | 
|  | 589 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; | 
|  | 590 | assert(0 && "Do not know how to expand this operator!"); | 
|  | 591 | abort(); | 
|  | 592 | case ISD::Constant: { | 
|  | 593 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); | 
|  | 594 | Lo = DAG.getConstant(Cst, NVT); | 
|  | 595 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); | 
|  | 596 | break; | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | case ISD::CopyFromReg: { | 
|  | 600 | unsigned Reg = cast<CopyRegSDNode>(Node)->getReg(); | 
|  | 601 | // Aggregate register values are always in consequtive pairs. | 
|  | 602 | Lo = DAG.getCopyFromReg(Reg, NVT); | 
|  | 603 | Hi = DAG.getCopyFromReg(Reg+1, NVT); | 
|  | 604 | assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!"); | 
|  | 605 | break; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | case ISD::LOAD: { | 
|  | 609 | SDOperand Ch = LegalizeOp(Node->getOperand(0));   // Legalize the chain. | 
|  | 610 | SDOperand Ptr = LegalizeOp(Node->getOperand(1));  // Legalize the pointer. | 
|  | 611 | Lo = DAG.getLoad(NVT, Ch, Ptr); | 
|  | 612 |  | 
|  | 613 | // Increment the pointer to the other half. | 
|  | 614 | unsigned IncrementSize; | 
|  | 615 | switch (Lo.getValueType()) { | 
|  | 616 | default: assert(0 && "Unknown ValueType to expand to!"); | 
|  | 617 | case MVT::i32: IncrementSize = 4; break; | 
|  | 618 | case MVT::i16: IncrementSize = 2; break; | 
|  | 619 | case MVT::i8:  IncrementSize = 1; break; | 
|  | 620 | } | 
|  | 621 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, | 
|  | 622 | getIntPtrConstant(IncrementSize)); | 
|  | 623 | // FIXME: This load is independent of the first one. | 
|  | 624 | Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr); | 
|  | 625 |  | 
|  | 626 | // Remember that we legalized the chain. | 
|  | 627 | bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1), | 
|  | 628 | Hi.getValue(1))).second; | 
|  | 629 | assert(isNew && "This node was already legalized!"); | 
|  | 630 | if (!TLI.isLittleEndian()) | 
|  | 631 | std::swap(Lo, Hi); | 
|  | 632 | break; | 
|  | 633 | } | 
|  | 634 | case ISD::CALL: { | 
|  | 635 | SDOperand Chain  = LegalizeOp(Node->getOperand(0));  // Legalize the chain. | 
|  | 636 | SDOperand Callee = LegalizeOp(Node->getOperand(1));  // Legalize the callee. | 
|  | 637 |  | 
|  | 638 | assert(Node->getNumValues() == 2 && Op.ResNo == 0 && | 
|  | 639 | "Can only expand a call once so far, not i64 -> i16!"); | 
|  | 640 |  | 
|  | 641 | std::vector<MVT::ValueType> RetTyVTs; | 
|  | 642 | RetTyVTs.reserve(3); | 
|  | 643 | RetTyVTs.push_back(NVT); | 
|  | 644 | RetTyVTs.push_back(NVT); | 
|  | 645 | RetTyVTs.push_back(MVT::Other); | 
|  | 646 | SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee); | 
|  | 647 | Lo = SDOperand(NC, 0); | 
|  | 648 | Hi = SDOperand(NC, 1); | 
|  | 649 |  | 
|  | 650 | // Insert the new chain mapping. | 
|  | 651 | bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1), | 
|  | 652 | Hi.getValue(2))).second; | 
|  | 653 | assert(isNew && "This node was already legalized!"); | 
|  | 654 | break; | 
|  | 655 | } | 
|  | 656 | case ISD::AND: | 
|  | 657 | case ISD::OR: | 
|  | 658 | case ISD::XOR: {   // Simple logical operators -> two trivial pieces. | 
|  | 659 | SDOperand LL, LH, RL, RH; | 
|  | 660 | ExpandOp(Node->getOperand(0), LL, LH); | 
|  | 661 | ExpandOp(Node->getOperand(1), RL, RH); | 
|  | 662 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); | 
|  | 663 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); | 
|  | 664 | break; | 
|  | 665 | } | 
|  | 666 | case ISD::SELECT: { | 
|  | 667 | SDOperand C, LL, LH, RL, RH; | 
|  | 668 | // FIXME: BOOLS MAY REQUIRE PROMOTION! | 
|  | 669 | C = LegalizeOp(Node->getOperand(0)); | 
|  | 670 | ExpandOp(Node->getOperand(1), LL, LH); | 
|  | 671 | ExpandOp(Node->getOperand(2), RL, RH); | 
|  | 672 | Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL); | 
|  | 673 | Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH); | 
|  | 674 | break; | 
|  | 675 | } | 
|  | 676 | case ISD::SIGN_EXTEND: { | 
|  | 677 | // The low part is just a sign extension of the input (which degenerates to | 
|  | 678 | // a copy). | 
|  | 679 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); | 
|  | 680 |  | 
|  | 681 | // The high part is obtained by SRA'ing all but one of the bits of the lo | 
|  | 682 | // part. | 
|  | 683 | unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType()); | 
|  | 684 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8)); | 
|  | 685 | break; | 
|  | 686 | } | 
|  | 687 | case ISD::ZERO_EXTEND: | 
|  | 688 | // The low part is just a zero extension of the input (which degenerates to | 
|  | 689 | // a copy). | 
|  | 690 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0))); | 
|  | 691 |  | 
|  | 692 | // The high part is just a zero. | 
|  | 693 | Hi = DAG.getConstant(0, NVT); | 
|  | 694 | break; | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | // Remember in a map if the values will be reused later. | 
|  | 698 | if (!Node->hasOneUse()) { | 
|  | 699 | bool isNew = ExpandedNodes.insert(std::make_pair(Op, | 
|  | 700 | std::make_pair(Lo, Hi))).second; | 
|  | 701 | assert(isNew && "Value already expanded?!?"); | 
|  | 702 | } | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 |  | 
|  | 706 | // SelectionDAG::Legalize - This is the entry point for the file. | 
|  | 707 | // | 
|  | 708 | void SelectionDAG::Legalize(TargetLowering &TLI) { | 
|  | 709 | /// run - This is the main entry point to this class. | 
|  | 710 | /// | 
|  | 711 | SelectionDAGLegalize(TLI, *this).Run(); | 
|  | 712 | } | 
|  | 713 |  |