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