Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating from LLVM IR into SelectionDAG IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "SelectionDAGBuild.h" |
| 16 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 5b22980 | 2008-09-04 20:49:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/CallingConv.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 24 | #include "llvm/InlineAsm.h" |
| 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Intrinsics.h" |
| 27 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/FastISel.h" |
| 29 | #include "llvm/CodeGen/GCStrategy.h" |
| 30 | #include "llvm/CodeGen/GCMetadata.h" |
| 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 34 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 35 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 37 | #include "llvm/CodeGen/SelectionDAG.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
| 39 | #include "llvm/Target/TargetData.h" |
| 40 | #include "llvm/Target/TargetFrameInfo.h" |
| 41 | #include "llvm/Target/TargetInstrInfo.h" |
| 42 | #include "llvm/Target/TargetLowering.h" |
| 43 | #include "llvm/Target/TargetMachine.h" |
| 44 | #include "llvm/Target/TargetOptions.h" |
| 45 | #include "llvm/Support/Compiler.h" |
| 46 | #include "llvm/Support/Debug.h" |
| 47 | #include "llvm/Support/MathExtras.h" |
| 48 | #include <algorithm> |
| 49 | using namespace llvm; |
| 50 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 51 | /// LimitFloatPrecision - Generate low-precision inline sequences for |
| 52 | /// some float libcalls (6, 8 or 12 bits). |
| 53 | static unsigned LimitFloatPrecision; |
| 54 | |
| 55 | static cl::opt<unsigned, true> |
| 56 | LimitFPPrecision("limit-float-precision", |
| 57 | cl::desc("Generate low-precision inline sequences " |
| 58 | "for some float libcalls"), |
| 59 | cl::location(LimitFloatPrecision), |
| 60 | cl::init(0)); |
| 61 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 62 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
| 63 | /// insertvalue or extractvalue indices that identify a member, return |
| 64 | /// the linearized index of the start of the member. |
| 65 | /// |
| 66 | static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, |
| 67 | const unsigned *Indices, |
| 68 | const unsigned *IndicesEnd, |
| 69 | unsigned CurIndex = 0) { |
| 70 | // Base case: We're done. |
| 71 | if (Indices && Indices == IndicesEnd) |
| 72 | return CurIndex; |
| 73 | |
| 74 | // Given a struct type, recursively traverse the elements. |
| 75 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 76 | for (StructType::element_iterator EB = STy->element_begin(), |
| 77 | EI = EB, |
| 78 | EE = STy->element_end(); |
| 79 | EI != EE; ++EI) { |
| 80 | if (Indices && *Indices == unsigned(EI - EB)) |
| 81 | return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); |
| 82 | CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); |
| 83 | } |
| 84 | } |
| 85 | // Given an array type, recursively traverse the elements. |
| 86 | else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 87 | const Type *EltTy = ATy->getElementType(); |
| 88 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 89 | if (Indices && *Indices == i) |
| 90 | return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); |
| 91 | CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); |
| 92 | } |
| 93 | } |
| 94 | // We haven't found the type we're looking for, so keep searching. |
| 95 | return CurIndex + 1; |
| 96 | } |
| 97 | |
| 98 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 99 | /// MVTs that represent all the individual underlying |
| 100 | /// non-aggregate types that comprise it. |
| 101 | /// |
| 102 | /// If Offsets is non-null, it points to a vector to be filled in |
| 103 | /// with the in-memory offsets of each of the individual values. |
| 104 | /// |
| 105 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
| 106 | SmallVectorImpl<MVT> &ValueVTs, |
| 107 | SmallVectorImpl<uint64_t> *Offsets = 0, |
| 108 | uint64_t StartingOffset = 0) { |
| 109 | // Given a struct type, recursively traverse the elements. |
| 110 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 111 | const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); |
| 112 | for (StructType::element_iterator EB = STy->element_begin(), |
| 113 | EI = EB, |
| 114 | EE = STy->element_end(); |
| 115 | EI != EE; ++EI) |
| 116 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 117 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 118 | return; |
| 119 | } |
| 120 | // Given an array type, recursively traverse the elements. |
| 121 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 122 | const Type *EltTy = ATy->getElementType(); |
| 123 | uint64_t EltSize = TLI.getTargetData()->getABITypeSize(EltTy); |
| 124 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 125 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 126 | StartingOffset + i * EltSize); |
| 127 | return; |
| 128 | } |
| 129 | // Base case: we can get an MVT for this LLVM IR type. |
| 130 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 131 | if (Offsets) |
| 132 | Offsets->push_back(StartingOffset); |
| 133 | } |
| 134 | |
Dan Gohman | 2a7c671 | 2008-09-03 23:18:39 +0000 | [diff] [blame] | 135 | namespace llvm { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 136 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 137 | /// that a particular set of values is assigned, and the type information about |
| 138 | /// the value. The most common situation is to represent one value at a time, |
| 139 | /// but struct or array values are handled element-wise as multiple values. |
| 140 | /// The splitting of aggregates is performed recursively, so that we never |
| 141 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 142 | /// have legal types, so each value may require one or more registers of some |
| 143 | /// legal type. |
| 144 | /// |
| 145 | struct VISIBILITY_HIDDEN RegsForValue { |
| 146 | /// TLI - The TargetLowering object. |
| 147 | /// |
| 148 | const TargetLowering *TLI; |
| 149 | |
| 150 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 151 | /// may need be promoted or synthesized from one or more registers. |
| 152 | /// |
| 153 | SmallVector<MVT, 4> ValueVTs; |
| 154 | |
| 155 | /// RegVTs - The value types of the registers. This is the same size as |
| 156 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 157 | /// register or registers are. (Individual values are never synthesized |
| 158 | /// from more than one type of register.) |
| 159 | /// |
| 160 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 161 | /// getRegisterType member function, however when with physical registers |
| 162 | /// it is necessary to have a separate record of the types. |
| 163 | /// |
| 164 | SmallVector<MVT, 4> RegVTs; |
| 165 | |
| 166 | /// Regs - This list holds the registers assigned to the values. |
| 167 | /// Each legal or promoted value requires one register, and each |
| 168 | /// expanded value requires multiple registers. |
| 169 | /// |
| 170 | SmallVector<unsigned, 4> Regs; |
| 171 | |
| 172 | RegsForValue() : TLI(0) {} |
| 173 | |
| 174 | RegsForValue(const TargetLowering &tli, |
| 175 | const SmallVector<unsigned, 4> ®s, |
| 176 | MVT regvt, MVT valuevt) |
| 177 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
| 178 | RegsForValue(const TargetLowering &tli, |
| 179 | const SmallVector<unsigned, 4> ®s, |
| 180 | const SmallVector<MVT, 4> ®vts, |
| 181 | const SmallVector<MVT, 4> &valuevts) |
| 182 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
| 183 | RegsForValue(const TargetLowering &tli, |
| 184 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 185 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 186 | |
| 187 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 188 | MVT ValueVT = ValueVTs[Value]; |
| 189 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 190 | MVT RegisterVT = TLI->getRegisterType(ValueVT); |
| 191 | for (unsigned i = 0; i != NumRegs; ++i) |
| 192 | Regs.push_back(Reg + i); |
| 193 | RegVTs.push_back(RegisterVT); |
| 194 | Reg += NumRegs; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// append - Add the specified values to this one. |
| 199 | void append(const RegsForValue &RHS) { |
| 200 | TLI = RHS.TLI; |
| 201 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 202 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 203 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 208 | /// this value and returns the result as a ValueVTs value. This uses |
| 209 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 210 | /// If the Flag pointer is NULL, no flag is used. |
| 211 | SDValue getCopyFromRegs(SelectionDAG &DAG, |
| 212 | SDValue &Chain, SDValue *Flag) const; |
| 213 | |
| 214 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 215 | /// specified value into the registers specified by this object. This uses |
| 216 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 217 | /// If the Flag pointer is NULL, no flag is used. |
| 218 | void getCopyToRegs(SDValue Val, SelectionDAG &DAG, |
| 219 | SDValue &Chain, SDValue *Flag) const; |
| 220 | |
| 221 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 222 | /// operand list. This adds the code marker and includes the number of |
| 223 | /// values added into it. |
| 224 | void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
| 225 | std::vector<SDValue> &Ops) const; |
| 226 | }; |
| 227 | } |
| 228 | |
| 229 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 230 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 231 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
| 232 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 233 | if (isa<PHINode>(I)) return true; |
| 234 | BasicBlock *BB = I->getParent(); |
| 235 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 236 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) || |
| 237 | // FIXME: Remove switchinst special case. |
| 238 | isa<SwitchInst>(*UI)) |
| 239 | return true; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 244 | /// entry block, return true. This includes arguments used by switches, since |
| 245 | /// the switch may expand into multiple basic blocks. |
| 246 | static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { |
| 247 | // With FastISel active, we may be splitting blocks, so force creation |
| 248 | // of virtual registers for all non-dead arguments. |
| 249 | if (EnableFastISel) |
| 250 | return A->use_empty(); |
| 251 | |
| 252 | BasicBlock *Entry = A->getParent()->begin(); |
| 253 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 254 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 255 | return false; // Use not in entry block. |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) |
| 260 | : TLI(tli) { |
| 261 | } |
| 262 | |
| 263 | void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, |
| 264 | bool EnableFastISel) { |
| 265 | Fn = &fn; |
| 266 | MF = &mf; |
| 267 | RegInfo = &MF->getRegInfo(); |
| 268 | |
| 269 | // Create a vreg for each argument register that is not dead and is used |
| 270 | // outside of the entry block for the function. |
| 271 | for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
| 272 | AI != E; ++AI) |
| 273 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 274 | InitializeRegForValue(AI); |
| 275 | |
| 276 | // Initialize the mapping of values to registers. This is only set up for |
| 277 | // instruction values that are used outside of the block that defines |
| 278 | // them. |
| 279 | Function::iterator BB = Fn->begin(), EB = Fn->end(); |
| 280 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 281 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 282 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
| 283 | const Type *Ty = AI->getAllocatedType(); |
| 284 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
| 285 | unsigned Align = |
| 286 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 287 | AI->getAlignment()); |
| 288 | |
| 289 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 290 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 291 | StaticAllocaMap[AI] = |
| 292 | MF->getFrameInfo()->CreateStackObject(TySize, Align); |
| 293 | } |
| 294 | |
| 295 | for (; BB != EB; ++BB) |
| 296 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 297 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 298 | if (!isa<AllocaInst>(I) || |
| 299 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 300 | InitializeRegForValue(I); |
| 301 | |
| 302 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 303 | // also creates the initial PHI MachineInstrs, though none of the input |
| 304 | // operands are populated. |
| 305 | for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { |
| 306 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 307 | MBBMap[BB] = MBB; |
| 308 | MF->push_back(MBB); |
| 309 | |
| 310 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 311 | // appropriate. |
| 312 | PHINode *PN; |
| 313 | for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){ |
| 314 | if (PN->use_empty()) continue; |
| 315 | |
| 316 | unsigned PHIReg = ValueMap[PN]; |
| 317 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 318 | |
| 319 | SmallVector<MVT, 4> ValueVTs; |
| 320 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 321 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 322 | MVT VT = ValueVTs[vti]; |
| 323 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
Dan Gohman | 6448d91 | 2008-09-04 15:39:15 +0000 | [diff] [blame] | 324 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 325 | for (unsigned i = 0; i != NumRegisters; ++i) |
| 326 | BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i); |
| 327 | PHIReg += NumRegisters; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | unsigned FunctionLoweringInfo::MakeReg(MVT VT) { |
| 334 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 335 | } |
| 336 | |
| 337 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 338 | /// the correctly promoted or expanded types. Assign these registers |
| 339 | /// consecutive vreg numbers and return the first assigned number. |
| 340 | /// |
| 341 | /// In the case that the given value has struct or array type, this function |
| 342 | /// will assign registers for each member or element. |
| 343 | /// |
| 344 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 345 | SmallVector<MVT, 4> ValueVTs; |
| 346 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
| 347 | |
| 348 | unsigned FirstReg = 0; |
| 349 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 350 | MVT ValueVT = ValueVTs[Value]; |
| 351 | MVT RegisterVT = TLI.getRegisterType(ValueVT); |
| 352 | |
| 353 | unsigned NumRegs = TLI.getNumRegisters(ValueVT); |
| 354 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 355 | unsigned R = MakeReg(RegisterVT); |
| 356 | if (!FirstReg) FirstReg = R; |
| 357 | } |
| 358 | } |
| 359 | return FirstReg; |
| 360 | } |
| 361 | |
| 362 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 363 | /// combined into the value they represent. If the parts combine to a type |
| 364 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 365 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
| 366 | /// (ISD::AssertSext). |
| 367 | static SDValue getCopyFromParts(SelectionDAG &DAG, |
| 368 | const SDValue *Parts, |
| 369 | unsigned NumParts, |
| 370 | MVT PartVT, |
| 371 | MVT ValueVT, |
| 372 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
| 373 | assert(NumParts > 0 && "No parts to assemble!"); |
| 374 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 375 | SDValue Val = Parts[0]; |
| 376 | |
| 377 | if (NumParts > 1) { |
| 378 | // Assemble the value from multiple parts. |
| 379 | if (!ValueVT.isVector()) { |
| 380 | unsigned PartBits = PartVT.getSizeInBits(); |
| 381 | unsigned ValueBits = ValueVT.getSizeInBits(); |
| 382 | |
| 383 | // Assemble the power of 2 part. |
| 384 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 385 | 1 << Log2_32(NumParts) : NumParts; |
| 386 | unsigned RoundBits = PartBits * RoundParts; |
| 387 | MVT RoundVT = RoundBits == ValueBits ? |
| 388 | ValueVT : MVT::getIntegerVT(RoundBits); |
| 389 | SDValue Lo, Hi; |
| 390 | |
| 391 | if (RoundParts > 2) { |
| 392 | MVT HalfVT = MVT::getIntegerVT(RoundBits/2); |
| 393 | Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT); |
| 394 | Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2, |
| 395 | PartVT, HalfVT); |
| 396 | } else { |
| 397 | Lo = Parts[0]; |
| 398 | Hi = Parts[1]; |
| 399 | } |
| 400 | if (TLI.isBigEndian()) |
| 401 | std::swap(Lo, Hi); |
| 402 | Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi); |
| 403 | |
| 404 | if (RoundParts < NumParts) { |
| 405 | // Assemble the trailing non-power-of-2 part. |
| 406 | unsigned OddParts = NumParts - RoundParts; |
| 407 | MVT OddVT = MVT::getIntegerVT(OddParts * PartBits); |
| 408 | Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT); |
| 409 | |
| 410 | // Combine the round and odd parts. |
| 411 | Lo = Val; |
| 412 | if (TLI.isBigEndian()) |
| 413 | std::swap(Lo, Hi); |
| 414 | MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits); |
| 415 | Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi); |
| 416 | Hi = DAG.getNode(ISD::SHL, TotalVT, Hi, |
| 417 | DAG.getConstant(Lo.getValueType().getSizeInBits(), |
| 418 | TLI.getShiftAmountTy())); |
| 419 | Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo); |
| 420 | Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi); |
| 421 | } |
| 422 | } else { |
| 423 | // Handle a multi-element vector. |
| 424 | MVT IntermediateVT, RegisterVT; |
| 425 | unsigned NumIntermediates; |
| 426 | unsigned NumRegs = |
| 427 | TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 428 | RegisterVT); |
| 429 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 430 | NumParts = NumRegs; // Silence a compiler warning. |
| 431 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 432 | assert(RegisterVT == Parts[0].getValueType() && |
| 433 | "Part type doesn't match part!"); |
| 434 | |
| 435 | // Assemble the parts into intermediate operands. |
| 436 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 437 | if (NumIntermediates == NumParts) { |
| 438 | // If the register was not expanded, truncate or copy the value, |
| 439 | // as appropriate. |
| 440 | for (unsigned i = 0; i != NumParts; ++i) |
| 441 | Ops[i] = getCopyFromParts(DAG, &Parts[i], 1, |
| 442 | PartVT, IntermediateVT); |
| 443 | } else if (NumParts > 0) { |
| 444 | // If the intermediate type was expanded, build the intermediate operands |
| 445 | // from the parts. |
| 446 | assert(NumParts % NumIntermediates == 0 && |
| 447 | "Must expand into a divisible number of parts!"); |
| 448 | unsigned Factor = NumParts / NumIntermediates; |
| 449 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 450 | Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor, |
| 451 | PartVT, IntermediateVT); |
| 452 | } |
| 453 | |
| 454 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 455 | // operands. |
| 456 | Val = DAG.getNode(IntermediateVT.isVector() ? |
| 457 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, |
| 458 | ValueVT, &Ops[0], NumIntermediates); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 463 | PartVT = Val.getValueType(); |
| 464 | |
| 465 | if (PartVT == ValueVT) |
| 466 | return Val; |
| 467 | |
| 468 | if (PartVT.isVector()) { |
| 469 | assert(ValueVT.isVector() && "Unknown vector conversion!"); |
| 470 | return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); |
| 471 | } |
| 472 | |
| 473 | if (ValueVT.isVector()) { |
| 474 | assert(ValueVT.getVectorElementType() == PartVT && |
| 475 | ValueVT.getVectorNumElements() == 1 && |
| 476 | "Only trivial scalar-to-vector conversions should get here!"); |
| 477 | return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val); |
| 478 | } |
| 479 | |
| 480 | if (PartVT.isInteger() && |
| 481 | ValueVT.isInteger()) { |
| 482 | if (ValueVT.bitsLT(PartVT)) { |
| 483 | // For a truncate, see if we have any information to |
| 484 | // indicate whether the truncated bits will always be |
| 485 | // zero or sign-extension. |
| 486 | if (AssertOp != ISD::DELETED_NODE) |
| 487 | Val = DAG.getNode(AssertOp, PartVT, Val, |
| 488 | DAG.getValueType(ValueVT)); |
| 489 | return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 490 | } else { |
| 491 | return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 496 | if (ValueVT.bitsLT(Val.getValueType())) |
| 497 | // FP_ROUND's are always exact here. |
| 498 | return DAG.getNode(ISD::FP_ROUND, ValueVT, Val, |
| 499 | DAG.getIntPtrConstant(1)); |
| 500 | return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val); |
| 501 | } |
| 502 | |
| 503 | if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) |
| 504 | return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); |
| 505 | |
| 506 | assert(0 && "Unknown mismatch!"); |
| 507 | return SDValue(); |
| 508 | } |
| 509 | |
| 510 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 511 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 512 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
| 513 | static void getCopyToParts(SelectionDAG &DAG, |
| 514 | SDValue Val, |
| 515 | SDValue *Parts, |
| 516 | unsigned NumParts, |
| 517 | MVT PartVT, |
| 518 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
| 519 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 520 | MVT PtrVT = TLI.getPointerTy(); |
| 521 | MVT ValueVT = Val.getValueType(); |
| 522 | unsigned PartBits = PartVT.getSizeInBits(); |
| 523 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
| 524 | |
| 525 | if (!NumParts) |
| 526 | return; |
| 527 | |
| 528 | if (!ValueVT.isVector()) { |
| 529 | if (PartVT == ValueVT) { |
| 530 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 531 | Parts[0] = Val; |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | if (NumParts * PartBits > ValueVT.getSizeInBits()) { |
| 536 | // If the parts cover more bits than the value has, promote the value. |
| 537 | if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { |
| 538 | assert(NumParts == 1 && "Do not know what to promote to!"); |
| 539 | Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val); |
| 540 | } else if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 541 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
| 542 | Val = DAG.getNode(ExtendKind, ValueVT, Val); |
| 543 | } else { |
| 544 | assert(0 && "Unknown mismatch!"); |
| 545 | } |
| 546 | } else if (PartBits == ValueVT.getSizeInBits()) { |
| 547 | // Different types of the same size. |
| 548 | assert(NumParts == 1 && PartVT != ValueVT); |
| 549 | Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); |
| 550 | } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { |
| 551 | // If the parts cover less bits than value has, truncate the value. |
| 552 | if (PartVT.isInteger() && ValueVT.isInteger()) { |
| 553 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
| 554 | Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 555 | } else { |
| 556 | assert(0 && "Unknown mismatch!"); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // The value may have changed - recompute ValueVT. |
| 561 | ValueVT = Val.getValueType(); |
| 562 | assert(NumParts * PartBits == ValueVT.getSizeInBits() && |
| 563 | "Failed to tile the value with PartVT!"); |
| 564 | |
| 565 | if (NumParts == 1) { |
| 566 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 567 | Parts[0] = Val; |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | // Expand the value into multiple parts. |
| 572 | if (NumParts & (NumParts - 1)) { |
| 573 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 574 | assert(PartVT.isInteger() && ValueVT.isInteger() && |
| 575 | "Do not know what to expand to!"); |
| 576 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 577 | unsigned RoundBits = RoundParts * PartBits; |
| 578 | unsigned OddParts = NumParts - RoundParts; |
| 579 | SDValue OddVal = DAG.getNode(ISD::SRL, ValueVT, Val, |
| 580 | DAG.getConstant(RoundBits, |
| 581 | TLI.getShiftAmountTy())); |
| 582 | getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT); |
| 583 | if (TLI.isBigEndian()) |
| 584 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 585 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 586 | NumParts = RoundParts; |
| 587 | ValueVT = MVT::getIntegerVT(NumParts * PartBits); |
| 588 | Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 589 | } |
| 590 | |
| 591 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 592 | // EXTRACT_ELEMENT. |
| 593 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, |
| 594 | MVT::getIntegerVT(ValueVT.getSizeInBits()), |
| 595 | Val); |
| 596 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 597 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 598 | unsigned ThisBits = StepSize * PartBits / 2; |
| 599 | MVT ThisVT = MVT::getIntegerVT (ThisBits); |
| 600 | SDValue &Part0 = Parts[i]; |
| 601 | SDValue &Part1 = Parts[i+StepSize/2]; |
| 602 | |
| 603 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, |
| 604 | DAG.getConstant(1, PtrVT)); |
| 605 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, |
| 606 | DAG.getConstant(0, PtrVT)); |
| 607 | |
| 608 | if (ThisBits == PartBits && ThisVT != PartVT) { |
| 609 | Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0); |
| 610 | Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (TLI.isBigEndian()) |
| 616 | std::reverse(Parts, Parts + NumParts); |
| 617 | |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | // Vector ValueVT. |
| 622 | if (NumParts == 1) { |
| 623 | if (PartVT != ValueVT) { |
| 624 | if (PartVT.isVector()) { |
| 625 | Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); |
| 626 | } else { |
| 627 | assert(ValueVT.getVectorElementType() == PartVT && |
| 628 | ValueVT.getVectorNumElements() == 1 && |
| 629 | "Only trivial vector-to-scalar conversions should get here!"); |
| 630 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val, |
| 631 | DAG.getConstant(0, PtrVT)); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | Parts[0] = Val; |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | // Handle a multi-element vector. |
| 640 | MVT IntermediateVT, RegisterVT; |
| 641 | unsigned NumIntermediates; |
| 642 | unsigned NumRegs = |
| 643 | DAG.getTargetLoweringInfo() |
| 644 | .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 645 | RegisterVT); |
| 646 | unsigned NumElements = ValueVT.getVectorNumElements(); |
| 647 | |
| 648 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 649 | NumParts = NumRegs; // Silence a compiler warning. |
| 650 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 651 | |
| 652 | // Split the vector into intermediate operands. |
| 653 | SmallVector<SDValue, 8> Ops(NumIntermediates); |
| 654 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 655 | if (IntermediateVT.isVector()) |
| 656 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, |
| 657 | IntermediateVT, Val, |
| 658 | DAG.getConstant(i * (NumElements / NumIntermediates), |
| 659 | PtrVT)); |
| 660 | else |
| 661 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 662 | IntermediateVT, Val, |
| 663 | DAG.getConstant(i, PtrVT)); |
| 664 | |
| 665 | // Split the intermediate operands into legal parts. |
| 666 | if (NumParts == NumIntermediates) { |
| 667 | // If the register was not expanded, promote or copy the value, |
| 668 | // as appropriate. |
| 669 | for (unsigned i = 0; i != NumParts; ++i) |
| 670 | getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT); |
| 671 | } else if (NumParts > 0) { |
| 672 | // If the intermediate type was expanded, split each the value into |
| 673 | // legal parts. |
| 674 | assert(NumParts % NumIntermediates == 0 && |
| 675 | "Must expand into a divisible number of parts!"); |
| 676 | unsigned Factor = NumParts / NumIntermediates; |
| 677 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 678 | getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | |
| 683 | void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { |
| 684 | AA = &aa; |
| 685 | GFI = gfi; |
| 686 | TD = DAG.getTarget().getTargetData(); |
| 687 | } |
| 688 | |
| 689 | /// clear - Clear out the curret SelectionDAG and the associated |
| 690 | /// state and prepare this SelectionDAGLowering object to be used |
| 691 | /// for a new block. This doesn't clear out information about |
| 692 | /// additional blocks that are needed to complete switch lowering |
| 693 | /// or PHI node updating; that information is cleared out as it is |
| 694 | /// consumed. |
| 695 | void SelectionDAGLowering::clear() { |
| 696 | NodeMap.clear(); |
| 697 | PendingLoads.clear(); |
| 698 | PendingExports.clear(); |
| 699 | DAG.clear(); |
| 700 | } |
| 701 | |
| 702 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 703 | /// flushing any PendingLoad items. This must be done before emitting |
| 704 | /// a store or any other node that may need to be ordered after any |
| 705 | /// prior load instructions. |
| 706 | /// |
| 707 | SDValue SelectionDAGLowering::getRoot() { |
| 708 | if (PendingLoads.empty()) |
| 709 | return DAG.getRoot(); |
| 710 | |
| 711 | if (PendingLoads.size() == 1) { |
| 712 | SDValue Root = PendingLoads[0]; |
| 713 | DAG.setRoot(Root); |
| 714 | PendingLoads.clear(); |
| 715 | return Root; |
| 716 | } |
| 717 | |
| 718 | // Otherwise, we have to make a token factor node. |
| 719 | SDValue Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 720 | &PendingLoads[0], PendingLoads.size()); |
| 721 | PendingLoads.clear(); |
| 722 | DAG.setRoot(Root); |
| 723 | return Root; |
| 724 | } |
| 725 | |
| 726 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 727 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 728 | /// to do this before emitting a terminator instruction. |
| 729 | /// |
| 730 | SDValue SelectionDAGLowering::getControlRoot() { |
| 731 | SDValue Root = DAG.getRoot(); |
| 732 | |
| 733 | if (PendingExports.empty()) |
| 734 | return Root; |
| 735 | |
| 736 | // Turn all of the CopyToReg chains into one factored node. |
| 737 | if (Root.getOpcode() != ISD::EntryToken) { |
| 738 | unsigned i = 0, e = PendingExports.size(); |
| 739 | for (; i != e; ++i) { |
| 740 | assert(PendingExports[i].getNode()->getNumOperands() > 1); |
| 741 | if (PendingExports[i].getNode()->getOperand(0) == Root) |
| 742 | break; // Don't add the root if we already indirectly depend on it. |
| 743 | } |
| 744 | |
| 745 | if (i == e) |
| 746 | PendingExports.push_back(Root); |
| 747 | } |
| 748 | |
| 749 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 750 | &PendingExports[0], |
| 751 | PendingExports.size()); |
| 752 | PendingExports.clear(); |
| 753 | DAG.setRoot(Root); |
| 754 | return Root; |
| 755 | } |
| 756 | |
| 757 | void SelectionDAGLowering::visit(Instruction &I) { |
| 758 | visit(I.getOpcode(), I); |
| 759 | } |
| 760 | |
| 761 | void SelectionDAGLowering::visit(unsigned Opcode, User &I) { |
| 762 | // Note: this doesn't use InstVisitor, because it has to work with |
| 763 | // ConstantExpr's in addition to instructions. |
| 764 | switch (Opcode) { |
| 765 | default: assert(0 && "Unknown instruction type encountered!"); |
| 766 | abort(); |
| 767 | // Build the switch statement using the Instruction.def file. |
| 768 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 769 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 770 | #include "llvm/Instruction.def" |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void SelectionDAGLowering::visitAdd(User &I) { |
| 775 | if (I.getType()->isFPOrFPVector()) |
| 776 | visitBinary(I, ISD::FADD); |
| 777 | else |
| 778 | visitBinary(I, ISD::ADD); |
| 779 | } |
| 780 | |
| 781 | void SelectionDAGLowering::visitMul(User &I) { |
| 782 | if (I.getType()->isFPOrFPVector()) |
| 783 | visitBinary(I, ISD::FMUL); |
| 784 | else |
| 785 | visitBinary(I, ISD::MUL); |
| 786 | } |
| 787 | |
| 788 | SDValue SelectionDAGLowering::getValue(const Value *V) { |
| 789 | SDValue &N = NodeMap[V]; |
| 790 | if (N.getNode()) return N; |
| 791 | |
| 792 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 793 | MVT VT = TLI.getValueType(V->getType(), true); |
| 794 | |
| 795 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 796 | return N = DAG.getConstant(*CI, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 797 | |
| 798 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 799 | return N = DAG.getGlobalAddress(GV, VT); |
| 800 | |
| 801 | if (isa<ConstantPointerNull>(C)) |
| 802 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 803 | |
| 804 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 805 | return N = DAG.getConstantFP(*CFP, VT); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 806 | |
| 807 | if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) && |
| 808 | !V->getType()->isAggregateType()) |
| 809 | return N = DAG.getNode(ISD::UNDEF, VT); |
| 810 | |
| 811 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 812 | visit(CE->getOpcode(), *CE); |
| 813 | SDValue N1 = NodeMap[V]; |
| 814 | assert(N1.getNode() && "visit didn't populate the ValueMap!"); |
| 815 | return N1; |
| 816 | } |
| 817 | |
| 818 | if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { |
| 819 | SmallVector<SDValue, 4> Constants; |
| 820 | for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); |
| 821 | OI != OE; ++OI) { |
| 822 | SDNode *Val = getValue(*OI).getNode(); |
| 823 | for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) |
| 824 | Constants.push_back(SDValue(Val, i)); |
| 825 | } |
| 826 | return DAG.getMergeValues(&Constants[0], Constants.size()); |
| 827 | } |
| 828 | |
| 829 | if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) { |
| 830 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 831 | "Unknown struct or array constant!"); |
| 832 | |
| 833 | SmallVector<MVT, 4> ValueVTs; |
| 834 | ComputeValueVTs(TLI, C->getType(), ValueVTs); |
| 835 | unsigned NumElts = ValueVTs.size(); |
| 836 | if (NumElts == 0) |
| 837 | return SDValue(); // empty struct |
| 838 | SmallVector<SDValue, 4> Constants(NumElts); |
| 839 | for (unsigned i = 0; i != NumElts; ++i) { |
| 840 | MVT EltVT = ValueVTs[i]; |
| 841 | if (isa<UndefValue>(C)) |
| 842 | Constants[i] = DAG.getNode(ISD::UNDEF, EltVT); |
| 843 | else if (EltVT.isFloatingPoint()) |
| 844 | Constants[i] = DAG.getConstantFP(0, EltVT); |
| 845 | else |
| 846 | Constants[i] = DAG.getConstant(0, EltVT); |
| 847 | } |
| 848 | return DAG.getMergeValues(&Constants[0], NumElts); |
| 849 | } |
| 850 | |
| 851 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
| 852 | unsigned NumElements = VecTy->getNumElements(); |
| 853 | |
| 854 | // Now that we know the number and type of the elements, get that number of |
| 855 | // elements into the Ops array based on what kind of constant it is. |
| 856 | SmallVector<SDValue, 16> Ops; |
| 857 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 858 | for (unsigned i = 0; i != NumElements; ++i) |
| 859 | Ops.push_back(getValue(CP->getOperand(i))); |
| 860 | } else { |
| 861 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 862 | "Unknown vector constant!"); |
| 863 | MVT EltVT = TLI.getValueType(VecTy->getElementType()); |
| 864 | |
| 865 | SDValue Op; |
| 866 | if (isa<UndefValue>(C)) |
| 867 | Op = DAG.getNode(ISD::UNDEF, EltVT); |
| 868 | else if (EltVT.isFloatingPoint()) |
| 869 | Op = DAG.getConstantFP(0, EltVT); |
| 870 | else |
| 871 | Op = DAG.getConstant(0, EltVT); |
| 872 | Ops.assign(NumElements, Op); |
| 873 | } |
| 874 | |
| 875 | // Create a BUILD_VECTOR node. |
| 876 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
| 877 | } |
| 878 | |
| 879 | // If this is a static alloca, generate it as the frameindex instead of |
| 880 | // computation. |
| 881 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 882 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 883 | FuncInfo.StaticAllocaMap.find(AI); |
| 884 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 885 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 886 | } |
| 887 | |
| 888 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 889 | assert(InReg && "Value not in map!"); |
| 890 | |
| 891 | RegsForValue RFV(TLI, InReg, V->getType()); |
| 892 | SDValue Chain = DAG.getEntryNode(); |
| 893 | return RFV.getCopyFromRegs(DAG, Chain, NULL); |
| 894 | } |
| 895 | |
| 896 | |
| 897 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 898 | if (I.getNumOperands() == 0) { |
| 899 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getControlRoot())); |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | SmallVector<SDValue, 8> NewValues; |
| 904 | NewValues.push_back(getControlRoot()); |
| 905 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 906 | SDValue RetOp = getValue(I.getOperand(i)); |
| 907 | |
| 908 | SmallVector<MVT, 4> ValueVTs; |
| 909 | ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs); |
| 910 | for (unsigned j = 0, f = ValueVTs.size(); j != f; ++j) { |
| 911 | MVT VT = ValueVTs[j]; |
| 912 | |
| 913 | // FIXME: C calling convention requires the return type to be promoted to |
| 914 | // at least 32-bit. But this is not necessary for non-C calling conventions. |
| 915 | if (VT.isInteger()) { |
| 916 | MVT MinVT = TLI.getRegisterType(MVT::i32); |
| 917 | if (VT.bitsLT(MinVT)) |
| 918 | VT = MinVT; |
| 919 | } |
| 920 | |
| 921 | unsigned NumParts = TLI.getNumRegisters(VT); |
| 922 | MVT PartVT = TLI.getRegisterType(VT); |
| 923 | SmallVector<SDValue, 4> Parts(NumParts); |
| 924 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 925 | |
| 926 | const Function *F = I.getParent()->getParent(); |
| 927 | if (F->paramHasAttr(0, ParamAttr::SExt)) |
| 928 | ExtendKind = ISD::SIGN_EXTEND; |
| 929 | else if (F->paramHasAttr(0, ParamAttr::ZExt)) |
| 930 | ExtendKind = ISD::ZERO_EXTEND; |
| 931 | |
| 932 | getCopyToParts(DAG, SDValue(RetOp.getNode(), RetOp.getResNo() + j), |
| 933 | &Parts[0], NumParts, PartVT, ExtendKind); |
| 934 | |
| 935 | for (unsigned i = 0; i < NumParts; ++i) { |
| 936 | NewValues.push_back(Parts[i]); |
| 937 | NewValues.push_back(DAG.getArgFlags(ISD::ArgFlagsTy())); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, |
| 942 | &NewValues[0], NewValues.size())); |
| 943 | } |
| 944 | |
| 945 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 946 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 947 | /// CopyTo/FromReg. |
| 948 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 949 | // No need to export constants. |
| 950 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
| 951 | |
| 952 | // Already exported? |
| 953 | if (FuncInfo.isExportedInst(V)) return; |
| 954 | |
| 955 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 956 | CopyValueToVirtualRegister(V, Reg); |
| 957 | } |
| 958 | |
| 959 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 960 | const BasicBlock *FromBB) { |
| 961 | // The operands of the setcc have to be in this block. We don't know |
| 962 | // how to export them from some other block. |
| 963 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 964 | // Can export from current BB. |
| 965 | if (VI->getParent() == FromBB) |
| 966 | return true; |
| 967 | |
| 968 | // Is already exported, noop. |
| 969 | return FuncInfo.isExportedInst(V); |
| 970 | } |
| 971 | |
| 972 | // If this is an argument, we can export it if the BB is the entry block or |
| 973 | // if it is already exported. |
| 974 | if (isa<Argument>(V)) { |
| 975 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 976 | return true; |
| 977 | |
| 978 | // Otherwise, can only export this if it is already exported. |
| 979 | return FuncInfo.isExportedInst(V); |
| 980 | } |
| 981 | |
| 982 | // Otherwise, constants can always be exported. |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 987 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 988 | return I->getParent() == BB; |
| 989 | return true; |
| 990 | } |
| 991 | |
| 992 | /// FindMergedConditions - If Cond is an expression like |
| 993 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 994 | MachineBasicBlock *TBB, |
| 995 | MachineBasicBlock *FBB, |
| 996 | MachineBasicBlock *CurBB, |
| 997 | unsigned Opc) { |
| 998 | // If this node is not part of the or/and tree, emit it as a branch. |
| 999 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
| 1000 | |
| 1001 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
| 1002 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
| 1003 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1004 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1005 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
| 1006 | const BasicBlock *BB = CurBB->getBasicBlock(); |
| 1007 | |
| 1008 | // If the leaf of the tree is a comparison, merge the condition into |
| 1009 | // the caseblock. |
| 1010 | if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) && |
| 1011 | // The operands of the cmp have to be in this block. We don't know |
| 1012 | // how to export them from some other block. If this is the first block |
| 1013 | // of the sequence, no exporting is needed. |
| 1014 | (CurBB == CurMBB || |
| 1015 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1016 | isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) { |
| 1017 | BOp = cast<Instruction>(Cond); |
| 1018 | ISD::CondCode Condition; |
| 1019 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
| 1020 | switch (IC->getPredicate()) { |
| 1021 | default: assert(0 && "Unknown icmp predicate opcode!"); |
| 1022 | case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break; |
| 1023 | case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break; |
| 1024 | case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break; |
| 1025 | case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break; |
| 1026 | case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break; |
| 1027 | case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break; |
| 1028 | case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break; |
| 1029 | case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break; |
| 1030 | case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break; |
| 1031 | case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break; |
| 1032 | } |
| 1033 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
| 1034 | ISD::CondCode FPC, FOC; |
| 1035 | switch (FC->getPredicate()) { |
| 1036 | default: assert(0 && "Unknown fcmp predicate opcode!"); |
| 1037 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1038 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1039 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1040 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1041 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1042 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1043 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1044 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1045 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 1046 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1047 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1048 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1049 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1050 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1051 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1052 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1053 | } |
| 1054 | if (FiniteOnlyFPMath()) |
| 1055 | Condition = FOC; |
| 1056 | else |
| 1057 | Condition = FPC; |
| 1058 | } else { |
| 1059 | Condition = ISD::SETEQ; // silence warning. |
| 1060 | assert(0 && "Unknown compare instruction"); |
| 1061 | } |
| 1062 | |
| 1063 | CaseBlock CB(Condition, BOp->getOperand(0), |
| 1064 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
| 1065 | SwitchCases.push_back(CB); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | // Create a CaseBlock record representing this branch. |
| 1070 | CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(), |
| 1071 | NULL, TBB, FBB, CurBB); |
| 1072 | SwitchCases.push_back(CB); |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | // Create TmpBB after CurBB. |
| 1078 | MachineFunction::iterator BBI = CurBB; |
| 1079 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1080 | MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); |
| 1081 | CurBB->getParent()->insert(++BBI, TmpBB); |
| 1082 | |
| 1083 | if (Opc == Instruction::Or) { |
| 1084 | // Codegen X | Y as: |
| 1085 | // jmp_if_X TBB |
| 1086 | // jmp TmpBB |
| 1087 | // TmpBB: |
| 1088 | // jmp_if_Y TBB |
| 1089 | // jmp FBB |
| 1090 | // |
| 1091 | |
| 1092 | // Emit the LHS condition. |
| 1093 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
| 1094 | |
| 1095 | // Emit the RHS condition into TmpBB. |
| 1096 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1097 | } else { |
| 1098 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1099 | // Codegen X & Y as: |
| 1100 | // jmp_if_X TmpBB |
| 1101 | // jmp FBB |
| 1102 | // TmpBB: |
| 1103 | // jmp_if_Y TBB |
| 1104 | // jmp FBB |
| 1105 | // |
| 1106 | // This requires creation of TmpBB after CurBB. |
| 1107 | |
| 1108 | // Emit the LHS condition. |
| 1109 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
| 1110 | |
| 1111 | // Emit the RHS condition into TmpBB. |
| 1112 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1117 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1118 | /// false. |
| 1119 | bool |
| 1120 | SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){ |
| 1121 | if (Cases.size() != 2) return true; |
| 1122 | |
| 1123 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1124 | // will get folded into a single comparison, so don't emit two blocks. |
| 1125 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1126 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1127 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1128 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | return true; |
| 1133 | } |
| 1134 | |
| 1135 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1136 | // Update machine-CFG edges. |
| 1137 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1138 | |
| 1139 | // Figure out which block is immediately after the current one. |
| 1140 | MachineBasicBlock *NextBlock = 0; |
| 1141 | MachineFunction::iterator BBI = CurMBB; |
| 1142 | if (++BBI != CurMBB->getParent()->end()) |
| 1143 | NextBlock = BBI; |
| 1144 | |
| 1145 | if (I.isUnconditional()) { |
| 1146 | // Update machine-CFG edges. |
| 1147 | CurMBB->addSuccessor(Succ0MBB); |
| 1148 | |
| 1149 | // If this is not a fall-through branch, emit the branch. |
| 1150 | if (Succ0MBB != NextBlock) |
| 1151 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
| 1152 | DAG.getBasicBlock(Succ0MBB))); |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | // If this condition is one of the special cases we handle, do special stuff |
| 1157 | // now. |
| 1158 | Value *CondVal = I.getCondition(); |
| 1159 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1160 | |
| 1161 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1162 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1163 | // For example, instead of something like: |
| 1164 | // cmp A, B |
| 1165 | // C = seteq |
| 1166 | // cmp D, E |
| 1167 | // F = setle |
| 1168 | // or C, F |
| 1169 | // jnz foo |
| 1170 | // Emit: |
| 1171 | // cmp A, B |
| 1172 | // je foo |
| 1173 | // cmp D, E |
| 1174 | // jle foo |
| 1175 | // |
| 1176 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
| 1177 | if (BOp->hasOneUse() && |
| 1178 | (BOp->getOpcode() == Instruction::And || |
| 1179 | BOp->getOpcode() == Instruction::Or)) { |
| 1180 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
| 1181 | // If the compares in later blocks need to use values not currently |
| 1182 | // exported from this block, export them now. This block should always |
| 1183 | // be the first entry. |
| 1184 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
| 1185 | |
| 1186 | // Allow some cases to be rejected. |
| 1187 | if (ShouldEmitAsBranches(SwitchCases)) { |
| 1188 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1189 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1190 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1191 | } |
| 1192 | |
| 1193 | // Emit the branch for this block. |
| 1194 | visitSwitchCase(SwitchCases[0]); |
| 1195 | SwitchCases.erase(SwitchCases.begin()); |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1200 | // SwitchCases. |
| 1201 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1202 | CurMBB->getParent()->erase(SwitchCases[i].ThisBB); |
| 1203 | |
| 1204 | SwitchCases.clear(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // Create a CaseBlock record representing this branch. |
| 1209 | CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(), |
| 1210 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
| 1211 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1212 | // cond branch. |
| 1213 | visitSwitchCase(CB); |
| 1214 | } |
| 1215 | |
| 1216 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1217 | /// the binary search tree resulting from lowering a switch instruction. |
| 1218 | void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) { |
| 1219 | SDValue Cond; |
| 1220 | SDValue CondLHS = getValue(CB.CmpLHS); |
| 1221 | |
| 1222 | // Build the setcc now. |
| 1223 | if (CB.CmpMHS == NULL) { |
| 1224 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1225 | // handle common cases produced by branch lowering. |
| 1226 | if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ) |
| 1227 | Cond = CondLHS; |
| 1228 | else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) { |
| 1229 | SDValue True = DAG.getConstant(1, CondLHS.getValueType()); |
| 1230 | Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True); |
| 1231 | } else |
| 1232 | Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
| 1233 | } else { |
| 1234 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
| 1235 | |
| 1236 | uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue(); |
| 1237 | uint64_t High = cast<ConstantInt>(CB.CmpRHS)->getSExtValue(); |
| 1238 | |
| 1239 | SDValue CmpOp = getValue(CB.CmpMHS); |
| 1240 | MVT VT = CmpOp.getValueType(); |
| 1241 | |
| 1242 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
| 1243 | Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE); |
| 1244 | } else { |
| 1245 | SDValue SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT)); |
| 1246 | Cond = DAG.getSetCC(MVT::i1, SUB, |
| 1247 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | // Update successor info |
| 1252 | CurMBB->addSuccessor(CB.TrueBB); |
| 1253 | CurMBB->addSuccessor(CB.FalseBB); |
| 1254 | |
| 1255 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1256 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1257 | MachineBasicBlock *NextBlock = 0; |
| 1258 | MachineFunction::iterator BBI = CurMBB; |
| 1259 | if (++BBI != CurMBB->getParent()->end()) |
| 1260 | NextBlock = BBI; |
| 1261 | |
| 1262 | // If the lhs block is the next block, invert the condition so that we can |
| 1263 | // fall through to the lhs instead of the rhs block. |
| 1264 | if (CB.TrueBB == NextBlock) { |
| 1265 | std::swap(CB.TrueBB, CB.FalseBB); |
| 1266 | SDValue True = DAG.getConstant(1, Cond.getValueType()); |
| 1267 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
| 1268 | } |
| 1269 | SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond, |
| 1270 | DAG.getBasicBlock(CB.TrueBB)); |
| 1271 | |
| 1272 | // If the branch was constant folded, fix up the CFG. |
| 1273 | if (BrCond.getOpcode() == ISD::BR) { |
| 1274 | CurMBB->removeSuccessor(CB.FalseBB); |
| 1275 | DAG.setRoot(BrCond); |
| 1276 | } else { |
| 1277 | // Otherwise, go ahead and insert the false branch. |
| 1278 | if (BrCond == getControlRoot()) |
| 1279 | CurMBB->removeSuccessor(CB.TrueBB); |
| 1280 | |
| 1281 | if (CB.FalseBB == NextBlock) |
| 1282 | DAG.setRoot(BrCond); |
| 1283 | else |
| 1284 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
| 1285 | DAG.getBasicBlock(CB.FalseBB))); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | /// visitJumpTable - Emit JumpTable node in the current MBB |
| 1290 | void SelectionDAGLowering::visitJumpTable(JumpTable &JT) { |
| 1291 | // Emit the code for the jump table |
| 1292 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
| 1293 | MVT PTy = TLI.getPointerTy(); |
| 1294 | SDValue Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy); |
| 1295 | SDValue Table = DAG.getJumpTable(JT.JTI, PTy); |
| 1296 | DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1), |
| 1297 | Table, Index)); |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1302 | /// in the JumpTable from switch case. |
| 1303 | void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT, |
| 1304 | JumpTableHeader &JTH) { |
| 1305 | // Subtract the lowest switch case value from the value being switched on |
| 1306 | // and conditional branch to default mbb if the result is greater than the |
| 1307 | // difference between smallest and largest cases. |
| 1308 | SDValue SwitchOp = getValue(JTH.SValue); |
| 1309 | MVT VT = SwitchOp.getValueType(); |
| 1310 | SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1311 | DAG.getConstant(JTH.First, VT)); |
| 1312 | |
| 1313 | // The SDNode we just created, which holds the value being switched on |
| 1314 | // minus the the smallest case value, needs to be copied to a virtual |
| 1315 | // register so it can be used as an index into the jump table in a |
| 1316 | // subsequent basic block. This value may be smaller or larger than the |
| 1317 | // target's pointer type, and therefore require extension or truncating. |
| 1318 | if (VT.bitsGT(TLI.getPointerTy())) |
| 1319 | SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB); |
| 1320 | else |
| 1321 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB); |
| 1322 | |
| 1323 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1324 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp); |
| 1325 | JT.Reg = JumpTableReg; |
| 1326 | |
| 1327 | // Emit the range check for the jump table, and branch to the default |
| 1328 | // block for the switch statement if the value being switched on exceeds |
| 1329 | // the largest case in the switch. |
| 1330 | SDValue CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB, |
| 1331 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1332 | ISD::SETUGT); |
| 1333 | |
| 1334 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1335 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1336 | MachineBasicBlock *NextBlock = 0; |
| 1337 | MachineFunction::iterator BBI = CurMBB; |
| 1338 | if (++BBI != CurMBB->getParent()->end()) |
| 1339 | NextBlock = BBI; |
| 1340 | |
| 1341 | SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP, |
| 1342 | DAG.getBasicBlock(JT.Default)); |
| 1343 | |
| 1344 | if (JT.MBB == NextBlock) |
| 1345 | DAG.setRoot(BrCond); |
| 1346 | else |
| 1347 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
| 1348 | DAG.getBasicBlock(JT.MBB))); |
| 1349 | |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1354 | /// suitable for "bit tests" |
| 1355 | void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) { |
| 1356 | // Subtract the minimum value |
| 1357 | SDValue SwitchOp = getValue(B.SValue); |
| 1358 | MVT VT = SwitchOp.getValueType(); |
| 1359 | SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1360 | DAG.getConstant(B.First, VT)); |
| 1361 | |
| 1362 | // Check range |
| 1363 | SDValue RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB, |
| 1364 | DAG.getConstant(B.Range, VT), |
| 1365 | ISD::SETUGT); |
| 1366 | |
| 1367 | SDValue ShiftOp; |
| 1368 | if (VT.bitsGT(TLI.getShiftAmountTy())) |
| 1369 | ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB); |
| 1370 | else |
| 1371 | ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB); |
| 1372 | |
| 1373 | // Make desired shift |
| 1374 | SDValue SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(), |
| 1375 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1376 | ShiftOp); |
| 1377 | |
| 1378 | unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1379 | SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal); |
| 1380 | B.Reg = SwitchReg; |
| 1381 | |
| 1382 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1383 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1384 | MachineBasicBlock *NextBlock = 0; |
| 1385 | MachineFunction::iterator BBI = CurMBB; |
| 1386 | if (++BBI != CurMBB->getParent()->end()) |
| 1387 | NextBlock = BBI; |
| 1388 | |
| 1389 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1390 | |
| 1391 | CurMBB->addSuccessor(B.Default); |
| 1392 | CurMBB->addSuccessor(MBB); |
| 1393 | |
| 1394 | SDValue BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp, |
| 1395 | DAG.getBasicBlock(B.Default)); |
| 1396 | |
| 1397 | if (MBB == NextBlock) |
| 1398 | DAG.setRoot(BrRange); |
| 1399 | else |
| 1400 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo, |
| 1401 | DAG.getBasicBlock(MBB))); |
| 1402 | |
| 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | /// visitBitTestCase - this function produces one "bit test" |
| 1407 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1408 | unsigned Reg, |
| 1409 | BitTestCase &B) { |
| 1410 | // Emit bit tests and jumps |
| 1411 | SDValue SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg, |
| 1412 | TLI.getPointerTy()); |
| 1413 | |
| 1414 | SDValue AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(), SwitchVal, |
| 1415 | DAG.getConstant(B.Mask, TLI.getPointerTy())); |
| 1416 | SDValue AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp, |
| 1417 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1418 | ISD::SETNE); |
| 1419 | |
| 1420 | CurMBB->addSuccessor(B.TargetBB); |
| 1421 | CurMBB->addSuccessor(NextMBB); |
| 1422 | |
| 1423 | SDValue BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), |
| 1424 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
| 1425 | |
| 1426 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1427 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1428 | MachineBasicBlock *NextBlock = 0; |
| 1429 | MachineFunction::iterator BBI = CurMBB; |
| 1430 | if (++BBI != CurMBB->getParent()->end()) |
| 1431 | NextBlock = BBI; |
| 1432 | |
| 1433 | if (NextMBB == NextBlock) |
| 1434 | DAG.setRoot(BrAnd); |
| 1435 | else |
| 1436 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd, |
| 1437 | DAG.getBasicBlock(NextMBB))); |
| 1438 | |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1443 | // Retrieve successors. |
| 1444 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1445 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1446 | |
| 1447 | if (isa<InlineAsm>(I.getCalledValue())) |
| 1448 | visitInlineAsm(&I); |
| 1449 | else |
| 1450 | LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad); |
| 1451 | |
| 1452 | // If the value of the invoke is used outside of its defining block, make it |
| 1453 | // available as a virtual register. |
| 1454 | if (!I.use_empty()) { |
| 1455 | DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I); |
| 1456 | if (VMI != FuncInfo.ValueMap.end()) |
| 1457 | CopyValueToVirtualRegister(&I, VMI->second); |
| 1458 | } |
| 1459 | |
| 1460 | // Update successor info |
| 1461 | CurMBB->addSuccessor(Return); |
| 1462 | CurMBB->addSuccessor(LandingPad); |
| 1463 | |
| 1464 | // Drop into normal successor. |
| 1465 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
| 1466 | DAG.getBasicBlock(Return))); |
| 1467 | } |
| 1468 | |
| 1469 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1470 | } |
| 1471 | |
| 1472 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1473 | /// small case ranges). |
| 1474 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
| 1475 | CaseRecVector& WorkList, |
| 1476 | Value* SV, |
| 1477 | MachineBasicBlock* Default) { |
| 1478 | Case& BackCase = *(CR.Range.second-1); |
| 1479 | |
| 1480 | // Size is the number of Cases represented by this range. |
| 1481 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1482 | if (Size > 3) |
| 1483 | return false; |
| 1484 | |
| 1485 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1486 | // inserting any additional MBBs necessary to represent the switch. |
| 1487 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1488 | |
| 1489 | // Figure out which block is immediately after the current one. |
| 1490 | MachineBasicBlock *NextBlock = 0; |
| 1491 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1492 | |
| 1493 | if (++BBI != CurMBB->getParent()->end()) |
| 1494 | NextBlock = BBI; |
| 1495 | |
| 1496 | // TODO: If any two of the cases has the same destination, and if one value |
| 1497 | // is the same as the other, but has one bit unset that the other has set, |
| 1498 | // use bit manipulation to do two compares at once. For example: |
| 1499 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
| 1500 | |
| 1501 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1502 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
| 1503 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1504 | // branches in this order. See if rearranging a case value would help. |
| 1505 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1506 | if (I->BB == NextBlock) { |
| 1507 | std::swap(*I, BackCase); |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | // Create a CaseBlock record representing a conditional branch to |
| 1514 | // the Case's target mbb if the value being switched on SV is equal |
| 1515 | // to C. |
| 1516 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1517 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1518 | MachineBasicBlock *FallThrough; |
| 1519 | if (I != E-1) { |
| 1520 | FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock()); |
| 1521 | CurMF->insert(BBI, FallThrough); |
| 1522 | } else { |
| 1523 | // If the last case doesn't match, go to the default block. |
| 1524 | FallThrough = Default; |
| 1525 | } |
| 1526 | |
| 1527 | Value *RHS, *LHS, *MHS; |
| 1528 | ISD::CondCode CC; |
| 1529 | if (I->High == I->Low) { |
| 1530 | // This is just small small case range :) containing exactly 1 case |
| 1531 | CC = ISD::SETEQ; |
| 1532 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1533 | } else { |
| 1534 | CC = ISD::SETLE; |
| 1535 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1536 | } |
| 1537 | CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock); |
| 1538 | |
| 1539 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1540 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1541 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1542 | // before the next MBB. |
| 1543 | if (CurBlock == CurMBB) |
| 1544 | visitSwitchCase(CB); |
| 1545 | else |
| 1546 | SwitchCases.push_back(CB); |
| 1547 | |
| 1548 | CurBlock = FallThrough; |
| 1549 | } |
| 1550 | |
| 1551 | return true; |
| 1552 | } |
| 1553 | |
| 1554 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1555 | return !DisableJumpTables && |
| 1556 | (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) || |
| 1557 | TLI.isOperationLegal(ISD::BRIND, MVT::Other)); |
| 1558 | } |
| 1559 | |
| 1560 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
| 1561 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
| 1562 | CaseRecVector& WorkList, |
| 1563 | Value* SV, |
| 1564 | MachineBasicBlock* Default) { |
| 1565 | Case& FrontCase = *CR.Range.first; |
| 1566 | Case& BackCase = *(CR.Range.second-1); |
| 1567 | |
| 1568 | int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue(); |
| 1569 | int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue(); |
| 1570 | |
| 1571 | uint64_t TSize = 0; |
| 1572 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1573 | I!=E; ++I) |
| 1574 | TSize += I->size(); |
| 1575 | |
| 1576 | if (!areJTsAllowed(TLI) || TSize <= 3) |
| 1577 | return false; |
| 1578 | |
| 1579 | double Density = (double)TSize / (double)((Last - First) + 1ULL); |
| 1580 | if (Density < 0.4) |
| 1581 | return false; |
| 1582 | |
| 1583 | DOUT << "Lowering jump table\n" |
| 1584 | << "First entry: " << First << ". Last entry: " << Last << "\n" |
| 1585 | << "Size: " << TSize << ". Density: " << Density << "\n\n"; |
| 1586 | |
| 1587 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1588 | // inserting any additional MBBs necessary to represent the switch. |
| 1589 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1590 | |
| 1591 | // Figure out which block is immediately after the current one. |
| 1592 | MachineBasicBlock *NextBlock = 0; |
| 1593 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1594 | |
| 1595 | if (++BBI != CurMBB->getParent()->end()) |
| 1596 | NextBlock = BBI; |
| 1597 | |
| 1598 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1599 | |
| 1600 | // Create a new basic block to hold the code for loading the address |
| 1601 | // of the jump table, and jumping to it. Update successor information; |
| 1602 | // we will either branch to the default case for the switch, or the jump |
| 1603 | // table. |
| 1604 | MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1605 | CurMF->insert(BBI, JumpTableBB); |
| 1606 | CR.CaseBB->addSuccessor(Default); |
| 1607 | CR.CaseBB->addSuccessor(JumpTableBB); |
| 1608 | |
| 1609 | // Build a vector of destination BBs, corresponding to each target |
| 1610 | // of the jump table. If the value of the jump table slot corresponds to |
| 1611 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1612 | // the default BB. |
| 1613 | std::vector<MachineBasicBlock*> DestBBs; |
| 1614 | int64_t TEI = First; |
| 1615 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) { |
| 1616 | int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue(); |
| 1617 | int64_t High = cast<ConstantInt>(I->High)->getSExtValue(); |
| 1618 | |
| 1619 | if ((Low <= TEI) && (TEI <= High)) { |
| 1620 | DestBBs.push_back(I->BB); |
| 1621 | if (TEI==High) |
| 1622 | ++I; |
| 1623 | } else { |
| 1624 | DestBBs.push_back(Default); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // Update successor info. Add one edge to each unique successor. |
| 1629 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1630 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
| 1631 | E = DestBBs.end(); I != E; ++I) { |
| 1632 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1633 | SuccsHandled[(*I)->getNumber()] = true; |
| 1634 | JumpTableBB->addSuccessor(*I); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | // Create a jump table index for this jump table, or return an existing |
| 1639 | // one. |
| 1640 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
| 1641 | |
| 1642 | // Set the jump table information so that we can codegen it as a second |
| 1643 | // MachineBasicBlock |
| 1644 | JumpTable JT(-1U, JTI, JumpTableBB, Default); |
| 1645 | JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB)); |
| 1646 | if (CR.CaseBB == CurMBB) |
| 1647 | visitJumpTableHeader(JT, JTH); |
| 1648 | |
| 1649 | JTCases.push_back(JumpTableBlock(JTH, JT)); |
| 1650 | |
| 1651 | return true; |
| 1652 | } |
| 1653 | |
| 1654 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1655 | /// 2 subtrees. |
| 1656 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
| 1657 | CaseRecVector& WorkList, |
| 1658 | Value* SV, |
| 1659 | MachineBasicBlock* Default) { |
| 1660 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1661 | // inserting any additional MBBs necessary to represent the switch. |
| 1662 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1663 | |
| 1664 | // Figure out which block is immediately after the current one. |
| 1665 | MachineBasicBlock *NextBlock = 0; |
| 1666 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1667 | |
| 1668 | if (++BBI != CurMBB->getParent()->end()) |
| 1669 | NextBlock = BBI; |
| 1670 | |
| 1671 | Case& FrontCase = *CR.Range.first; |
| 1672 | Case& BackCase = *(CR.Range.second-1); |
| 1673 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1674 | |
| 1675 | // Size is the number of Cases represented by this range. |
| 1676 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1677 | |
| 1678 | int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue(); |
| 1679 | int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue(); |
| 1680 | double FMetric = 0; |
| 1681 | CaseItr Pivot = CR.Range.first + Size/2; |
| 1682 | |
| 1683 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1684 | // (heuristically) allow us to emit JumpTable's later. |
| 1685 | uint64_t TSize = 0; |
| 1686 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1687 | I!=E; ++I) |
| 1688 | TSize += I->size(); |
| 1689 | |
| 1690 | uint64_t LSize = FrontCase.size(); |
| 1691 | uint64_t RSize = TSize-LSize; |
| 1692 | DOUT << "Selecting best pivot: \n" |
| 1693 | << "First: " << First << ", Last: " << Last <<"\n" |
| 1694 | << "LSize: " << LSize << ", RSize: " << RSize << "\n"; |
| 1695 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1696 | J!=E; ++I, ++J) { |
| 1697 | int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue(); |
| 1698 | int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue(); |
| 1699 | assert((RBegin-LEnd>=1) && "Invalid case distance"); |
| 1700 | double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL); |
| 1701 | double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL); |
| 1702 | double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity); |
| 1703 | // Should always split in some non-trivial place |
| 1704 | DOUT <<"=>Step\n" |
| 1705 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n" |
| 1706 | << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n" |
| 1707 | << "Metric: " << Metric << "\n"; |
| 1708 | if (FMetric < Metric) { |
| 1709 | Pivot = J; |
| 1710 | FMetric = Metric; |
| 1711 | DOUT << "Current metric set to: " << FMetric << "\n"; |
| 1712 | } |
| 1713 | |
| 1714 | LSize += J->size(); |
| 1715 | RSize -= J->size(); |
| 1716 | } |
| 1717 | if (areJTsAllowed(TLI)) { |
| 1718 | // If our case is dense we *really* should handle it earlier! |
| 1719 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1720 | } else { |
| 1721 | Pivot = CR.Range.first + Size/2; |
| 1722 | } |
| 1723 | |
| 1724 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1725 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1726 | Constant *C = Pivot->Low; |
| 1727 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
| 1728 | |
| 1729 | // We know that we branch to the LHS if the Value being switched on is |
| 1730 | // less than the Pivot value, C. We use this to optimize our binary |
| 1731 | // tree a bit, by recognizing that if SV is greater than or equal to the |
| 1732 | // LHS's Case Value, and that Case Value is exactly one less than the |
| 1733 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1734 | // rather than creating a leaf node for it. |
| 1735 | if ((LHSR.second - LHSR.first) == 1 && |
| 1736 | LHSR.first->High == CR.GE && |
| 1737 | cast<ConstantInt>(C)->getSExtValue() == |
| 1738 | (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) { |
| 1739 | TrueBB = LHSR.first->BB; |
| 1740 | } else { |
| 1741 | TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1742 | CurMF->insert(BBI, TrueBB); |
| 1743 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
| 1744 | } |
| 1745 | |
| 1746 | // Similar to the optimization above, if the Value being switched on is |
| 1747 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1748 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1749 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1750 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
| 1751 | cast<ConstantInt>(RHSR.first->Low)->getSExtValue() == |
| 1752 | (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) { |
| 1753 | FalseBB = RHSR.first->BB; |
| 1754 | } else { |
| 1755 | FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1756 | CurMF->insert(BBI, FalseBB); |
| 1757 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
| 1758 | } |
| 1759 | |
| 1760 | // Create a CaseBlock record representing a conditional branch to |
| 1761 | // the LHS node if the value being switched on SV is less than C. |
| 1762 | // Otherwise, branch to LHS. |
| 1763 | CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB); |
| 1764 | |
| 1765 | if (CR.CaseBB == CurMBB) |
| 1766 | visitSwitchCase(CB); |
| 1767 | else |
| 1768 | SwitchCases.push_back(CB); |
| 1769 | |
| 1770 | return true; |
| 1771 | } |
| 1772 | |
| 1773 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1774 | /// range span less, than machine word bitwidth, encode case range into series |
| 1775 | /// of masks and emit bit tests with these masks. |
| 1776 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1777 | CaseRecVector& WorkList, |
| 1778 | Value* SV, |
| 1779 | MachineBasicBlock* Default){ |
| 1780 | unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits(); |
| 1781 | |
| 1782 | Case& FrontCase = *CR.Range.first; |
| 1783 | Case& BackCase = *(CR.Range.second-1); |
| 1784 | |
| 1785 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1786 | // inserting any additional MBBs necessary to represent the switch. |
| 1787 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1788 | |
| 1789 | unsigned numCmps = 0; |
| 1790 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1791 | I!=E; ++I) { |
| 1792 | // Single case counts one, case range - two. |
| 1793 | if (I->Low == I->High) |
| 1794 | numCmps +=1; |
| 1795 | else |
| 1796 | numCmps +=2; |
| 1797 | } |
| 1798 | |
| 1799 | // Count unique destinations |
| 1800 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 1801 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1802 | Dests.insert(I->BB); |
| 1803 | if (Dests.size() > 3) |
| 1804 | // Don't bother the code below, if there are too much unique destinations |
| 1805 | return false; |
| 1806 | } |
| 1807 | DOUT << "Total number of unique destinations: " << Dests.size() << "\n" |
| 1808 | << "Total number of comparisons: " << numCmps << "\n"; |
| 1809 | |
| 1810 | // Compute span of values. |
| 1811 | Constant* minValue = FrontCase.Low; |
| 1812 | Constant* maxValue = BackCase.High; |
| 1813 | uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() - |
| 1814 | cast<ConstantInt>(minValue)->getSExtValue(); |
| 1815 | DOUT << "Compare range: " << range << "\n" |
| 1816 | << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n" |
| 1817 | << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n"; |
| 1818 | |
| 1819 | if (range>=IntPtrBits || |
| 1820 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 1821 | !(Dests.size() == 2 && numCmps >= 5) && |
| 1822 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 1823 | return false; |
| 1824 | |
| 1825 | DOUT << "Emitting bit tests\n"; |
| 1826 | int64_t lowBound = 0; |
| 1827 | |
| 1828 | // Optimize the case where all the case values fit in a |
| 1829 | // word without having to subtract minValue. In this case, |
| 1830 | // we can optimize away the subtraction. |
| 1831 | if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 && |
| 1832 | cast<ConstantInt>(maxValue)->getSExtValue() < IntPtrBits) { |
| 1833 | range = cast<ConstantInt>(maxValue)->getSExtValue(); |
| 1834 | } else { |
| 1835 | lowBound = cast<ConstantInt>(minValue)->getSExtValue(); |
| 1836 | } |
| 1837 | |
| 1838 | CaseBitsVector CasesBits; |
| 1839 | unsigned i, count = 0; |
| 1840 | |
| 1841 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 1842 | MachineBasicBlock* Dest = I->BB; |
| 1843 | for (i = 0; i < count; ++i) |
| 1844 | if (Dest == CasesBits[i].BB) |
| 1845 | break; |
| 1846 | |
| 1847 | if (i == count) { |
| 1848 | assert((count < 3) && "Too much destinations to test!"); |
| 1849 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 1850 | count++; |
| 1851 | } |
| 1852 | |
| 1853 | uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound; |
| 1854 | uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound; |
| 1855 | |
| 1856 | for (uint64_t j = lo; j <= hi; j++) { |
| 1857 | CasesBits[i].Mask |= 1ULL << j; |
| 1858 | CasesBits[i].Bits++; |
| 1859 | } |
| 1860 | |
| 1861 | } |
| 1862 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
| 1863 | |
| 1864 | BitTestInfo BTC; |
| 1865 | |
| 1866 | // Figure out which block is immediately after the current one. |
| 1867 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1868 | ++BBI; |
| 1869 | |
| 1870 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1871 | |
| 1872 | DOUT << "Cases:\n"; |
| 1873 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
| 1874 | DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits |
| 1875 | << ", BB: " << CasesBits[i].BB << "\n"; |
| 1876 | |
| 1877 | MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB); |
| 1878 | CurMF->insert(BBI, CaseBB); |
| 1879 | BTC.push_back(BitTestCase(CasesBits[i].Mask, |
| 1880 | CaseBB, |
| 1881 | CasesBits[i].BB)); |
| 1882 | } |
| 1883 | |
| 1884 | BitTestBlock BTB(lowBound, range, SV, |
| 1885 | -1U, (CR.CaseBB == CurMBB), |
| 1886 | CR.CaseBB, Default, BTC); |
| 1887 | |
| 1888 | if (CR.CaseBB == CurMBB) |
| 1889 | visitBitTestHeader(BTB); |
| 1890 | |
| 1891 | BitTestCases.push_back(BTB); |
| 1892 | |
| 1893 | return true; |
| 1894 | } |
| 1895 | |
| 1896 | |
| 1897 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
| 1898 | unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases, |
| 1899 | const SwitchInst& SI) { |
| 1900 | unsigned numCmps = 0; |
| 1901 | |
| 1902 | // Start with "simple" cases |
| 1903 | for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) { |
| 1904 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 1905 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 1906 | SI.getSuccessorValue(i), |
| 1907 | SMBB)); |
| 1908 | } |
| 1909 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1910 | |
| 1911 | // Merge case into clusters |
| 1912 | if (Cases.size()>=2) |
| 1913 | // Must recompute end() each iteration because it may be |
| 1914 | // invalidated by erase if we hold on to it |
| 1915 | for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) { |
| 1916 | int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue(); |
| 1917 | int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue(); |
| 1918 | MachineBasicBlock* nextBB = J->BB; |
| 1919 | MachineBasicBlock* currentBB = I->BB; |
| 1920 | |
| 1921 | // If the two neighboring cases go to the same destination, merge them |
| 1922 | // into a single case. |
| 1923 | if ((nextValue-currentValue==1) && (currentBB == nextBB)) { |
| 1924 | I->High = J->High; |
| 1925 | J = Cases.erase(J); |
| 1926 | } else { |
| 1927 | I = J++; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 1932 | if (I->Low != I->High) |
| 1933 | // A range counts double, since it requires two compares. |
| 1934 | ++numCmps; |
| 1935 | } |
| 1936 | |
| 1937 | return numCmps; |
| 1938 | } |
| 1939 | |
| 1940 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
| 1941 | // Figure out which block is immediately after the current one. |
| 1942 | MachineBasicBlock *NextBlock = 0; |
| 1943 | MachineFunction::iterator BBI = CurMBB; |
| 1944 | |
| 1945 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
| 1946 | |
| 1947 | // If there is only the default destination, branch to it if it is not the |
| 1948 | // next basic block. Otherwise, just fall through. |
| 1949 | if (SI.getNumOperands() == 2) { |
| 1950 | // Update machine-CFG edges. |
| 1951 | |
| 1952 | // If this is not a fall-through branch, emit the branch. |
| 1953 | CurMBB->addSuccessor(Default); |
| 1954 | if (Default != NextBlock) |
| 1955 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
| 1956 | DAG.getBasicBlock(Default))); |
| 1957 | |
| 1958 | return; |
| 1959 | } |
| 1960 | |
| 1961 | // If there are any non-default case statements, create a vector of Cases |
| 1962 | // representing each one, and sort the vector so that we can efficiently |
| 1963 | // create a binary search tree from them. |
| 1964 | CaseVector Cases; |
| 1965 | unsigned numCmps = Clusterify(Cases, SI); |
| 1966 | DOUT << "Clusterify finished. Total clusters: " << Cases.size() |
| 1967 | << ". Total compares: " << numCmps << "\n"; |
| 1968 | |
| 1969 | // Get the Value to be switched on and default basic blocks, which will be |
| 1970 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 1971 | // search tree. |
| 1972 | Value *SV = SI.getOperand(0); |
| 1973 | |
| 1974 | // Push the initial CaseRec onto the worklist |
| 1975 | CaseRecVector WorkList; |
| 1976 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 1977 | |
| 1978 | while (!WorkList.empty()) { |
| 1979 | // Grab a record representing a case range to process off the worklist |
| 1980 | CaseRec CR = WorkList.back(); |
| 1981 | WorkList.pop_back(); |
| 1982 | |
| 1983 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 1984 | continue; |
| 1985 | |
| 1986 | // If the range has few cases (two or less) emit a series of specific |
| 1987 | // tests. |
| 1988 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 1989 | continue; |
| 1990 | |
| 1991 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
| 1992 | // target supports indirect branches, then emit a jump table rather than |
| 1993 | // lowering the switch to a binary tree of conditional branches. |
| 1994 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 1995 | continue; |
| 1996 | |
| 1997 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 1998 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 1999 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | void SelectionDAGLowering::visitSub(User &I) { |
| 2005 | // -0.0 - X --> fneg |
| 2006 | const Type *Ty = I.getType(); |
| 2007 | if (isa<VectorType>(Ty)) { |
| 2008 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2009 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2010 | const Type *ElTy = DestTy->getElementType(); |
| 2011 | if (ElTy->isFloatingPoint()) { |
| 2012 | unsigned VL = DestTy->getNumElements(); |
| 2013 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
| 2014 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
| 2015 | if (CV == CNZ) { |
| 2016 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2017 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 2018 | return; |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | if (Ty->isFloatingPoint()) { |
| 2024 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 2025 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
| 2026 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2027 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 2028 | return; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB); |
| 2033 | } |
| 2034 | |
| 2035 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
| 2036 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2037 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2038 | |
| 2039 | setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2)); |
| 2040 | } |
| 2041 | |
| 2042 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2043 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2044 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2045 | if (!isa<VectorType>(I.getType())) { |
| 2046 | if (TLI.getShiftAmountTy().bitsLT(Op2.getValueType())) |
| 2047 | Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2); |
| 2048 | else if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType())) |
| 2049 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 2050 | } |
| 2051 | |
| 2052 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 2053 | } |
| 2054 | |
| 2055 | void SelectionDAGLowering::visitICmp(User &I) { |
| 2056 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2057 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2058 | predicate = IC->getPredicate(); |
| 2059 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2060 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2061 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2062 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2063 | ISD::CondCode Opcode; |
| 2064 | switch (predicate) { |
| 2065 | case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break; |
| 2066 | case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break; |
| 2067 | case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break; |
| 2068 | case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break; |
| 2069 | case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break; |
| 2070 | case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break; |
| 2071 | case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break; |
| 2072 | case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break; |
| 2073 | case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break; |
| 2074 | case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break; |
| 2075 | default: |
| 2076 | assert(!"Invalid ICmp predicate value"); |
| 2077 | Opcode = ISD::SETEQ; |
| 2078 | break; |
| 2079 | } |
| 2080 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
| 2081 | } |
| 2082 | |
| 2083 | void SelectionDAGLowering::visitFCmp(User &I) { |
| 2084 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2085 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2086 | predicate = FC->getPredicate(); |
| 2087 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2088 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2089 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2090 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2091 | ISD::CondCode Condition, FOC, FPC; |
| 2092 | switch (predicate) { |
| 2093 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 2094 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 2095 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 2096 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 2097 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 2098 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 2099 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 2100 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 2101 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 2102 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 2103 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 2104 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 2105 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 2106 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 2107 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 2108 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 2109 | default: |
| 2110 | assert(!"Invalid FCmp predicate value"); |
| 2111 | FOC = FPC = ISD::SETFALSE; |
| 2112 | break; |
| 2113 | } |
| 2114 | if (FiniteOnlyFPMath()) |
| 2115 | Condition = FOC; |
| 2116 | else |
| 2117 | Condition = FPC; |
| 2118 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition)); |
| 2119 | } |
| 2120 | |
| 2121 | void SelectionDAGLowering::visitVICmp(User &I) { |
| 2122 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2123 | if (VICmpInst *IC = dyn_cast<VICmpInst>(&I)) |
| 2124 | predicate = IC->getPredicate(); |
| 2125 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2126 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2127 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2128 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2129 | ISD::CondCode Opcode; |
| 2130 | switch (predicate) { |
| 2131 | case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break; |
| 2132 | case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break; |
| 2133 | case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break; |
| 2134 | case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break; |
| 2135 | case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break; |
| 2136 | case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break; |
| 2137 | case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break; |
| 2138 | case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break; |
| 2139 | case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break; |
| 2140 | case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break; |
| 2141 | default: |
| 2142 | assert(!"Invalid ICmp predicate value"); |
| 2143 | Opcode = ISD::SETEQ; |
| 2144 | break; |
| 2145 | } |
| 2146 | setValue(&I, DAG.getVSetCC(Op1.getValueType(), Op1, Op2, Opcode)); |
| 2147 | } |
| 2148 | |
| 2149 | void SelectionDAGLowering::visitVFCmp(User &I) { |
| 2150 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2151 | if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I)) |
| 2152 | predicate = FC->getPredicate(); |
| 2153 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2154 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
| 2155 | SDValue Op1 = getValue(I.getOperand(0)); |
| 2156 | SDValue Op2 = getValue(I.getOperand(1)); |
| 2157 | ISD::CondCode Condition, FOC, FPC; |
| 2158 | switch (predicate) { |
| 2159 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 2160 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 2161 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 2162 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 2163 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 2164 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 2165 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 2166 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 2167 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
| 2168 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 2169 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 2170 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 2171 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 2172 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 2173 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 2174 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 2175 | default: |
| 2176 | assert(!"Invalid VFCmp predicate value"); |
| 2177 | FOC = FPC = ISD::SETFALSE; |
| 2178 | break; |
| 2179 | } |
| 2180 | if (FiniteOnlyFPMath()) |
| 2181 | Condition = FOC; |
| 2182 | else |
| 2183 | Condition = FPC; |
| 2184 | |
| 2185 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2186 | |
| 2187 | setValue(&I, DAG.getVSetCC(DestVT, Op1, Op2, Condition)); |
| 2188 | } |
| 2189 | |
| 2190 | void SelectionDAGLowering::visitSelect(User &I) { |
| 2191 | SDValue Cond = getValue(I.getOperand(0)); |
| 2192 | SDValue TrueVal = getValue(I.getOperand(1)); |
| 2193 | SDValue FalseVal = getValue(I.getOperand(2)); |
| 2194 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 2195 | TrueVal, FalseVal)); |
| 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2200 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2201 | SDValue N = getValue(I.getOperand(0)); |
| 2202 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2203 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 2204 | } |
| 2205 | |
| 2206 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2207 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2208 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2209 | SDValue N = getValue(I.getOperand(0)); |
| 2210 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2211 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 2212 | } |
| 2213 | |
| 2214 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2215 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2216 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2217 | SDValue N = getValue(I.getOperand(0)); |
| 2218 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2219 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N)); |
| 2220 | } |
| 2221 | |
| 2222 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2223 | // FPTrunc is never a no-op cast, no need to check |
| 2224 | SDValue N = getValue(I.getOperand(0)); |
| 2225 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2226 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0))); |
| 2227 | } |
| 2228 | |
| 2229 | void SelectionDAGLowering::visitFPExt(User &I){ |
| 2230 | // FPTrunc is never a no-op cast, no need to check |
| 2231 | SDValue N = getValue(I.getOperand(0)); |
| 2232 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2233 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N)); |
| 2234 | } |
| 2235 | |
| 2236 | void SelectionDAGLowering::visitFPToUI(User &I) { |
| 2237 | // FPToUI is never a no-op cast, no need to check |
| 2238 | SDValue N = getValue(I.getOperand(0)); |
| 2239 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2240 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N)); |
| 2241 | } |
| 2242 | |
| 2243 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2244 | // FPToSI is never a no-op cast, no need to check |
| 2245 | SDValue N = getValue(I.getOperand(0)); |
| 2246 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2247 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N)); |
| 2248 | } |
| 2249 | |
| 2250 | void SelectionDAGLowering::visitUIToFP(User &I) { |
| 2251 | // UIToFP is never a no-op cast, no need to check |
| 2252 | SDValue N = getValue(I.getOperand(0)); |
| 2253 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2254 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N)); |
| 2255 | } |
| 2256 | |
| 2257 | void SelectionDAGLowering::visitSIToFP(User &I){ |
| 2258 | // UIToFP is never a no-op cast, no need to check |
| 2259 | SDValue N = getValue(I.getOperand(0)); |
| 2260 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2261 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N)); |
| 2262 | } |
| 2263 | |
| 2264 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2265 | // What to do depends on the size of the integer and the size of the pointer. |
| 2266 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2267 | SDValue N = getValue(I.getOperand(0)); |
| 2268 | MVT SrcVT = N.getValueType(); |
| 2269 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2270 | SDValue Result; |
| 2271 | if (DestVT.bitsLT(SrcVT)) |
| 2272 | Result = DAG.getNode(ISD::TRUNCATE, DestVT, N); |
| 2273 | else |
| 2274 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 2275 | Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N); |
| 2276 | setValue(&I, Result); |
| 2277 | } |
| 2278 | |
| 2279 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2280 | // What to do depends on the size of the integer and the size of the pointer. |
| 2281 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2282 | SDValue N = getValue(I.getOperand(0)); |
| 2283 | MVT SrcVT = N.getValueType(); |
| 2284 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2285 | if (DestVT.bitsLT(SrcVT)) |
| 2286 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 2287 | else |
| 2288 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 2289 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 2290 | } |
| 2291 | |
| 2292 | void SelectionDAGLowering::visitBitCast(User &I) { |
| 2293 | SDValue N = getValue(I.getOperand(0)); |
| 2294 | MVT DestVT = TLI.getValueType(I.getType()); |
| 2295 | |
| 2296 | // BitCast assures us that source and destination are the same size so this |
| 2297 | // is either a BIT_CONVERT or a no-op. |
| 2298 | if (DestVT != N.getValueType()) |
| 2299 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types |
| 2300 | else |
| 2301 | setValue(&I, N); // noop cast. |
| 2302 | } |
| 2303 | |
| 2304 | void SelectionDAGLowering::visitInsertElement(User &I) { |
| 2305 | SDValue InVec = getValue(I.getOperand(0)); |
| 2306 | SDValue InVal = getValue(I.getOperand(1)); |
| 2307 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 2308 | getValue(I.getOperand(2))); |
| 2309 | |
| 2310 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, |
| 2311 | TLI.getValueType(I.getType()), |
| 2312 | InVec, InVal, InIdx)); |
| 2313 | } |
| 2314 | |
| 2315 | void SelectionDAGLowering::visitExtractElement(User &I) { |
| 2316 | SDValue InVec = getValue(I.getOperand(0)); |
| 2317 | SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 2318 | getValue(I.getOperand(1))); |
| 2319 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 2320 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2321 | } |
| 2322 | |
| 2323 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
| 2324 | SDValue V1 = getValue(I.getOperand(0)); |
| 2325 | SDValue V2 = getValue(I.getOperand(1)); |
| 2326 | SDValue Mask = getValue(I.getOperand(2)); |
| 2327 | |
| 2328 | setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, |
| 2329 | TLI.getValueType(I.getType()), |
| 2330 | V1, V2, Mask)); |
| 2331 | } |
| 2332 | |
| 2333 | void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) { |
| 2334 | const Value *Op0 = I.getOperand(0); |
| 2335 | const Value *Op1 = I.getOperand(1); |
| 2336 | const Type *AggTy = I.getType(); |
| 2337 | const Type *ValTy = Op1->getType(); |
| 2338 | bool IntoUndef = isa<UndefValue>(Op0); |
| 2339 | bool FromUndef = isa<UndefValue>(Op1); |
| 2340 | |
| 2341 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2342 | I.idx_begin(), I.idx_end()); |
| 2343 | |
| 2344 | SmallVector<MVT, 4> AggValueVTs; |
| 2345 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 2346 | SmallVector<MVT, 4> ValValueVTs; |
| 2347 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2348 | |
| 2349 | unsigned NumAggValues = AggValueVTs.size(); |
| 2350 | unsigned NumValValues = ValValueVTs.size(); |
| 2351 | SmallVector<SDValue, 4> Values(NumAggValues); |
| 2352 | |
| 2353 | SDValue Agg = getValue(Op0); |
| 2354 | SDValue Val = getValue(Op1); |
| 2355 | unsigned i = 0; |
| 2356 | // Copy the beginning value(s) from the original aggregate. |
| 2357 | for (; i != LinearIndex; ++i) |
| 2358 | Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) : |
| 2359 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2360 | // Copy values from the inserted value(s). |
| 2361 | for (; i != LinearIndex + NumValValues; ++i) |
| 2362 | Values[i] = FromUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) : |
| 2363 | SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); |
| 2364 | // Copy remaining value(s) from the original aggregate. |
| 2365 | for (; i != NumAggValues; ++i) |
| 2366 | Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) : |
| 2367 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2368 | |
| 2369 | setValue(&I, DAG.getMergeValues(DAG.getVTList(&AggValueVTs[0], NumAggValues), |
| 2370 | &Values[0], NumAggValues)); |
| 2371 | } |
| 2372 | |
| 2373 | void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) { |
| 2374 | const Value *Op0 = I.getOperand(0); |
| 2375 | const Type *AggTy = Op0->getType(); |
| 2376 | const Type *ValTy = I.getType(); |
| 2377 | bool OutOfUndef = isa<UndefValue>(Op0); |
| 2378 | |
| 2379 | unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy, |
| 2380 | I.idx_begin(), I.idx_end()); |
| 2381 | |
| 2382 | SmallVector<MVT, 4> ValValueVTs; |
| 2383 | ComputeValueVTs(TLI, ValTy, ValValueVTs); |
| 2384 | |
| 2385 | unsigned NumValValues = ValValueVTs.size(); |
| 2386 | SmallVector<SDValue, 4> Values(NumValValues); |
| 2387 | |
| 2388 | SDValue Agg = getValue(Op0); |
| 2389 | // Copy out the selected value(s). |
| 2390 | for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) |
| 2391 | Values[i - LinearIndex] = |
| 2392 | OutOfUndef ? DAG.getNode(ISD::UNDEF, Agg.getNode()->getValueType(Agg.getResNo() + i)) : |
| 2393 | SDValue(Agg.getNode(), Agg.getResNo() + i); |
| 2394 | |
| 2395 | setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValValueVTs[0], NumValValues), |
| 2396 | &Values[0], NumValValues)); |
| 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2401 | SDValue N = getValue(I.getOperand(0)); |
| 2402 | const Type *Ty = I.getOperand(0)->getType(); |
| 2403 | |
| 2404 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2405 | OI != E; ++OI) { |
| 2406 | Value *Idx = *OI; |
| 2407 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2408 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 2409 | if (Field) { |
| 2410 | // N = N + Offset |
| 2411 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
| 2412 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
| 2413 | DAG.getIntPtrConstant(Offset)); |
| 2414 | } |
| 2415 | Ty = StTy->getElementType(Field); |
| 2416 | } else { |
| 2417 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2418 | |
| 2419 | // If this is a constant subscript, handle it quickly. |
| 2420 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2421 | if (CI->getZExtValue() == 0) continue; |
| 2422 | uint64_t Offs = |
| 2423 | TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
| 2424 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
| 2425 | DAG.getIntPtrConstant(Offs)); |
| 2426 | continue; |
| 2427 | } |
| 2428 | |
| 2429 | // N = N + Idx * ElementSize; |
| 2430 | uint64_t ElementSize = TD->getABITypeSize(Ty); |
| 2431 | SDValue IdxN = getValue(Idx); |
| 2432 | |
| 2433 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2434 | // it. |
| 2435 | if (IdxN.getValueType().bitsLT(N.getValueType())) |
| 2436 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
| 2437 | else if (IdxN.getValueType().bitsGT(N.getValueType())) |
| 2438 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 2439 | |
| 2440 | // If this is a multiply by a power of two, turn it into a shl |
| 2441 | // immediately. This is a very common case. |
| 2442 | if (ElementSize != 1) { |
| 2443 | if (isPowerOf2_64(ElementSize)) { |
| 2444 | unsigned Amt = Log2_64(ElementSize); |
| 2445 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
| 2446 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
| 2447 | } else { |
| 2448 | SDValue Scale = DAG.getIntPtrConstant(ElementSize); |
| 2449 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 2454 | } |
| 2455 | } |
| 2456 | setValue(&I, N); |
| 2457 | } |
| 2458 | |
| 2459 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2460 | // If this is a fixed sized alloca in the entry block of the function, |
| 2461 | // allocate it statically on the stack. |
| 2462 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2463 | return; // getValue will auto-populate this. |
| 2464 | |
| 2465 | const Type *Ty = I.getAllocatedType(); |
| 2466 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
| 2467 | unsigned Align = |
| 2468 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 2469 | I.getAlignment()); |
| 2470 | |
| 2471 | SDValue AllocSize = getValue(I.getArraySize()); |
| 2472 | MVT IntPtr = TLI.getPointerTy(); |
| 2473 | if (IntPtr.bitsLT(AllocSize.getValueType())) |
| 2474 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 2475 | else if (IntPtr.bitsGT(AllocSize.getValueType())) |
| 2476 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
| 2477 | |
| 2478 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
| 2479 | DAG.getIntPtrConstant(TySize)); |
| 2480 | |
| 2481 | // Handle alignment. If the requested alignment is less than or equal to |
| 2482 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2483 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
| 2484 | unsigned StackAlign = |
| 2485 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 2486 | if (Align <= StackAlign) |
| 2487 | Align = 0; |
| 2488 | |
| 2489 | // Round the size of the allocation up to the stack alignment size |
| 2490 | // by add SA-1 to the size. |
| 2491 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 2492 | DAG.getIntPtrConstant(StackAlign-1)); |
| 2493 | // Mask out the low bits for alignment purposes. |
| 2494 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 2495 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 2496 | |
| 2497 | SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
| 2498 | const MVT *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(), |
| 2499 | MVT::Other); |
| 2500 | SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3); |
| 2501 | setValue(&I, DSA); |
| 2502 | DAG.setRoot(DSA.getValue(1)); |
| 2503 | |
| 2504 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2505 | // object. |
| 2506 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 2507 | } |
| 2508 | |
| 2509 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2510 | const Value *SV = I.getOperand(0); |
| 2511 | SDValue Ptr = getValue(SV); |
| 2512 | |
| 2513 | const Type *Ty = I.getType(); |
| 2514 | bool isVolatile = I.isVolatile(); |
| 2515 | unsigned Alignment = I.getAlignment(); |
| 2516 | |
| 2517 | SmallVector<MVT, 4> ValueVTs; |
| 2518 | SmallVector<uint64_t, 4> Offsets; |
| 2519 | ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets); |
| 2520 | unsigned NumValues = ValueVTs.size(); |
| 2521 | if (NumValues == 0) |
| 2522 | return; |
| 2523 | |
| 2524 | SDValue Root; |
| 2525 | bool ConstantMemory = false; |
| 2526 | if (I.isVolatile()) |
| 2527 | // Serialize volatile loads with other side effects. |
| 2528 | Root = getRoot(); |
| 2529 | else if (AA->pointsToConstantMemory(SV)) { |
| 2530 | // Do not serialize (non-volatile) loads of constant memory with anything. |
| 2531 | Root = DAG.getEntryNode(); |
| 2532 | ConstantMemory = true; |
| 2533 | } else { |
| 2534 | // Do not serialize non-volatile loads against each other. |
| 2535 | Root = DAG.getRoot(); |
| 2536 | } |
| 2537 | |
| 2538 | SmallVector<SDValue, 4> Values(NumValues); |
| 2539 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2540 | MVT PtrVT = Ptr.getValueType(); |
| 2541 | for (unsigned i = 0; i != NumValues; ++i) { |
| 2542 | SDValue L = DAG.getLoad(ValueVTs[i], Root, |
| 2543 | DAG.getNode(ISD::ADD, PtrVT, Ptr, |
| 2544 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2545 | SV, Offsets[i], |
| 2546 | isVolatile, Alignment); |
| 2547 | Values[i] = L; |
| 2548 | Chains[i] = L.getValue(1); |
| 2549 | } |
| 2550 | |
| 2551 | if (!ConstantMemory) { |
| 2552 | SDValue Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 2553 | &Chains[0], NumValues); |
| 2554 | if (isVolatile) |
| 2555 | DAG.setRoot(Chain); |
| 2556 | else |
| 2557 | PendingLoads.push_back(Chain); |
| 2558 | } |
| 2559 | |
| 2560 | setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], NumValues), |
| 2561 | &Values[0], NumValues)); |
| 2562 | } |
| 2563 | |
| 2564 | |
| 2565 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2566 | Value *SrcV = I.getOperand(0); |
| 2567 | Value *PtrV = I.getOperand(1); |
| 2568 | |
| 2569 | SmallVector<MVT, 4> ValueVTs; |
| 2570 | SmallVector<uint64_t, 4> Offsets; |
| 2571 | ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets); |
| 2572 | unsigned NumValues = ValueVTs.size(); |
| 2573 | if (NumValues == 0) |
| 2574 | return; |
| 2575 | |
| 2576 | // Get the lowered operands. Note that we do this after |
| 2577 | // checking if NumResults is zero, because with zero results |
| 2578 | // the operands won't have values in the map. |
| 2579 | SDValue Src = getValue(SrcV); |
| 2580 | SDValue Ptr = getValue(PtrV); |
| 2581 | |
| 2582 | SDValue Root = getRoot(); |
| 2583 | SmallVector<SDValue, 4> Chains(NumValues); |
| 2584 | MVT PtrVT = Ptr.getValueType(); |
| 2585 | bool isVolatile = I.isVolatile(); |
| 2586 | unsigned Alignment = I.getAlignment(); |
| 2587 | for (unsigned i = 0; i != NumValues; ++i) |
| 2588 | Chains[i] = DAG.getStore(Root, SDValue(Src.getNode(), Src.getResNo() + i), |
| 2589 | DAG.getNode(ISD::ADD, PtrVT, Ptr, |
| 2590 | DAG.getConstant(Offsets[i], PtrVT)), |
| 2591 | PtrV, Offsets[i], |
| 2592 | isVolatile, Alignment); |
| 2593 | |
| 2594 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumValues)); |
| 2595 | } |
| 2596 | |
| 2597 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2598 | /// node. |
| 2599 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
| 2600 | unsigned Intrinsic) { |
| 2601 | bool HasChain = !I.doesNotAccessMemory(); |
| 2602 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2603 | |
| 2604 | // Build the operand list. |
| 2605 | SmallVector<SDValue, 8> Ops; |
| 2606 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2607 | if (OnlyLoad) { |
| 2608 | // We don't need to serialize loads against other loads. |
| 2609 | Ops.push_back(DAG.getRoot()); |
| 2610 | } else { |
| 2611 | Ops.push_back(getRoot()); |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | // Add the intrinsic ID as an integer operand. |
| 2616 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 2617 | |
| 2618 | // Add all operands of the call to the operand list. |
| 2619 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2620 | SDValue Op = getValue(I.getOperand(i)); |
| 2621 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2622 | "Intrinsic uses a non-legal type?"); |
| 2623 | Ops.push_back(Op); |
| 2624 | } |
| 2625 | |
| 2626 | std::vector<MVT> VTs; |
| 2627 | if (I.getType() != Type::VoidTy) { |
| 2628 | MVT VT = TLI.getValueType(I.getType()); |
| 2629 | if (VT.isVector()) { |
| 2630 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2631 | MVT EltVT = TLI.getValueType(DestTy->getElementType()); |
| 2632 | |
| 2633 | VT = MVT::getVectorVT(EltVT, DestTy->getNumElements()); |
| 2634 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2635 | } |
| 2636 | |
| 2637 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
| 2638 | VTs.push_back(VT); |
| 2639 | } |
| 2640 | if (HasChain) |
| 2641 | VTs.push_back(MVT::Other); |
| 2642 | |
| 2643 | const MVT *VTList = DAG.getNodeValueTypes(VTs); |
| 2644 | |
| 2645 | // Create the node. |
| 2646 | SDValue Result; |
| 2647 | if (!HasChain) |
| 2648 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(), |
| 2649 | &Ops[0], Ops.size()); |
| 2650 | else if (I.getType() != Type::VoidTy) |
| 2651 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(), |
| 2652 | &Ops[0], Ops.size()); |
| 2653 | else |
| 2654 | Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(), |
| 2655 | &Ops[0], Ops.size()); |
| 2656 | |
| 2657 | if (HasChain) { |
| 2658 | SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); |
| 2659 | if (OnlyLoad) |
| 2660 | PendingLoads.push_back(Chain); |
| 2661 | else |
| 2662 | DAG.setRoot(Chain); |
| 2663 | } |
| 2664 | if (I.getType() != Type::VoidTy) { |
| 2665 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
| 2666 | MVT VT = TLI.getValueType(PTy); |
| 2667 | Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result); |
| 2668 | } |
| 2669 | setValue(&I, Result); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 2674 | static GlobalVariable *ExtractTypeInfo(Value *V) { |
| 2675 | V = V->stripPointerCasts(); |
| 2676 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 2677 | assert ((GV || isa<ConstantPointerNull>(V)) && |
| 2678 | "TypeInfo must be a global variable or NULL"); |
| 2679 | return GV; |
| 2680 | } |
| 2681 | |
| 2682 | namespace llvm { |
| 2683 | |
| 2684 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 2685 | /// call, and add them to the specified machine basic block. |
| 2686 | void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 2687 | MachineBasicBlock *MBB) { |
| 2688 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 2689 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 2690 | assert(CE->getOpcode() == Instruction::BitCast && |
| 2691 | isa<Function>(CE->getOperand(0)) && |
| 2692 | "Personality should be a function"); |
| 2693 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 2694 | |
| 2695 | // Gather all the type infos for this landing pad and pass them along to |
| 2696 | // MachineModuleInfo. |
| 2697 | std::vector<GlobalVariable *> TyInfo; |
| 2698 | unsigned N = I.getNumOperands(); |
| 2699 | |
| 2700 | for (unsigned i = N - 1; i > 2; --i) { |
| 2701 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 2702 | unsigned FilterLength = CI->getZExtValue(); |
| 2703 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
| 2704 | assert (FirstCatch <= N && "Invalid filter length"); |
| 2705 | |
| 2706 | if (FirstCatch < N) { |
| 2707 | TyInfo.reserve(N - FirstCatch); |
| 2708 | for (unsigned j = FirstCatch; j < N; ++j) |
| 2709 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 2710 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 2711 | TyInfo.clear(); |
| 2712 | } |
| 2713 | |
| 2714 | if (!FilterLength) { |
| 2715 | // Cleanup. |
| 2716 | MMI->addCleanup(MBB); |
| 2717 | } else { |
| 2718 | // Filter. |
| 2719 | TyInfo.reserve(FilterLength - 1); |
| 2720 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 2721 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 2722 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 2723 | TyInfo.clear(); |
| 2724 | } |
| 2725 | |
| 2726 | N = i; |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | if (N > 3) { |
| 2731 | TyInfo.reserve(N - 3); |
| 2732 | for (unsigned j = 3; j < N; ++j) |
| 2733 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 2734 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | } |
| 2739 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2740 | /// GetSignificand - Get the significand and build it into a floating-point |
| 2741 | /// number with exponent of 1: |
| 2742 | /// |
| 2743 | /// Op = (Op & 0x007fffff) | 0x3f800000; |
| 2744 | /// |
| 2745 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2746 | static SDValue |
| 2747 | GetSignificand(SelectionDAG &DAG, SDValue Op) { |
| 2748 | SDValue t1 = DAG.getNode(ISD::AND, MVT::i32, Op, |
| 2749 | DAG.getConstant(0x007fffff, MVT::i32)); |
| 2750 | SDValue t2 = DAG.getNode(ISD::OR, MVT::i32, t1, |
| 2751 | DAG.getConstant(0x3f800000, MVT::i32)); |
| 2752 | return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t2); |
| 2753 | } |
| 2754 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2755 | /// GetExponent - Get the exponent: |
| 2756 | /// |
| 2757 | /// (float)((Op1 >> 23) - 127); |
| 2758 | /// |
| 2759 | /// where Op is the hexidecimal representation of floating point value. |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2760 | static SDValue |
| 2761 | GetExponent(SelectionDAG &DAG, SDValue Op) { |
Bill Wendling | fc2508e | 2008-09-10 06:26:10 +0000 | [diff] [blame] | 2762 | SDValue t1 = DAG.getNode(ISD::SRL, MVT::i32, Op, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2763 | DAG.getConstant(23, MVT::i32)); |
Bill Wendling | fc2508e | 2008-09-10 06:26:10 +0000 | [diff] [blame] | 2764 | SDValue t2 = DAG.getNode(ISD::SUB, MVT::i32, t1, |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2765 | DAG.getConstant(127, MVT::i32)); |
Bill Wendling | fc2508e | 2008-09-10 06:26:10 +0000 | [diff] [blame] | 2766 | return DAG.getNode(ISD::UINT_TO_FP, MVT::f32, t2); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2769 | /// getF32Constant - Get 32-bit floating point constant. |
| 2770 | static SDValue |
| 2771 | getF32Constant(SelectionDAG &DAG, unsigned Flt) { |
| 2772 | return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32); |
| 2773 | } |
| 2774 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 2775 | /// Inlined utility function to implement binary input atomic intrinsics for |
| 2776 | /// visitIntrinsicCall: I is a call instruction |
| 2777 | /// Op is the associated NodeType for I |
| 2778 | const char * |
| 2779 | SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) { |
| 2780 | SDValue Root = getRoot(); |
| 2781 | SDValue L = DAG.getAtomic(Op, Root, |
| 2782 | getValue(I.getOperand(1)), |
| 2783 | getValue(I.getOperand(2)), |
| 2784 | I.getOperand(1)); |
| 2785 | setValue(&I, L); |
| 2786 | DAG.setRoot(L.getValue(1)); |
| 2787 | return 0; |
| 2788 | } |
| 2789 | |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2790 | /// visitExp - Lower an exp intrinsic. Handles the special sequences for |
| 2791 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2792 | void |
| 2793 | SelectionDAGLowering::visitExp(CallInst &I) { |
| 2794 | SDValue result; |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2795 | |
| 2796 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 2797 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2798 | SDValue Op = getValue(I.getOperand(1)); |
| 2799 | |
| 2800 | // Put the exponent in the right bit position for later addition to the |
| 2801 | // final result: |
| 2802 | // |
| 2803 | // #define LOG2OFe 1.4426950f |
| 2804 | // IntegerPartOfX = ((int32_t)(X * LOG2OFe)); |
| 2805 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2806 | getF32Constant(DAG, 0x3fb8aa3b)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2807 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0); |
| 2808 | |
| 2809 | // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX; |
| 2810 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX); |
| 2811 | SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1); |
| 2812 | |
| 2813 | // IntegerPartOfX <<= 23; |
| 2814 | IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX, |
| 2815 | DAG.getConstant(23, MVT::i32)); |
| 2816 | |
| 2817 | if (LimitFloatPrecision <= 6) { |
| 2818 | // For floating-point precision of 6: |
| 2819 | // |
| 2820 | // TwoToFractionalPartOfX = |
| 2821 | // 0.997535578f + |
| 2822 | // (0.735607626f + 0.252464424f * x) * x; |
| 2823 | // |
| 2824 | // error 0.0144103317, which is 6 bits |
| 2825 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2826 | getF32Constant(DAG, 0x3e814304)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2827 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2828 | getF32Constant(DAG, 0x3f3c50c8)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2829 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 2830 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2831 | getF32Constant(DAG, 0x3f7f5e7e)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2832 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5); |
| 2833 | |
| 2834 | // Add the exponent into the result in integer domain. |
| 2835 | SDValue t6 = DAG.getNode(ISD::ADD, MVT::i32, |
| 2836 | TwoToFracPartOfX, IntegerPartOfX); |
| 2837 | |
| 2838 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t6); |
| 2839 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 2840 | // For floating-point precision of 12: |
| 2841 | // |
| 2842 | // TwoToFractionalPartOfX = |
| 2843 | // 0.999892986f + |
| 2844 | // (0.696457318f + |
| 2845 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 2846 | // |
| 2847 | // 0.000107046256 error, which is 13 to 14 bits |
| 2848 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2849 | getF32Constant(DAG, 0x3da235e3)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2850 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2851 | getF32Constant(DAG, 0x3e65b8f3)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2852 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 2853 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2854 | getF32Constant(DAG, 0x3f324b07)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2855 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 2856 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2857 | getF32Constant(DAG, 0x3f7ff8fd)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2858 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7); |
| 2859 | |
| 2860 | // Add the exponent into the result in integer domain. |
| 2861 | SDValue t8 = DAG.getNode(ISD::ADD, MVT::i32, |
| 2862 | TwoToFracPartOfX, IntegerPartOfX); |
| 2863 | |
| 2864 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t8); |
| 2865 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 2866 | // For floating-point precision of 18: |
| 2867 | // |
| 2868 | // TwoToFractionalPartOfX = |
| 2869 | // 0.999999982f + |
| 2870 | // (0.693148872f + |
| 2871 | // (0.240227044f + |
| 2872 | // (0.554906021e-1f + |
| 2873 | // (0.961591928e-2f + |
| 2874 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 2875 | // |
| 2876 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 2877 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2878 | getF32Constant(DAG, 0x3924b03e)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2879 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2880 | getF32Constant(DAG, 0x3ab24b87)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2881 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 2882 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2883 | getF32Constant(DAG, 0x3c1d8c17)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2884 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 2885 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2886 | getF32Constant(DAG, 0x3d634a1d)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2887 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
| 2888 | SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2889 | getF32Constant(DAG, 0x3e75fe14)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2890 | SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X); |
| 2891 | SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2892 | getF32Constant(DAG, 0x3f317234)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2893 | SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X); |
| 2894 | SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2895 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | b4ec283 | 2008-09-09 22:13:54 +0000 | [diff] [blame] | 2896 | SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13); |
| 2897 | |
| 2898 | // Add the exponent into the result in integer domain. |
| 2899 | SDValue t14 = DAG.getNode(ISD::ADD, MVT::i32, |
| 2900 | TwoToFracPartOfX, IntegerPartOfX); |
| 2901 | |
| 2902 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t14); |
| 2903 | } |
| 2904 | } else { |
| 2905 | // No special expansion. |
| 2906 | result = DAG.getNode(ISD::FEXP, |
| 2907 | getValue(I.getOperand(1)).getValueType(), |
| 2908 | getValue(I.getOperand(1))); |
| 2909 | } |
| 2910 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2911 | setValue(&I, result); |
| 2912 | } |
| 2913 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2914 | /// visitLog - Lower a log intrinsic. Handles the special sequences for |
| 2915 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 2916 | void |
| 2917 | SelectionDAGLowering::visitLog(CallInst &I) { |
| 2918 | SDValue result; |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2919 | |
| 2920 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
| 2921 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 2922 | SDValue Op = getValue(I.getOperand(1)); |
| 2923 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op); |
| 2924 | |
| 2925 | // Scale the exponent by log(2) [0.69314718f]. |
| 2926 | SDValue Exp = GetExponent(DAG, Op1); |
| 2927 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2928 | getF32Constant(DAG, 0x3f317218)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Get the significand and build it into a floating-point number with |
| 2931 | // exponent of 1. |
| 2932 | SDValue X = GetSignificand(DAG, Op1); |
| 2933 | |
| 2934 | if (LimitFloatPrecision <= 6) { |
| 2935 | // For floating-point precision of 6: |
| 2936 | // |
| 2937 | // LogofMantissa = |
| 2938 | // -1.1609546f + |
| 2939 | // (1.4034025f - 0.23903021f * x) * x; |
| 2940 | // |
| 2941 | // error 0.0034276066, which is better than 8 bits |
| 2942 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2943 | getF32Constant(DAG, 0xbe74c456)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2944 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2945 | getF32Constant(DAG, 0x3fb3a2b1)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2946 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 2947 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2948 | getF32Constant(DAG, 0x3f949a29)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2949 | |
| 2950 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa); |
| 2951 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 2952 | // For floating-point precision of 12: |
| 2953 | // |
| 2954 | // LogOfMantissa = |
| 2955 | // -1.7417939f + |
| 2956 | // (2.8212026f + |
| 2957 | // (-1.4699568f + |
| 2958 | // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; |
| 2959 | // |
| 2960 | // error 0.000061011436, which is 14 bits |
| 2961 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2962 | getF32Constant(DAG, 0xbd67b6d6)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2963 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2964 | getF32Constant(DAG, 0x3ee4f4b8)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2965 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 2966 | SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2967 | getF32Constant(DAG, 0x3fbc278b)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2968 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 2969 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2970 | getF32Constant(DAG, 0x40348e95)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2971 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 2972 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2973 | getF32Constant(DAG, 0x3fdef31a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2974 | |
| 2975 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa); |
| 2976 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 2977 | // For floating-point precision of 18: |
| 2978 | // |
| 2979 | // LogOfMantissa = |
| 2980 | // -2.1072184f + |
| 2981 | // (4.2372794f + |
| 2982 | // (-3.7029485f + |
| 2983 | // (2.2781945f + |
| 2984 | // (-0.87823314f + |
| 2985 | // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; |
| 2986 | // |
| 2987 | // error 0.0000023660568, which is better than 18 bits |
| 2988 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2989 | getF32Constant(DAG, 0xbc91e5ac)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2990 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2991 | getF32Constant(DAG, 0x3e4350aa)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2992 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 2993 | SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2994 | getF32Constant(DAG, 0x3f60d3e3)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2995 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 2996 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 2997 | getF32Constant(DAG, 0x4011cdf0)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 2998 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 2999 | SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3000 | getF32Constant(DAG, 0x406cfd1c)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3001 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
| 3002 | SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3003 | getF32Constant(DAG, 0x408797cb)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3004 | SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X); |
| 3005 | SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3006 | getF32Constant(DAG, 0x4006dcab)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3007 | |
| 3008 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa); |
| 3009 | } |
| 3010 | } else { |
| 3011 | // No special expansion. |
| 3012 | result = DAG.getNode(ISD::FLOG, |
| 3013 | getValue(I.getOperand(1)).getValueType(), |
| 3014 | getValue(I.getOperand(1))); |
| 3015 | } |
| 3016 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3017 | setValue(&I, result); |
| 3018 | } |
| 3019 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3020 | /// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for |
| 3021 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3022 | void |
| 3023 | SelectionDAGLowering::visitLog2(CallInst &I) { |
| 3024 | SDValue result; |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3025 | |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3026 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3027 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3028 | SDValue Op = getValue(I.getOperand(1)); |
| 3029 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op); |
| 3030 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3031 | // Get the exponent. |
| 3032 | SDValue LogOfExponent = GetExponent(DAG, Op1); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3035 | // exponent of 1. |
| 3036 | SDValue X = GetSignificand(DAG, Op1); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3037 | |
| 3038 | // Different possible minimax approximations of significand in |
| 3039 | // floating-point for various degrees of accuracy over [1,2]. |
| 3040 | if (LimitFloatPrecision <= 6) { |
| 3041 | // For floating-point precision of 6: |
| 3042 | // |
| 3043 | // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; |
| 3044 | // |
| 3045 | // error 0.0049451742, which is more than 7 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3046 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3047 | getF32Constant(DAG, 0xbeb08fe0)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3048 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3049 | getF32Constant(DAG, 0x40019463)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3050 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3051 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3052 | getF32Constant(DAG, 0x3fd6633d)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3053 | |
| 3054 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3055 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3056 | // For floating-point precision of 12: |
| 3057 | // |
| 3058 | // Log2ofMantissa = |
| 3059 | // -2.51285454f + |
| 3060 | // (4.07009056f + |
| 3061 | // (-2.12067489f + |
| 3062 | // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; |
| 3063 | // |
| 3064 | // error 0.0000876136000, which is better than 13 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3065 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3066 | getF32Constant(DAG, 0xbda7262e)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3067 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3068 | getF32Constant(DAG, 0x3f25280b)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3069 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3070 | SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3071 | getF32Constant(DAG, 0x4007b923)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3072 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3073 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3074 | getF32Constant(DAG, 0x40823e2f)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3075 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3076 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3077 | getF32Constant(DAG, 0x4020d29c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3078 | |
| 3079 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3080 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3081 | // For floating-point precision of 18: |
| 3082 | // |
| 3083 | // Log2ofMantissa = |
| 3084 | // -3.0400495f + |
| 3085 | // (6.1129976f + |
| 3086 | // (-5.3420409f + |
| 3087 | // (3.2865683f + |
| 3088 | // (-1.2669343f + |
| 3089 | // (0.27515199f - |
| 3090 | // 0.25691327e-1f * x) * x) * x) * x) * x) * x; |
| 3091 | // |
| 3092 | // error 0.0000018516, which is better than 18 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3093 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3094 | getF32Constant(DAG, 0xbcd2769e)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3095 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3096 | getF32Constant(DAG, 0x3e8ce0b9)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3097 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3098 | SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3099 | getF32Constant(DAG, 0x3fa22ae7)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3100 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3101 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3102 | getF32Constant(DAG, 0x40525723)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3103 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3104 | SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3105 | getF32Constant(DAG, 0x40aaf200)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3106 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
| 3107 | SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3108 | getF32Constant(DAG, 0x40c39dad)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3109 | SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3110 | SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3111 | getF32Constant(DAG, 0x4042902c)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3112 | |
| 3113 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa); |
| 3114 | } |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3115 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3116 | // No special expansion. |
Dale Johannesen | 853244f | 2008-09-05 23:49:37 +0000 | [diff] [blame] | 3117 | result = DAG.getNode(ISD::FLOG2, |
| 3118 | getValue(I.getOperand(1)).getValueType(), |
| 3119 | getValue(I.getOperand(1))); |
| 3120 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3121 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3122 | setValue(&I, result); |
| 3123 | } |
| 3124 | |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3125 | /// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for |
| 3126 | /// limited-precision mode. |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3127 | void |
| 3128 | SelectionDAGLowering::visitLog10(CallInst &I) { |
| 3129 | SDValue result; |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3130 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3131 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3132 | SDValue Op = getValue(I.getOperand(1)); |
| 3133 | SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op); |
| 3134 | |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3135 | // Scale the exponent by log10(2) [0.30102999f]. |
| 3136 | SDValue Exp = GetExponent(DAG, Op1); |
| 3137 | SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3138 | getF32Constant(DAG, 0x3e9a209a)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3139 | |
| 3140 | // Get the significand and build it into a floating-point number with |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3141 | // exponent of 1. |
| 3142 | SDValue X = GetSignificand(DAG, Op1); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3143 | |
| 3144 | if (LimitFloatPrecision <= 6) { |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3145 | // For floating-point precision of 6: |
| 3146 | // |
| 3147 | // Log10ofMantissa = |
| 3148 | // -0.50419619f + |
| 3149 | // (0.60948995f - 0.10380950f * x) * x; |
| 3150 | // |
| 3151 | // error 0.0014886165, which is 6 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3152 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3153 | getF32Constant(DAG, 0xbdd49a13)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3154 | SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3155 | getF32Constant(DAG, 0x3f1c0789)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3156 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3157 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3158 | getF32Constant(DAG, 0x3f011300)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3159 | |
| 3160 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3161 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3162 | // For floating-point precision of 12: |
| 3163 | // |
| 3164 | // Log10ofMantissa = |
| 3165 | // -0.64831180f + |
| 3166 | // (0.91751397f + |
| 3167 | // (-0.31664806f + 0.47637168e-1f * x) * x) * x; |
| 3168 | // |
| 3169 | // error 0.00019228036, which is better than 12 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3170 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3171 | getF32Constant(DAG, 0x3d431f31)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3172 | SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3173 | getF32Constant(DAG, 0x3ea21fb2)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3174 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3175 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3176 | getF32Constant(DAG, 0x3f6ae232)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3177 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3178 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3179 | getF32Constant(DAG, 0x3f25f7c3)); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3180 | |
| 3181 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa); |
| 3182 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3183 | // For floating-point precision of 18: |
| 3184 | // |
| 3185 | // Log10ofMantissa = |
| 3186 | // -0.84299375f + |
| 3187 | // (1.5327582f + |
| 3188 | // (-1.0688956f + |
| 3189 | // (0.49102474f + |
| 3190 | // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; |
| 3191 | // |
| 3192 | // error 0.0000037995730, which is better than 18 bits |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3193 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3194 | getF32Constant(DAG, 0x3c5d51ce)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3195 | SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3196 | getF32Constant(DAG, 0x3e00685a)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3197 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X); |
| 3198 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3199 | getF32Constant(DAG, 0x3efb6798)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3200 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3201 | SDValue t5 = DAG.getNode(ISD::FSUB, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3202 | getF32Constant(DAG, 0x3f88d192)); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3203 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3204 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3205 | getF32Constant(DAG, 0x3fc4316c)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3206 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
Bill Wendling | 3915025 | 2008-09-09 20:39:27 +0000 | [diff] [blame] | 3207 | SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3208 | getF32Constant(DAG, 0x3f57ce70)); |
Bill Wendling | bd297bc | 2008-09-09 18:42:23 +0000 | [diff] [blame] | 3209 | |
| 3210 | result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa); |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3211 | } |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3212 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3213 | // No special expansion. |
Dale Johannesen | 852680a | 2008-09-05 21:27:19 +0000 | [diff] [blame] | 3214 | result = DAG.getNode(ISD::FLOG10, |
| 3215 | getValue(I.getOperand(1)).getValueType(), |
| 3216 | getValue(I.getOperand(1))); |
| 3217 | } |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3218 | |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3219 | setValue(&I, result); |
| 3220 | } |
| 3221 | |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3222 | /// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for |
| 3223 | /// limited-precision mode. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3224 | void |
| 3225 | SelectionDAGLowering::visitExp2(CallInst &I) { |
| 3226 | SDValue result; |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3227 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3228 | if (getValue(I.getOperand(1)).getValueType() == MVT::f32 && |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3229 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3230 | SDValue Op = getValue(I.getOperand(1)); |
| 3231 | |
| 3232 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, Op); |
| 3233 | |
| 3234 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
| 3235 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX); |
| 3236 | SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, Op, t1); |
| 3237 | |
| 3238 | // IntegerPartOfX <<= 23; |
| 3239 | IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX, |
| 3240 | DAG.getConstant(23, MVT::i32)); |
| 3241 | |
| 3242 | if (LimitFloatPrecision <= 6) { |
| 3243 | // For floating-point precision of 6: |
| 3244 | // |
| 3245 | // TwoToFractionalPartOfX = |
| 3246 | // 0.997535578f + |
| 3247 | // (0.735607626f + 0.252464424f * x) * x; |
| 3248 | // |
| 3249 | // error 0.0144103317, which is 6 bits |
| 3250 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3251 | getF32Constant(DAG, 0x3e814304)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3252 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3253 | getF32Constant(DAG, 0x3f3c50c8)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3254 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3255 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3256 | getF32Constant(DAG, 0x3f7f5e7e)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3257 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5); |
| 3258 | SDValue TwoToFractionalPartOfX = |
| 3259 | DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX); |
| 3260 | |
| 3261 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3262 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3263 | // For floating-point precision of 12: |
| 3264 | // |
| 3265 | // TwoToFractionalPartOfX = |
| 3266 | // 0.999892986f + |
| 3267 | // (0.696457318f + |
| 3268 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3269 | // |
| 3270 | // error 0.000107046256, which is 13 to 14 bits |
| 3271 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3272 | getF32Constant(DAG, 0x3da235e3)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3273 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3274 | getF32Constant(DAG, 0x3e65b8f3)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3275 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3276 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3277 | getF32Constant(DAG, 0x3f324b07)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3278 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3279 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3280 | getF32Constant(DAG, 0x3f7ff8fd)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3281 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7); |
| 3282 | SDValue TwoToFractionalPartOfX = |
| 3283 | DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX); |
| 3284 | |
| 3285 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3286 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3287 | // For floating-point precision of 18: |
| 3288 | // |
| 3289 | // TwoToFractionalPartOfX = |
| 3290 | // 0.999999982f + |
| 3291 | // (0.693148872f + |
| 3292 | // (0.240227044f + |
| 3293 | // (0.554906021e-1f + |
| 3294 | // (0.961591928e-2f + |
| 3295 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3296 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 3297 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3298 | getF32Constant(DAG, 0x3924b03e)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3299 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3300 | getF32Constant(DAG, 0x3ab24b87)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3301 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3302 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3303 | getF32Constant(DAG, 0x3c1d8c17)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3304 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3305 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3306 | getF32Constant(DAG, 0x3d634a1d)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3307 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
| 3308 | SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3309 | getF32Constant(DAG, 0x3e75fe14)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3310 | SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X); |
| 3311 | SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3312 | getF32Constant(DAG, 0x3f317234)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3313 | SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X); |
| 3314 | SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3315 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3316 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13); |
| 3317 | SDValue TwoToFractionalPartOfX = |
| 3318 | DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX); |
| 3319 | |
| 3320 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3321 | } |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3322 | } else { |
Bill Wendling | 3eb5940 | 2008-09-09 00:28:24 +0000 | [diff] [blame] | 3323 | // No special expansion. |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3324 | result = DAG.getNode(ISD::FEXP2, |
| 3325 | getValue(I.getOperand(1)).getValueType(), |
| 3326 | getValue(I.getOperand(1))); |
| 3327 | } |
Bill Wendling | e10c814 | 2008-09-09 22:39:21 +0000 | [diff] [blame] | 3328 | |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3329 | setValue(&I, result); |
| 3330 | } |
| 3331 | |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3332 | /// visitPow - Lower a pow intrinsic. Handles the special sequences for |
| 3333 | /// limited-precision mode with x == 10.0f. |
| 3334 | void |
| 3335 | SelectionDAGLowering::visitPow(CallInst &I) { |
| 3336 | SDValue result; |
| 3337 | Value *Val = I.getOperand(1); |
| 3338 | bool IsExp10 = false; |
| 3339 | |
| 3340 | if (getValue(Val).getValueType() == MVT::f32 && |
Bill Wendling | 277fc24 | 2008-09-10 00:24:59 +0000 | [diff] [blame] | 3341 | getValue(I.getOperand(2)).getValueType() == MVT::f32 && |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3342 | LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3343 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) { |
| 3344 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3345 | APFloat Ten(10.0f); |
| 3346 | IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten); |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { |
| 3352 | SDValue Op = getValue(I.getOperand(2)); |
| 3353 | |
| 3354 | // Put the exponent in the right bit position for later addition to the |
| 3355 | // final result: |
| 3356 | // |
| 3357 | // #define LOG2OF10 3.3219281f |
| 3358 | // IntegerPartOfX = (int32_t)(x * LOG2OF10); |
| 3359 | SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3360 | getF32Constant(DAG, 0x40549a78)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3361 | SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0); |
| 3362 | |
| 3363 | // FractionalPartOfX = x - (float)IntegerPartOfX; |
| 3364 | SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX); |
| 3365 | SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1); |
| 3366 | |
| 3367 | // IntegerPartOfX <<= 23; |
| 3368 | IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX, |
| 3369 | DAG.getConstant(23, MVT::i32)); |
| 3370 | |
| 3371 | if (LimitFloatPrecision <= 6) { |
| 3372 | // For floating-point precision of 6: |
| 3373 | // |
| 3374 | // twoToFractionalPartOfX = |
| 3375 | // 0.997535578f + |
| 3376 | // (0.735607626f + 0.252464424f * x) * x; |
| 3377 | // |
| 3378 | // error 0.0144103317, which is 6 bits |
| 3379 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3380 | getF32Constant(DAG, 0x3e814304)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3381 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3382 | getF32Constant(DAG, 0x3f3c50c8)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3383 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3384 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3385 | getF32Constant(DAG, 0x3f7f5e7e)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3386 | SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5); |
| 3387 | SDValue TwoToFractionalPartOfX = |
| 3388 | DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX); |
| 3389 | |
| 3390 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3391 | } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) { |
| 3392 | // For floating-point precision of 12: |
| 3393 | // |
| 3394 | // TwoToFractionalPartOfX = |
| 3395 | // 0.999892986f + |
| 3396 | // (0.696457318f + |
| 3397 | // (0.224338339f + 0.792043434e-1f * x) * x) * x; |
| 3398 | // |
| 3399 | // error 0.000107046256, which is 13 to 14 bits |
| 3400 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3401 | getF32Constant(DAG, 0x3da235e3)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3402 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3403 | getF32Constant(DAG, 0x3e65b8f3)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3404 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3405 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3406 | getF32Constant(DAG, 0x3f324b07)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3407 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3408 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3409 | getF32Constant(DAG, 0x3f7ff8fd)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3410 | SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7); |
| 3411 | SDValue TwoToFractionalPartOfX = |
| 3412 | DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX); |
| 3413 | |
| 3414 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3415 | } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18 |
| 3416 | // For floating-point precision of 18: |
| 3417 | // |
| 3418 | // TwoToFractionalPartOfX = |
| 3419 | // 0.999999982f + |
| 3420 | // (0.693148872f + |
| 3421 | // (0.240227044f + |
| 3422 | // (0.554906021e-1f + |
| 3423 | // (0.961591928e-2f + |
| 3424 | // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; |
| 3425 | // error 2.47208000*10^(-7), which is better than 18 bits |
| 3426 | SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3427 | getF32Constant(DAG, 0x3924b03e)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3428 | SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3429 | getF32Constant(DAG, 0x3ab24b87)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3430 | SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X); |
| 3431 | SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3432 | getF32Constant(DAG, 0x3c1d8c17)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3433 | SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X); |
| 3434 | SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3435 | getF32Constant(DAG, 0x3d634a1d)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3436 | SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X); |
| 3437 | SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3438 | getF32Constant(DAG, 0x3e75fe14)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3439 | SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X); |
| 3440 | SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3441 | getF32Constant(DAG, 0x3f317234)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3442 | SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X); |
| 3443 | SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12, |
Bill Wendling | cd4c73a | 2008-09-22 00:44:35 +0000 | [diff] [blame] | 3444 | getF32Constant(DAG, 0x3f800000)); |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3445 | SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13); |
| 3446 | SDValue TwoToFractionalPartOfX = |
| 3447 | DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX); |
| 3448 | |
| 3449 | result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX); |
| 3450 | } |
| 3451 | } else { |
| 3452 | // No special expansion. |
| 3453 | result = DAG.getNode(ISD::FPOW, |
| 3454 | getValue(I.getOperand(1)).getValueType(), |
| 3455 | getValue(I.getOperand(1)), |
| 3456 | getValue(I.getOperand(2))); |
| 3457 | } |
| 3458 | |
| 3459 | setValue(&I, result); |
| 3460 | } |
| 3461 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3462 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 3463 | /// we want to emit this as a call to a named external function, return the name |
| 3464 | /// otherwise lower it and return null. |
| 3465 | const char * |
| 3466 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 3467 | switch (Intrinsic) { |
| 3468 | default: |
| 3469 | // By default, turn this into a target intrinsic node. |
| 3470 | visitTargetIntrinsic(I, Intrinsic); |
| 3471 | return 0; |
| 3472 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 3473 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 3474 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 3475 | case Intrinsic::returnaddress: |
| 3476 | setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(), |
| 3477 | getValue(I.getOperand(1)))); |
| 3478 | return 0; |
| 3479 | case Intrinsic::frameaddress: |
| 3480 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(), |
| 3481 | getValue(I.getOperand(1)))); |
| 3482 | return 0; |
| 3483 | case Intrinsic::setjmp: |
| 3484 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
| 3485 | break; |
| 3486 | case Intrinsic::longjmp: |
| 3487 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
| 3488 | break; |
| 3489 | case Intrinsic::memcpy_i32: |
| 3490 | case Intrinsic::memcpy_i64: { |
| 3491 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3492 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3493 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3494 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3495 | DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false, |
| 3496 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3497 | return 0; |
| 3498 | } |
| 3499 | case Intrinsic::memset_i32: |
| 3500 | case Intrinsic::memset_i64: { |
| 3501 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3502 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3503 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3504 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3505 | DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align, |
| 3506 | I.getOperand(1), 0)); |
| 3507 | return 0; |
| 3508 | } |
| 3509 | case Intrinsic::memmove_i32: |
| 3510 | case Intrinsic::memmove_i64: { |
| 3511 | SDValue Op1 = getValue(I.getOperand(1)); |
| 3512 | SDValue Op2 = getValue(I.getOperand(2)); |
| 3513 | SDValue Op3 = getValue(I.getOperand(3)); |
| 3514 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 3515 | |
| 3516 | // If the source and destination are known to not be aliases, we can |
| 3517 | // lower memmove as memcpy. |
| 3518 | uint64_t Size = -1ULL; |
| 3519 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3520 | Size = C->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3521 | if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 3522 | AliasAnalysis::NoAlias) { |
| 3523 | DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false, |
| 3524 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3525 | return 0; |
| 3526 | } |
| 3527 | |
| 3528 | DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align, |
| 3529 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 3530 | return 0; |
| 3531 | } |
| 3532 | case Intrinsic::dbg_stoppoint: { |
| 3533 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3534 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
| 3535 | if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) { |
| 3536 | DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext()); |
| 3537 | assert(DD && "Not a debug information descriptor"); |
| 3538 | DAG.setRoot(DAG.getDbgStopPoint(getRoot(), |
| 3539 | SPI.getLine(), |
| 3540 | SPI.getColumn(), |
| 3541 | cast<CompileUnitDesc>(DD))); |
| 3542 | } |
| 3543 | |
| 3544 | return 0; |
| 3545 | } |
| 3546 | case Intrinsic::dbg_region_start: { |
| 3547 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3548 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
| 3549 | if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) { |
| 3550 | unsigned LabelID = MMI->RecordRegionStart(RSI.getContext()); |
| 3551 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID)); |
| 3552 | } |
| 3553 | |
| 3554 | return 0; |
| 3555 | } |
| 3556 | case Intrinsic::dbg_region_end: { |
| 3557 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3558 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
| 3559 | if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) { |
| 3560 | unsigned LabelID = MMI->RecordRegionEnd(REI.getContext()); |
| 3561 | DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID)); |
| 3562 | } |
| 3563 | |
| 3564 | return 0; |
| 3565 | } |
| 3566 | case Intrinsic::dbg_func_start: { |
| 3567 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3568 | if (!MMI) return 0; |
| 3569 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
| 3570 | Value *SP = FSI.getSubprogram(); |
| 3571 | if (SP && MMI->Verify(SP)) { |
| 3572 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is |
| 3573 | // what (most?) gdb expects. |
| 3574 | DebugInfoDesc *DD = MMI->getDescFor(SP); |
| 3575 | assert(DD && "Not a debug information descriptor"); |
| 3576 | SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD); |
| 3577 | const CompileUnitDesc *CompileUnit = Subprogram->getFile(); |
| 3578 | unsigned SrcFile = MMI->RecordSource(CompileUnit); |
| 3579 | // Record the source line but does create a label. It will be emitted |
| 3580 | // at asm emission time. |
| 3581 | MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile); |
| 3582 | } |
| 3583 | |
| 3584 | return 0; |
| 3585 | } |
| 3586 | case Intrinsic::dbg_declare: { |
| 3587 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3588 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 3589 | Value *Variable = DI.getVariable(); |
| 3590 | if (MMI && Variable && MMI->Verify(Variable)) |
| 3591 | DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(), |
| 3592 | getValue(DI.getAddress()), getValue(Variable))); |
| 3593 | return 0; |
| 3594 | } |
| 3595 | |
| 3596 | case Intrinsic::eh_exception: { |
| 3597 | if (!CurMBB->isLandingPad()) { |
| 3598 | // FIXME: Mark exception register as live in. Hack for PR1508. |
| 3599 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 3600 | if (Reg) CurMBB->addLiveIn(Reg); |
| 3601 | } |
| 3602 | // Insert the EXCEPTIONADDR instruction. |
| 3603 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 3604 | SDValue Ops[1]; |
| 3605 | Ops[0] = DAG.getRoot(); |
| 3606 | SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1); |
| 3607 | setValue(&I, Op); |
| 3608 | DAG.setRoot(Op.getValue(1)); |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | case Intrinsic::eh_selector_i32: |
| 3613 | case Intrinsic::eh_selector_i64: { |
| 3614 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3615 | MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ? |
| 3616 | MVT::i32 : MVT::i64); |
| 3617 | |
| 3618 | if (MMI) { |
| 3619 | if (CurMBB->isLandingPad()) |
| 3620 | AddCatchInfo(I, MMI, CurMBB); |
| 3621 | else { |
| 3622 | #ifndef NDEBUG |
| 3623 | FuncInfo.CatchInfoLost.insert(&I); |
| 3624 | #endif |
| 3625 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 3626 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 3627 | if (Reg) CurMBB->addLiveIn(Reg); |
| 3628 | } |
| 3629 | |
| 3630 | // Insert the EHSELECTION instruction. |
| 3631 | SDVTList VTs = DAG.getVTList(VT, MVT::Other); |
| 3632 | SDValue Ops[2]; |
| 3633 | Ops[0] = getValue(I.getOperand(1)); |
| 3634 | Ops[1] = getRoot(); |
| 3635 | SDValue Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2); |
| 3636 | setValue(&I, Op); |
| 3637 | DAG.setRoot(Op.getValue(1)); |
| 3638 | } else { |
| 3639 | setValue(&I, DAG.getConstant(0, VT)); |
| 3640 | } |
| 3641 | |
| 3642 | return 0; |
| 3643 | } |
| 3644 | |
| 3645 | case Intrinsic::eh_typeid_for_i32: |
| 3646 | case Intrinsic::eh_typeid_for_i64: { |
| 3647 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3648 | MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ? |
| 3649 | MVT::i32 : MVT::i64); |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3650 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3651 | if (MMI) { |
| 3652 | // Find the type id for the given typeinfo. |
| 3653 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
| 3654 | |
| 3655 | unsigned TypeID = MMI->getTypeIDFor(GV); |
| 3656 | setValue(&I, DAG.getConstant(TypeID, VT)); |
| 3657 | } else { |
| 3658 | // Return something different to eh_selector. |
| 3659 | setValue(&I, DAG.getConstant(1, VT)); |
| 3660 | } |
| 3661 | |
| 3662 | return 0; |
| 3663 | } |
| 3664 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3665 | case Intrinsic::eh_return_i32: |
| 3666 | case Intrinsic::eh_return_i64: |
| 3667 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3668 | MMI->setCallsEHReturn(true); |
| 3669 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, |
| 3670 | MVT::Other, |
| 3671 | getControlRoot(), |
| 3672 | getValue(I.getOperand(1)), |
| 3673 | getValue(I.getOperand(2)))); |
| 3674 | } else { |
| 3675 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 3676 | } |
| 3677 | |
| 3678 | return 0; |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3679 | case Intrinsic::eh_unwind_init: |
| 3680 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 3681 | MMI->setCallsUnwindInit(true); |
| 3682 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3683 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3684 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3685 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3686 | case Intrinsic::eh_dwarf_cfa: { |
| 3687 | MVT VT = getValue(I.getOperand(1)).getValueType(); |
| 3688 | SDValue CfaArg; |
| 3689 | if (VT.bitsGT(TLI.getPointerTy())) |
| 3690 | CfaArg = DAG.getNode(ISD::TRUNCATE, |
| 3691 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
| 3692 | else |
| 3693 | CfaArg = DAG.getNode(ISD::SIGN_EXTEND, |
| 3694 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3695 | |
Anton Korobeynikov | a0e8a1e | 2008-09-08 21:13:56 +0000 | [diff] [blame] | 3696 | SDValue Offset = DAG.getNode(ISD::ADD, |
| 3697 | TLI.getPointerTy(), |
| 3698 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, |
| 3699 | TLI.getPointerTy()), |
| 3700 | CfaArg); |
| 3701 | setValue(&I, DAG.getNode(ISD::ADD, |
| 3702 | TLI.getPointerTy(), |
| 3703 | DAG.getNode(ISD::FRAMEADDR, |
| 3704 | TLI.getPointerTy(), |
| 3705 | DAG.getConstant(0, |
| 3706 | TLI.getPointerTy())), |
| 3707 | Offset)); |
| 3708 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | case Intrinsic::sqrt: |
| 3712 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 3713 | getValue(I.getOperand(1)).getValueType(), |
| 3714 | getValue(I.getOperand(1)))); |
| 3715 | return 0; |
| 3716 | case Intrinsic::powi: |
| 3717 | setValue(&I, DAG.getNode(ISD::FPOWI, |
| 3718 | getValue(I.getOperand(1)).getValueType(), |
| 3719 | getValue(I.getOperand(1)), |
| 3720 | getValue(I.getOperand(2)))); |
| 3721 | return 0; |
| 3722 | case Intrinsic::sin: |
| 3723 | setValue(&I, DAG.getNode(ISD::FSIN, |
| 3724 | getValue(I.getOperand(1)).getValueType(), |
| 3725 | getValue(I.getOperand(1)))); |
| 3726 | return 0; |
| 3727 | case Intrinsic::cos: |
| 3728 | setValue(&I, DAG.getNode(ISD::FCOS, |
| 3729 | getValue(I.getOperand(1)).getValueType(), |
| 3730 | getValue(I.getOperand(1)))); |
| 3731 | return 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3732 | case Intrinsic::log: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3733 | visitLog(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3734 | return 0; |
| 3735 | case Intrinsic::log2: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3736 | visitLog2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3737 | return 0; |
| 3738 | case Intrinsic::log10: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3739 | visitLog10(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3740 | return 0; |
| 3741 | case Intrinsic::exp: |
Dale Johannesen | 59e577f | 2008-09-05 18:38:42 +0000 | [diff] [blame] | 3742 | visitExp(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3743 | return 0; |
| 3744 | case Intrinsic::exp2: |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 3745 | visitExp2(I); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 3746 | return 0; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3747 | case Intrinsic::pow: |
Bill Wendling | aeb5c7b | 2008-09-10 00:20:20 +0000 | [diff] [blame] | 3748 | visitPow(I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 3749 | return 0; |
| 3750 | case Intrinsic::pcmarker: { |
| 3751 | SDValue Tmp = getValue(I.getOperand(1)); |
| 3752 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 3753 | return 0; |
| 3754 | } |
| 3755 | case Intrinsic::readcyclecounter: { |
| 3756 | SDValue Op = getRoot(); |
| 3757 | SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, |
| 3758 | DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2, |
| 3759 | &Op, 1); |
| 3760 | setValue(&I, Tmp); |
| 3761 | DAG.setRoot(Tmp.getValue(1)); |
| 3762 | return 0; |
| 3763 | } |
| 3764 | case Intrinsic::part_select: { |
| 3765 | // Currently not implemented: just abort |
| 3766 | assert(0 && "part_select intrinsic not implemented"); |
| 3767 | abort(); |
| 3768 | } |
| 3769 | case Intrinsic::part_set: { |
| 3770 | // Currently not implemented: just abort |
| 3771 | assert(0 && "part_set intrinsic not implemented"); |
| 3772 | abort(); |
| 3773 | } |
| 3774 | case Intrinsic::bswap: |
| 3775 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 3776 | getValue(I.getOperand(1)).getValueType(), |
| 3777 | getValue(I.getOperand(1)))); |
| 3778 | return 0; |
| 3779 | case Intrinsic::cttz: { |
| 3780 | SDValue Arg = getValue(I.getOperand(1)); |
| 3781 | MVT Ty = Arg.getValueType(); |
| 3782 | SDValue result = DAG.getNode(ISD::CTTZ, Ty, Arg); |
| 3783 | setValue(&I, result); |
| 3784 | return 0; |
| 3785 | } |
| 3786 | case Intrinsic::ctlz: { |
| 3787 | SDValue Arg = getValue(I.getOperand(1)); |
| 3788 | MVT Ty = Arg.getValueType(); |
| 3789 | SDValue result = DAG.getNode(ISD::CTLZ, Ty, Arg); |
| 3790 | setValue(&I, result); |
| 3791 | return 0; |
| 3792 | } |
| 3793 | case Intrinsic::ctpop: { |
| 3794 | SDValue Arg = getValue(I.getOperand(1)); |
| 3795 | MVT Ty = Arg.getValueType(); |
| 3796 | SDValue result = DAG.getNode(ISD::CTPOP, Ty, Arg); |
| 3797 | setValue(&I, result); |
| 3798 | return 0; |
| 3799 | } |
| 3800 | case Intrinsic::stacksave: { |
| 3801 | SDValue Op = getRoot(); |
| 3802 | SDValue Tmp = DAG.getNode(ISD::STACKSAVE, |
| 3803 | DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1); |
| 3804 | setValue(&I, Tmp); |
| 3805 | DAG.setRoot(Tmp.getValue(1)); |
| 3806 | return 0; |
| 3807 | } |
| 3808 | case Intrinsic::stackrestore: { |
| 3809 | SDValue Tmp = getValue(I.getOperand(1)); |
| 3810 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp)); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | case Intrinsic::var_annotation: |
| 3814 | // Discard annotate attributes |
| 3815 | return 0; |
| 3816 | |
| 3817 | case Intrinsic::init_trampoline: { |
| 3818 | const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts()); |
| 3819 | |
| 3820 | SDValue Ops[6]; |
| 3821 | Ops[0] = getRoot(); |
| 3822 | Ops[1] = getValue(I.getOperand(1)); |
| 3823 | Ops[2] = getValue(I.getOperand(2)); |
| 3824 | Ops[3] = getValue(I.getOperand(3)); |
| 3825 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 3826 | Ops[5] = DAG.getSrcValue(F); |
| 3827 | |
| 3828 | SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, |
| 3829 | DAG.getNodeValueTypes(TLI.getPointerTy(), |
| 3830 | MVT::Other), 2, |
| 3831 | Ops, 6); |
| 3832 | |
| 3833 | setValue(&I, Tmp); |
| 3834 | DAG.setRoot(Tmp.getValue(1)); |
| 3835 | return 0; |
| 3836 | } |
| 3837 | |
| 3838 | case Intrinsic::gcroot: |
| 3839 | if (GFI) { |
| 3840 | Value *Alloca = I.getOperand(1); |
| 3841 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
| 3842 | |
| 3843 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); |
| 3844 | GFI->addStackRoot(FI->getIndex(), TypeMap); |
| 3845 | } |
| 3846 | return 0; |
| 3847 | |
| 3848 | case Intrinsic::gcread: |
| 3849 | case Intrinsic::gcwrite: |
| 3850 | assert(0 && "GC failed to lower gcread/gcwrite intrinsics!"); |
| 3851 | return 0; |
| 3852 | |
| 3853 | case Intrinsic::flt_rounds: { |
| 3854 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32)); |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
| 3858 | case Intrinsic::trap: { |
| 3859 | DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot())); |
| 3860 | return 0; |
| 3861 | } |
| 3862 | case Intrinsic::prefetch: { |
| 3863 | SDValue Ops[4]; |
| 3864 | Ops[0] = getRoot(); |
| 3865 | Ops[1] = getValue(I.getOperand(1)); |
| 3866 | Ops[2] = getValue(I.getOperand(2)); |
| 3867 | Ops[3] = getValue(I.getOperand(3)); |
| 3868 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4)); |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
| 3872 | case Intrinsic::memory_barrier: { |
| 3873 | SDValue Ops[6]; |
| 3874 | Ops[0] = getRoot(); |
| 3875 | for (int x = 1; x < 6; ++x) |
| 3876 | Ops[x] = getValue(I.getOperand(x)); |
| 3877 | |
| 3878 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6)); |
| 3879 | return 0; |
| 3880 | } |
| 3881 | case Intrinsic::atomic_cmp_swap: { |
| 3882 | SDValue Root = getRoot(); |
| 3883 | SDValue L; |
| 3884 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3885 | case MVT::i8: |
| 3886 | L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_8, Root, |
| 3887 | getValue(I.getOperand(1)), |
| 3888 | getValue(I.getOperand(2)), |
| 3889 | getValue(I.getOperand(3)), |
| 3890 | I.getOperand(1)); |
| 3891 | break; |
| 3892 | case MVT::i16: |
| 3893 | L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_16, Root, |
| 3894 | getValue(I.getOperand(1)), |
| 3895 | getValue(I.getOperand(2)), |
| 3896 | getValue(I.getOperand(3)), |
| 3897 | I.getOperand(1)); |
| 3898 | break; |
| 3899 | case MVT::i32: |
| 3900 | L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_32, Root, |
| 3901 | getValue(I.getOperand(1)), |
| 3902 | getValue(I.getOperand(2)), |
| 3903 | getValue(I.getOperand(3)), |
| 3904 | I.getOperand(1)); |
| 3905 | break; |
| 3906 | case MVT::i64: |
| 3907 | L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_64, Root, |
| 3908 | getValue(I.getOperand(1)), |
| 3909 | getValue(I.getOperand(2)), |
| 3910 | getValue(I.getOperand(3)), |
| 3911 | I.getOperand(1)); |
| 3912 | break; |
| 3913 | default: |
| 3914 | assert(0 && "Invalid atomic type"); |
| 3915 | abort(); |
| 3916 | } |
| 3917 | setValue(&I, L); |
| 3918 | DAG.setRoot(L.getValue(1)); |
| 3919 | return 0; |
| 3920 | } |
| 3921 | case Intrinsic::atomic_load_add: |
| 3922 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3923 | case MVT::i8: |
| 3924 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_8); |
| 3925 | case MVT::i16: |
| 3926 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_16); |
| 3927 | case MVT::i32: |
| 3928 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_32); |
| 3929 | case MVT::i64: |
| 3930 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_64); |
| 3931 | default: |
| 3932 | assert(0 && "Invalid atomic type"); |
| 3933 | abort(); |
| 3934 | } |
| 3935 | case Intrinsic::atomic_load_sub: |
| 3936 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3937 | case MVT::i8: |
| 3938 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_8); |
| 3939 | case MVT::i16: |
| 3940 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_16); |
| 3941 | case MVT::i32: |
| 3942 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_32); |
| 3943 | case MVT::i64: |
| 3944 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_64); |
| 3945 | default: |
| 3946 | assert(0 && "Invalid atomic type"); |
| 3947 | abort(); |
| 3948 | } |
| 3949 | case Intrinsic::atomic_load_or: |
| 3950 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3951 | case MVT::i8: |
| 3952 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_8); |
| 3953 | case MVT::i16: |
| 3954 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_16); |
| 3955 | case MVT::i32: |
| 3956 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_32); |
| 3957 | case MVT::i64: |
| 3958 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_64); |
| 3959 | default: |
| 3960 | assert(0 && "Invalid atomic type"); |
| 3961 | abort(); |
| 3962 | } |
| 3963 | case Intrinsic::atomic_load_xor: |
| 3964 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3965 | case MVT::i8: |
| 3966 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_8); |
| 3967 | case MVT::i16: |
| 3968 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_16); |
| 3969 | case MVT::i32: |
| 3970 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_32); |
| 3971 | case MVT::i64: |
| 3972 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_64); |
| 3973 | default: |
| 3974 | assert(0 && "Invalid atomic type"); |
| 3975 | abort(); |
| 3976 | } |
| 3977 | case Intrinsic::atomic_load_and: |
| 3978 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3979 | case MVT::i8: |
| 3980 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_8); |
| 3981 | case MVT::i16: |
| 3982 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_16); |
| 3983 | case MVT::i32: |
| 3984 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_32); |
| 3985 | case MVT::i64: |
| 3986 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_64); |
| 3987 | default: |
| 3988 | assert(0 && "Invalid atomic type"); |
| 3989 | abort(); |
| 3990 | } |
| 3991 | case Intrinsic::atomic_load_nand: |
| 3992 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 3993 | case MVT::i8: |
| 3994 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_8); |
| 3995 | case MVT::i16: |
| 3996 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_16); |
| 3997 | case MVT::i32: |
| 3998 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_32); |
| 3999 | case MVT::i64: |
| 4000 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_64); |
| 4001 | default: |
| 4002 | assert(0 && "Invalid atomic type"); |
| 4003 | abort(); |
| 4004 | } |
| 4005 | case Intrinsic::atomic_load_max: |
| 4006 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 4007 | case MVT::i8: |
| 4008 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_8); |
| 4009 | case MVT::i16: |
| 4010 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_16); |
| 4011 | case MVT::i32: |
| 4012 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_32); |
| 4013 | case MVT::i64: |
| 4014 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_64); |
| 4015 | default: |
| 4016 | assert(0 && "Invalid atomic type"); |
| 4017 | abort(); |
| 4018 | } |
| 4019 | case Intrinsic::atomic_load_min: |
| 4020 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 4021 | case MVT::i8: |
| 4022 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_8); |
| 4023 | case MVT::i16: |
| 4024 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_16); |
| 4025 | case MVT::i32: |
| 4026 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_32); |
| 4027 | case MVT::i64: |
| 4028 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_64); |
| 4029 | default: |
| 4030 | assert(0 && "Invalid atomic type"); |
| 4031 | abort(); |
| 4032 | } |
| 4033 | case Intrinsic::atomic_load_umin: |
| 4034 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 4035 | case MVT::i8: |
| 4036 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_8); |
| 4037 | case MVT::i16: |
| 4038 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_16); |
| 4039 | case MVT::i32: |
| 4040 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_32); |
| 4041 | case MVT::i64: |
| 4042 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_64); |
| 4043 | default: |
| 4044 | assert(0 && "Invalid atomic type"); |
| 4045 | abort(); |
| 4046 | } |
| 4047 | case Intrinsic::atomic_load_umax: |
| 4048 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 4049 | case MVT::i8: |
| 4050 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_8); |
| 4051 | case MVT::i16: |
| 4052 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_16); |
| 4053 | case MVT::i32: |
| 4054 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_32); |
| 4055 | case MVT::i64: |
| 4056 | return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_64); |
| 4057 | default: |
| 4058 | assert(0 && "Invalid atomic type"); |
| 4059 | abort(); |
| 4060 | } |
| 4061 | case Intrinsic::atomic_swap: |
| 4062 | switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) { |
| 4063 | case MVT::i8: |
| 4064 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_8); |
| 4065 | case MVT::i16: |
| 4066 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_16); |
| 4067 | case MVT::i32: |
| 4068 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_32); |
| 4069 | case MVT::i64: |
| 4070 | return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_64); |
| 4071 | default: |
| 4072 | assert(0 && "Invalid atomic type"); |
| 4073 | abort(); |
| 4074 | } |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | |
| 4079 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee, |
| 4080 | bool IsTailCall, |
| 4081 | MachineBasicBlock *LandingPad) { |
| 4082 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 4083 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 4084 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 4085 | unsigned BeginLabel = 0, EndLabel = 0; |
| 4086 | |
| 4087 | TargetLowering::ArgListTy Args; |
| 4088 | TargetLowering::ArgListEntry Entry; |
| 4089 | Args.reserve(CS.arg_size()); |
| 4090 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 4091 | i != e; ++i) { |
| 4092 | SDValue ArgNode = getValue(*i); |
| 4093 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
| 4094 | |
| 4095 | unsigned attrInd = i - CS.arg_begin() + 1; |
| 4096 | Entry.isSExt = CS.paramHasAttr(attrInd, ParamAttr::SExt); |
| 4097 | Entry.isZExt = CS.paramHasAttr(attrInd, ParamAttr::ZExt); |
| 4098 | Entry.isInReg = CS.paramHasAttr(attrInd, ParamAttr::InReg); |
| 4099 | Entry.isSRet = CS.paramHasAttr(attrInd, ParamAttr::StructRet); |
| 4100 | Entry.isNest = CS.paramHasAttr(attrInd, ParamAttr::Nest); |
| 4101 | Entry.isByVal = CS.paramHasAttr(attrInd, ParamAttr::ByVal); |
| 4102 | Entry.Alignment = CS.getParamAlignment(attrInd); |
| 4103 | Args.push_back(Entry); |
| 4104 | } |
| 4105 | |
| 4106 | if (LandingPad && MMI) { |
| 4107 | // Insert a label before the invoke call to mark the try range. This can be |
| 4108 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 4109 | BeginLabel = MMI->NextLabelID(); |
| 4110 | // Both PendingLoads and PendingExports must be flushed here; |
| 4111 | // this call might not return. |
| 4112 | (void)getRoot(); |
| 4113 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getControlRoot(), BeginLabel)); |
| 4114 | } |
| 4115 | |
| 4116 | std::pair<SDValue,SDValue> Result = |
| 4117 | TLI.LowerCallTo(getRoot(), CS.getType(), |
| 4118 | CS.paramHasAttr(0, ParamAttr::SExt), |
| 4119 | CS.paramHasAttr(0, ParamAttr::ZExt), |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 4120 | FTy->isVarArg(), CS.getCallingConv(), |
| 4121 | IsTailCall && PerformTailCallOpt, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4122 | Callee, Args, DAG); |
| 4123 | if (CS.getType() != Type::VoidTy) |
| 4124 | setValue(CS.getInstruction(), Result.first); |
| 4125 | DAG.setRoot(Result.second); |
| 4126 | |
| 4127 | if (LandingPad && MMI) { |
| 4128 | // Insert a label at the end of the invoke call to mark the try range. This |
| 4129 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 4130 | EndLabel = MMI->NextLabelID(); |
| 4131 | DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getRoot(), EndLabel)); |
| 4132 | |
| 4133 | // Inform MachineModuleInfo of range. |
| 4134 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | |
| 4139 | void SelectionDAGLowering::visitCall(CallInst &I) { |
| 4140 | const char *RenameFn = 0; |
| 4141 | if (Function *F = I.getCalledFunction()) { |
| 4142 | if (F->isDeclaration()) { |
| 4143 | if (unsigned IID = F->getIntrinsicID()) { |
| 4144 | RenameFn = visitIntrinsicCall(I, IID); |
| 4145 | if (!RenameFn) |
| 4146 | return; |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | // Check for well-known libc/libm calls. If the function is internal, it |
| 4151 | // can't be a library call. |
| 4152 | unsigned NameLen = F->getNameLen(); |
| 4153 | if (!F->hasInternalLinkage() && NameLen) { |
| 4154 | const char *NameStr = F->getNameStart(); |
| 4155 | if (NameStr[0] == 'c' && |
| 4156 | ((NameLen == 8 && !strcmp(NameStr, "copysign")) || |
| 4157 | (NameLen == 9 && !strcmp(NameStr, "copysignf")))) { |
| 4158 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 4159 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4160 | I.getType() == I.getOperand(1)->getType() && |
| 4161 | I.getType() == I.getOperand(2)->getType()) { |
| 4162 | SDValue LHS = getValue(I.getOperand(1)); |
| 4163 | SDValue RHS = getValue(I.getOperand(2)); |
| 4164 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(), |
| 4165 | LHS, RHS)); |
| 4166 | return; |
| 4167 | } |
| 4168 | } else if (NameStr[0] == 'f' && |
| 4169 | ((NameLen == 4 && !strcmp(NameStr, "fabs")) || |
| 4170 | (NameLen == 5 && !strcmp(NameStr, "fabsf")) || |
| 4171 | (NameLen == 5 && !strcmp(NameStr, "fabsl")))) { |
| 4172 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4173 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4174 | I.getType() == I.getOperand(1)->getType()) { |
| 4175 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4176 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 4177 | return; |
| 4178 | } |
| 4179 | } else if (NameStr[0] == 's' && |
| 4180 | ((NameLen == 3 && !strcmp(NameStr, "sin")) || |
| 4181 | (NameLen == 4 && !strcmp(NameStr, "sinf")) || |
| 4182 | (NameLen == 4 && !strcmp(NameStr, "sinl")))) { |
| 4183 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4184 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4185 | I.getType() == I.getOperand(1)->getType()) { |
| 4186 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4187 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 4188 | return; |
| 4189 | } |
| 4190 | } else if (NameStr[0] == 'c' && |
| 4191 | ((NameLen == 3 && !strcmp(NameStr, "cos")) || |
| 4192 | (NameLen == 4 && !strcmp(NameStr, "cosf")) || |
| 4193 | (NameLen == 4 && !strcmp(NameStr, "cosl")))) { |
| 4194 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 4195 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 4196 | I.getType() == I.getOperand(1)->getType()) { |
| 4197 | SDValue Tmp = getValue(I.getOperand(1)); |
| 4198 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 4199 | return; |
| 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 4204 | visitInlineAsm(&I); |
| 4205 | return; |
| 4206 | } |
| 4207 | |
| 4208 | SDValue Callee; |
| 4209 | if (!RenameFn) |
| 4210 | Callee = getValue(I.getOperand(0)); |
| 4211 | else |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4212 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4213 | |
| 4214 | LowerCallTo(&I, Callee, I.isTailCall()); |
| 4215 | } |
| 4216 | |
| 4217 | |
| 4218 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 4219 | /// this value and returns the result as a ValueVT value. This uses |
| 4220 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4221 | /// If the Flag pointer is NULL, no flag is used. |
| 4222 | SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
| 4223 | SDValue &Chain, |
| 4224 | SDValue *Flag) const { |
| 4225 | // Assemble the legal parts into the final values. |
| 4226 | SmallVector<SDValue, 4> Values(ValueVTs.size()); |
| 4227 | SmallVector<SDValue, 8> Parts; |
| 4228 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4229 | // Copy the legal parts from the registers. |
| 4230 | MVT ValueVT = ValueVTs[Value]; |
| 4231 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 4232 | MVT RegisterVT = RegVTs[Value]; |
| 4233 | |
| 4234 | Parts.resize(NumRegs); |
| 4235 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4236 | SDValue P; |
| 4237 | if (Flag == 0) |
| 4238 | P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT); |
| 4239 | else { |
| 4240 | P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag); |
| 4241 | *Flag = P.getValue(2); |
| 4242 | } |
| 4243 | Chain = P.getValue(1); |
| 4244 | |
| 4245 | // If the source register was virtual and if we know something about it, |
| 4246 | // add an assert node. |
| 4247 | if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) && |
| 4248 | RegisterVT.isInteger() && !RegisterVT.isVector()) { |
| 4249 | unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister; |
| 4250 | FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo(); |
| 4251 | if (FLI.LiveOutRegInfo.size() > SlotNo) { |
| 4252 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo]; |
| 4253 | |
| 4254 | unsigned RegSize = RegisterVT.getSizeInBits(); |
| 4255 | unsigned NumSignBits = LOI.NumSignBits; |
| 4256 | unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes(); |
| 4257 | |
| 4258 | // FIXME: We capture more information than the dag can represent. For |
| 4259 | // now, just use the tightest assertzext/assertsext possible. |
| 4260 | bool isSExt = true; |
| 4261 | MVT FromVT(MVT::Other); |
| 4262 | if (NumSignBits == RegSize) |
| 4263 | isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1 |
| 4264 | else if (NumZeroBits >= RegSize-1) |
| 4265 | isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1 |
| 4266 | else if (NumSignBits > RegSize-8) |
| 4267 | isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8 |
| 4268 | else if (NumZeroBits >= RegSize-9) |
| 4269 | isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8 |
| 4270 | else if (NumSignBits > RegSize-16) |
| 4271 | isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16 |
| 4272 | else if (NumZeroBits >= RegSize-17) |
| 4273 | isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16 |
| 4274 | else if (NumSignBits > RegSize-32) |
| 4275 | isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32 |
| 4276 | else if (NumZeroBits >= RegSize-33) |
| 4277 | isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32 |
| 4278 | |
| 4279 | if (FromVT != MVT::Other) { |
| 4280 | P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, |
| 4281 | RegisterVT, P, DAG.getValueType(FromVT)); |
| 4282 | |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | Parts[i] = P; |
| 4288 | } |
| 4289 | |
| 4290 | Values[Value] = getCopyFromParts(DAG, Parts.begin(), NumRegs, RegisterVT, |
| 4291 | ValueVT); |
| 4292 | Part += NumRegs; |
| 4293 | Parts.clear(); |
| 4294 | } |
| 4295 | |
| 4296 | return DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 4297 | &Values[0], ValueVTs.size()); |
| 4298 | } |
| 4299 | |
| 4300 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 4301 | /// specified value into the registers specified by this object. This uses |
| 4302 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 4303 | /// If the Flag pointer is NULL, no flag is used. |
| 4304 | void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, |
| 4305 | SDValue &Chain, SDValue *Flag) const { |
| 4306 | // Get the list of the values's legal parts. |
| 4307 | unsigned NumRegs = Regs.size(); |
| 4308 | SmallVector<SDValue, 8> Parts(NumRegs); |
| 4309 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4310 | MVT ValueVT = ValueVTs[Value]; |
| 4311 | unsigned NumParts = TLI->getNumRegisters(ValueVT); |
| 4312 | MVT RegisterVT = RegVTs[Value]; |
| 4313 | |
| 4314 | getCopyToParts(DAG, Val.getValue(Val.getResNo() + Value), |
| 4315 | &Parts[Part], NumParts, RegisterVT); |
| 4316 | Part += NumParts; |
| 4317 | } |
| 4318 | |
| 4319 | // Copy the parts into the registers. |
| 4320 | SmallVector<SDValue, 8> Chains(NumRegs); |
| 4321 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4322 | SDValue Part; |
| 4323 | if (Flag == 0) |
| 4324 | Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]); |
| 4325 | else { |
| 4326 | Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag); |
| 4327 | *Flag = Part.getValue(1); |
| 4328 | } |
| 4329 | Chains[i] = Part.getValue(0); |
| 4330 | } |
| 4331 | |
| 4332 | if (NumRegs == 1 || Flag) |
| 4333 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 4334 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 4335 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 4336 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 4337 | // user as well as a successor (the TF operands are flagged to the user). |
| 4338 | // c1, f1 = CopyToReg |
| 4339 | // c2, f2 = CopyToReg |
| 4340 | // c3 = TokenFactor c1, c2 |
| 4341 | // ... |
| 4342 | // = op c3, ..., f2 |
| 4343 | Chain = Chains[NumRegs-1]; |
| 4344 | else |
| 4345 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs); |
| 4346 | } |
| 4347 | |
| 4348 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 4349 | /// operand list. This adds the code marker and includes the number of |
| 4350 | /// values added into it. |
| 4351 | void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
| 4352 | std::vector<SDValue> &Ops) const { |
| 4353 | MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
| 4354 | Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy)); |
| 4355 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 4356 | unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]); |
| 4357 | MVT RegisterVT = RegVTs[Value]; |
| 4358 | for (unsigned i = 0; i != NumRegs; ++i) |
| 4359 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 4364 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 4365 | /// register class for the register. Otherwise, return null. |
| 4366 | static const TargetRegisterClass * |
| 4367 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 4368 | const TargetLowering &TLI, |
| 4369 | const TargetRegisterInfo *TRI) { |
| 4370 | MVT FoundVT = MVT::Other; |
| 4371 | const TargetRegisterClass *FoundRC = 0; |
| 4372 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 4373 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
| 4374 | MVT ThisVT = MVT::Other; |
| 4375 | |
| 4376 | const TargetRegisterClass *RC = *RCI; |
| 4377 | // If none of the the value types for this register class are valid, we |
| 4378 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 4379 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 4380 | I != E; ++I) { |
| 4381 | if (TLI.isTypeLegal(*I)) { |
| 4382 | // If we have already found this register in a different register class, |
| 4383 | // choose the one with the largest VT specified. For example, on |
| 4384 | // PowerPC, we favor f64 register classes over f32. |
| 4385 | if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) { |
| 4386 | ThisVT = *I; |
| 4387 | break; |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | if (ThisVT == MVT::Other) continue; |
| 4393 | |
| 4394 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 4395 | // frame pointer in functions that need it (due to them not being taken |
| 4396 | // out of allocation, because a variable sized allocation hasn't been seen |
| 4397 | // yet). This is a slight code pessimization, but should still work. |
| 4398 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 4399 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 4400 | if (*I == Reg) { |
| 4401 | // We found a matching register class. Keep looking at others in case |
| 4402 | // we find one with larger registers that this physreg is also in. |
| 4403 | FoundRC = RC; |
| 4404 | FoundVT = ThisVT; |
| 4405 | break; |
| 4406 | } |
| 4407 | } |
| 4408 | return FoundRC; |
| 4409 | } |
| 4410 | |
| 4411 | |
| 4412 | namespace llvm { |
| 4413 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 4414 | /// lowering. |
Daniel Dunbar | c0c3b9a | 2008-09-10 04:16:29 +0000 | [diff] [blame] | 4415 | struct VISIBILITY_HIDDEN SDISelAsmOperandInfo : |
| 4416 | public TargetLowering::AsmOperandInfo { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4417 | /// CallOperand - If this is the result output operand or a clobber |
| 4418 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 4419 | /// This gets modified as the asm is processed. |
| 4420 | SDValue CallOperand; |
| 4421 | |
| 4422 | /// AssignedRegs - If this is a register or register class operand, this |
| 4423 | /// contains the set of register corresponding to the operand. |
| 4424 | RegsForValue AssignedRegs; |
| 4425 | |
| 4426 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
| 4427 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
| 4428 | } |
| 4429 | |
| 4430 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 4431 | /// busy in OutputRegs/InputRegs. |
| 4432 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
| 4433 | std::set<unsigned> &OutputRegs, |
| 4434 | std::set<unsigned> &InputRegs, |
| 4435 | const TargetRegisterInfo &TRI) const { |
| 4436 | if (isOutReg) { |
| 4437 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4438 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 4439 | } |
| 4440 | if (isInReg) { |
| 4441 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 4442 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | private: |
| 4447 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 4448 | /// specified set. |
| 4449 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
| 4450 | const TargetRegisterInfo &TRI) { |
| 4451 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 4452 | Regs.insert(Reg); |
| 4453 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 4454 | for (; *Aliases; ++Aliases) |
| 4455 | Regs.insert(*Aliases); |
| 4456 | } |
| 4457 | }; |
| 4458 | } // end llvm namespace. |
| 4459 | |
| 4460 | |
| 4461 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 4462 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 4463 | /// register allocator handle the assignment process. However, if the asm uses |
| 4464 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 4465 | /// allocation. This produces generally horrible, but correct, code. |
| 4466 | /// |
| 4467 | /// OpInfo describes the operand. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4468 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 4469 | /// |
| 4470 | void SelectionDAGLowering:: |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4471 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4472 | std::set<unsigned> &OutputRegs, |
| 4473 | std::set<unsigned> &InputRegs) { |
| 4474 | // Compute whether this value requires an input register, an output register, |
| 4475 | // or both. |
| 4476 | bool isOutReg = false; |
| 4477 | bool isInReg = false; |
| 4478 | switch (OpInfo.Type) { |
| 4479 | case InlineAsm::isOutput: |
| 4480 | isOutReg = true; |
| 4481 | |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4482 | // If there is an input constraint that matches this, we need to reserve |
| 4483 | // the input register so no other inputs allocate to it. |
| 4484 | isInReg = OpInfo.hasMatchingInput; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4485 | break; |
| 4486 | case InlineAsm::isInput: |
| 4487 | isInReg = true; |
| 4488 | isOutReg = false; |
| 4489 | break; |
| 4490 | case InlineAsm::isClobber: |
| 4491 | isOutReg = true; |
| 4492 | isInReg = true; |
| 4493 | break; |
| 4494 | } |
| 4495 | |
| 4496 | |
| 4497 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4498 | SmallVector<unsigned, 4> Regs; |
| 4499 | |
| 4500 | // If this is a constraint for a single physreg, or a constraint for a |
| 4501 | // register class, find it. |
| 4502 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 4503 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 4504 | OpInfo.ConstraintVT); |
| 4505 | |
| 4506 | unsigned NumRegs = 1; |
| 4507 | if (OpInfo.ConstraintVT != MVT::Other) |
| 4508 | NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT); |
| 4509 | MVT RegVT; |
| 4510 | MVT ValueVT = OpInfo.ConstraintVT; |
| 4511 | |
| 4512 | |
| 4513 | // If this is a constraint for a specific physical register, like {r17}, |
| 4514 | // assign it now. |
| 4515 | if (PhysReg.first) { |
| 4516 | if (OpInfo.ConstraintVT == MVT::Other) |
| 4517 | ValueVT = *PhysReg.second->vt_begin(); |
| 4518 | |
| 4519 | // Get the actual register value type. This is important, because the user |
| 4520 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 4521 | // remember that AX is actually i16 to get the right extension. |
| 4522 | RegVT = *PhysReg.second->vt_begin(); |
| 4523 | |
| 4524 | // This is a explicit reference to a physical register. |
| 4525 | Regs.push_back(PhysReg.first); |
| 4526 | |
| 4527 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 4528 | if (NumRegs != 1) { |
| 4529 | TargetRegisterClass::iterator I = PhysReg.second->begin(); |
| 4530 | for (; *I != PhysReg.first; ++I) |
| 4531 | assert(I != PhysReg.second->end() && "Didn't find reg!"); |
| 4532 | |
| 4533 | // Already added the first reg. |
| 4534 | --NumRegs; ++I; |
| 4535 | for (; NumRegs; --NumRegs, ++I) { |
| 4536 | assert(I != PhysReg.second->end() && "Ran out of registers to allocate!"); |
| 4537 | Regs.push_back(*I); |
| 4538 | } |
| 4539 | } |
| 4540 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 4541 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 4542 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 4543 | return; |
| 4544 | } |
| 4545 | |
| 4546 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 4547 | // for this reference. |
| 4548 | std::vector<unsigned> RegClassRegs; |
| 4549 | const TargetRegisterClass *RC = PhysReg.second; |
| 4550 | if (RC) { |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4551 | // If this is a tied register, our regalloc doesn't know how to maintain |
| 4552 | // the constraint. If it isn't, go ahead and create vreg |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4553 | // and let the regalloc do the right thing. |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4554 | if (!OpInfo.hasMatchingInput) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4555 | RegVT = *PhysReg.second->vt_begin(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4556 | if (OpInfo.ConstraintVT == MVT::Other) |
| 4557 | ValueVT = RegVT; |
| 4558 | |
| 4559 | // Create the appropriate number of virtual registers. |
| 4560 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 4561 | for (; NumRegs; --NumRegs) |
| 4562 | Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second)); |
| 4563 | |
| 4564 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
| 4565 | return; |
| 4566 | } |
| 4567 | |
| 4568 | // Otherwise, we can't allocate it. Let the code below figure out how to |
| 4569 | // maintain these constraints. |
| 4570 | RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end()); |
| 4571 | |
| 4572 | } else { |
| 4573 | // This is a reference to a register class that doesn't directly correspond |
| 4574 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 4575 | // registers from the class. |
| 4576 | RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 4577 | OpInfo.ConstraintVT); |
| 4578 | } |
| 4579 | |
| 4580 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 4581 | unsigned NumAllocated = 0; |
| 4582 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 4583 | unsigned Reg = RegClassRegs[i]; |
| 4584 | // See if this register is available. |
| 4585 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 4586 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 4587 | // Make sure we find consecutive registers. |
| 4588 | NumAllocated = 0; |
| 4589 | continue; |
| 4590 | } |
| 4591 | |
| 4592 | // Check to see if this register is allocatable (i.e. don't give out the |
| 4593 | // stack pointer). |
| 4594 | if (RC == 0) { |
| 4595 | RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
| 4596 | if (!RC) { // Couldn't allocate this register. |
| 4597 | // Reset NumAllocated to make sure we return consecutive registers. |
| 4598 | NumAllocated = 0; |
| 4599 | continue; |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | // Okay, this register is good, we can use it. |
| 4604 | ++NumAllocated; |
| 4605 | |
| 4606 | // If we allocated enough consecutive registers, succeed. |
| 4607 | if (NumAllocated == NumRegs) { |
| 4608 | unsigned RegStart = (i-NumAllocated)+1; |
| 4609 | unsigned RegEnd = i+1; |
| 4610 | // Mark all of the allocated registers used. |
| 4611 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 4612 | Regs.push_back(RegClassRegs[i]); |
| 4613 | |
| 4614 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
| 4615 | OpInfo.ConstraintVT); |
| 4616 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
| 4617 | return; |
| 4618 | } |
| 4619 | } |
| 4620 | |
| 4621 | // Otherwise, we couldn't allocate enough registers for this. |
| 4622 | } |
| 4623 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4624 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 4625 | /// processed uses a memory 'm' constraint. |
| 4626 | static bool |
| 4627 | hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, |
| 4628 | TargetLowering &TLI) { |
| 4629 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 4630 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 4631 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 4632 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 4633 | if (CType == TargetLowering::C_Memory) |
| 4634 | return true; |
| 4635 | } |
| 4636 | } |
| 4637 | |
| 4638 | return false; |
| 4639 | } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4640 | |
| 4641 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 4642 | /// |
| 4643 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 4644 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 4645 | |
| 4646 | /// ConstraintOperands - Information about all of the constraints. |
| 4647 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
| 4648 | |
| 4649 | SDValue Chain = getRoot(); |
| 4650 | SDValue Flag; |
| 4651 | |
| 4652 | std::set<unsigned> OutputRegs, InputRegs; |
| 4653 | |
| 4654 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 4655 | // ConstraintOperands list. |
| 4656 | std::vector<InlineAsm::ConstraintInfo> |
| 4657 | ConstraintInfos = IA->ParseConstraints(); |
| 4658 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4659 | bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4660 | |
| 4661 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 4662 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
| 4663 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 4664 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 4665 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 4666 | |
| 4667 | MVT OpVT = MVT::Other; |
| 4668 | |
| 4669 | // Compute the value type for each operand. |
| 4670 | switch (OpInfo.Type) { |
| 4671 | case InlineAsm::isOutput: |
| 4672 | // Indirect outputs just consume an argument. |
| 4673 | if (OpInfo.isIndirect) { |
| 4674 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 4675 | break; |
| 4676 | } |
| 4677 | // The return value of the call is this value. As such, there is no |
| 4678 | // corresponding argument. |
| 4679 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 4680 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 4681 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 4682 | } else { |
| 4683 | assert(ResNo == 0 && "Asm only has one result!"); |
| 4684 | OpVT = TLI.getValueType(CS.getType()); |
| 4685 | } |
| 4686 | ++ResNo; |
| 4687 | break; |
| 4688 | case InlineAsm::isInput: |
| 4689 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 4690 | break; |
| 4691 | case InlineAsm::isClobber: |
| 4692 | // Nothing to do. |
| 4693 | break; |
| 4694 | } |
| 4695 | |
| 4696 | // If this is an input or an indirect output, process the call argument. |
| 4697 | // BasicBlocks are labels, currently appearing only in asm's. |
| 4698 | if (OpInfo.CallOperandVal) { |
| 4699 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) |
| 4700 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
| 4701 | else { |
| 4702 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
| 4703 | const Type *OpTy = OpInfo.CallOperandVal->getType(); |
| 4704 | // If this is an indirect operand, the operand is a pointer to the |
| 4705 | // accessed type. |
| 4706 | if (OpInfo.isIndirect) |
| 4707 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
| 4708 | |
| 4709 | // If OpTy is not a single value, it may be a struct/union that we |
| 4710 | // can tile with integers. |
| 4711 | if (!OpTy->isSingleValueType() && OpTy->isSized()) { |
| 4712 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 4713 | switch (BitSize) { |
| 4714 | default: break; |
| 4715 | case 1: |
| 4716 | case 8: |
| 4717 | case 16: |
| 4718 | case 32: |
| 4719 | case 64: |
| 4720 | OpTy = IntegerType::get(BitSize); |
| 4721 | break; |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | OpVT = TLI.getValueType(OpTy, true); |
| 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | OpInfo.ConstraintVT = OpVT; |
| 4730 | |
| 4731 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4732 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4733 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4734 | // If this is a memory input, and if the operand is not indirect, do what we |
| 4735 | // need to to provide an address for the memory input. |
| 4736 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 4737 | !OpInfo.isIndirect) { |
| 4738 | assert(OpInfo.Type == InlineAsm::isInput && |
| 4739 | "Can only indirectify direct input operands!"); |
| 4740 | |
| 4741 | // Memory operands really want the address of the value. If we don't have |
| 4742 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 4743 | // it to a stack slot. |
| 4744 | |
| 4745 | // If the operand is a float, integer, or vector constant, spill to a |
| 4746 | // constant pool entry to get its address. |
| 4747 | Value *OpVal = OpInfo.CallOperandVal; |
| 4748 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 4749 | isa<ConstantVector>(OpVal)) { |
| 4750 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 4751 | TLI.getPointerTy()); |
| 4752 | } else { |
| 4753 | // Otherwise, create a stack slot and emit a store to it before the |
| 4754 | // asm. |
| 4755 | const Type *Ty = OpVal->getType(); |
| 4756 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
| 4757 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 4758 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4759 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 4760 | SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4761 | Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0); |
| 4762 | OpInfo.CallOperand = StackSlot; |
| 4763 | } |
| 4764 | |
| 4765 | // There is no longer a Value* corresponding to this operand. |
| 4766 | OpInfo.CallOperandVal = 0; |
| 4767 | // It is now an indirect operand. |
| 4768 | OpInfo.isIndirect = true; |
| 4769 | } |
| 4770 | |
| 4771 | // If this constraint is for a specific register, allocate it before |
| 4772 | // anything else. |
| 4773 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4774 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4775 | } |
| 4776 | ConstraintInfos.clear(); |
| 4777 | |
| 4778 | |
| 4779 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
| 4780 | // to registerclass operands. |
| 4781 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 4782 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 4783 | |
| 4784 | // C_Register operands have already been allocated, Other/Memory don't need |
| 4785 | // to be. |
| 4786 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame^] | 4787 | GetRegistersForValue(OpInfo, OutputRegs, InputRegs); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 4791 | std::vector<SDValue> AsmNodeOperands; |
| 4792 | AsmNodeOperands.push_back(SDValue()); // reserve space for input chain |
| 4793 | AsmNodeOperands.push_back( |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4794 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4795 | |
| 4796 | |
| 4797 | // Loop over all of the inputs, copying the operand values into the |
| 4798 | // appropriate registers and processing the output regs. |
| 4799 | RegsForValue RetValRegs; |
| 4800 | |
| 4801 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 4802 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
| 4803 | |
| 4804 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
| 4805 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
| 4806 | |
| 4807 | switch (OpInfo.Type) { |
| 4808 | case InlineAsm::isOutput: { |
| 4809 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 4810 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
| 4811 | // Memory output, or 'other' output (e.g. 'X' constraint). |
| 4812 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
| 4813 | |
| 4814 | // Add information to the INLINEASM node to know about this output. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4815 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 4816 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4817 | TLI.getPointerTy())); |
| 4818 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
| 4819 | break; |
| 4820 | } |
| 4821 | |
| 4822 | // Otherwise, this is a register or register class output. |
| 4823 | |
| 4824 | // Copy the output from the appropriate register. Find a register that |
| 4825 | // we can use. |
| 4826 | if (OpInfo.AssignedRegs.Regs.empty()) { |
| 4827 | cerr << "Couldn't allocate output reg for constraint '" |
| 4828 | << OpInfo.ConstraintCode << "'!\n"; |
| 4829 | exit(1); |
| 4830 | } |
| 4831 | |
| 4832 | // If this is an indirect operand, store through the pointer after the |
| 4833 | // asm. |
| 4834 | if (OpInfo.isIndirect) { |
| 4835 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
| 4836 | OpInfo.CallOperandVal)); |
| 4837 | } else { |
| 4838 | // This is the result value of the call. |
| 4839 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 4840 | // Concatenate this output onto the outputs list. |
| 4841 | RetValRegs.append(OpInfo.AssignedRegs); |
| 4842 | } |
| 4843 | |
| 4844 | // Add information to the INLINEASM node to know that this register is |
| 4845 | // set. |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 4846 | OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ? |
| 4847 | 6 /* EARLYCLOBBER REGDEF */ : |
| 4848 | 2 /* REGDEF */ , |
| 4849 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4850 | break; |
| 4851 | } |
| 4852 | case InlineAsm::isInput: { |
| 4853 | SDValue InOperandVal = OpInfo.CallOperand; |
| 4854 | |
| 4855 | if (isdigit(OpInfo.ConstraintCode[0])) { // Matching constraint? |
| 4856 | // If this is required to match an output register we have already set, |
| 4857 | // just use its register. |
| 4858 | unsigned OperandNo = atoi(OpInfo.ConstraintCode.c_str()); |
| 4859 | |
| 4860 | // Scan until we find the definition we already emitted of this operand. |
| 4861 | // When we find it, create a RegsForValue operand. |
| 4862 | unsigned CurOp = 2; // The first operand. |
| 4863 | for (; OperandNo; --OperandNo) { |
| 4864 | // Advance to the next operand. |
| 4865 | unsigned NumOps = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4866 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4867 | assert(((NumOps & 7) == 2 /*REGDEF*/ || |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 4868 | (NumOps & 7) == 6 /*EARLYCLOBBER REGDEF*/ || |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4869 | (NumOps & 7) == 4 /*MEM*/) && |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4870 | "Skipped past definitions?"); |
| 4871 | CurOp += (NumOps>>3)+1; |
| 4872 | } |
| 4873 | |
| 4874 | unsigned NumOps = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4875 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue(); |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 4876 | if ((NumOps & 7) == 2 /*REGDEF*/ |
| 4877 | || (NumOps & 7) == 6 /* EARLYCLOBBER REGDEF */) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4878 | // Add NumOps>>3 registers to MatchedRegs. |
| 4879 | RegsForValue MatchedRegs; |
| 4880 | MatchedRegs.TLI = &TLI; |
| 4881 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
| 4882 | MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType()); |
| 4883 | for (unsigned i = 0, e = NumOps>>3; i != e; ++i) { |
| 4884 | unsigned Reg = |
| 4885 | cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg(); |
| 4886 | MatchedRegs.Regs.push_back(Reg); |
| 4887 | } |
| 4888 | |
| 4889 | // Use the produced MatchedRegs object to |
| 4890 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4891 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4892 | break; |
| 4893 | } else { |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4894 | assert(((NumOps & 7) == 4) && "Unknown matching constraint!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4895 | assert((NumOps >> 3) == 1 && "Unexpected number of operands"); |
| 4896 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 4897 | AsmNodeOperands.push_back(DAG.getTargetConstant(NumOps, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4898 | TLI.getPointerTy())); |
| 4899 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 4900 | break; |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
| 4905 | assert(!OpInfo.isIndirect && |
| 4906 | "Don't know how to handle indirect other inputs yet!"); |
| 4907 | |
| 4908 | std::vector<SDValue> Ops; |
| 4909 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4910 | hasMemory, Ops, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4911 | if (Ops.empty()) { |
| 4912 | cerr << "Invalid operand for inline asm constraint '" |
| 4913 | << OpInfo.ConstraintCode << "'!\n"; |
| 4914 | exit(1); |
| 4915 | } |
| 4916 | |
| 4917 | // Add information to the INLINEASM node to know about this input. |
| 4918 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
| 4919 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 4920 | TLI.getPointerTy())); |
| 4921 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
| 4922 | break; |
| 4923 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
| 4924 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
| 4925 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 4926 | "Memory operands expect pointer values"); |
| 4927 | |
| 4928 | // Add information to the INLINEASM node to know about this input. |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4929 | unsigned ResOpType = 4/*MEM*/ | (1<<3); |
| 4930 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4931 | TLI.getPointerTy())); |
| 4932 | AsmNodeOperands.push_back(InOperandVal); |
| 4933 | break; |
| 4934 | } |
| 4935 | |
| 4936 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 4937 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 4938 | "Unknown constraint type!"); |
| 4939 | assert(!OpInfo.isIndirect && |
| 4940 | "Don't know how to handle indirect register inputs yet!"); |
| 4941 | |
| 4942 | // Copy the input into the appropriate registers. |
| 4943 | assert(!OpInfo.AssignedRegs.Regs.empty() && |
| 4944 | "Couldn't allocate input reg!"); |
| 4945 | |
| 4946 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag); |
| 4947 | |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 4948 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, |
| 4949 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4950 | break; |
| 4951 | } |
| 4952 | case InlineAsm::isClobber: { |
| 4953 | // Add the clobbered value to the operand list, so that the register |
| 4954 | // allocator is aware that the physreg got clobbered. |
| 4955 | if (!OpInfo.AssignedRegs.Regs.empty()) |
Dale Johannesen | 91aac10 | 2008-09-17 21:13:11 +0000 | [diff] [blame] | 4956 | OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */, |
| 4957 | DAG, AsmNodeOperands); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 4958 | break; |
| 4959 | } |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | // Finish up input operands. |
| 4964 | AsmNodeOperands[0] = Chain; |
| 4965 | if (Flag.getNode()) AsmNodeOperands.push_back(Flag); |
| 4966 | |
| 4967 | Chain = DAG.getNode(ISD::INLINEASM, |
| 4968 | DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2, |
| 4969 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
| 4970 | Flag = Chain.getValue(1); |
| 4971 | |
| 4972 | // If this asm returns a register value, copy the result from that register |
| 4973 | // and set it as the value of the call. |
| 4974 | if (!RetValRegs.Regs.empty()) { |
| 4975 | SDValue Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag); |
| 4976 | |
| 4977 | // If any of the results of the inline asm is a vector, it may have the |
| 4978 | // wrong width/num elts. This can happen for register classes that can |
| 4979 | // contain multiple different value types. The preg or vreg allocated may |
| 4980 | // not have the same VT as was expected. Convert it to the right type with |
| 4981 | // bit_convert. |
| 4982 | if (const StructType *ResSTy = dyn_cast<StructType>(CS.getType())) { |
| 4983 | for (unsigned i = 0, e = ResSTy->getNumElements(); i != e; ++i) { |
| 4984 | if (Val.getNode()->getValueType(i).isVector()) |
| 4985 | Val = DAG.getNode(ISD::BIT_CONVERT, |
| 4986 | TLI.getValueType(ResSTy->getElementType(i)), Val); |
| 4987 | } |
| 4988 | } else { |
| 4989 | if (Val.getValueType().isVector()) |
| 4990 | Val = DAG.getNode(ISD::BIT_CONVERT, TLI.getValueType(CS.getType()), |
| 4991 | Val); |
| 4992 | } |
| 4993 | |
| 4994 | setValue(CS.getInstruction(), Val); |
| 4995 | } |
| 4996 | |
| 4997 | std::vector<std::pair<SDValue, Value*> > StoresToEmit; |
| 4998 | |
| 4999 | // Process indirect outputs, first output all of the flagged copies out of |
| 5000 | // physregs. |
| 5001 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
| 5002 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
| 5003 | Value *Ptr = IndirectStoresToEmit[i].second; |
| 5004 | SDValue OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag); |
| 5005 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
| 5006 | } |
| 5007 | |
| 5008 | // Emit the non-flagged stores from the physregs. |
| 5009 | SmallVector<SDValue, 8> OutChains; |
| 5010 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
| 5011 | OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first, |
| 5012 | getValue(StoresToEmit[i].second), |
| 5013 | StoresToEmit[i].second, 0)); |
| 5014 | if (!OutChains.empty()) |
| 5015 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 5016 | &OutChains[0], OutChains.size()); |
| 5017 | DAG.setRoot(Chain); |
| 5018 | } |
| 5019 | |
| 5020 | |
| 5021 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 5022 | SDValue Src = getValue(I.getOperand(0)); |
| 5023 | |
| 5024 | MVT IntPtr = TLI.getPointerTy(); |
| 5025 | |
| 5026 | if (IntPtr.bitsLT(Src.getValueType())) |
| 5027 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 5028 | else if (IntPtr.bitsGT(Src.getValueType())) |
| 5029 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
| 5030 | |
| 5031 | // Scale the source by the type size. |
| 5032 | uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType()); |
| 5033 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 5034 | Src, DAG.getIntPtrConstant(ElementSize)); |
| 5035 | |
| 5036 | TargetLowering::ArgListTy Args; |
| 5037 | TargetLowering::ArgListEntry Entry; |
| 5038 | Entry.Node = Src; |
| 5039 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5040 | Args.push_back(Entry); |
| 5041 | |
| 5042 | std::pair<SDValue,SDValue> Result = |
| 5043 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, CallingConv::C, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5044 | PerformTailCallOpt, DAG.getExternalSymbol("malloc", IntPtr), |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5045 | Args, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5046 | setValue(&I, Result.first); // Pointers always fit in registers |
| 5047 | DAG.setRoot(Result.second); |
| 5048 | } |
| 5049 | |
| 5050 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 5051 | TargetLowering::ArgListTy Args; |
| 5052 | TargetLowering::ArgListEntry Entry; |
| 5053 | Entry.Node = getValue(I.getOperand(0)); |
| 5054 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 5055 | Args.push_back(Entry); |
| 5056 | MVT IntPtr = TLI.getPointerTy(); |
| 5057 | std::pair<SDValue,SDValue> Result = |
| 5058 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5059 | CallingConv::C, PerformTailCallOpt, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5060 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5061 | DAG.setRoot(Result.second); |
| 5062 | } |
| 5063 | |
| 5064 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
| 5065 | DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), |
| 5066 | getValue(I.getOperand(1)), |
| 5067 | DAG.getSrcValue(I.getOperand(1)))); |
| 5068 | } |
| 5069 | |
| 5070 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 5071 | SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(), |
| 5072 | getValue(I.getOperand(0)), |
| 5073 | DAG.getSrcValue(I.getOperand(0))); |
| 5074 | setValue(&I, V); |
| 5075 | DAG.setRoot(V.getValue(1)); |
| 5076 | } |
| 5077 | |
| 5078 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
| 5079 | DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(), |
| 5080 | getValue(I.getOperand(1)), |
| 5081 | DAG.getSrcValue(I.getOperand(1)))); |
| 5082 | } |
| 5083 | |
| 5084 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
| 5085 | DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), |
| 5086 | getValue(I.getOperand(1)), |
| 5087 | getValue(I.getOperand(2)), |
| 5088 | DAG.getSrcValue(I.getOperand(1)), |
| 5089 | DAG.getSrcValue(I.getOperand(2)))); |
| 5090 | } |
| 5091 | |
| 5092 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 5093 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
| 5094 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
| 5095 | /// integrated into SDISel. |
| 5096 | void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG, |
| 5097 | SmallVectorImpl<SDValue> &ArgValues) { |
| 5098 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 5099 | SmallVector<SDValue, 3+16> Ops; |
| 5100 | Ops.push_back(DAG.getRoot()); |
| 5101 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 5102 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 5103 | |
| 5104 | // Add one result value for each formal argument. |
| 5105 | SmallVector<MVT, 16> RetVals; |
| 5106 | unsigned j = 1; |
| 5107 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 5108 | I != E; ++I, ++j) { |
| 5109 | SmallVector<MVT, 4> ValueVTs; |
| 5110 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5111 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5112 | Value != NumValues; ++Value) { |
| 5113 | MVT VT = ValueVTs[Value]; |
| 5114 | const Type *ArgTy = VT.getTypeForMVT(); |
| 5115 | ISD::ArgFlagsTy Flags; |
| 5116 | unsigned OriginalAlignment = |
| 5117 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5118 | |
| 5119 | if (F.paramHasAttr(j, ParamAttr::ZExt)) |
| 5120 | Flags.setZExt(); |
| 5121 | if (F.paramHasAttr(j, ParamAttr::SExt)) |
| 5122 | Flags.setSExt(); |
| 5123 | if (F.paramHasAttr(j, ParamAttr::InReg)) |
| 5124 | Flags.setInReg(); |
| 5125 | if (F.paramHasAttr(j, ParamAttr::StructRet)) |
| 5126 | Flags.setSRet(); |
| 5127 | if (F.paramHasAttr(j, ParamAttr::ByVal)) { |
| 5128 | Flags.setByVal(); |
| 5129 | const PointerType *Ty = cast<PointerType>(I->getType()); |
| 5130 | const Type *ElementTy = Ty->getElementType(); |
| 5131 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
| 5132 | unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy); |
| 5133 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 5134 | // this info is not there but there are cases it cannot get right. |
| 5135 | if (F.getParamAlignment(j)) |
| 5136 | FrameAlign = F.getParamAlignment(j); |
| 5137 | Flags.setByValAlign(FrameAlign); |
| 5138 | Flags.setByValSize(FrameSize); |
| 5139 | } |
| 5140 | if (F.paramHasAttr(j, ParamAttr::Nest)) |
| 5141 | Flags.setNest(); |
| 5142 | Flags.setOrigAlign(OriginalAlignment); |
| 5143 | |
| 5144 | MVT RegisterVT = getRegisterType(VT); |
| 5145 | unsigned NumRegs = getNumRegisters(VT); |
| 5146 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 5147 | RetVals.push_back(RegisterVT); |
| 5148 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5149 | if (NumRegs > 1 && i == 0) |
| 5150 | MyFlags.setSplit(); |
| 5151 | // if it isn't first piece, alignment must be 1 |
| 5152 | else if (i > 0) |
| 5153 | MyFlags.setOrigAlign(1); |
| 5154 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5155 | } |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | RetVals.push_back(MVT::Other); |
| 5160 | |
| 5161 | // Create the node. |
| 5162 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, |
| 5163 | DAG.getVTList(&RetVals[0], RetVals.size()), |
| 5164 | &Ops[0], Ops.size()).getNode(); |
| 5165 | |
| 5166 | // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but |
| 5167 | // allows exposing the loads that may be part of the argument access to the |
| 5168 | // first DAGCombiner pass. |
| 5169 | SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG); |
| 5170 | |
| 5171 | // The number of results should match up, except that the lowered one may have |
| 5172 | // an extra flag result. |
| 5173 | assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() || |
| 5174 | (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() && |
| 5175 | TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag)) |
| 5176 | && "Lowering produced unexpected number of results!"); |
| 5177 | |
| 5178 | // The FORMAL_ARGUMENTS node itself is likely no longer needed. |
| 5179 | if (Result != TmpRes.getNode() && Result->use_empty()) { |
| 5180 | HandleSDNode Dummy(DAG.getRoot()); |
| 5181 | DAG.RemoveDeadNode(Result); |
| 5182 | } |
| 5183 | |
| 5184 | Result = TmpRes.getNode(); |
| 5185 | |
| 5186 | unsigned NumArgRegs = Result->getNumValues() - 1; |
| 5187 | DAG.setRoot(SDValue(Result, NumArgRegs)); |
| 5188 | |
| 5189 | // Set up the return result vector. |
| 5190 | unsigned i = 0; |
| 5191 | unsigned Idx = 1; |
| 5192 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 5193 | ++I, ++Idx) { |
| 5194 | SmallVector<MVT, 4> ValueVTs; |
| 5195 | ComputeValueVTs(*this, I->getType(), ValueVTs); |
| 5196 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5197 | Value != NumValues; ++Value) { |
| 5198 | MVT VT = ValueVTs[Value]; |
| 5199 | MVT PartVT = getRegisterType(VT); |
| 5200 | |
| 5201 | unsigned NumParts = getNumRegisters(VT); |
| 5202 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5203 | for (unsigned j = 0; j != NumParts; ++j) |
| 5204 | Parts[j] = SDValue(Result, i++); |
| 5205 | |
| 5206 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5207 | if (F.paramHasAttr(Idx, ParamAttr::SExt)) |
| 5208 | AssertOp = ISD::AssertSext; |
| 5209 | else if (F.paramHasAttr(Idx, ParamAttr::ZExt)) |
| 5210 | AssertOp = ISD::AssertZext; |
| 5211 | |
| 5212 | ArgValues.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT, |
| 5213 | AssertOp)); |
| 5214 | } |
| 5215 | } |
| 5216 | assert(i == NumArgRegs && "Argument register count mismatch!"); |
| 5217 | } |
| 5218 | |
| 5219 | |
| 5220 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 5221 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 5222 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 5223 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 5224 | std::pair<SDValue, SDValue> |
| 5225 | TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy, |
| 5226 | bool RetSExt, bool RetZExt, bool isVarArg, |
| 5227 | unsigned CallingConv, bool isTailCall, |
| 5228 | SDValue Callee, |
| 5229 | ArgListTy &Args, SelectionDAG &DAG) { |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 5230 | assert((!isTailCall || PerformTailCallOpt) && |
| 5231 | "isTailCall set when tail-call optimizations are disabled!"); |
| 5232 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5233 | SmallVector<SDValue, 32> Ops; |
| 5234 | Ops.push_back(Chain); // Op#0 - Chain |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5235 | Ops.push_back(Callee); |
| 5236 | |
| 5237 | // Handle all of the outgoing arguments. |
| 5238 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5239 | SmallVector<MVT, 4> ValueVTs; |
| 5240 | ComputeValueVTs(*this, Args[i].Ty, ValueVTs); |
| 5241 | for (unsigned Value = 0, NumValues = ValueVTs.size(); |
| 5242 | Value != NumValues; ++Value) { |
| 5243 | MVT VT = ValueVTs[Value]; |
| 5244 | const Type *ArgTy = VT.getTypeForMVT(); |
| 5245 | SDValue Op = SDValue(Args[i].Node.getNode(), Args[i].Node.getResNo() + Value); |
| 5246 | ISD::ArgFlagsTy Flags; |
| 5247 | unsigned OriginalAlignment = |
| 5248 | getTargetData()->getABITypeAlignment(ArgTy); |
| 5249 | |
| 5250 | if (Args[i].isZExt) |
| 5251 | Flags.setZExt(); |
| 5252 | if (Args[i].isSExt) |
| 5253 | Flags.setSExt(); |
| 5254 | if (Args[i].isInReg) |
| 5255 | Flags.setInReg(); |
| 5256 | if (Args[i].isSRet) |
| 5257 | Flags.setSRet(); |
| 5258 | if (Args[i].isByVal) { |
| 5259 | Flags.setByVal(); |
| 5260 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
| 5261 | const Type *ElementTy = Ty->getElementType(); |
| 5262 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
| 5263 | unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy); |
| 5264 | // For ByVal, alignment should come from FE. BE will guess if this |
| 5265 | // info is not there but there are cases it cannot get right. |
| 5266 | if (Args[i].Alignment) |
| 5267 | FrameAlign = Args[i].Alignment; |
| 5268 | Flags.setByValAlign(FrameAlign); |
| 5269 | Flags.setByValSize(FrameSize); |
| 5270 | } |
| 5271 | if (Args[i].isNest) |
| 5272 | Flags.setNest(); |
| 5273 | Flags.setOrigAlign(OriginalAlignment); |
| 5274 | |
| 5275 | MVT PartVT = getRegisterType(VT); |
| 5276 | unsigned NumParts = getNumRegisters(VT); |
| 5277 | SmallVector<SDValue, 4> Parts(NumParts); |
| 5278 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 5279 | |
| 5280 | if (Args[i].isSExt) |
| 5281 | ExtendKind = ISD::SIGN_EXTEND; |
| 5282 | else if (Args[i].isZExt) |
| 5283 | ExtendKind = ISD::ZERO_EXTEND; |
| 5284 | |
| 5285 | getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
| 5286 | |
| 5287 | for (unsigned i = 0; i != NumParts; ++i) { |
| 5288 | // if it isn't first piece, alignment must be 1 |
| 5289 | ISD::ArgFlagsTy MyFlags = Flags; |
| 5290 | if (NumParts > 1 && i == 0) |
| 5291 | MyFlags.setSplit(); |
| 5292 | else if (i != 0) |
| 5293 | MyFlags.setOrigAlign(1); |
| 5294 | |
| 5295 | Ops.push_back(Parts[i]); |
| 5296 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | // Figure out the result value types. We start by making a list of |
| 5302 | // the potentially illegal return value types. |
| 5303 | SmallVector<MVT, 4> LoweredRetTys; |
| 5304 | SmallVector<MVT, 4> RetTys; |
| 5305 | ComputeValueVTs(*this, RetTy, RetTys); |
| 5306 | |
| 5307 | // Then we translate that to a list of legal types. |
| 5308 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5309 | MVT VT = RetTys[I]; |
| 5310 | MVT RegisterVT = getRegisterType(VT); |
| 5311 | unsigned NumRegs = getNumRegisters(VT); |
| 5312 | for (unsigned i = 0; i != NumRegs; ++i) |
| 5313 | LoweredRetTys.push_back(RegisterVT); |
| 5314 | } |
| 5315 | |
| 5316 | LoweredRetTys.push_back(MVT::Other); // Always has a chain. |
| 5317 | |
| 5318 | // Create the CALL node. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5319 | SDValue Res = DAG.getCall(CallingConv, isVarArg, isTailCall, |
| 5320 | DAG.getVTList(&LoweredRetTys[0], |
| 5321 | LoweredRetTys.size()), |
| 5322 | &Ops[0], Ops.size()); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5323 | Chain = Res.getValue(LoweredRetTys.size() - 1); |
| 5324 | |
| 5325 | // Gather up the call result into a single value. |
| 5326 | if (RetTy != Type::VoidTy) { |
| 5327 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 5328 | |
| 5329 | if (RetSExt) |
| 5330 | AssertOp = ISD::AssertSext; |
| 5331 | else if (RetZExt) |
| 5332 | AssertOp = ISD::AssertZext; |
| 5333 | |
| 5334 | SmallVector<SDValue, 4> ReturnValues; |
| 5335 | unsigned RegNo = 0; |
| 5336 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 5337 | MVT VT = RetTys[I]; |
| 5338 | MVT RegisterVT = getRegisterType(VT); |
| 5339 | unsigned NumRegs = getNumRegisters(VT); |
| 5340 | unsigned RegNoEnd = NumRegs + RegNo; |
| 5341 | SmallVector<SDValue, 4> Results; |
| 5342 | for (; RegNo != RegNoEnd; ++RegNo) |
| 5343 | Results.push_back(Res.getValue(RegNo)); |
| 5344 | SDValue ReturnValue = |
| 5345 | getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT, |
| 5346 | AssertOp); |
| 5347 | ReturnValues.push_back(ReturnValue); |
| 5348 | } |
| 5349 | Res = DAG.getMergeValues(DAG.getVTList(&RetTys[0], RetTys.size()), |
| 5350 | &ReturnValues[0], ReturnValues.size()); |
| 5351 | } |
| 5352 | |
| 5353 | return std::make_pair(Res, Chain); |
| 5354 | } |
| 5355 | |
| 5356 | SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
| 5357 | assert(0 && "LowerOperation not implemented for this target!"); |
| 5358 | abort(); |
| 5359 | return SDValue(); |
| 5360 | } |
| 5361 | |
| 5362 | |
| 5363 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
| 5364 | SDValue Op = getValue(V); |
| 5365 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 5366 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
| 5367 | "Copy from a reg to the same reg!"); |
| 5368 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
| 5369 | |
| 5370 | RegsForValue RFV(TLI, Reg, V->getType()); |
| 5371 | SDValue Chain = DAG.getEntryNode(); |
| 5372 | RFV.getCopyToRegs(Op, DAG, Chain, 0); |
| 5373 | PendingExports.push_back(Chain); |
| 5374 | } |
| 5375 | |
| 5376 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 5377 | |
| 5378 | void SelectionDAGISel:: |
| 5379 | LowerArguments(BasicBlock *LLVMBB) { |
| 5380 | // If this is the entry block, emit arguments. |
| 5381 | Function &F = *LLVMBB->getParent(); |
| 5382 | SDValue OldRoot = SDL->DAG.getRoot(); |
| 5383 | SmallVector<SDValue, 16> Args; |
| 5384 | TLI.LowerArguments(F, SDL->DAG, Args); |
| 5385 | |
| 5386 | unsigned a = 0; |
| 5387 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 5388 | AI != E; ++AI) { |
| 5389 | SmallVector<MVT, 4> ValueVTs; |
| 5390 | ComputeValueVTs(TLI, AI->getType(), ValueVTs); |
| 5391 | unsigned NumValues = ValueVTs.size(); |
| 5392 | if (!AI->use_empty()) { |
| 5393 | SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues)); |
| 5394 | // If this argument is live outside of the entry block, insert a copy from |
| 5395 | // whereever we got it to the vreg that other BB's will reference it as. |
| 5396 | DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo->ValueMap.find(AI); |
| 5397 | if (VMI != FuncInfo->ValueMap.end()) { |
| 5398 | SDL->CopyValueToVirtualRegister(AI, VMI->second); |
| 5399 | } |
| 5400 | } |
| 5401 | a += NumValues; |
| 5402 | } |
| 5403 | |
| 5404 | // Finally, if the target has anything special to do, allow it to do so. |
| 5405 | // FIXME: this should insert code into the DAG! |
| 5406 | EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction()); |
| 5407 | } |
| 5408 | |
| 5409 | /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 5410 | /// ensure constants are generated when needed. Remember the virtual registers |
| 5411 | /// that need to be added to the Machine PHI nodes as input. We cannot just |
| 5412 | /// directly add them, because expansion might result in multiple MBB's for one |
| 5413 | /// BB. As such, the start of the BB might correspond to a different MBB than |
| 5414 | /// the end. |
| 5415 | /// |
| 5416 | void |
| 5417 | SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) { |
| 5418 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 5419 | |
| 5420 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5421 | |
| 5422 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5423 | // from this block. |
| 5424 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5425 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5426 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5427 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
| 5428 | |
| 5429 | // If this terminator has multiple identical successors (common for |
| 5430 | // switches), only handle each succ once. |
| 5431 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 5432 | |
| 5433 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5434 | PHINode *PN; |
| 5435 | |
| 5436 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5437 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5438 | // emitted yet. |
| 5439 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5440 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5441 | // Ignore dead phi's. |
| 5442 | if (PN->use_empty()) continue; |
| 5443 | |
| 5444 | unsigned Reg; |
| 5445 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5446 | |
| 5447 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 5448 | unsigned &RegOut = SDL->ConstantsOut[C]; |
| 5449 | if (RegOut == 0) { |
| 5450 | RegOut = FuncInfo->CreateRegForValue(C); |
| 5451 | SDL->CopyValueToVirtualRegister(C, RegOut); |
| 5452 | } |
| 5453 | Reg = RegOut; |
| 5454 | } else { |
| 5455 | Reg = FuncInfo->ValueMap[PHIOp]; |
| 5456 | if (Reg == 0) { |
| 5457 | assert(isa<AllocaInst>(PHIOp) && |
| 5458 | FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 5459 | "Didn't codegen value into a register!??"); |
| 5460 | Reg = FuncInfo->CreateRegForValue(PHIOp); |
| 5461 | SDL->CopyValueToVirtualRegister(PHIOp, Reg); |
| 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | // Remember that this register needs to added to the machine PHI node as |
| 5466 | // the input for this MBB. |
| 5467 | SmallVector<MVT, 4> ValueVTs; |
| 5468 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 5469 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 5470 | MVT VT = ValueVTs[vti]; |
| 5471 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
| 5472 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
| 5473 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 5474 | Reg += NumRegisters; |
| 5475 | } |
| 5476 | } |
| 5477 | } |
| 5478 | SDL->ConstantsOut.clear(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5481 | /// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only |
| 5482 | /// supports legal types, and it emits MachineInstrs directly instead of |
| 5483 | /// creating SelectionDAG nodes. |
| 5484 | /// |
| 5485 | bool |
| 5486 | SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB, |
| 5487 | FastISel *F) { |
| 5488 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 5489 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5490 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 5491 | unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size(); |
| 5492 | |
| 5493 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 5494 | // from this block. |
| 5495 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 5496 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 5497 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 5498 | MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB]; |
| 5499 | |
| 5500 | // If this terminator has multiple identical successors (common for |
| 5501 | // switches), only handle each succ once. |
| 5502 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 5503 | |
| 5504 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 5505 | PHINode *PN; |
| 5506 | |
| 5507 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 5508 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 5509 | // emitted yet. |
| 5510 | for (BasicBlock::iterator I = SuccBB->begin(); |
| 5511 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 5512 | // Ignore dead phi's. |
| 5513 | if (PN->use_empty()) continue; |
| 5514 | |
| 5515 | // Only handle legal types. Two interesting things to note here. First, |
| 5516 | // by bailing out early, we may leave behind some dead instructions, |
| 5517 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 5518 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 5519 | // use CreateRegForValue to create registers, so it always creates |
| 5520 | // exactly one register for each non-void instruction. |
| 5521 | MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 5522 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
Dan Gohman | 74321ab | 2008-09-10 21:01:31 +0000 | [diff] [blame] | 5523 | // Promote MVT::i1. |
| 5524 | if (VT == MVT::i1) |
| 5525 | VT = TLI.getTypeToTransformTo(VT); |
| 5526 | else { |
| 5527 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 5528 | return false; |
| 5529 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 5533 | |
| 5534 | unsigned Reg = F->getRegForValue(PHIOp); |
| 5535 | if (Reg == 0) { |
| 5536 | SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 5537 | return false; |
| 5538 | } |
| 5539 | SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | return true; |
| 5544 | } |