LLVM API Documentation
00001 //===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements routines for translating from LLVM IR into SelectionDAG IR. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "isel" 00015 #include "SelectionDAGBuild.h" 00016 #include "llvm/ADT/BitVector.h" 00017 #include "llvm/ADT/SmallSet.h" 00018 #include "llvm/Analysis/AliasAnalysis.h" 00019 #include "llvm/Constants.h" 00020 #include "llvm/CallingConv.h" 00021 #include "llvm/DerivedTypes.h" 00022 #include "llvm/Function.h" 00023 #include "llvm/GlobalVariable.h" 00024 #include "llvm/InlineAsm.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Intrinsics.h" 00027 #include "llvm/IntrinsicInst.h" 00028 #include "llvm/Module.h" 00029 #include "llvm/CodeGen/FastISel.h" 00030 #include "llvm/CodeGen/GCStrategy.h" 00031 #include "llvm/CodeGen/GCMetadata.h" 00032 #include "llvm/CodeGen/MachineFunction.h" 00033 #include "llvm/CodeGen/MachineFrameInfo.h" 00034 #include "llvm/CodeGen/MachineInstrBuilder.h" 00035 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00036 #include "llvm/CodeGen/MachineModuleInfo.h" 00037 #include "llvm/CodeGen/MachineRegisterInfo.h" 00038 #include "llvm/CodeGen/PseudoSourceValue.h" 00039 #include "llvm/CodeGen/SelectionDAG.h" 00040 #include "llvm/Target/TargetRegisterInfo.h" 00041 #include "llvm/Target/TargetData.h" 00042 #include "llvm/Target/TargetFrameInfo.h" 00043 #include "llvm/Target/TargetInstrInfo.h" 00044 #include "llvm/Target/TargetLowering.h" 00045 #include "llvm/Target/TargetMachine.h" 00046 #include "llvm/Target/TargetOptions.h" 00047 #include "llvm/Support/Compiler.h" 00048 #include "llvm/Support/Debug.h" 00049 #include "llvm/Support/MathExtras.h" 00050 #include "llvm/Support/raw_ostream.h" 00051 #include <algorithm> 00052 using namespace llvm; 00053 00054 /// LimitFloatPrecision - Generate low-precision inline sequences for 00055 /// some float libcalls (6, 8 or 12 bits). 00056 static unsigned LimitFloatPrecision; 00057 00058 static cl::opt<unsigned, true> 00059 LimitFPPrecision("limit-float-precision", 00060 cl::desc("Generate low-precision inline sequences " 00061 "for some float libcalls"), 00062 cl::location(LimitFloatPrecision), 00063 cl::init(0)); 00064 00065 /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence 00066 /// of insertvalue or extractvalue indices that identify a member, return 00067 /// the linearized index of the start of the member. 00068 /// 00069 static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, 00070 const unsigned *Indices, 00071 const unsigned *IndicesEnd, 00072 unsigned CurIndex = 0) { 00073 // Base case: We're done. 00074 if (Indices && Indices == IndicesEnd) 00075 return CurIndex; 00076 00077 // Given a struct type, recursively traverse the elements. 00078 if (const StructType *STy = dyn_cast<StructType>(Ty)) { 00079 for (StructType::element_iterator EB = STy->element_begin(), 00080 EI = EB, 00081 EE = STy->element_end(); 00082 EI != EE; ++EI) { 00083 if (Indices && *Indices == unsigned(EI - EB)) 00084 return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); 00085 CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); 00086 } 00087 return CurIndex; 00088 } 00089 // Given an array type, recursively traverse the elements. 00090 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 00091 const Type *EltTy = ATy->getElementType(); 00092 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { 00093 if (Indices && *Indices == i) 00094 return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); 00095 CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); 00096 } 00097 return CurIndex; 00098 } 00099 // We haven't found the type we're looking for, so keep searching. 00100 return CurIndex + 1; 00101 } 00102 00103 /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of 00104 /// MVTs that represent all the individual underlying 00105 /// non-aggregate types that comprise it. 00106 /// 00107 /// If Offsets is non-null, it points to a vector to be filled in 00108 /// with the in-memory offsets of each of the individual values. 00109 /// 00110 static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, 00111 SmallVectorImpl<MVT> &ValueVTs, 00112 SmallVectorImpl<uint64_t> *Offsets = 0, 00113 uint64_t StartingOffset = 0) { 00114 // Given a struct type, recursively traverse the elements. 00115 if (const StructType *STy = dyn_cast<StructType>(Ty)) { 00116 const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); 00117 for (StructType::element_iterator EB = STy->element_begin(), 00118 EI = EB, 00119 EE = STy->element_end(); 00120 EI != EE; ++EI) 00121 ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, 00122 StartingOffset + SL->getElementOffset(EI - EB)); 00123 return; 00124 } 00125 // Given an array type, recursively traverse the elements. 00126 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 00127 const Type *EltTy = ATy->getElementType(); 00128 uint64_t EltSize = TLI.getTargetData()->getABITypeSize(EltTy); 00129 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) 00130 ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, 00131 StartingOffset + i * EltSize); 00132 return; 00133 } 00134 // Base case: we can get an MVT for this LLVM IR type. 00135 ValueVTs.push_back(TLI.getValueType(Ty)); 00136 if (Offsets) 00137 Offsets->push_back(StartingOffset); 00138 } 00139 00140 namespace llvm { 00141 /// RegsForValue - This struct represents the registers (physical or virtual) 00142 /// that a particular set of values is assigned, and the type information about 00143 /// the value. The most common situation is to represent one value at a time, 00144 /// but struct or array values are handled element-wise as multiple values. 00145 /// The splitting of aggregates is performed recursively, so that we never 00146 /// have aggregate-typed registers. The values at this point do not necessarily 00147 /// have legal types, so each value may require one or more registers of some 00148 /// legal type. 00149 /// 00150 struct VISIBILITY_HIDDEN RegsForValue { 00151 /// TLI - The TargetLowering object. 00152 /// 00153 const TargetLowering *TLI; 00154 00155 /// ValueVTs - The value types of the values, which may not be legal, and 00156 /// may need be promoted or synthesized from one or more registers. 00157 /// 00158 SmallVector<MVT, 4> ValueVTs; 00159 00160 /// RegVTs - The value types of the registers. This is the same size as 00161 /// ValueVTs and it records, for each value, what the type of the assigned 00162 /// register or registers are. (Individual values are never synthesized 00163 /// from more than one type of register.) 00164 /// 00165 /// With virtual registers, the contents of RegVTs is redundant with TLI's 00166 /// getRegisterType member function, however when with physical registers 00167 /// it is necessary to have a separate record of the types. 00168 /// 00169 SmallVector<MVT, 4> RegVTs; 00170 00171 /// Regs - This list holds the registers assigned to the values. 00172 /// Each legal or promoted value requires one register, and each 00173 /// expanded value requires multiple registers. 00174 /// 00175 SmallVector<unsigned, 4> Regs; 00176 00177 RegsForValue() : TLI(0) {} 00178 00179 RegsForValue(const TargetLowering &tli, 00180 const SmallVector<unsigned, 4> ®s, 00181 MVT regvt, MVT valuevt) 00182 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} 00183 RegsForValue(const TargetLowering &tli, 00184 const SmallVector<unsigned, 4> ®s, 00185 const SmallVector<MVT, 4> ®vts, 00186 const SmallVector<MVT, 4> &valuevts) 00187 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} 00188 RegsForValue(const TargetLowering &tli, 00189 unsigned Reg, const Type *Ty) : TLI(&tli) { 00190 ComputeValueVTs(tli, Ty, ValueVTs); 00191 00192 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { 00193 MVT ValueVT = ValueVTs[Value]; 00194 unsigned NumRegs = TLI->getNumRegisters(ValueVT); 00195 MVT RegisterVT = TLI->getRegisterType(ValueVT); 00196 for (unsigned i = 0; i != NumRegs; ++i) 00197 Regs.push_back(Reg + i); 00198 RegVTs.push_back(RegisterVT); 00199 Reg += NumRegs; 00200 } 00201 } 00202 00203 /// append - Add the specified values to this one. 00204 void append(const RegsForValue &RHS) { 00205 TLI = RHS.TLI; 00206 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); 00207 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); 00208 Regs.append(RHS.Regs.begin(), RHS.Regs.end()); 00209 } 00210 00211 00212 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from 00213 /// this value and returns the result as a ValueVTs value. This uses 00214 /// Chain/Flag as the input and updates them for the output Chain/Flag. 00215 /// If the Flag pointer is NULL, no flag is used. 00216 SDValue getCopyFromRegs(SelectionDAG &DAG, 00217 SDValue &Chain, SDValue *Flag) const; 00218 00219 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the 00220 /// specified value into the registers specified by this object. This uses 00221 /// Chain/Flag as the input and updates them for the output Chain/Flag. 00222 /// If the Flag pointer is NULL, no flag is used. 00223 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, 00224 SDValue &Chain, SDValue *Flag) const; 00225 00226 /// AddInlineAsmOperands - Add this value to the specified inlineasm node 00227 /// operand list. This adds the code marker and includes the number of 00228 /// values added into it. 00229 void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, 00230 std::vector<SDValue> &Ops) const; 00231 }; 00232 } 00233 00234 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by 00235 /// PHI nodes or outside of the basic block that defines it, or used by a 00236 /// switch or atomic instruction, which may expand to multiple basic blocks. 00237 static bool isUsedOutsideOfDefiningBlock(Instruction *I) { 00238 if (isa<PHINode>(I)) return true; 00239 BasicBlock *BB = I->getParent(); 00240 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) 00241 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) || 00242 // FIXME: Remove switchinst special case. 00243 isa<SwitchInst>(*UI)) 00244 return true; 00245 return false; 00246 } 00247 00248 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the 00249 /// entry block, return true. This includes arguments used by switches, since 00250 /// the switch may expand into multiple basic blocks. 00251 static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) { 00252 // With FastISel active, we may be splitting blocks, so force creation 00253 // of virtual registers for all non-dead arguments. 00254 // Don't force virtual registers for byval arguments though, because 00255 // fast-isel can't handle those in all cases. 00256 if (EnableFastISel && !A->hasByValAttr()) 00257 return A->use_empty(); 00258 00259 BasicBlock *Entry = A->getParent()->begin(); 00260 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) 00261 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) 00262 return false; // Use not in entry block. 00263 return true; 00264 } 00265 00266 FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli) 00267 : TLI(tli) { 00268 } 00269 00270 void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf, 00271 bool EnableFastISel) { 00272 Fn = &fn; 00273 MF = &mf; 00274 RegInfo = &MF->getRegInfo(); 00275 00276 // Create a vreg for each argument register that is not dead and is used 00277 // outside of the entry block for the function. 00278 for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); 00279 AI != E; ++AI) 00280 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) 00281 InitializeRegForValue(AI); 00282 00283 // Initialize the mapping of values to registers. This is only set up for 00284 // instruction values that are used outside of the block that defines 00285 // them. 00286 Function::iterator BB = Fn->begin(), EB = Fn->end(); 00287 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00288 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) 00289 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { 00290 const Type *Ty = AI->getAllocatedType(); 00291 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); 00292 unsigned Align = 00293 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), 00294 AI->getAlignment()); 00295 00296 TySize *= CUI->getZExtValue(); // Get total allocated size. 00297 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. 00298 StaticAllocaMap[AI] = 00299 MF->getFrameInfo()->CreateStackObject(TySize, Align); 00300 } 00301 00302 for (; BB != EB; ++BB) 00303 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00304 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) 00305 if (!isa<AllocaInst>(I) || 00306 !StaticAllocaMap.count(cast<AllocaInst>(I))) 00307 InitializeRegForValue(I); 00308 00309 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This 00310 // also creates the initial PHI MachineInstrs, though none of the input 00311 // operands are populated. 00312 for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) { 00313 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); 00314 MBBMap[BB] = MBB; 00315 MF->push_back(MBB); 00316 00317 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as 00318 // appropriate. 00319 PHINode *PN; 00320 for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){ 00321 if (PN->use_empty()) continue; 00322 00323 unsigned PHIReg = ValueMap[PN]; 00324 assert(PHIReg && "PHI node does not have an assigned virtual register!"); 00325 00326 SmallVector<MVT, 4> ValueVTs; 00327 ComputeValueVTs(TLI, PN->getType(), ValueVTs); 00328 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { 00329 MVT VT = ValueVTs[vti]; 00330 unsigned NumRegisters = TLI.getNumRegisters(VT); 00331 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00332 for (unsigned i = 0; i != NumRegisters; ++i) 00333 BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i); 00334 PHIReg += NumRegisters; 00335 } 00336 } 00337 } 00338 } 00339 00340 unsigned FunctionLoweringInfo::MakeReg(MVT VT) { 00341 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); 00342 } 00343 00344 /// CreateRegForValue - Allocate the appropriate number of virtual registers of 00345 /// the correctly promoted or expanded types. Assign these registers 00346 /// consecutive vreg numbers and return the first assigned number. 00347 /// 00348 /// In the case that the given value has struct or array type, this function 00349 /// will assign registers for each member or element. 00350 /// 00351 unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { 00352 SmallVector<MVT, 4> ValueVTs; 00353 ComputeValueVTs(TLI, V->getType(), ValueVTs); 00354 00355 unsigned FirstReg = 0; 00356 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { 00357 MVT ValueVT = ValueVTs[Value]; 00358 MVT RegisterVT = TLI.getRegisterType(ValueVT); 00359 00360 unsigned NumRegs = TLI.getNumRegisters(ValueVT); 00361 for (unsigned i = 0; i != NumRegs; ++i) { 00362 unsigned R = MakeReg(RegisterVT); 00363 if (!FirstReg) FirstReg = R; 00364 } 00365 } 00366 return FirstReg; 00367 } 00368 00369 /// getCopyFromParts - Create a value that contains the specified legal parts 00370 /// combined into the value they represent. If the parts combine to a type 00371 /// larger then ValueVT then AssertOp can be used to specify whether the extra 00372 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT 00373 /// (ISD::AssertSext). 00374 static SDValue getCopyFromParts(SelectionDAG &DAG, 00375 const SDValue *Parts, 00376 unsigned NumParts, 00377 MVT PartVT, 00378 MVT ValueVT, 00379 ISD::NodeType AssertOp = ISD::DELETED_NODE) { 00380 assert(NumParts > 0 && "No parts to assemble!"); 00381 TargetLowering &TLI = DAG.getTargetLoweringInfo(); 00382 SDValue Val = Parts[0]; 00383 00384 if (NumParts > 1) { 00385 // Assemble the value from multiple parts. 00386 if (!ValueVT.isVector()) { 00387 unsigned PartBits = PartVT.getSizeInBits(); 00388 unsigned ValueBits = ValueVT.getSizeInBits(); 00389 00390 // Assemble the power of 2 part. 00391 unsigned RoundParts = NumParts & (NumParts - 1) ? 00392 1 << Log2_32(NumParts) : NumParts; 00393 unsigned RoundBits = PartBits * RoundParts; 00394 MVT RoundVT = RoundBits == ValueBits ? 00395 ValueVT : MVT::getIntegerVT(RoundBits); 00396 SDValue Lo, Hi; 00397 00398 MVT HalfVT = ValueVT.isInteger() ? 00399 MVT::getIntegerVT(RoundBits/2) : 00400 MVT::getFloatingPointVT(RoundBits/2); 00401 00402 if (RoundParts > 2) { 00403 Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT); 00404 Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2, 00405 PartVT, HalfVT); 00406 } else { 00407 Lo = DAG.getNode(ISD::BIT_CONVERT, HalfVT, Parts[0]); 00408 Hi = DAG.getNode(ISD::BIT_CONVERT, HalfVT, Parts[1]); 00409 } 00410 if (TLI.isBigEndian()) 00411 std::swap(Lo, Hi); 00412 Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi); 00413 00414 if (RoundParts < NumParts) { 00415 // Assemble the trailing non-power-of-2 part. 00416 unsigned OddParts = NumParts - RoundParts; 00417 MVT OddVT = MVT::getIntegerVT(OddParts * PartBits); 00418 Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT); 00419 00420 // Combine the round and odd parts. 00421 Lo = Val; 00422 if (TLI.isBigEndian()) 00423 std::swap(Lo, Hi); 00424 MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits); 00425 Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi); 00426 Hi = DAG.getNode(ISD::SHL, TotalVT, Hi, 00427 DAG.getConstant(Lo.getValueType().getSizeInBits(), 00428 TLI.getShiftAmountTy())); 00429 Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo); 00430 Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi); 00431 } 00432 } else { 00433 // Handle a multi-element vector. 00434 MVT IntermediateVT, RegisterVT; 00435 unsigned NumIntermediates; 00436 unsigned NumRegs = 00437 TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, 00438 RegisterVT); 00439 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); 00440 NumParts = NumRegs; // Silence a compiler warning. 00441 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); 00442 assert(RegisterVT == Parts[0].getValueType() && 00443 "Part type doesn't match part!"); 00444 00445 // Assemble the parts into intermediate operands. 00446 SmallVector<SDValue, 8> Ops(NumIntermediates); 00447 if (NumIntermediates == NumParts) { 00448 // If the register was not expanded, truncate or copy the value, 00449 // as appropriate. 00450 for (unsigned i = 0; i != NumParts; ++i) 00451 Ops[i] = getCopyFromParts(DAG, &Parts[i], 1, 00452 PartVT, IntermediateVT); 00453 } else if (NumParts > 0) { 00454 // If the intermediate type was expanded, build the intermediate operands 00455 // from the parts. 00456 assert(NumParts % NumIntermediates == 0 && 00457 "Must expand into a divisible number of parts!"); 00458 unsigned Factor = NumParts / NumIntermediates; 00459 for (unsigned i = 0; i != NumIntermediates; ++i) 00460 Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor, 00461 PartVT, IntermediateVT); 00462 } 00463 00464 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate 00465 // operands. 00466 Val = DAG.getNode(IntermediateVT.isVector() ? 00467 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, 00468 ValueVT, &Ops[0], NumIntermediates); 00469 } 00470 } 00471 00472 // There is now one part, held in Val. Correct it to match ValueVT. 00473 PartVT = Val.getValueType(); 00474 00475 if (PartVT == ValueVT) 00476 return Val; 00477 00478 if (PartVT.isVector()) { 00479 assert(ValueVT.isVector() && "Unknown vector conversion!"); 00480 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); 00481 } 00482 00483 if (ValueVT.isVector()) { 00484 assert(ValueVT.getVectorElementType() == PartVT && 00485 ValueVT.getVectorNumElements() == 1 && 00486 "Only trivial scalar-to-vector conversions should get here!"); 00487 return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val); 00488 } 00489 00490 if (PartVT.isInteger() && 00491 ValueVT.isInteger()) { 00492 if (ValueVT.bitsLT(PartVT)) { 00493 // For a truncate, see if we have any information to 00494 // indicate whether the truncated bits will always be 00495 // zero or sign-extension. 00496 if (AssertOp != ISD::DELETED_NODE) 00497 Val = DAG.getNode(AssertOp, PartVT, Val, 00498 DAG.getValueType(ValueVT)); 00499 return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); 00500 } else { 00501 return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val); 00502 } 00503 } 00504 00505 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { 00506 if (ValueVT.bitsLT(Val.getValueType())) 00507 // FP_ROUND's are always exact here. 00508 return DAG.getNode(ISD::FP_ROUND, ValueVT, Val, 00509 DAG.getIntPtrConstant(1)); 00510 return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val); 00511 } 00512 00513 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) 00514 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); 00515 00516 assert(0 && "Unknown mismatch!"); 00517 return SDValue(); 00518 } 00519 00520 /// getCopyToParts - Create a series of nodes that contain the specified value 00521 /// split into legal parts. If the parts contain more bits than Val, then, for 00522 /// integers, ExtendKind can be used to specify how to generate the extra bits. 00523 static void getCopyToParts(SelectionDAG &DAG, SDValue Val, 00524 SDValue *Parts, unsigned NumParts, MVT PartVT, 00525 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { 00526 TargetLowering &TLI = DAG.getTargetLoweringInfo(); 00527 MVT PtrVT = TLI.getPointerTy(); 00528 MVT ValueVT = Val.getValueType(); 00529 unsigned PartBits = PartVT.getSizeInBits(); 00530 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); 00531 00532 if (!NumParts) 00533 return; 00534 00535 if (!ValueVT.isVector()) { 00536 if (PartVT == ValueVT) { 00537 assert(NumParts == 1 && "No-op copy with multiple parts!"); 00538 Parts[0] = Val; 00539 return; 00540 } 00541 00542 if (NumParts * PartBits > ValueVT.getSizeInBits()) { 00543 // If the parts cover more bits than the value has, promote the value. 00544 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { 00545 assert(NumParts == 1 && "Do not know what to promote to!"); 00546 Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val); 00547 } else if (PartVT.isInteger() && ValueVT.isInteger()) { 00548 ValueVT = MVT::getIntegerVT(NumParts * PartBits); 00549 Val = DAG.getNode(ExtendKind, ValueVT, Val); 00550 } else { 00551 assert(0 && "Unknown mismatch!"); 00552 } 00553 } else if (PartBits == ValueVT.getSizeInBits()) { 00554 // Different types of the same size. 00555 assert(NumParts == 1 && PartVT != ValueVT); 00556 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); 00557 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { 00558 // If the parts cover less bits than value has, truncate the value. 00559 if (PartVT.isInteger() && ValueVT.isInteger()) { 00560 ValueVT = MVT::getIntegerVT(NumParts * PartBits); 00561 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); 00562 } else { 00563 assert(0 && "Unknown mismatch!"); 00564 } 00565 } 00566 00567 // The value may have changed - recompute ValueVT. 00568 ValueVT = Val.getValueType(); 00569 assert(NumParts * PartBits == ValueVT.getSizeInBits() && 00570 "Failed to tile the value with PartVT!"); 00571 00572 if (NumParts == 1) { 00573 assert(PartVT == ValueVT && "Type conversion failed!"); 00574 Parts[0] = Val; 00575 return; 00576 } 00577 00578 // Expand the value into multiple parts. 00579 if (NumParts & (NumParts - 1)) { 00580 // The number of parts is not a power of 2. Split off and copy the tail. 00581 assert(PartVT.isInteger() && ValueVT.isInteger() && 00582 "Do not know what to expand to!"); 00583 unsigned RoundParts = 1 << Log2_32(NumParts); 00584 unsigned RoundBits = RoundParts * PartBits; 00585 unsigned OddParts = NumParts - RoundParts; 00586 SDValue OddVal = DAG.getNode(ISD::SRL, ValueVT, Val, 00587 DAG.getConstant(RoundBits, 00588 TLI.getShiftAmountTy())); 00589 getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT); 00590 if (TLI.isBigEndian()) 00591 // The odd parts were reversed by getCopyToParts - unreverse them. 00592 std::reverse(Parts + RoundParts, Parts + NumParts); 00593 NumParts = RoundParts; 00594 ValueVT = MVT::getIntegerVT(NumParts * PartBits); 00595 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); 00596 } 00597 00598 // The number of parts is a power of 2. Repeatedly bisect the value using 00599 // EXTRACT_ELEMENT. 00600 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, 00601 MVT::getIntegerVT(ValueVT.getSizeInBits()), 00602 Val); 00603 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { 00604 for (unsigned i = 0; i < NumParts; i += StepSize) { 00605 unsigned ThisBits = StepSize * PartBits / 2; 00606 MVT ThisVT = MVT::getIntegerVT (ThisBits); 00607 SDValue &Part0 = Parts[i]; 00608 SDValue &Part1 = Parts[i+StepSize/2]; 00609 00610 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, 00611 DAG.getConstant(1, PtrVT)); 00612 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, 00613 DAG.getConstant(0, PtrVT)); 00614 00615 if (ThisBits == PartBits && ThisVT != PartVT) { 00616 Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0); 00617 Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1); 00618 } 00619 } 00620 } 00621 00622 if (TLI.isBigEndian()) 00623 std::reverse(Parts, Parts + NumParts); 00624 00625 return; 00626 } 00627 00628 // Vector ValueVT. 00629 if (NumParts == 1) { 00630 if (PartVT != ValueVT) { 00631 if (PartVT.isVector()) { 00632 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); 00633 } else { 00634 assert(ValueVT.getVectorElementType() == PartVT && 00635 ValueVT.getVectorNumElements() == 1 && 00636 "Only trivial vector-to-scalar conversions should get here!"); 00637 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val, 00638 DAG.getConstant(0, PtrVT)); 00639 } 00640 } 00641 00642 Parts[0] = Val; 00643 return; 00644 } 00645 00646 // Handle a multi-element vector. 00647 MVT IntermediateVT, RegisterVT; 00648 unsigned NumIntermediates; 00649 unsigned NumRegs = 00650 DAG.getTargetLoweringInfo() 00651 .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, 00652 RegisterVT); 00653 unsigned NumElements = ValueVT.getVectorNumElements(); 00654 00655 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); 00656 NumParts = NumRegs; // Silence a compiler warning. 00657 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); 00658 00659 // Split the vector into intermediate operands. 00660 SmallVector<SDValue, 8> Ops(NumIntermediates); 00661 for (unsigned i = 0; i != NumIntermediates; ++i) 00662 if (IntermediateVT.isVector()) 00663 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, 00664 IntermediateVT, Val, 00665 DAG.getConstant(i * (NumElements / NumIntermediates), 00666 PtrVT)); 00667 else 00668 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, 00669 IntermediateVT, Val, 00670 DAG.getConstant(i, PtrVT)); 00671 00672 // Split the intermediate operands into legal parts. 00673 if (NumParts == NumIntermediates) { 00674 // If the register was not expanded, promote or copy the value, 00675 // as appropriate. 00676 for (unsigned i = 0; i != NumParts; ++i) 00677 getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT); 00678 } else if (NumParts > 0) { 00679 // If the intermediate type was expanded, split each the value into 00680 // legal parts. 00681 assert(NumParts % NumIntermediates == 0 && 00682 "Must expand into a divisible number of parts!"); 00683 unsigned Factor = NumParts / NumIntermediates; 00684 for (unsigned i = 0; i != NumIntermediates; ++i) 00685 getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT); 00686 } 00687 } 00688 00689 00690 void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) { 00691 AA = &aa; 00692 GFI = gfi; 00693 TD = DAG.getTarget().getTargetData(); 00694 } 00695 00696 /// clear - Clear out the curret SelectionDAG and the associated 00697 /// state and prepare this SelectionDAGLowering object to be used 00698 /// for a new block. This doesn't clear out information about 00699 /// additional blocks that are needed to complete switch lowering 00700 /// or PHI node updating; that information is cleared out as it is 00701 /// consumed. 00702 void SelectionDAGLowering::clear() { 00703 NodeMap.clear(); 00704 PendingLoads.clear(); 00705 PendingExports.clear(); 00706 DAG.clear(); 00707 } 00708 00709 /// getRoot - Return the current virtual root of the Selection DAG, 00710 /// flushing any PendingLoad items. This must be done before emitting 00711 /// a store or any other node that may need to be ordered after any 00712 /// prior load instructions. 00713 /// 00714 SDValue SelectionDAGLowering::getRoot() { 00715 if (PendingLoads.empty()) 00716 return DAG.getRoot(); 00717 00718 if (PendingLoads.size() == 1) { 00719 SDValue Root = PendingLoads[0]; 00720 DAG.setRoot(Root); 00721 PendingLoads.clear(); 00722 return Root; 00723 } 00724 00725 // Otherwise, we have to make a token factor node. 00726 SDValue Root = DAG.getNode(ISD::TokenFactor, MVT::Other, 00727 &PendingLoads[0], PendingLoads.size()); 00728 PendingLoads.clear(); 00729 DAG.setRoot(Root); 00730 return Root; 00731 } 00732 00733 /// getControlRoot - Similar to getRoot, but instead of flushing all the 00734 /// PendingLoad items, flush all the PendingExports items. It is necessary 00735 /// to do this before emitting a terminator instruction. 00736 /// 00737 SDValue SelectionDAGLowering::getControlRoot() { 00738 SDValue Root = DAG.getRoot(); 00739 00740 if (PendingExports.empty()) 00741 return Root; 00742 00743 // Turn all of the CopyToReg chains into one factored node. 00744 if (Root.getOpcode() != ISD::EntryToken) { 00745 unsigned i = 0, e = PendingExports.size(); 00746 for (; i != e; ++i) { 00747 assert(PendingExports[i].getNode()->getNumOperands() > 1); 00748 if (PendingExports[i].getNode()->getOperand(0) == Root) 00749 break; // Don't add the root if we already indirectly depend on it. 00750 } 00751 00752 if (i == e) 00753 PendingExports.push_back(Root); 00754 } 00755 00756 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, 00757 &PendingExports[0], 00758 PendingExports.size()); 00759 PendingExports.clear(); 00760 DAG.setRoot(Root); 00761 return Root; 00762 } 00763 00764 void SelectionDAGLowering::visit(Instruction &I) { 00765 visit(I.getOpcode(), I); 00766 } 00767 00768 void SelectionDAGLowering::visit(unsigned Opcode, User &I) { 00769 // Note: this doesn't use InstVisitor, because it has to work with 00770 // ConstantExpr's in addition to instructions. 00771 switch (Opcode) { 00772 default: assert(0 && "Unknown instruction type encountered!"); 00773 abort(); 00774 // Build the switch statement using the Instruction.def file. 00775 #define HANDLE_INST(NUM, OPCODE, CLASS) \ 00776 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); 00777 #include "llvm/Instruction.def" 00778 } 00779 } 00780 00781 void SelectionDAGLowering::visitAdd(User &I) { 00782 if (I.getType()->isFPOrFPVector()) 00783 visitBinary(I, ISD::FADD); 00784 else 00785 visitBinary(I, ISD::ADD); 00786 } 00787 00788 void SelectionDAGLowering::visitMul(User &I) { 00789 if (I.getType()->isFPOrFPVector()) 00790 visitBinary(I, ISD::FMUL); 00791 else 00792 visitBinary(I, ISD::MUL); 00793 } 00794 00795 SDValue SelectionDAGLowering::getValue(const Value *V) { 00796 SDValue &N = NodeMap[V]; 00797 if (N.getNode()) return N; 00798 00799 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 00800 MVT VT = TLI.getValueType(V->getType(), true); 00801 00802 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) 00803 return N = DAG.getConstant(*CI, VT); 00804 00805 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 00806 return N = DAG.getGlobalAddress(GV, VT); 00807 00808 if (isa<ConstantPointerNull>(C)) 00809 return N = DAG.getConstant(0, TLI.getPointerTy()); 00810 00811 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 00812 return N = DAG.getConstantFP(*CFP, VT); 00813 00814 if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) && 00815 !V->getType()->isAggregateType()) 00816 return N = DAG.getNode(ISD::UNDEF, VT); 00817 00818 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 00819 visit(CE->getOpcode(), *CE); 00820 SDValue N1 = NodeMap[V]; 00821 assert(N1.getNode() && "visit didn't populate the ValueMap!"); 00822 return N1; 00823 } 00824 00825 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {