diff options
Diffstat (limited to 'contrib/llvm/lib/VMCore')
29 files changed, 2409 insertions, 2386 deletions
diff --git a/contrib/llvm/lib/VMCore/AsmWriter.cpp b/contrib/llvm/lib/VMCore/AsmWriter.cpp index 18308f2..7b39efb 100644 --- a/contrib/llvm/lib/VMCore/AsmWriter.cpp +++ b/contrib/llvm/lib/VMCore/AsmWriter.cpp @@ -89,7 +89,6 @@ enum PrefixType { static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) { assert(!Name.empty() && "Cannot get empty name!"); switch (Prefix) { - default: llvm_unreachable("Bad prefix!"); case NoPrefix: break; case GlobalPrefix: OS << '@'; break; case LabelPrefix: break; @@ -189,6 +188,7 @@ void TypePrinting::incorporateTypes(const Module &M) { void TypePrinting::print(Type *Ty, raw_ostream &OS) { switch (Ty->getTypeID()) { case Type::VoidTyID: OS << "void"; break; + case Type::HalfTyID: OS << "half"; break; case Type::FloatTyID: OS << "float"; break; case Type::DoubleTyID: OS << "double"; break; case Type::X86_FP80TyID: OS << "x86_fp80"; break; @@ -231,7 +231,7 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) { if (I != NumberedTypes.end()) OS << '%' << I->second; else // Not enumerated, print the hex address. - OS << "%\"type 0x" << STy << '\"'; + OS << "%\"type " << STy << '\"'; return; } case Type::PointerTyID: { @@ -708,31 +708,37 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, } if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { - if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble || - &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) { + if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf || + &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle || + &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) { // We would like to output the FP constant value in exponential notation, // but we cannot do this if doing so will lose precision. Check here to // make sure that we only output it in exponential format if we can parse // the value back and get the same value. // bool ignored; + bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf; bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble; - double Val = isDouble ? CFP->getValueAPF().convertToDouble() : - CFP->getValueAPF().convertToFloat(); - SmallString<128> StrVal; - raw_svector_ostream(StrVal) << Val; - - // Check to make sure that the stringized number is not some string like - // "Inf" or NaN, that atof will accept, but the lexer will not. Check - // that the string matches the "[-+]?[0-9]" regex. - // - if ((StrVal[0] >= '0' && StrVal[0] <= '9') || - ((StrVal[0] == '-' || StrVal[0] == '+') && - (StrVal[1] >= '0' && StrVal[1] <= '9'))) { - // Reparse stringized version! - if (atof(StrVal.c_str()) == Val) { - Out << StrVal.str(); - return; + bool isInf = CFP->getValueAPF().isInfinity(); + bool isNaN = CFP->getValueAPF().isNaN(); + if (!isHalf && !isInf && !isNaN) { + double Val = isDouble ? CFP->getValueAPF().convertToDouble() : + CFP->getValueAPF().convertToFloat(); + SmallString<128> StrVal; + raw_svector_ostream(StrVal) << Val; + + // Check to make sure that the stringized number is not some string like + // "Inf" or NaN, that atof will accept, but the lexer will not. Check + // that the string matches the "[-+]?[0-9]" regex. + // + if ((StrVal[0] >= '0' && StrVal[0] <= '9') || + ((StrVal[0] == '-' || StrVal[0] == '+') && + (StrVal[1] >= '0' && StrVal[1] <= '9'))) { + // Reparse stringized version! + if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) { + Out << StrVal.str(); + return; + } } } // Otherwise we could not reparse it to exactly the same value, so we must @@ -743,7 +749,7 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, "assuming that double is 64 bits!"); char Buffer[40]; APFloat apf = CFP->getValueAPF(); - // Floats are represented in ASCII IR as double, convert. + // Halves and floats are represented in ASCII IR as double, convert. if (!isDouble) apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored); @@ -823,35 +829,53 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, } if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { + Type *ETy = CA->getType()->getElementType(); + Out << '['; + TypePrinter.print(ETy, Out); + Out << ' '; + WriteAsOperandInternal(Out, CA->getOperand(0), + &TypePrinter, Machine, + Context); + for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { + Out << ", "; + TypePrinter.print(ETy, Out); + Out << ' '; + WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine, + Context); + } + Out << ']'; + return; + } + + if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) { // As a special case, print the array as a string if it is an array of // i8 with ConstantInt values. - // - Type *ETy = CA->getType()->getElementType(); if (CA->isString()) { Out << "c\""; PrintEscapedString(CA->getAsString(), Out); Out << '"'; - } else { // Cannot output in string format... - Out << '['; - if (CA->getNumOperands()) { - TypePrinter.print(ETy, Out); - Out << ' '; - WriteAsOperandInternal(Out, CA->getOperand(0), - &TypePrinter, Machine, - Context); - for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { - Out << ", "; - TypePrinter.print(ETy, Out); - Out << ' '; - WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine, - Context); - } - } - Out << ']'; + return; } + + Type *ETy = CA->getType()->getElementType(); + Out << '['; + TypePrinter.print(ETy, Out); + Out << ' '; + WriteAsOperandInternal(Out, CA->getElementAsConstant(0), + &TypePrinter, Machine, + Context); + for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) { + Out << ", "; + TypePrinter.print(ETy, Out); + Out << ' '; + WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter, + Machine, Context); + } + Out << ']'; return; } + if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { if (CS->getType()->isPacked()) Out << '<'; @@ -882,21 +906,19 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, return; } - if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { - Type *ETy = CP->getType()->getElementType(); - assert(CP->getNumOperands() > 0 && - "Number of operands for a PackedConst must be > 0"); + if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) { + Type *ETy = CV->getType()->getVectorElementType(); Out << '<'; TypePrinter.print(ETy, Out); Out << ' '; - WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine, - Context); - for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { + WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter, + Machine, Context); + for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){ Out << ", "; TypePrinter.print(ETy, Out); Out << ' '; - WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine, - Context); + WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter, + Machine, Context); } Out << '>'; return; @@ -1162,7 +1184,6 @@ void AssemblyWriter::writeAtomic(AtomicOrdering Ordering, return; switch (SynchScope) { - default: Out << " <bad scope " << int(SynchScope) << ">"; break; case SingleThread: Out << " singlethread"; break; case CrossThread: break; } @@ -1710,13 +1731,12 @@ void AssemblyWriter::printInstruction(const Instruction &I) { Out << ", "; writeOperand(SI.getDefaultDest(), true); Out << " ["; - // Skip the first item since that's the default case. - unsigned NumCases = SI.getNumCases(); - for (unsigned i = 1; i < NumCases; ++i) { + for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); + i != e; ++i) { Out << "\n "; - writeOperand(SI.getCaseValue(i), true); + writeOperand(i.getCaseValue(), true); Out << ", "; - writeOperand(SI.getSuccessor(i), true); + writeOperand(i.getCaseSuccessor(), true); } Out << "\n ]"; } else if (isa<IndirectBrInst>(I)) { @@ -1988,7 +2008,7 @@ static void WriteMDNodeComment(const MDNode *Node, if (!CI) return; APInt Val = CI->getValue(); APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask); - if (Val.ult(LLVMDebugVersion)) + if (Val.ult(LLVMDebugVersion11)) return; Out.PadToColumn(50); @@ -2110,3 +2130,6 @@ void Type::dump() const { print(dbgs()); } // Module::dump() - Allow printing of Modules from the debugger. void Module::dump() const { print(dbgs(), 0); } + +// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. +void NamedMDNode::dump() const { print(dbgs(), 0); } diff --git a/contrib/llvm/lib/VMCore/Attributes.cpp b/contrib/llvm/lib/VMCore/Attributes.cpp index 485be75..c05132b 100644 --- a/contrib/llvm/lib/VMCore/Attributes.cpp +++ b/contrib/llvm/lib/VMCore/Attributes.cpp @@ -76,6 +76,8 @@ std::string Attribute::getAsString(Attributes Attrs) { Result += "naked "; if (Attrs & Attribute::NonLazyBind) Result += "nonlazybind "; + if (Attrs & Attribute::AddressSafety) + Result += "address_safety "; if (Attrs & Attribute::StackAlignment) { Result += "alignstack("; Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs)); @@ -152,8 +154,10 @@ public: } static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr, unsigned NumAttrs) { - for (unsigned i = 0; i != NumAttrs; ++i) - ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index)); + for (unsigned i = 0; i != NumAttrs; ++i) { + ID.AddInteger(Attr[i].Attrs.Raw()); + ID.AddInteger(Attr[i].Index); + } } }; } diff --git a/contrib/llvm/lib/VMCore/AutoUpgrade.cpp b/contrib/llvm/lib/VMCore/AutoUpgrade.cpp index b849d3e..ea3d4ba 100644 --- a/contrib/llvm/lib/VMCore/AutoUpgrade.cpp +++ b/contrib/llvm/lib/VMCore/AutoUpgrade.cpp @@ -38,105 +38,31 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { return false; Name = Name.substr(5); // Strip off "llvm." - FunctionType *FTy = F->getFunctionType(); - Module *M = F->getParent(); - switch (Name[0]) { default: break; - case 'a': - if (Name.startswith("atomic.cmp.swap") || - Name.startswith("atomic.swap") || - Name.startswith("atomic.load.add") || - Name.startswith("atomic.load.sub") || - Name.startswith("atomic.load.and") || - Name.startswith("atomic.load.nand") || - Name.startswith("atomic.load.or") || - Name.startswith("atomic.load.xor") || - Name.startswith("atomic.load.max") || - Name.startswith("atomic.load.min") || - Name.startswith("atomic.load.umax") || - Name.startswith("atomic.load.umin")) - return true; - case 'i': - // This upgrades the old llvm.init.trampoline to the new - // llvm.init.trampoline and llvm.adjust.trampoline pair. - if (Name == "init.trampoline") { - // The new llvm.init.trampoline returns nothing. - if (FTy->getReturnType()->isVoidTy()) - break; - - assert(FTy->getNumParams() == 3 && "old init.trampoline takes 3 args!"); - - // Change the name of the old intrinsic so that we can play with its type. - std::string NameTmp = F->getName(); - F->setName(""); - NewFn = cast<Function>(M->getOrInsertFunction( - NameTmp, - Type::getVoidTy(M->getContext()), - FTy->getParamType(0), FTy->getParamType(1), - FTy->getParamType(2), (Type *)0)); + case 'c': { + if (Name.startswith("ctlz.") && F->arg_size() == 1) { + F->setName(Name + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, + F->arg_begin()->getType()); return true; } - case 'm': - if (Name == "memory.barrier") - return true; - case 'p': - // This upgrades the llvm.prefetch intrinsic to accept one more parameter, - // which is a instruction / data cache identifier. The old version only - // implicitly accepted the data version. - if (Name == "prefetch") { - // Don't do anything if it has the correct number of arguments already - if (FTy->getNumParams() == 4) - break; - - assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!"); - // We first need to change the name of the old (bad) intrinsic, because - // its type is incorrect, but we cannot overload that name. We - // arbitrarily unique it here allowing us to construct a correctly named - // and typed function below. - std::string NameTmp = F->getName(); - F->setName(""); - NewFn = cast<Function>(M->getOrInsertFunction(NameTmp, - FTy->getReturnType(), - FTy->getParamType(0), - FTy->getParamType(1), - FTy->getParamType(2), - FTy->getParamType(2), - (Type*)0)); + if (Name.startswith("cttz.") && F->arg_size() == 1) { + F->setName(Name + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, + F->arg_begin()->getType()); return true; } - break; + } case 'x': { - const char *NewFnName = NULL; - // This fixes the poorly named crc32 intrinsics. - if (Name == "x86.sse42.crc32.8") - NewFnName = "llvm.x86.sse42.crc32.32.8"; - else if (Name == "x86.sse42.crc32.16") - NewFnName = "llvm.x86.sse42.crc32.32.16"; - else if (Name == "x86.sse42.crc32.32") - NewFnName = "llvm.x86.sse42.crc32.32.32"; - else if (Name == "x86.sse42.crc64.8") - NewFnName = "llvm.x86.sse42.crc32.64.8"; - else if (Name == "x86.sse42.crc64.64") - NewFnName = "llvm.x86.sse42.crc32.64.64"; - - if (NewFnName) { - F->setName(NewFnName); - NewFn = F; + if (Name.startswith("x86.sse2.pcmpeq.") || + Name.startswith("x86.sse2.pcmpgt.") || + Name.startswith("x86.avx2.pcmpeq.") || + Name.startswith("x86.avx2.pcmpgt.")) { + NewFn = 0; return true; } - - // Calls to these instructions are transformed into unaligned loads. - if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" || - Name == "x86.sse2.loadu.pd") - return true; - - // Calls to these instructions are transformed into nontemporal stores. - if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" || - Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i") - return true; - break; } } @@ -171,188 +97,52 @@ bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { Function *F = CI->getCalledFunction(); LLVMContext &C = CI->getContext(); - ImmutableCallSite CS(CI); + IRBuilder<> Builder(C); + Builder.SetInsertPoint(CI->getParent(), CI); - assert(F && "CallInst has no function associated with it."); + assert(F && "Intrinsic call is not direct?"); if (!NewFn) { - if (F->getName() == "llvm.x86.sse.loadu.ps" || - F->getName() == "llvm.x86.sse2.loadu.dq" || - F->getName() == "llvm.x86.sse2.loadu.pd") { - // Convert to a native, unaligned load. - Type *VecTy = CI->getType(); - Type *IntTy = IntegerType::get(C, 128); - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - - Value *BC = Builder.CreateBitCast(CI->getArgOperand(0), - PointerType::getUnqual(IntTy), - "cast"); - LoadInst *LI = Builder.CreateLoad(BC, CI->getName()); - LI->setAlignment(1); // Unaligned load. - BC = Builder.CreateBitCast(LI, VecTy, "new.cast"); - - // Fix up all the uses with our new load. - if (!CI->use_empty()) - CI->replaceAllUsesWith(BC); - - // Remove intrinsic. - CI->eraseFromParent(); - } else if (F->getName() == "llvm.x86.sse.movnt.ps" || - F->getName() == "llvm.x86.sse2.movnt.dq" || - F->getName() == "llvm.x86.sse2.movnt.pd" || - F->getName() == "llvm.x86.sse2.movnt.i") { - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - - Module *M = F->getParent(); - SmallVector<Value *, 1> Elts; - Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); - MDNode *Node = MDNode::get(C, Elts); - - Value *Arg0 = CI->getArgOperand(0); - Value *Arg1 = CI->getArgOperand(1); - - // Convert the type of the pointer to a pointer to the stored type. - Value *BC = Builder.CreateBitCast(Arg0, - PointerType::getUnqual(Arg1->getType()), - "cast"); - StoreInst *SI = Builder.CreateStore(Arg1, BC); - SI->setMetadata(M->getMDKindID("nontemporal"), Node); - SI->setAlignment(16); - - // Remove intrinsic. - CI->eraseFromParent(); - } else if (F->getName().startswith("llvm.atomic.cmp.swap")) { - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - Value *Val = Builder.CreateAtomicCmpXchg(CI->getArgOperand(0), - CI->getArgOperand(1), - CI->getArgOperand(2), - Monotonic); - - // Replace intrinsic. - Val->takeName(CI); - if (!CI->use_empty()) - CI->replaceAllUsesWith(Val); - CI->eraseFromParent(); - } else if (F->getName().startswith("llvm.atomic")) { - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - - AtomicRMWInst::BinOp Op; - if (F->getName().startswith("llvm.atomic.swap")) - Op = AtomicRMWInst::Xchg; - else if (F->getName().startswith("llvm.atomic.load.add")) - Op = AtomicRMWInst::Add; - else if (F->getName().startswith("llvm.atomic.load.sub")) - Op = AtomicRMWInst::Sub; - else if (F->getName().startswith("llvm.atomic.load.and")) - Op = AtomicRMWInst::And; - else if (F->getName().startswith("llvm.atomic.load.nand")) - Op = AtomicRMWInst::Nand; - else if (F->getName().startswith("llvm.atomic.load.or")) - Op = AtomicRMWInst::Or; - else if (F->getName().startswith("llvm.atomic.load.xor")) - Op = AtomicRMWInst::Xor; - else if (F->getName().startswith("llvm.atomic.load.max")) - Op = AtomicRMWInst::Max; - else if (F->getName().startswith("llvm.atomic.load.min")) - Op = AtomicRMWInst::Min; - else if (F->getName().startswith("llvm.atomic.load.umax")) - Op = AtomicRMWInst::UMax; - else if (F->getName().startswith("llvm.atomic.load.umin")) - Op = AtomicRMWInst::UMin; - else - llvm_unreachable("Unknown atomic"); - - Value *Val = Builder.CreateAtomicRMW(Op, CI->getArgOperand(0), - CI->getArgOperand(1), - Monotonic); - - // Replace intrinsic. - Val->takeName(CI); - if (!CI->use_empty()) - CI->replaceAllUsesWith(Val); - CI->eraseFromParent(); - } else if (F->getName() == "llvm.memory.barrier") { - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - - // Note that this conversion ignores the "device" bit; it was not really - // well-defined, and got abused because nobody paid enough attention to - // get it right. In practice, this probably doesn't matter; application - // code generally doesn't need anything stronger than - // SequentiallyConsistent (and realistically, SequentiallyConsistent - // is lowered to a strong enough barrier for almost anything). - - if (cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue()) - Builder.CreateFence(SequentiallyConsistent); - else if (!cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue()) - Builder.CreateFence(Release); - else if (!cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue()) - Builder.CreateFence(Acquire); - else - Builder.CreateFence(AcquireRelease); - - // Remove intrinsic. - CI->eraseFromParent(); + // Get the Function's name. + StringRef Name = F->getName(); + + Value *Rep; + // Upgrade packed integer vector compares intrinsics to compare instructions + if (Name.startswith("llvm.x86.sse2.pcmpeq.") || + Name.startswith("llvm.x86.avx2.pcmpeq.")) { + Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), + "pcmpeq"); + // need to sign extend since icmp returns vector of i1 + Rep = Builder.CreateSExt(Rep, CI->getType(), ""); + } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || + Name.startswith("llvm.x86.avx2.pcmpgt.")) { + Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), + "pcmpgt"); + // need to sign extend since icmp returns vector of i1 + Rep = Builder.CreateSExt(Rep, CI->getType(), ""); } else { llvm_unreachable("Unknown function for CallInst upgrade."); } - return; - } - switch (NewFn->getIntrinsicID()) { - case Intrinsic::prefetch: { - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI->getParent(), CI); - llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext()); - - // Add the extra "data cache" argument - Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), - llvm::ConstantInt::get(I32Ty, 1) }; - CallInst *NewCI = CallInst::Create(NewFn, Operands, - CI->getName(), CI); - NewCI->setTailCall(CI->isTailCall()); - NewCI->setCallingConv(CI->getCallingConv()); - // Handle any uses of the old CallInst. - if (!CI->use_empty()) - // Replace all uses of the old call with the new cast which has the - // correct type. - CI->replaceAllUsesWith(NewCI); - - // Clean up the old call now that it has been completely upgraded. + CI->replaceAllUsesWith(Rep); CI->eraseFromParent(); - break; + return; } - case Intrinsic::init_trampoline: { - - // Transform - // %tramp = call i8* llvm.init.trampoline (i8* x, i8* y, i8* z) - // to - // call void llvm.init.trampoline (i8* %x, i8* %y, i8* %z) - // %tramp = call i8* llvm.adjust.trampoline (i8* %x) - - Function *AdjustTrampolineFn = - cast<Function>(Intrinsic::getDeclaration(F->getParent(), - Intrinsic::adjust_trampoline)); - - IRBuilder<> Builder(C); - Builder.SetInsertPoint(CI); - Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2)); - - CallInst *AdjustCall = Builder.CreateCall(AdjustTrampolineFn, - CI->getArgOperand(0), - CI->getName()); - if (!CI->use_empty()) - CI->replaceAllUsesWith(AdjustCall); + switch (NewFn->getIntrinsicID()) { + default: + llvm_unreachable("Unknown function for CallInst upgrade."); + + case Intrinsic::ctlz: + case Intrinsic::cttz: + assert(CI->getNumArgOperands() == 1 && + "Mismatch between function args and call args"); + StringRef Name = CI->getName(); + CI->setName(Name + ".old"); + CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), + Builder.getFalse(), Name)); CI->eraseFromParent(); - break; - } + return; } } @@ -378,291 +168,3 @@ void llvm::UpgradeCallsToIntrinsic(Function* F) { } } -/// This function strips all debug info intrinsics, except for llvm.dbg.declare. -/// If an llvm.dbg.declare intrinsic is invalid, then this function simply -/// strips that use. -void llvm::CheckDebugInfoIntrinsics(Module *M) { - if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) { - while (!FuncStart->use_empty()) - cast<CallInst>(FuncStart->use_back())->eraseFromParent(); - FuncStart->eraseFromParent(); - } - - if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) { - while (!StopPoint->use_empty()) - cast<CallInst>(StopPoint->use_back())->eraseFromParent(); - StopPoint->eraseFromParent(); - } - - if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) { - while (!RegionStart->use_empty()) - cast<CallInst>(RegionStart->use_back())->eraseFromParent(); - RegionStart->eraseFromParent(); - } - - if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) { - while (!RegionEnd->use_empty()) - cast<CallInst>(RegionEnd->use_back())->eraseFromParent(); - RegionEnd->eraseFromParent(); - } - - if (Function *Declare = M->getFunction("llvm.dbg.declare")) { - if (!Declare->use_empty()) { - DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back()); - if (!isa<MDNode>(DDI->getArgOperand(0)) || - !isa<MDNode>(DDI->getArgOperand(1))) { - while (!Declare->use_empty()) { - CallInst *CI = cast<CallInst>(Declare->use_back()); - CI->eraseFromParent(); - } - Declare->eraseFromParent(); - } - } - } -} - -/// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic -/// calls reachable from the unwind basic block. -static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn, - CallInst *&Sel, - SmallPtrSet<BasicBlock*, 8> &Visited) { - if (!Visited.insert(BB)) return; - - for (BasicBlock::iterator - I = BB->begin(), E = BB->end(); I != E; ++I) { - if (CallInst *CI = dyn_cast<CallInst>(I)) { - switch (CI->getCalledFunction()->getIntrinsicID()) { - default: break; - case Intrinsic::eh_exception: - assert(!Exn && "Found more than one eh.exception call!"); - Exn = CI; - break; - case Intrinsic::eh_selector: - assert(!Sel && "Found more than one eh.selector call!"); - Sel = CI; - break; - } - - if (Exn && Sel) return; - } - } - - if (Exn && Sel) return; - - for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { - FindExnAndSelIntrinsics(*I, Exn, Sel, Visited); - if (Exn && Sel) return; - } -} - -/// TransferClausesToLandingPadInst - Transfer the exception handling clauses -/// from the eh_selector call to the new landingpad instruction. -static void TransferClausesToLandingPadInst(LandingPadInst *LPI, - CallInst *EHSel) { - LLVMContext &Context = LPI->getContext(); - unsigned N = EHSel->getNumArgOperands(); - - for (unsigned i = N - 1; i > 1; --i) { - if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){ - unsigned FilterLength = CI->getZExtValue(); - unsigned FirstCatch = i + FilterLength + !FilterLength; - assert(FirstCatch <= N && "Invalid filter length"); - - if (FirstCatch < N) - for (unsigned j = FirstCatch; j < N; ++j) { - Value *Val = EHSel->getArgOperand(j); - if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") { - LPI->addClause(EHSel->getArgOperand(j)); - } else { - GlobalVariable *GV = cast<GlobalVariable>(Val); - LPI->addClause(GV->getInitializer()); - } - } - - if (!FilterLength) { - // Cleanup. - LPI->setCleanup(true); - } else { - // Filter. - SmallVector<Constant *, 4> TyInfo; - TyInfo.reserve(FilterLength - 1); - for (unsigned j = i + 1; j < FirstCatch; ++j) - TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j))); - ArrayType *AType = - ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() : - PointerType::getUnqual(Type::getInt8Ty(Context)), - TyInfo.size()); - LPI->addClause(ConstantArray::get(AType, TyInfo)); - } - - N = i; - } - } - - if (N > 2) - for (unsigned j = 2; j < N; ++j) { - Value *Val = EHSel->getArgOperand(j); - if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") { - LPI->addClause(EHSel->getArgOperand(j)); - } else { - GlobalVariable *GV = cast<GlobalVariable>(Val); - LPI->addClause(GV->getInitializer()); - } - } -} - -/// This function upgrades the old pre-3.0 exception handling system to the new -/// one. N.B. This will be removed in 3.1. -void llvm::UpgradeExceptionHandling(Module *M) { - Function *EHException = M->getFunction("llvm.eh.exception"); - Function *EHSelector = M->getFunction("llvm.eh.selector"); - if (!EHException || !EHSelector) - return; - - LLVMContext &Context = M->getContext(); - Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context)); - Type *SelTy = Type::getInt32Ty(Context); - Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL); - - // This map links the invoke instruction with the eh.exception and eh.selector - // calls associated with it. - DenseMap<InvokeInst*, std::pair<Value*, Value*> > InvokeToIntrinsicsMap; - for (Module::iterator - I = M->begin(), E = M->end(); I != E; ++I) { - Function &F = *I; - - for (Function::iterator - II = F.begin(), IE = F.end(); II != IE; ++II) { - BasicBlock *BB = &*II; - InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator()); - if (!Inst) continue; - BasicBlock *UnwindDest = Inst->getUnwindDest(); - if (UnwindDest->isLandingPad()) continue; // Already converted. - - SmallPtrSet<BasicBlock*, 8> Visited; - CallInst *Exn = 0; - CallInst *Sel = 0; - FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited); - assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!"); - InvokeToIntrinsicsMap[Inst] = std::make_pair(Exn, Sel); - } - } - - // This map stores the slots where the exception object and selector value are - // stored within a function. - DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap; - SmallPtrSet<Instruction*, 32> DeadInsts; - for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator - I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end(); - I != E; ++I) { - InvokeInst *Invoke = I->first; - BasicBlock *UnwindDest = Invoke->getUnwindDest(); - Function *F = UnwindDest->getParent(); - std::pair<Value*, Value*> EHIntrinsics = I->second; - CallInst *Exn = cast<CallInst>(EHIntrinsics.first); - CallInst *Sel = cast<CallInst>(EHIntrinsics.second); - - // Store the exception object and selector value in the entry block. - Value *ExnSlot = 0; - Value *SelSlot = 0; - if (!FnToLPadSlotMap[F].first) { - BasicBlock *Entry = &F->front(); - ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator()); - SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator()); - FnToLPadSlotMap[F] = std::make_pair(ExnSlot, SelSlot); - } else { - ExnSlot = FnToLPadSlotMap[F].first; - SelSlot = FnToLPadSlotMap[F].second; - } - - if (!UnwindDest->getSinglePredecessor()) { - // The unwind destination doesn't have a single predecessor. Create an - // unwind destination which has only one predecessor. - BasicBlock *NewBB = BasicBlock::Create(Context, "new.lpad", - UnwindDest->getParent()); - BranchInst::Create(UnwindDest, NewBB); - Invoke->setUnwindDest(NewBB); - - // Fix up any PHIs in the original unwind destination block. - for (BasicBlock::iterator - II = UnwindDest->begin(); isa<PHINode>(II); ++II) { - PHINode *PN = cast<PHINode>(II); - int Idx = PN->getBasicBlockIndex(Invoke->getParent()); - if (Idx == -1) continue; - PN->setIncomingBlock(Idx, NewBB); - } - - UnwindDest = NewBB; - } - - IRBuilder<> Builder(Context); - Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt()); - - Value *PersFn = Sel->getArgOperand(1); - LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0); - Value *LPExn = Builder.CreateExtractValue(LPI, 0); - Value *LPSel = Builder.CreateExtractValue(LPI, 1); - Builder.CreateStore(LPExn, ExnSlot); - Builder.CreateStore(LPSel, SelSlot); - - TransferClausesToLandingPadInst(LPI, Sel); - - DeadInsts.insert(Exn); - DeadInsts.insert(Sel); - } - - // Replace the old intrinsic calls with the values from the landingpad - // instruction(s). These values were stored in allocas for us to use here. - for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator - I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end(); - I != E; ++I) { - std::pair<Value*, Value*> EHIntrinsics = I->second; - CallInst *Exn = cast<CallInst>(EHIntrinsics.first); - CallInst *Sel = cast<CallInst>(EHIntrinsics.second); - BasicBlock *Parent = Exn->getParent(); - - std::pair<Value*,Value*> ExnSelSlots = FnToLPadSlotMap[Parent->getParent()]; - - IRBuilder<> Builder(Context); - Builder.SetInsertPoint(Parent, Exn); - LoadInst *LPExn = Builder.CreateLoad(ExnSelSlots.first, "exn.load"); - LoadInst *LPSel = Builder.CreateLoad(ExnSelSlots.second, "sel.load"); - - Exn->replaceAllUsesWith(LPExn); - Sel->replaceAllUsesWith(LPSel); - } - - // Remove the dead instructions. - for (SmallPtrSet<Instruction*, 32>::iterator - I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) { - Instruction *Inst = *I; - Inst->eraseFromParent(); - } - - // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the - // exception and selector values from the stored place. - Function *EHResume = M->getFunction("llvm.eh.resume"); - if (!EHResume) return; - - while (!EHResume->use_empty()) { - CallInst *Resume = cast<CallInst>(EHResume->use_back()); - BasicBlock *BB = Resume->getParent(); - - IRBuilder<> Builder(Context); - Builder.SetInsertPoint(BB, Resume); - - Value *LPadVal = - Builder.CreateInsertValue(UndefValue::get(LPadSlotTy), - Resume->getArgOperand(0), 0, "lpad.val"); - LPadVal = Builder.CreateInsertValue(LPadVal, Resume->getArgOperand(1), - 1, "lpad.val"); - Builder.CreateResume(LPadVal); - - // Remove all instructions after the 'resume.' - BasicBlock::iterator I = Resume; - while (I != BB->end()) { - Instruction *Inst = &*I++; - Inst->eraseFromParent(); - } - } -} diff --git a/contrib/llvm/lib/VMCore/BasicBlock.cpp b/contrib/llvm/lib/VMCore/BasicBlock.cpp index d0aa275..d353b0a 100644 --- a/contrib/llvm/lib/VMCore/BasicBlock.cpp +++ b/contrib/llvm/lib/VMCore/BasicBlock.cpp @@ -366,3 +366,6 @@ bool BasicBlock::isLandingPad() const { LandingPadInst *BasicBlock::getLandingPadInst() { return dyn_cast<LandingPadInst>(getFirstNonPHI()); } +const LandingPadInst *BasicBlock::getLandingPadInst() const { + return dyn_cast<LandingPadInst>(getFirstNonPHI()); +} diff --git a/contrib/llvm/lib/VMCore/ConstantFold.cpp b/contrib/llvm/lib/VMCore/ConstantFold.cpp index 30bae71..b743287 100644 --- a/contrib/llvm/lib/VMCore/ConstantFold.cpp +++ b/contrib/llvm/lib/VMCore/ConstantFold.cpp @@ -38,11 +38,10 @@ using namespace llvm; // ConstantFold*Instruction Implementations //===----------------------------------------------------------------------===// -/// BitCastConstantVector - Convert the specified ConstantVector node to the +/// BitCastConstantVector - Convert the specified vector Constant node to the /// specified vector type. At this point, we know that the elements of the /// input vector constant are all simple integer or FP values. -static Constant *BitCastConstantVector(ConstantVector *CV, - VectorType *DstTy) { +static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) { if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy); if (CV->isNullValue()) return Constant::getNullValue(DstTy); @@ -51,22 +50,21 @@ static Constant *BitCastConstantVector(ConstantVector *CV, // doing so requires endianness information. This should be handled by // Analysis/ConstantFolding.cpp unsigned NumElts = DstTy->getNumElements(); - if (NumElts != CV->getNumOperands()) + if (NumElts != CV->getType()->getVectorNumElements()) return 0; + + Type *DstEltTy = DstTy->getElementType(); // Check to verify that all elements of the input are simple. + SmallVector<Constant*, 16> Result; for (unsigned i = 0; i != NumElts; ++i) { - if (!isa<ConstantInt>(CV->getOperand(i)) && - !isa<ConstantFP>(CV->getOperand(i))) - return 0; + Constant *C = CV->getAggregateElement(i); + if (C == 0) return 0; + C = ConstantExpr::getBitCast(C, DstEltTy); + if (isa<ConstantExpr>(C)) return 0; + Result.push_back(C); } - // Bitcast each element now. - std::vector<Constant*> Result; - Type *DstEltTy = DstTy->getElementType(); - for (unsigned i = 0; i != NumElts; ++i) - Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), - DstEltTy)); return ConstantVector::get(Result); } @@ -104,7 +102,8 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) { // the first element. If so, return the appropriate GEP instruction. if (PointerType *PTy = dyn_cast<PointerType>(V->getType())) if (PointerType *DPTy = dyn_cast<PointerType>(DestTy)) - if (PTy->getAddressSpace() == DPTy->getAddressSpace()) { + if (PTy->getAddressSpace() == DPTy->getAddressSpace() + && DPTy->getElementType()->isSized()) { SmallVector<Value*, 8> IdxList; Value *Zero = Constant::getNullValue(Type::getInt32Ty(DPTy->getContext())); @@ -141,8 +140,8 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) { if (isa<ConstantAggregateZero>(V)) return Constant::getNullValue(DestTy); - if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) - return BitCastConstantVector(CV, DestPTy); + // Handle ConstantVector and ConstantAggregateVector. + return BitCastConstantVector(V, DestPTy); } // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts @@ -548,18 +547,17 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, // If the cast operand is a constant vector, perform the cast by // operating on each element. In the cast of bitcasts, the element // count may be mismatched; don't attempt to handle that here. - if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) - if (DestTy->isVectorTy() && - cast<VectorType>(DestTy)->getNumElements() == - CV->getType()->getNumElements()) { - std::vector<Constant*> res; - VectorType *DestVecTy = cast<VectorType>(DestTy); - Type *DstEltTy = DestVecTy->getElementType(); - for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) - res.push_back(ConstantExpr::getCast(opc, - CV->getOperand(i), DstEltTy)); - return ConstantVector::get(res); - } + if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) && + DestTy->isVectorTy() && + DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) { + SmallVector<Constant*, 16> res; + VectorType *DestVecTy = cast<VectorType>(DestTy); + Type *DstEltTy = DestVecTy->getElementType(); + for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) + res.push_back(ConstantExpr::getCast(opc, + V->getAggregateElement(i), DstEltTy)); + return ConstantVector::get(res); + } // We actually have to do a cast now. Perform the cast according to the // opcode specified. @@ -571,7 +569,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { bool ignored; APFloat Val = FPC->getValueAPF(); - Val.convert(DestTy->isFloatTy() ? APFloat::IEEEsingle : + Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf : + DestTy->isFloatTy() ? APFloat::IEEEsingle : DestTy->isDoubleTy() ? APFloat::IEEEdouble : DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended : DestTy->isFP128Ty() ? APFloat::IEEEquad : @@ -690,45 +689,27 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2) { - if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) - return CB->getZExtValue() ? V1 : V2; - - // Check for zero aggregate and ConstantVector of zeros + // Check for i1 and vector true/false conditions. if (Cond->isNullValue()) return V2; - - if (ConstantVector* CondV = dyn_cast<ConstantVector>(Cond)) { - - if (CondV->isAllOnesValue()) return V1; - - VectorType *VTy = cast<VectorType>(V1->getType()); - ConstantVector *CP1 = dyn_cast<ConstantVector>(V1); - ConstantVector *CP2 = dyn_cast<ConstantVector>(V2); - - if ((CP1 || isa<ConstantAggregateZero>(V1)) && - (CP2 || isa<ConstantAggregateZero>(V2))) { - - // Find the element type of the returned vector - Type *EltTy = VTy->getElementType(); - unsigned NumElem = VTy->getNumElements(); - std::vector<Constant*> Res(NumElem); - - bool Valid = true; - for (unsigned i = 0; i < NumElem; ++i) { - ConstantInt* c = dyn_cast<ConstantInt>(CondV->getOperand(i)); - if (!c) { - Valid = false; - break; - } - Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res[i] = c->getZExtValue() ? C1 : C2; - } - // If we were able to build the vector, return it - if (Valid) return ConstantVector::get(Res); + if (Cond->isAllOnesValue()) return V1; + + // If the condition is a vector constant, fold the result elementwise. + if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { + SmallVector<Constant*, 16> Result; + for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ + ConstantInt *Cond = dyn_cast<ConstantInt>(CondV->getOperand(i)); + if (Cond == 0) break; + + Constant *Res = (Cond->getZExtValue() ? V2 : V1)->getAggregateElement(i); + if (Res == 0) break; + Result.push_back(Res); } + + // If we were able to build the vector, return it. + if (Result.size() == V1->getType()->getVectorNumElements()) + return ConstantVector::get(Result); } - if (isa<UndefValue>(Cond)) { if (isa<UndefValue>(V1)) return V1; return V2; @@ -754,22 +735,19 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx) { if (isa<UndefValue>(Val)) // ee(undef, x) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); + return UndefValue::get(Val->getType()->getVectorElementType()); if (Val->isNullValue()) // ee(zero, x) -> zero - return Constant::getNullValue( - cast<VectorType>(Val->getType())->getElementType()); - - if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { - if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { - uint64_t Index = CIdx->getZExtValue(); - if (Index >= CVal->getNumOperands()) - // ee({w,x,y,z}, wrong_value) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); - return CVal->getOperand(CIdx->getZExtValue()); - } else if (isa<UndefValue>(Idx)) { - // ee({w,x,y,z}, undef) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); - } + return Constant::getNullValue(Val->getType()->getVectorElementType()); + // ee({w,x,y,z}, undef) -> undef + if (isa<UndefValue>(Idx)) + return UndefValue::get(Val->getType()->getVectorElementType()); + + if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { + uint64_t Index = CIdx->getZExtValue(); + // ee({w,x,y,z}, wrong_value) -> undef + if (Index >= Val->getType()->getVectorNumElements()) + return UndefValue::get(Val->getType()->getVectorElementType()); + return Val->getAggregateElement(Index); } return 0; } @@ -779,103 +757,55 @@ Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, Constant *Idx) { ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); if (!CIdx) return 0; - APInt idxVal = CIdx->getValue(); - if (isa<UndefValue>(Val)) { - // Insertion of scalar constant into vector undef - // Optimize away insertion of undef - if (isa<UndefValue>(Elt)) - return Val; - // Otherwise break the aggregate undef into multiple undefs and do - // the insertion - unsigned numOps = - cast<VectorType>(Val->getType())->getNumElements(); - std::vector<Constant*> Ops; - Ops.reserve(numOps); - for (unsigned i = 0; i < numOps; ++i) { - Constant *Op = - (idxVal == i) ? Elt : UndefValue::get(Elt->getType()); - Ops.push_back(Op); - } - return ConstantVector::get(Ops); - } - if (isa<ConstantAggregateZero>(Val)) { - // Insertion of scalar constant into vector aggregate zero - // Optimize away insertion of zero - if (Elt->isNullValue()) - return Val; - // Otherwise break the aggregate zero into multiple zeros and do - // the insertion - unsigned numOps = - cast<VectorType>(Val->getType())->getNumElements(); - std::vector<Constant*> Ops; - Ops.reserve(numOps); - for (unsigned i = 0; i < numOps; ++i) { - Constant *Op = - (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType()); - Ops.push_back(Op); - } - return ConstantVector::get(Ops); - } - if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { - // Insertion of scalar constant into vector constant - std::vector<Constant*> Ops; - Ops.reserve(CVal->getNumOperands()); - for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { - Constant *Op = - (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i)); - Ops.push_back(Op); + const APInt &IdxVal = CIdx->getValue(); + + SmallVector<Constant*, 16> Result; + for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){ + if (i == IdxVal) { + Result.push_back(Elt); + continue; } - return ConstantVector::get(Ops); + + if (Constant *C = Val->getAggregateElement(i)) + Result.push_back(C); + else + return 0; } - - return 0; -} - -/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef -/// return the specified element value. Otherwise return null. -static Constant *GetVectorElement(Constant *C, unsigned EltNo) { - if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) - return CV->getOperand(EltNo); - - Type *EltTy = cast<VectorType>(C->getType())->getElementType(); - if (isa<ConstantAggregateZero>(C)) - return Constant::getNullValue(EltTy); - if (isa<UndefValue>(C)) - return UndefValue::get(EltTy); - return 0; + + return ConstantVector::get(Result); } Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, Constant *Mask) { + unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); + Type *EltTy = V1->getType()->getVectorElementType(); + // Undefined shuffle mask -> undefined value. - if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType()); + if (isa<UndefValue>(Mask)) + return UndefValue::get(VectorType::get(EltTy, MaskNumElts)); - unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements(); - unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements(); - Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); + // Don't break the bitcode reader hack. + if (isa<ConstantExpr>(Mask)) return 0; + + unsigned SrcNumElts = V1->getType()->getVectorNumElements(); // Loop over the shuffle mask, evaluating each element. SmallVector<Constant*, 32> Result; for (unsigned i = 0; i != MaskNumElts; ++i) { - Constant *InElt = GetVectorElement(Mask, i); - if (InElt == 0) return 0; - - if (isa<UndefValue>(InElt)) - InElt = UndefValue::get(EltTy); - else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) { - unsigned Elt = CI->getZExtValue(); - if (Elt >= SrcNumElts*2) - InElt = UndefValue::get(EltTy); - else if (Elt >= SrcNumElts) - InElt = GetVectorElement(V2, Elt - SrcNumElts); - else - InElt = GetVectorElement(V1, Elt); - if (InElt == 0) return 0; - } else { - // Unknown value. - return 0; + int Elt = ShuffleVectorInst::getMaskValue(Mask, i); + if (Elt == -1) { + Result.push_back(UndefValue::get(EltTy)); + continue; } + Constant *InElt; + if (unsigned(Elt) >= SrcNumElts*2) + InElt = UndefValue::get(EltTy); + else if (unsigned(Elt) >= SrcNumElts) + InElt = V2->getAggregateElement(Elt - SrcNumElts); + else + InElt = V1->getAggregateElement(Elt); + if (InElt == 0) return 0; Result.push_back(InElt); } @@ -888,26 +818,10 @@ Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, if (Idxs.empty()) return Agg; - if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef - return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(), - Idxs)); - - if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0 - return - Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(), - Idxs)); - - // Otherwise recurse. - if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) - return ConstantFoldExtractValueInstruction(CS->getOperand(Idxs[0]), - Idxs.slice(1)); - - if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) - return ConstantFoldExtractValueInstruction(CA->getOperand(Idxs[0]), - Idxs.slice(1)); - ConstantVector *CV = cast<ConstantVector>(Agg); - return ConstantFoldExtractValueInstruction(CV->getOperand(Idxs[0]), - Idxs.slice(1)); + if (Constant *C = Agg->getAggregateElement(Idxs[0])) + return ConstantFoldExtractValueInstruction(C, Idxs.slice(1)); + + return 0; } Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, @@ -917,84 +831,30 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, if (Idxs.empty()) return Val; - if (isa<UndefValue>(Agg)) { - // Insertion of constant into aggregate undef - // Optimize away insertion of undef. - if (isa<UndefValue>(Val)) - return Agg; - - // Otherwise break the aggregate undef into multiple undefs and do - // the insertion. - CompositeType *AggTy = cast<CompositeType>(Agg->getType()); - unsigned numOps; - if (ArrayType *AR = dyn_cast<ArrayType>(AggTy)) - numOps = AR->getNumElements(); - else - numOps = cast<StructType>(AggTy)->getNumElements(); - - std::vector<Constant*> Ops(numOps); - for (unsigned i = 0; i < numOps; ++i) { - Type *MemberTy = AggTy->getTypeAtIndex(i); - Constant *Op = - (Idxs[0] == i) ? - ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy), - Val, Idxs.slice(1)) : - UndefValue::get(MemberTy); - Ops[i] = Op; - } - - if (StructType* ST = dyn_cast<StructType>(AggTy)) - return ConstantStruct::get(ST, Ops); - return ConstantArray::get(cast<ArrayType>(AggTy), Ops); - } + unsigned NumElts; + if (StructType *ST = dyn_cast<StructType>(Agg->getType())) + NumElts = ST->getNumElements(); + else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType())) + NumElts = AT->getNumElements(); + else + NumElts = AT->getVectorNumElements(); - if (isa<ConstantAggregateZero>(Agg)) { - // Insertion of constant into aggregate zero - // Optimize away insertion of zero. - if (Val->isNullValue()) - return Agg; - - // Otherwise break the aggregate zero into multiple zeros and do - // the insertion. - CompositeType *AggTy = cast<CompositeType>(Agg->getType()); - unsigned numOps; - if (ArrayType *AR = dyn_cast<ArrayType>(AggTy)) - numOps = AR->getNumElements(); - else - numOps = cast<StructType>(AggTy)->getNumElements(); + SmallVector<Constant*, 32> Result; + for (unsigned i = 0; i != NumElts; ++i) { + Constant *C = Agg->getAggregateElement(i); + if (C == 0) return 0; - std::vector<Constant*> Ops(numOps); - for (unsigned i = 0; i < numOps; ++i) { - Type *MemberTy = AggTy->getTypeAtIndex(i); - Constant *Op = - (Idxs[0] == i) ? - ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy), - Val, Idxs.slice(1)) : - Constant::getNullValue(MemberTy); - Ops[i] = Op; - } + if (Idxs[0] == i) + C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1)); - if (StructType *ST = dyn_cast<StructType>(AggTy)) - return ConstantStruct::get(ST, Ops); - return ConstantArray::get(cast<ArrayType>(AggTy), Ops); + Result.push_back(C); } - if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) { - // Insertion of constant into aggregate constant. - std::vector<Constant*> Ops(Agg->getNumOperands()); - for (unsigned i = 0; i < Agg->getNumOperands(); ++i) { - Constant *Op = cast<Constant>(Agg->getOperand(i)); - if (Idxs[0] == i) - Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs.slice(1)); - Ops[i] = Op; - } - - if (StructType* ST = dyn_cast<StructType>(Agg->getType())) - return ConstantStruct::get(ST, Ops); - return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops); - } - - return 0; + if (StructType *ST = dyn_cast<StructType>(Agg->getType())) + return ConstantStruct::get(ST, Result); + if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType())) + return ConstantArray::get(AT, Result); + return ConstantVector::get(Result); } @@ -1172,7 +1032,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // At this point we know neither constant is an UndefValue. if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { - using namespace APIntOps; const APInt &C1V = CI1->getValue(); const APInt &C2V = CI2->getValue(); switch (Opcode) { @@ -1269,145 +1128,18 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, } } } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { - ConstantVector *CP1 = dyn_cast<ConstantVector>(C1); - ConstantVector *CP2 = dyn_cast<ConstantVector>(C2); - if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) && - (CP2 != NULL || isa<ConstantAggregateZero>(C2))) { - std::vector<Constant*> Res; - Type* EltTy = VTy->getElementType(); - Constant *C1 = 0; - Constant *C2 = 0; - switch (Opcode) { - default: - break; - case Instruction::Add: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getAdd(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::FAdd: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getFAdd(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::Sub: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getSub(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::FSub: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getFSub(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::Mul: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getMul(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::FMul: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getFMul(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::UDiv: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getUDiv(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::SDiv: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getSDiv(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::FDiv: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getFDiv(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::URem: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getURem(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::SRem: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getSRem(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::FRem: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getFRem(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::And: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getAnd(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::Or: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getOr(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::Xor: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getXor(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::LShr: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getLShr(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::AShr: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getAShr(C1, C2)); - } - return ConstantVector::get(Res); - case Instruction::Shl: - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); - C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); - Res.push_back(ConstantExpr::getShl(C1, C2)); - } - return ConstantVector::get(Res); - } + // Perform elementwise folding. + SmallVector<Constant*, 16> Result; + for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { + Constant *LHS = C1->getAggregateElement(i); + Constant *RHS = C2->getAggregateElement(i); + if (LHS == 0 || RHS == 0) break; + + Result.push_back(ConstantExpr::get(Opcode, LHS, RHS)); } + + if (Result.size() == VTy->getNumElements()) + return ConstantVector::get(Result); } if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { @@ -1906,7 +1638,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, APInt V1 = cast<ConstantInt>(C1)->getValue(); APInt V2 = cast<ConstantInt>(C2)->getValue(); switch (pred) { - default: llvm_unreachable("Invalid ICmp Predicate"); return 0; + default: llvm_unreachable("Invalid ICmp Predicate"); case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2); case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2); case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2)); @@ -1923,7 +1655,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, APFloat C2V = cast<ConstantFP>(C2)->getValueAPF(); APFloat::cmpResult R = C1V.compare(C2V); switch (pred) { - default: llvm_unreachable("Invalid FCmp Predicate"); return 0; + default: llvm_unreachable("Invalid FCmp Predicate"); case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy); case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy); case FCmpInst::FCMP_UNO: @@ -1962,20 +1694,20 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, R==APFloat::cmpEqual); } } else if (C1->getType()->isVectorTy()) { - SmallVector<Constant*, 16> C1Elts, C2Elts; - C1->getVectorElements(C1Elts); - C2->getVectorElements(C2Elts); - if (C1Elts.empty() || C2Elts.empty()) - return 0; - // If we can constant fold the comparison of each element, constant fold // the whole vector comparison. SmallVector<Constant*, 4> ResElts; // Compare the elements, producing an i1 result or constant expr. - for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) - ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i])); - - return ConstantVector::get(ResElts); + for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){ + Constant *C1E = C1->getAggregateElement(i); + Constant *C2E = C2->getAggregateElement(i); + if (C1E == 0 || C2E == 0) break; + + ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E)); + } + + if (ResElts.size() == C1->getType()->getVectorNumElements()) + return ConstantVector::get(ResElts); } if (C1->getType()->isFloatingPointTy()) { @@ -2209,7 +1941,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, I != E; ++I) LastTy = *I; - if ((LastTy && LastTy->isArrayTy()) || Idx0->isNullValue()) { + if ((LastTy && isa<SequentialType>(LastTy)) || Idx0->isNullValue()) { SmallVector<Value*, 16> NewIndices; NewIndices.reserve(Idxs.size() + CE->getNumOperands()); for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) diff --git a/contrib/llvm/lib/VMCore/Constants.cpp b/contrib/llvm/lib/VMCore/Constants.cpp index a84a046..6dbc144 100644 --- a/contrib/llvm/lib/VMCore/Constants.cpp +++ b/contrib/llvm/lib/VMCore/Constants.cpp @@ -40,6 +40,8 @@ using namespace llvm; // Constant Class //===----------------------------------------------------------------------===// +void Constant::anchor() { } + bool Constant::isNegativeZeroValue() const { // Floating point values have an explicit -0.0 value. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) @@ -71,17 +73,27 @@ bool Constant::isAllOnesValue() const { if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue(); - // Check for constant vectors + // Check for constant vectors which are splats of -1 values. if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) - return CV->isAllOnesValue(); + if (Constant *Splat = CV->getSplatValue()) + return Splat->isAllOnesValue(); + + // Check for constant vectors which are splats of -1 values. + if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) + if (Constant *Splat = CV->getSplatValue()) + return Splat->isAllOnesValue(); return false; } + // Constructor to create a '0' constant of arbitrary type... Constant *Constant::getNullValue(Type *Ty) { switch (Ty->getTypeID()) { case Type::IntegerTyID: return ConstantInt::get(Ty, 0); + case Type::HalfTyID: + return ConstantFP::get(Ty->getContext(), + APFloat::getZero(APFloat::IEEEhalf)); case Type::FloatTyID: return ConstantFP::get(Ty->getContext(), APFloat::getZero(APFloat::IEEEsingle)); @@ -105,8 +117,7 @@ Constant *Constant::getNullValue(Type *Ty) { return ConstantAggregateZero::get(Ty); default: // Function, Label, or Opaque type? - assert(0 && "Cannot create a null constant of that type!"); - return 0; + llvm_unreachable("Cannot create a null constant of that type!"); } } @@ -122,7 +133,7 @@ Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { // Broadcast a scalar to a vector, if necessary. if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); + C = ConstantVector::getSplat(VTy->getNumElements(), C); return C; } @@ -138,13 +149,44 @@ Constant *Constant::getAllOnesValue(Type *Ty) { return ConstantFP::get(Ty->getContext(), FL); } - SmallVector<Constant*, 16> Elts; VectorType *VTy = cast<VectorType>(Ty); - Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); - assert(Elts[0] && "Invalid AllOnes value!"); - return cast<ConstantVector>(ConstantVector::get(Elts)); + return ConstantVector::getSplat(VTy->getNumElements(), + getAllOnesValue(VTy->getElementType())); } +/// getAggregateElement - For aggregates (struct/array/vector) return the +/// constant that corresponds to the specified element if possible, or null if +/// not. This can return null if the element index is a ConstantExpr, or if +/// 'this' is a constant expr. +Constant *Constant::getAggregateElement(unsigned Elt) const { + if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(this)) + return Elt < CS->getNumOperands() ? CS->getOperand(Elt) : 0; + + if (const ConstantArray *CA = dyn_cast<ConstantArray>(this)) + return Elt < CA->getNumOperands() ? CA->getOperand(Elt) : 0; + + if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) + return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : 0; + + if (const ConstantAggregateZero *CAZ =dyn_cast<ConstantAggregateZero>(this)) + return CAZ->getElementValue(Elt); + + if (const UndefValue *UV = dyn_cast<UndefValue>(this)) + return UV->getElementValue(Elt); + + if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this)) + return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) : 0; + return 0; +} + +Constant *Constant::getAggregateElement(Constant *Elt) const { + assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer"); + if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) + return getAggregateElement(CI->getZExtValue()); + return 0; +} + + void Constant::destroyConstantImpl() { // When a Constant is destroyed, there may be lingering // references to the constant by other constants in the constant pool. These @@ -163,8 +205,7 @@ void Constant::destroyConstantImpl() { } #endif assert(isa<Constant>(V) && "References remain to Constant being destroyed"); - Constant *CV = cast<Constant>(V); - CV->destroyConstant(); + cast<Constant>(V)->destroyConstant(); // The constant should remove itself from our use list... assert((use_empty() || use_back() != V) && "Constant not removed!"); @@ -270,36 +311,6 @@ Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { return Result; } - -/// getVectorElements - This method, which is only valid on constant of vector -/// type, returns the elements of the vector in the specified smallvector. -/// This handles breaking down a vector undef into undef elements, etc. For -/// constant exprs and other cases we can't handle, we return an empty vector. -void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const { - assert(getType()->isVectorTy() && "Not a vector constant!"); - - if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { - for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) - Elts.push_back(CV->getOperand(i)); - return; - } - - VectorType *VT = cast<VectorType>(getType()); - if (isa<ConstantAggregateZero>(this)) { - Elts.assign(VT->getNumElements(), - Constant::getNullValue(VT->getElementType())); - return; - } - - if (isa<UndefValue>(this)) { - Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType())); - return; - } - - // Unknown type, must be constant expr etc. -} - - /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove /// it. This involves recursively eliminating any dead users of the /// constantexpr. @@ -358,6 +369,8 @@ void Constant::removeDeadConstantUsers() const { // ConstantInt //===----------------------------------------------------------------------===// +void ConstantInt::anchor() { } + ConstantInt::ConstantInt(IntegerType *Ty, const APInt& V) : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); @@ -385,9 +398,8 @@ Constant *ConstantInt::getTrue(Type *Ty) { } assert(VTy->getElementType()->isIntegerTy(1) && "True must be vector of i1 or i1."); - SmallVector<Constant*, 16> Splat(VTy->getNumElements(), - ConstantInt::getTrue(Ty->getContext())); - return ConstantVector::get(Splat); + return ConstantVector::getSplat(VTy->getNumElements(), + ConstantInt::getTrue(Ty->getContext())); } Constant *ConstantInt::getFalse(Type *Ty) { @@ -398,9 +410,8 @@ Constant *ConstantInt::getFalse(Type *Ty) { } assert(VTy->getElementType()->isIntegerTy(1) && "False must be vector of i1 or i1."); - SmallVector<Constant*, 16> Splat(VTy->getNumElements(), - ConstantInt::getFalse(Ty->getContext())); - return ConstantVector::get(Splat); + return ConstantVector::getSplat(VTy->getNumElements(), + ConstantInt::getFalse(Ty->getContext())); } @@ -424,18 +435,17 @@ Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - return ConstantVector::get(SmallVector<Constant*, - 16>(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } -ConstantInt* ConstantInt::get(IntegerType* Ty, uint64_t V, +ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) { return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); } -ConstantInt* ConstantInt::getSigned(IntegerType* Ty, int64_t V) { +ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) { return get(Ty, V, true); } @@ -443,20 +453,19 @@ Constant *ConstantInt::getSigned(Type *Ty, int64_t V) { return get(Ty, V, true); } -Constant *ConstantInt::get(Type* Ty, const APInt& V) { +Constant *ConstantInt::get(Type *Ty, const APInt& V) { ConstantInt *C = get(Ty->getContext(), V); assert(C->getType() == Ty->getScalarType() && "ConstantInt type doesn't match the type implied by its value!"); // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - return ConstantVector::get( - SmallVector<Constant *, 16>(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } -ConstantInt* ConstantInt::get(IntegerType* Ty, StringRef Str, +ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) { return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); } @@ -466,6 +475,8 @@ ConstantInt* ConstantInt::get(IntegerType* Ty, StringRef Str, //===----------------------------------------------------------------------===// static const fltSemantics *TypeToFloatSemantics(Type *Ty) { + if (Ty->isHalfTy()) + return &APFloat::IEEEhalf; if (Ty->isFloatTy()) return &APFloat::IEEEsingle; if (Ty->isDoubleTy()) @@ -479,10 +490,12 @@ static const fltSemantics *TypeToFloatSemantics(Type *Ty) { return &APFloat::PPCDoubleDouble; } +void ConstantFP::anchor() { } + /// get() - This returns a constant fp for the specified value in the /// specified type. This should only be used for simple constant values like /// 2.0/1.0 etc, that are known-valid both as double and as the target format. -Constant *ConstantFP::get(Type* Ty, double V) { +Constant *ConstantFP::get(Type *Ty, double V) { LLVMContext &Context = Ty->getContext(); APFloat FV(V); @@ -493,14 +506,13 @@ Constant *ConstantFP::get(Type* Ty, double V) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - return ConstantVector::get( - SmallVector<Constant *, 16>(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } -Constant *ConstantFP::get(Type* Ty, StringRef Str) { +Constant *ConstantFP::get(Type *Ty, StringRef Str) { LLVMContext &Context = Ty->getContext(); APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str); @@ -508,31 +520,28 @@ Constant *ConstantFP::get(Type* Ty, StringRef Str) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - return ConstantVector::get( - SmallVector<Constant *, 16>(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } -ConstantFP* ConstantFP::getNegativeZero(Type* Ty) { +ConstantFP *ConstantFP::getNegativeZero(Type *Ty) { LLVMContext &Context = Ty->getContext(); - APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); + APFloat apf = cast<ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); apf.changeSign(); return get(Context, apf); } -Constant *ConstantFP::getZeroValueForNegation(Type* Ty) { - if (VectorType *PTy = dyn_cast<VectorType>(Ty)) - if (PTy->getElementType()->isFloatingPointTy()) { - SmallVector<Constant*, 16> zeros(PTy->getNumElements(), - getNegativeZero(PTy->getElementType())); - return ConstantVector::get(zeros); - } - - if (Ty->isFloatingPointTy()) - return getNegativeZero(Ty); +Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { + Type *ScalarTy = Ty->getScalarType(); + if (ScalarTy->isFloatingPointTy()) { + Constant *C = getNegativeZero(ScalarTy); + if (VectorType *VTy = dyn_cast<VectorType>(Ty)) + return ConstantVector::getSplat(VTy->getNumElements(), C); + return C; + } return Constant::getNullValue(Ty); } @@ -548,7 +557,9 @@ ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { if (!Slot) { Type *Ty; - if (&V.getSemantics() == &APFloat::IEEEsingle) + if (&V.getSemantics() == &APFloat::IEEEhalf) + Ty = Type::getHalfTy(Context); + else if (&V.getSemantics() == &APFloat::IEEEsingle) Ty = Type::getFloatTy(Context); else if (&V.getSemantics() == &APFloat::IEEEdouble) Ty = Type::getDoubleTy(Context); @@ -584,9 +595,83 @@ bool ConstantFP::isExactlyValue(const APFloat &V) const { } //===----------------------------------------------------------------------===// +// ConstantAggregateZero Implementation +//===----------------------------------------------------------------------===// + +/// getSequentialElement - If this CAZ has array or vector type, return a zero +/// with the right element type. +Constant *ConstantAggregateZero::getSequentialElement() const { + return Constant::getNullValue(getType()->getSequentialElementType()); +} + +/// getStructElement - If this CAZ has struct type, return a zero with the +/// right element type for the specified element. +Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { + return Constant::getNullValue(getType()->getStructElementType(Elt)); +} + +/// getElementValue - Return a zero of the right value for the specified GEP +/// index if we can, otherwise return null (e.g. if C is a ConstantExpr). +Constant *ConstantAggregateZero::getElementValue(Constant *C) const { + if (isa<SequentialType>(getType())) + return getSequentialElement(); + return getStructElement(cast<ConstantInt>(C)->getZExtValue()); +} + +/// getElementValue - Return a zero of the right value for the specified GEP +/// index. +Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { + if (isa<SequentialType>(getType())) + return getSequentialElement(); + return getStructElement(Idx); +} + + +//===----------------------------------------------------------------------===// +// UndefValue Implementation +//===----------------------------------------------------------------------===// + +/// getSequentialElement - If this undef has array or vector type, return an +/// undef with the right element type. +UndefValue *UndefValue::getSequentialElement() const { + return UndefValue::get(getType()->getSequentialElementType()); +} + +/// getStructElement - If this undef has struct type, return a zero with the +/// right element type for the specified element. +UndefValue *UndefValue::getStructElement(unsigned Elt) const { + return UndefValue::get(getType()->getStructElementType(Elt)); +} + +/// getElementValue - Return an undef of the right value for the specified GEP +/// index if we can, otherwise return null (e.g. if C is a ConstantExpr). +UndefValue *UndefValue::getElementValue(Constant *C) const { + if (isa<SequentialType>(getType())) + return getSequentialElement(); + return getStructElement(cast<ConstantInt>(C)->getZExtValue()); +} + +/// getElementValue - Return an undef of the right value for the specified GEP +/// index. +UndefValue *UndefValue::getElementValue(unsigned Idx) const { + if (isa<SequentialType>(getType())) + return getSequentialElement(); + return getStructElement(Idx); +} + + + +//===----------------------------------------------------------------------===// // ConstantXXX Classes //===----------------------------------------------------------------------===// +template <typename ItTy, typename EltTy> +static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) { + for (; Start != End; ++Start) + if (*Start != Elt) + return false; + return true; +} ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) : Constant(T, ConstantArrayVal, @@ -601,45 +686,97 @@ ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) } Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { + // Empty arrays are canonicalized to ConstantAggregateZero. + if (V.empty()) + return ConstantAggregateZero::get(Ty); + for (unsigned i = 0, e = V.size(); i != e; ++i) { assert(V[i]->getType() == Ty->getElementType() && "Wrong type in array element initializer"); } LLVMContextImpl *pImpl = Ty->getContext().pImpl; - // If this is an all-zero array, return a ConstantAggregateZero object - if (!V.empty()) { - Constant *C = V[0]; - if (!C->isNullValue()) - return pImpl->ArrayConstants.getOrCreate(Ty, V); - - for (unsigned i = 1, e = V.size(); i != e; ++i) - if (V[i] != C) - return pImpl->ArrayConstants.getOrCreate(Ty, V); - } - return ConstantAggregateZero::get(Ty); -} + // If this is an all-zero array, return a ConstantAggregateZero object. If + // all undef, return an UndefValue, if "all simple", then return a + // ConstantDataArray. + Constant *C = V[0]; + if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C)) + return UndefValue::get(Ty); -/// ConstantArray::get(const string&) - Return an array that is initialized to -/// contain the specified string. If length is zero then a null terminator is -/// added to the specified string so that it may be used in a natural way. -/// Otherwise, the length parameter specifies how much of the string to use -/// and it won't be null terminated. -/// -Constant *ConstantArray::get(LLVMContext &Context, StringRef Str, - bool AddNull) { - std::vector<Constant*> ElementVals; - ElementVals.reserve(Str.size() + size_t(AddNull)); - for (unsigned i = 0; i < Str.size(); ++i) - ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i])); - - // Add a null terminator to the string... - if (AddNull) { - ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); + if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C)) + return ConstantAggregateZero::get(Ty); + + // Check to see if all of the elements are ConstantFP or ConstantInt and if + // the element type is compatible with ConstantDataVector. If so, use it. + if (ConstantDataSequential::isElementTypeCompatible(C->getType())) { + // We speculatively build the elements here even if it turns out that there + // is a constantexpr or something else weird in the array, since it is so + // uncommon for that to happen. + if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { + if (CI->getType()->isIntegerTy(8)) { + SmallVector<uint8_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(16)) { + SmallVector<uint16_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(32)) { + SmallVector<uint32_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(64)) { + SmallVector<uint64_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } + } + + if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { + if (CFP->getType()->isFloatTy()) { + SmallVector<float, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) + Elts.push_back(CFP->getValueAPF().convertToFloat()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } else if (CFP->getType()->isDoubleTy()) { + SmallVector<double, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) + Elts.push_back(CFP->getValueAPF().convertToDouble()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataArray::get(C->getContext(), Elts); + } + } } - ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size()); - return get(ATy, ElementVals); + // Otherwise, we really do want to create a ConstantArray. + return pImpl->ArrayConstants.getOrCreate(Ty, V); } /// getTypeForElements - Return an anonymous struct type to use for a constant @@ -647,9 +784,10 @@ Constant *ConstantArray::get(LLVMContext &Context, StringRef Str, StructType *ConstantStruct::getTypeForElements(LLVMContext &Context, ArrayRef<Constant*> V, bool Packed) { - SmallVector<Type*, 16> EltTypes; - for (unsigned i = 0, e = V.size(); i != e; ++i) - EltTypes.push_back(V[i]->getType()); + unsigned VecSize = V.size(); + SmallVector<Type*, 16> EltTypes(VecSize); + for (unsigned i = 0; i != VecSize; ++i) + EltTypes[i] = V[i]->getType(); return StructType::get(Context, EltTypes, Packed); } @@ -677,14 +815,31 @@ ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V) // ConstantStruct accessors. Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) { - // Create a ConstantAggregateZero value if all elements are zeros. - for (unsigned i = 0, e = V.size(); i != e; ++i) - if (!V[i]->isNullValue()) - return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V); - assert((ST->isOpaque() || ST->getNumElements() == V.size()) && "Incorrect # elements specified to ConstantStruct::get"); - return ConstantAggregateZero::get(ST); + + // Create a ConstantAggregateZero value if all elements are zeros. + bool isZero = true; + bool isUndef = false; + + if (!V.empty()) { + isUndef = isa<UndefValue>(V[0]); + isZero = V[0]->isNullValue(); + if (isUndef || isZero) { + for (unsigned i = 0, e = V.size(); i != e; ++i) { + if (!V[i]->isNullValue()) + isZero = false; + if (!isa<UndefValue>(V[i])) + isUndef = false; + } + } + } + if (isZero) + return ConstantAggregateZero::get(ST); + if (isUndef) + return UndefValue::get(ST); + + return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V); } Constant *ConstantStruct::get(StructType *T, ...) { @@ -731,10 +886,93 @@ Constant *ConstantVector::get(ArrayRef<Constant*> V) { return ConstantAggregateZero::get(T); if (isUndef) return UndefValue::get(T); + + // Check to see if all of the elements are ConstantFP or ConstantInt and if + // the element type is compatible with ConstantDataVector. If so, use it. + if (ConstantDataSequential::isElementTypeCompatible(C->getType())) { + // We speculatively build the elements here even if it turns out that there + // is a constantexpr or something else weird in the array, since it is so + // uncommon for that to happen. + if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { + if (CI->getType()->isIntegerTy(8)) { + SmallVector<uint8_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(16)) { + SmallVector<uint16_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(32)) { + SmallVector<uint32_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } else if (CI->getType()->isIntegerTy(64)) { + SmallVector<uint64_t, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) + Elts.push_back(CI->getZExtValue()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } + } + if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { + if (CFP->getType()->isFloatTy()) { + SmallVector<float, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) + Elts.push_back(CFP->getValueAPF().convertToFloat()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } else if (CFP->getType()->isDoubleTy()) { + SmallVector<double, 16> Elts; + for (unsigned i = 0, e = V.size(); i != e; ++i) + if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) + Elts.push_back(CFP->getValueAPF().convertToDouble()); + else + break; + if (Elts.size() == V.size()) + return ConstantDataVector::get(C->getContext(), Elts); + } + } + } + + // Otherwise, the element type isn't compatible with ConstantDataVector, or + // the operand list constants a ConstantExpr or something else strange. return pImpl->VectorConstants.getOrCreate(T, V); } +Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { + // If this splat is compatible with ConstantDataVector, use it instead of + // ConstantVector. + if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) && + ConstantDataSequential::isElementTypeCompatible(V->getType())) + return ConstantDataVector::getSplat(NumElts, V); + + SmallVector<Constant*, 32> Elts(NumElts, V); + return get(Elts); +} + + // Utility function for determining if a ConstantExpr is a CastOp or not. This // can't be inline because we don't want to #include Instruction.h into // Constant.h @@ -793,66 +1031,16 @@ unsigned ConstantExpr::getPredicate() const { /// one, but with the specified operand set to the specified value. Constant * ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { - assert(OpNo < getNumOperands() && "Operand num is out of range!"); assert(Op->getType() == getOperand(OpNo)->getType() && "Replacing operand with value of different type!"); if (getOperand(OpNo) == Op) return const_cast<ConstantExpr*>(this); + + SmallVector<Constant*, 8> NewOps; + for (unsigned i = 0, e = getNumOperands(); i != e; ++i) + NewOps.push_back(i == OpNo ? Op : getOperand(i)); - Constant *Op0, *Op1, *Op2; - switch (getOpcode()) { - case Instruction::Trunc: - case Instruction::ZExt: - case Instruction::SExt: - case Instruction::FPTrunc: - case Instruction::FPExt: - case Instruction::UIToFP: - case Instruction::SIToFP: - case Instruction::FPToUI: - case Instruction::FPToSI: - case Instruction::PtrToInt: - case Instruction::IntToPtr: - case Instruction::BitCast: - return ConstantExpr::getCast(getOpcode(), Op, getType()); - case Instruction::Select: - Op0 = (OpNo == 0) ? Op : getOperand(0); - Op1 = (OpNo == 1) ? Op : getOperand(1); - Op2 = (OpNo == 2) ? Op : getOperand(2); - return ConstantExpr::getSelect(Op0, Op1, Op2); - case Instruction::InsertElement: - Op0 = (OpNo == 0) ? Op : getOperand(0); - Op1 = (OpNo == 1) ? Op : getOperand(1); - Op2 = (OpNo == 2) ? Op : getOperand(2); - return ConstantExpr::getInsertElement(Op0, Op1, Op2); - case Instruction::ExtractElement: - Op0 = (OpNo == 0) ? Op : getOperand(0); - Op1 = (OpNo == 1) ? Op : getOperand(1); - return ConstantExpr::getExtractElement(Op0, Op1); - case Instruction::ShuffleVector: - Op0 = (OpNo == 0) ? Op : getOperand(0); - Op1 = (OpNo == 1) ? Op : getOperand(1); - Op2 = (OpNo == 2) ? Op : getOperand(2); - return ConstantExpr::getShuffleVector(Op0, Op1, Op2); - case Instruction::GetElementPtr: { - SmallVector<Constant*, 8> Ops; - Ops.resize(getNumOperands()-1); - for (unsigned i = 1, e = getNumOperands(); i != e; ++i) - Ops[i-1] = getOperand(i); - if (OpNo == 0) - return - ConstantExpr::getGetElementPtr(Op, Ops, - cast<GEPOperator>(this)->isInBounds()); - Ops[OpNo-1] = Op; - return - ConstantExpr::getGetElementPtr(getOperand(0), Ops, - cast<GEPOperator>(this)->isInBounds()); - } - default: - assert(getNumOperands() == 2 && "Must be binary operator?"); - Op0 = (OpNo == 0) ? Op : getOperand(0); - Op1 = (OpNo == 1) ? Op : getOperand(1); - return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData); - } + return getWithOperands(NewOps); } /// getWithOperands - This returns the current constant expression with the @@ -888,12 +1076,15 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); case Instruction::ExtractElement: return ConstantExpr::getExtractElement(Ops[0], Ops[1]); + case Instruction::InsertValue: + return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices()); + case Instruction::ExtractValue: + return ConstantExpr::getExtractValue(Ops[0], getIndices()); case Instruction::ShuffleVector: return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); case Instruction::GetElementPtr: - return - ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1), - cast<GEPOperator>(this)->isInBounds()); + return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1), + cast<GEPOperator>(this)->isInBounds()); case Instruction::ICmp: case Instruction::FCmp: return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); @@ -908,8 +1099,8 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { // isValueValidForType implementations bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { - unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay - if (Ty == Type::getInt1Ty(Ty->getContext())) + unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay + if (Ty->isIntegerTy(1)) return Val == 0 || Val == 1; if (NumBits >= 64) return true; // always true, has to fit in largest type @@ -918,8 +1109,8 @@ bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { } bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { - unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay - if (Ty == Type::getInt1Ty(Ty->getContext())) + unsigned NumBits = Ty->getIntegerBitWidth(); + if (Ty->isIntegerTy(1)) return Val == 0 || Val == 1 || Val == -1; if (NumBits >= 64) return true; // always true, has to fit in largest type @@ -937,6 +1128,12 @@ bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { return false; // These can't be represented as floating point! // FIXME rounding mode needs to be more flexible + case Type::HalfTyID: { + if (&Val2.getSemantics() == &APFloat::IEEEhalf) + return true; + Val2.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &losesInfo); + return !losesInfo; + } case Type::FloatTyID: { if (&Val2.getSemantics() == &APFloat::IEEEsingle) return true; @@ -944,42 +1141,50 @@ bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { return !losesInfo; } case Type::DoubleTyID: { - if (&Val2.getSemantics() == &APFloat::IEEEsingle || + if (&Val2.getSemantics() == &APFloat::IEEEhalf || + &Val2.getSemantics() == &APFloat::IEEEsingle || &Val2.getSemantics() == &APFloat::IEEEdouble) return true; Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); return !losesInfo; } case Type::X86_FP80TyID: - return &Val2.getSemantics() == &APFloat::IEEEsingle || + return &Val2.getSemantics() == &APFloat::IEEEhalf || + &Val2.getSemantics() == &APFloat::IEEEsingle || &Val2.getSemantics() == &APFloat::IEEEdouble || &Val2.getSemantics() == &APFloat::x87DoubleExtended; case Type::FP128TyID: - return &Val2.getSemantics() == &APFloat::IEEEsingle || + return &Val2.getSemantics() == &APFloat::IEEEhalf || + &Val2.getSemantics() == &APFloat::IEEEsingle || &Val2.getSemantics() == &APFloat::IEEEdouble || &Val2.getSemantics() == &APFloat::IEEEquad; case Type::PPC_FP128TyID: - return &Val2.getSemantics() == &APFloat::IEEEsingle || + return &Val2.getSemantics() == &APFloat::IEEEhalf || + &Val2.getSemantics() == &APFloat::IEEEsingle || &Val2.getSemantics() == &APFloat::IEEEdouble || &Val2.getSemantics() == &APFloat::PPCDoubleDouble; } } + //===----------------------------------------------------------------------===// // Factory Function Implementation -ConstantAggregateZero* ConstantAggregateZero::get(Type* Ty) { +ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && "Cannot create an aggregate zero of non-aggregate type!"); - LLVMContextImpl *pImpl = Ty->getContext().pImpl; - return pImpl->AggZeroConstants.getOrCreate(Ty, 0); + ConstantAggregateZero *&Entry = Ty->getContext().pImpl->CAZConstants[Ty]; + if (Entry == 0) + Entry = new ConstantAggregateZero(Ty); + + return Entry; } -/// destroyConstant - Remove the constant from the constant table... +/// destroyConstant - Remove the constant from the constant table. /// void ConstantAggregateZero::destroyConstant() { - getType()->getContext().pImpl->AggZeroConstants.remove(this); + getContext().pImpl->CAZConstants.erase(getType()); destroyConstantImpl(); } @@ -990,69 +1195,6 @@ void ConstantArray::destroyConstant() { destroyConstantImpl(); } -/// isString - This method returns true if the array is an array of i8, and -/// if the elements of the array are all ConstantInt's. -bool ConstantArray::isString() const { - // Check the element type for i8... - if (!getType()->getElementType()->isIntegerTy(8)) - return false; - // Check the elements to make sure they are all integers, not constant - // expressions. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (!isa<ConstantInt>(getOperand(i))) - return false; - return true; -} - -/// isCString - This method returns true if the array is a string (see -/// isString) and it ends in a null byte \\0 and does not contains any other -/// null bytes except its terminator. -bool ConstantArray::isCString() const { - // Check the element type for i8... - if (!getType()->getElementType()->isIntegerTy(8)) - return false; - - // Last element must be a null. - if (!getOperand(getNumOperands()-1)->isNullValue()) - return false; - // Other elements must be non-null integers. - for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { - if (!isa<ConstantInt>(getOperand(i))) - return false; - if (getOperand(i)->isNullValue()) - return false; - } - return true; -} - - -/// convertToString - Helper function for getAsString() and getAsCString(). -static std::string convertToString(const User *U, unsigned len) { - std::string Result; - Result.reserve(len); - for (unsigned i = 0; i != len; ++i) - Result.push_back((char)cast<ConstantInt>(U->getOperand(i))->getZExtValue()); - return Result; -} - -/// getAsString - If this array is isString(), then this method converts the -/// array to an std::string and returns it. Otherwise, it asserts out. -/// -std::string ConstantArray::getAsString() const { - assert(isString() && "Not a string!"); - return convertToString(this, getNumOperands()); -} - - -/// getAsCString - If this array is isCString(), then this method converts the -/// array (without the trailing null byte) to an std::string and returns it. -/// Otherwise, it asserts out. -/// -std::string ConstantArray::getAsCString() const { - assert(isCString() && "Not a string!"); - return convertToString(this, getNumOperands() - 1); -} - //---- ConstantStruct::get() implementation... // @@ -1071,26 +1213,6 @@ void ConstantVector::destroyConstant() { destroyConstantImpl(); } -/// This function will return true iff every element in this vector constant -/// is set to all ones. -/// @returns true iff this constant's elements are all set to all ones. -/// @brief Determine if the value is all ones. -bool ConstantVector::isAllOnesValue() const { - // Check out first element. - const Constant *Elt = getOperand(0); - const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); - const ConstantFP *CF = dyn_cast<ConstantFP>(Elt); - - // Then make sure all remaining elements point to the same value. - for (unsigned I = 1, E = getNumOperands(); I < E; ++I) - if (getOperand(I) != Elt) - return false; - - // First value is all-ones. - return (CI && CI->isAllOnesValue()) || - (CF && CF->isAllOnesValue()); -} - /// getSplatValue - If this is a splat constant, where all of the /// elements have the same value, return that value. Otherwise return null. Constant *ConstantVector::getSplatValue() const { @@ -1107,13 +1229,18 @@ Constant *ConstantVector::getSplatValue() const { // ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { - return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0); + ConstantPointerNull *&Entry = Ty->getContext().pImpl->CPNConstants[Ty]; + if (Entry == 0) + Entry = new ConstantPointerNull(Ty); + + return Entry; } // destroyConstant - Remove the constant from the constant table... // void ConstantPointerNull::destroyConstant() { - getType()->getContext().pImpl->NullPtrConstants.remove(this); + getContext().pImpl->CPNConstants.erase(getType()); + // Free the constant and any dangling references to it. destroyConstantImpl(); } @@ -1122,13 +1249,18 @@ void ConstantPointerNull::destroyConstant() { // UndefValue *UndefValue::get(Type *Ty) { - return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0); + UndefValue *&Entry = Ty->getContext().pImpl->UVConstants[Ty]; + if (Entry == 0) + Entry = new UndefValue(Ty); + + return Entry; } // destroyConstant - Remove the constant from the constant table. // void UndefValue::destroyConstant() { - getType()->getContext().pImpl->UndefValueConstants.remove(this); + // Free the constant and any dangling references to it. + getContext().pImpl->UVConstants.erase(getType()); destroyConstantImpl(); } @@ -1236,7 +1368,6 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) { switch (opc) { default: llvm_unreachable("Invalid cast opcode"); - break; case Instruction::Trunc: return getTrunc(C, Ty); case Instruction::ZExt: return getZExt(C, Ty); case Instruction::SExt: return getSExt(C, Ty); @@ -1250,7 +1381,6 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) { case Instruction::IntToPtr: return getIntToPtr(C, Ty); case Instruction::BitCast: return getBitCast(C, Ty); } - return 0; } Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) { @@ -1416,14 +1546,26 @@ Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) { } Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) { - assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer"); - assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral"); + assert(C->getType()->getScalarType()->isPointerTy() && + "PtrToInt source must be pointer or pointer vector"); + assert(DstTy->getScalarType()->isIntegerTy() && + "PtrToInt destination must be integer or integer vector"); + assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); + if (isa<VectorType>(C->getType())) + assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& + "Invalid cast between a different number of vector elements"); return getFoldedCast(Instruction::PtrToInt, C, DstTy); } Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) { - assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral"); - assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer"); + assert(C->getType()->getScalarType()->isIntegerTy() && + "IntToPtr source must be integer or integer vector"); + assert(DstTy->getScalarType()->isPointerTy() && + "IntToPtr destination must be a pointer or pointer vector"); + assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); + if (isa<VectorType>(C->getType())) + assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& + "Invalid cast between a different number of vector elements"); return getFoldedCast(Instruction::IntToPtr, C, DstTy); } @@ -1603,7 +1745,7 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, // Get the result type of the getelementptr! Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), Idxs); assert(Ty && "GEP indices invalid!"); - unsigned AS = cast<PointerType>(C->getType())->getAddressSpace(); + unsigned AS = C->getType()->getPointerAddressSpace(); Type *ReqTy = Ty->getPointerTo(AS); assert(C->getType()->isPointerTy() && @@ -1683,7 +1825,7 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; - Type *ReqTy = cast<VectorType>(Val->getType())->getElementType(); + Type *ReqTy = Val->getType()->getVectorElementType(); return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } @@ -1691,8 +1833,8 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, Constant *Idx) { assert(Val->getType()->isVectorTy() && "Tried to create insertelement operation on non-vector type!"); - assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() - && "Insertelement types must match!"); + assert(Elt->getType() == Val->getType()->getVectorElementType() && + "Insertelement types must match!"); assert(Idx->getType()->isIntegerTy(32) && "Insertelement index must be i32 type!"); @@ -1716,8 +1858,8 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) return FC; // Fold a few common cases. - unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements(); - Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); + unsigned NElts = Mask->getType()->getVectorNumElements(); + Type *EltTy = V1->getType()->getVectorElementType(); Type *ShufTy = VectorType::get(EltTy, NElts); // Look up the constant in the table first to ensure uniqueness @@ -1879,7 +2021,7 @@ const char *ConstantExpr::getOpcodeName() const { GetElementPtrConstantExpr:: -GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, +GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList, Type *DestTy) : ConstantExpr(DestTy, Instruction::GetElementPtr, OperandTraits<GetElementPtrConstantExpr>::op_end(this) @@ -1889,6 +2031,341 @@ GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, OperandList[i+1] = IdxList[i]; } +//===----------------------------------------------------------------------===// +// ConstantData* implementations + +void ConstantDataArray::anchor() {} +void ConstantDataVector::anchor() {} + +/// getElementType - Return the element type of the array/vector. +Type *ConstantDataSequential::getElementType() const { + return getType()->getElementType(); +} + +StringRef ConstantDataSequential::getRawDataValues() const { + return StringRef(DataElements, getNumElements()*getElementByteSize()); +} + +/// isElementTypeCompatible - Return true if a ConstantDataSequential can be +/// formed with a vector or array of the specified element type. +/// ConstantDataArray only works with normal float and int types that are +/// stored densely in memory, not with things like i42 or x86_f80. +bool ConstantDataSequential::isElementTypeCompatible(const Type *Ty) { + if (Ty->isFloatTy() || Ty->isDoubleTy()) return true; + if (const IntegerType *IT = dyn_cast<IntegerType>(Ty)) { + switch (IT->getBitWidth()) { + case 8: + case 16: + case 32: + case 64: + return true; + default: break; + } + } + return false; +} + +/// getNumElements - Return the number of elements in the array or vector. +unsigned ConstantDataSequential::getNumElements() const { + if (ArrayType *AT = dyn_cast<ArrayType>(getType())) + return AT->getNumElements(); + return getType()->getVectorNumElements(); +} + + +/// getElementByteSize - Return the size in bytes of the elements in the data. +uint64_t ConstantDataSequential::getElementByteSize() const { + return getElementType()->getPrimitiveSizeInBits()/8; +} + +/// getElementPointer - Return the start of the specified element. +const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { + assert(Elt < getNumElements() && "Invalid Elt"); + return DataElements+Elt*getElementByteSize(); +} + + +/// isAllZeros - return true if the array is empty or all zeros. +static bool isAllZeros(StringRef Arr) { + for (StringRef::iterator I = Arr.begin(), E = Arr.end(); I != E; ++I) + if (*I != 0) + return false; + return true; +} + +/// getImpl - This is the underlying implementation of all of the +/// ConstantDataSequential::get methods. They all thunk down to here, providing +/// the correct element type. We take the bytes in as a StringRef because +/// we *want* an underlying "char*" to avoid TBAA type punning violations. +Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { + assert(isElementTypeCompatible(Ty->getSequentialElementType())); + // If the elements are all zero or there are no elements, return a CAZ, which + // is more dense and canonical. + if (isAllZeros(Elements)) + return ConstantAggregateZero::get(Ty); + + // Do a lookup to see if we have already formed one of these. + StringMap<ConstantDataSequential*>::MapEntryTy &Slot = + Ty->getContext().pImpl->CDSConstants.GetOrCreateValue(Elements); + + // The bucket can point to a linked list of different CDS's that have the same + // body but different types. For example, 0,0,0,1 could be a 4 element array + // of i8, or a 1-element array of i32. They'll both end up in the same + /// StringMap bucket, linked up by their Next pointers. Walk the list. + ConstantDataSequential **Entry = &Slot.getValue(); + for (ConstantDataSequential *Node = *Entry; Node != 0; + Entry = &Node->Next, Node = *Entry) + if (Node->getType() == Ty) + return Node; + + // Okay, we didn't get a hit. Create a node of the right class, link it in, + // and return it. + if (isa<ArrayType>(Ty)) + return *Entry = new ConstantDataArray(Ty, Slot.getKeyData()); + + assert(isa<VectorType>(Ty)); + return *Entry = new ConstantDataVector(Ty, Slot.getKeyData()); +} + +void ConstantDataSequential::destroyConstant() { + // Remove the constant from the StringMap. + StringMap<ConstantDataSequential*> &CDSConstants = + getType()->getContext().pImpl->CDSConstants; + + StringMap<ConstantDataSequential*>::iterator Slot = + CDSConstants.find(getRawDataValues()); + + assert(Slot != CDSConstants.end() && "CDS not found in uniquing table"); + + ConstantDataSequential **Entry = &Slot->getValue(); + + // Remove the entry from the hash table. + if ((*Entry)->Next == 0) { + // If there is only one value in the bucket (common case) it must be this + // entry, and removing the entry should remove the bucket completely. + assert((*Entry) == this && "Hash mismatch in ConstantDataSequential"); + getContext().pImpl->CDSConstants.erase(Slot); + } else { + // Otherwise, there are multiple entries linked off the bucket, unlink the + // node we care about but keep the bucket around. + for (ConstantDataSequential *Node = *Entry; ; + Entry = &Node->Next, Node = *Entry) { + assert(Node && "Didn't find entry in its uniquing hash table!"); + // If we found our entry, unlink it from the list and we're done. + if (Node == this) { + *Entry = Node->Next; + break; + } + } + } + + // If we were part of a list, make sure that we don't delete the list that is + // still owned by the uniquing map. + Next = 0; + + // Finally, actually delete it. + destroyConstantImpl(); +} + +/// get() constructors - Return a constant with array type with an element +/// count and element type matching the ArrayRef passed in. Note that this +/// can return a ConstantAggregateZero object. +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) { + Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*1), Ty); +} +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ + Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*2), Ty); +} +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ + Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*4), Ty); +} +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ + Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); +} +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) { + Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*4), Ty); +} +Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) { + Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); +} + +/// getString - This method constructs a CDS and initializes it with a text +/// string. The default behavior (AddNull==true) causes a null terminator to +/// be placed at the end of the array (increasing the length of the string by +/// one more than the StringRef would normally indicate. Pass AddNull=false +/// to disable this behavior. +Constant *ConstantDataArray::getString(LLVMContext &Context, + StringRef Str, bool AddNull) { + if (!AddNull) + return get(Context, ArrayRef<uint8_t>((uint8_t*)Str.data(), Str.size())); + + SmallVector<uint8_t, 64> ElementVals; + ElementVals.append(Str.begin(), Str.end()); + ElementVals.push_back(0); + return get(Context, ElementVals); +} + +/// get() constructors - Return a constant with vector type with an element +/// count and element type matching the ArrayRef passed in. Note that this +/// can return a ConstantAggregateZero object. +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){ + Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*1), Ty); +} +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ + Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*2), Ty); +} +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ + Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*4), Ty); +} +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ + Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); +} +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) { + Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*4), Ty); +} +Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) { + Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); + return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); +} + +Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { + assert(isElementTypeCompatible(V->getType()) && + "Element type not compatible with ConstantData"); + if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { + if (CI->getType()->isIntegerTy(8)) { + SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + if (CI->getType()->isIntegerTy(16)) { + SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + if (CI->getType()->isIntegerTy(32)) { + SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type"); + SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + + if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { + if (CFP->getType()->isFloatTy()) { + SmallVector<float, 16> Elts(NumElts, CFP->getValueAPF().convertToFloat()); + return get(V->getContext(), Elts); + } + if (CFP->getType()->isDoubleTy()) { + SmallVector<double, 16> Elts(NumElts, + CFP->getValueAPF().convertToDouble()); + return get(V->getContext(), Elts); + } + } + return ConstantVector::getSplat(NumElts, V); +} + + +/// getElementAsInteger - If this is a sequential container of integers (of +/// any size), return the specified element in the low bits of a uint64_t. +uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { + assert(isa<IntegerType>(getElementType()) && + "Accessor can only be used when element is an integer"); + const char *EltPtr = getElementPointer(Elt); + + // The data is stored in host byte order, make sure to cast back to the right + // type to load with the right endianness. + switch (getElementType()->getIntegerBitWidth()) { + default: llvm_unreachable("Invalid bitwidth for CDS"); + case 8: return *(uint8_t*)EltPtr; + case 16: return *(uint16_t*)EltPtr; + case 32: return *(uint32_t*)EltPtr; + case 64: return *(uint64_t*)EltPtr; + } +} + +/// getElementAsAPFloat - If this is a sequential container of floating point +/// type, return the specified element as an APFloat. +APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { + const char *EltPtr = getElementPointer(Elt); + + switch (getElementType()->getTypeID()) { + default: + llvm_unreachable("Accessor can only be used when element is float/double!"); + case Type::FloatTyID: return APFloat(*(float*)EltPtr); + case Type::DoubleTyID: return APFloat(*(double*)EltPtr); + } +} + +/// getElementAsFloat - If this is an sequential container of floats, return +/// the specified element as a float. +float ConstantDataSequential::getElementAsFloat(unsigned Elt) const { + assert(getElementType()->isFloatTy() && + "Accessor can only be used when element is a 'float'"); + return *(float*)getElementPointer(Elt); +} + +/// getElementAsDouble - If this is an sequential container of doubles, return +/// the specified element as a float. +double ConstantDataSequential::getElementAsDouble(unsigned Elt) const { + assert(getElementType()->isDoubleTy() && + "Accessor can only be used when element is a 'float'"); + return *(double*)getElementPointer(Elt); +} + +/// getElementAsConstant - Return a Constant for a specified index's element. +/// Note that this has to compute a new constant to return, so it isn't as +/// efficient as getElementAsInteger/Float/Double. +Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { + if (getElementType()->isFloatTy() || getElementType()->isDoubleTy()) + return ConstantFP::get(getContext(), getElementAsAPFloat(Elt)); + + return ConstantInt::get(getElementType(), getElementAsInteger(Elt)); +} + +/// isString - This method returns true if this is an array of i8. +bool ConstantDataSequential::isString() const { + return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(8); +} + +/// isCString - This method returns true if the array "isString", ends with a +/// nul byte, and does not contains any other nul bytes. +bool ConstantDataSequential::isCString() const { + if (!isString()) + return false; + + StringRef Str = getAsString(); + + // The last value must be nul. + if (Str.back() != 0) return false; + + // Other elements must be non-nul. + return Str.drop_back().find(0) == StringRef::npos; +} + +/// getSplatValue - If this is a splat constant, meaning that all of the +/// elements have the same value, return that value. Otherwise return NULL. +Constant *ConstantDataVector::getSplatValue() const { + const char *Base = getRawDataValues().data(); + + // Compare elements 1+ to the 0'th element. + unsigned EltSize = getElementByteSize(); + for (unsigned i = 1, e = getNumElements(); i != e; ++i) + if (memcmp(Base, Base+i*EltSize, EltSize)) + return 0; + + // If they're all the same, return the 0th one as a representative. + return getElementAsConstant(0); +} //===----------------------------------------------------------------------===// // replaceUsesOfWithOnConstant implementations @@ -1911,56 +2388,46 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, LLVMContextImpl *pImpl = getType()->getContext().pImpl; - std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup; - Lookup.first.first = cast<ArrayType>(getType()); - Lookup.second = this; - - std::vector<Constant*> &Values = Lookup.first.second; + SmallVector<Constant*, 8> Values; + LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup; + Lookup.first = cast<ArrayType>(getType()); Values.reserve(getNumOperands()); // Build replacement array. // Fill values with the modified operands of the constant array. Also, // compute whether this turns into an all-zeros array. - bool isAllZeros = false; unsigned NumUpdated = 0; - if (!ToC->isNullValue()) { - for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { - Constant *Val = cast<Constant>(O->get()); - if (Val == From) { - Val = ToC; - ++NumUpdated; - } - Values.push_back(Val); - } - } else { - isAllZeros = true; - for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) { - Constant *Val = cast<Constant>(O->get()); - if (Val == From) { - Val = ToC; - ++NumUpdated; - } - Values.push_back(Val); - if (isAllZeros) isAllZeros = Val->isNullValue(); + + // Keep track of whether all the values in the array are "ToC". + bool AllSame = true; + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { + Constant *Val = cast<Constant>(O->get()); + if (Val == From) { + Val = ToC; + ++NumUpdated; } + Values.push_back(Val); + AllSame &= Val == ToC; } Constant *Replacement = 0; - if (isAllZeros) { + if (AllSame && ToC->isNullValue()) { Replacement = ConstantAggregateZero::get(getType()); + } else if (AllSame && isa<UndefValue>(ToC)) { + Replacement = UndefValue::get(getType()); } else { // Check to see if we have this array type already. - bool Exists; + Lookup.second = makeArrayRef(Values); LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = - pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists); + pImpl->ArrayConstants.find(Lookup); - if (Exists) { - Replacement = I->second; + if (I != pImpl->ArrayConstants.map_end()) { + Replacement = I->first; } else { // Okay, the new shape doesn't exist in the system yet. Instead of // creating a new constant array, inserting it, replaceallusesof'ing the // old with the new, then deleting the old... just update the current one // in place! - pImpl->ArrayConstants.MoveConstantToNewSlot(this, I); + pImpl->ArrayConstants.remove(this); // Update to the new value. Optimize for the case when we have a single // operand that we're changing, but handle bulk updates efficiently. @@ -1974,6 +2441,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, if (getOperand(i) == From) setOperand(i, ToC); } + pImpl->ArrayConstants.insert(this); return; } } @@ -1996,26 +2464,32 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, unsigned OperandToUpdate = U-OperandList; assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); - std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup; - Lookup.first.first = cast<StructType>(getType()); - Lookup.second = this; - std::vector<Constant*> &Values = Lookup.first.second; + SmallVector<Constant*, 8> Values; + LLVMContextImpl::StructConstantsTy::LookupKey Lookup; + Lookup.first = cast<StructType>(getType()); Values.reserve(getNumOperands()); // Build replacement struct. - // Fill values with the modified operands of the constant struct. Also, // compute whether this turns into an all-zeros struct. bool isAllZeros = false; - if (!ToC->isNullValue()) { - for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) - Values.push_back(cast<Constant>(O->get())); - } else { + bool isAllUndef = false; + if (ToC->isNullValue()) { isAllZeros = true; for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { Constant *Val = cast<Constant>(O->get()); Values.push_back(Val); if (isAllZeros) isAllZeros = Val->isNullValue(); } + } else if (isa<UndefValue>(ToC)) { + isAllUndef = true; + for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { + Constant *Val = cast<Constant>(O->get()); + Values.push_back(Val); + if (isAllUndef) isAllUndef = isa<UndefValue>(Val); + } + } else { + for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) + Values.push_back(cast<Constant>(O->get())); } Values[OperandToUpdate] = ToC; @@ -2024,23 +2498,26 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, Constant *Replacement = 0; if (isAllZeros) { Replacement = ConstantAggregateZero::get(getType()); + } else if (isAllUndef) { + Replacement = UndefValue::get(getType()); } else { // Check to see if we have this struct type already. - bool Exists; + Lookup.second = makeArrayRef(Values); LLVMContextImpl::StructConstantsTy::MapTy::iterator I = - pImpl->StructConstants.InsertOrGetItem(Lookup, Exists); + pImpl->StructConstants.find(Lookup); - if (Exists) { - Replacement = I->second; + if (I != pImpl->StructConstants.map_end()) { + Replacement = I->first; } else { // Okay, the new shape doesn't exist in the system yet. Instead of // creating a new constant struct, inserting it, replaceallusesof'ing the // old with the new, then deleting the old... just update the current one // in place! - pImpl->StructConstants.MoveConstantToNewSlot(this, I); + pImpl->StructConstants.remove(this); // Update to the new value. setOperand(OperandToUpdate, ToC); + pImpl->StructConstants.insert(this); return; } } @@ -2058,7 +2535,7 @@ void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); - std::vector<Constant*> Values; + SmallVector<Constant*, 8> Values; Values.reserve(getNumOperands()); // Build replacement array... for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { Constant *Val = getOperand(i); @@ -2081,89 +2558,13 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); Constant *To = cast<Constant>(ToV); - Constant *Replacement = 0; - if (getOpcode() == Instruction::GetElementPtr) { - SmallVector<Constant*, 8> Indices; - Constant *Pointer = getOperand(0); - Indices.reserve(getNumOperands()-1); - if (Pointer == From) Pointer = To; - - for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { - Constant *Val = getOperand(i); - if (Val == From) Val = To; - Indices.push_back(Val); - } - Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices, - cast<GEPOperator>(this)->isInBounds()); - } else if (getOpcode() == Instruction::ExtractValue) { - Constant *Agg = getOperand(0); - if (Agg == From) Agg = To; - - ArrayRef<unsigned> Indices = getIndices(); - Replacement = ConstantExpr::getExtractValue(Agg, Indices); - } else if (getOpcode() == Instruction::InsertValue) { - Constant *Agg = getOperand(0); - Constant *Val = getOperand(1); - if (Agg == From) Agg = To; - if (Val == From) Val = To; - - ArrayRef<unsigned> Indices = getIndices(); - Replacement = ConstantExpr::getInsertValue(Agg, Val, Indices); - } else if (isCast()) { - assert(getOperand(0) == From && "Cast only has one use!"); - Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); - } else if (getOpcode() == Instruction::Select) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - Constant *C3 = getOperand(2); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - if (C3 == From) C3 = To; - Replacement = ConstantExpr::getSelect(C1, C2, C3); - } else if (getOpcode() == Instruction::ExtractElement) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - Replacement = ConstantExpr::getExtractElement(C1, C2); - } else if (getOpcode() == Instruction::InsertElement) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - Constant *C3 = getOperand(1); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - if (C3 == From) C3 = To; - Replacement = ConstantExpr::getInsertElement(C1, C2, C3); - } else if (getOpcode() == Instruction::ShuffleVector) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - Constant *C3 = getOperand(2); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - if (C3 == From) C3 = To; - Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); - } else if (isCompare()) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - if (getOpcode() == Instruction::ICmp) - Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); - else { - assert(getOpcode() == Instruction::FCmp); - Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); - } - } else if (getNumOperands() == 2) { - Constant *C1 = getOperand(0); - Constant *C2 = getOperand(1); - if (C1 == From) C1 = To; - if (C2 == From) C2 = To; - Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData); - } else { - llvm_unreachable("Unknown ConstantExpr type!"); - return; + SmallVector<Constant*, 8> NewOps; + for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { + Constant *Op = getOperand(i); + NewOps.push_back(Op == From ? To : Op); } + Constant *Replacement = getWithOperands(NewOps); assert(Replacement != this && "I didn't contain From!"); // Everyone using this now uses the replacement. diff --git a/contrib/llvm/lib/VMCore/ConstantsContext.h b/contrib/llvm/lib/VMCore/ConstantsContext.h index 1077004..8903a8f 100644 --- a/contrib/llvm/lib/VMCore/ConstantsContext.h +++ b/contrib/llvm/lib/VMCore/ConstantsContext.h @@ -15,6 +15,8 @@ #ifndef LLVM_CONSTANTSCONTEXT_H #define LLVM_CONSTANTSCONTEXT_H +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Hashing.h" #include "llvm/InlineAsm.h" #include "llvm/Instructions.h" #include "llvm/Operator.h" @@ -30,6 +32,7 @@ struct ConstantTraits; /// UnaryConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement unary constant exprs. class UnaryConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly one operand @@ -46,6 +49,7 @@ public: /// BinaryConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement binary constant exprs. class BinaryConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly two operands @@ -66,6 +70,7 @@ public: /// SelectConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement select constant exprs. class SelectConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly three operands @@ -86,6 +91,7 @@ public: /// Constants.cpp, and is used behind the scenes to implement /// extractelement constant exprs. class ExtractElementConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly two operands @@ -106,6 +112,7 @@ public: /// Constants.cpp, and is used behind the scenes to implement /// insertelement constant exprs. class InsertElementConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly three operands @@ -127,6 +134,7 @@ public: /// Constants.cpp, and is used behind the scenes to implement /// shufflevector constant exprs. class ShuffleVectorConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly three operands @@ -151,6 +159,7 @@ public: /// Constants.cpp, and is used behind the scenes to implement /// extractvalue constant exprs. class ExtractValueConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly one operand @@ -176,6 +185,7 @@ public: /// Constants.cpp, and is used behind the scenes to implement /// insertvalue constant exprs. class InsertValueConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT public: // allocate space for exactly one operand @@ -202,11 +212,12 @@ public: /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is /// used behind the scenes to implement getelementpr constant exprs. class GetElementPtrConstantExpr : public ConstantExpr { - GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, + virtual void anchor(); + GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList, Type *DestTy); public: static GetElementPtrConstantExpr *Create(Constant *C, - const std::vector<Constant*>&IdxList, + ArrayRef<Constant*> IdxList, Type *DestTy, unsigned Flags) { GetElementPtrConstantExpr *Result = @@ -221,8 +232,10 @@ public: // CompareConstantExpr - This class is private to Constants.cpp, and is used // behind the scenes to implement ICmp and FCmp constant expressions. This is // needed in order to store the predicate value for these instructions. -struct CompareConstantExpr : public ConstantExpr { +class CompareConstantExpr : public ConstantExpr { + virtual void anchor(); void *operator new(size_t, unsigned); // DO NOT IMPLEMENT +public: // allocate space for exactly two operands void *operator new(size_t s) { return User::operator new(s, 2); @@ -397,6 +410,13 @@ struct ConstantCreator { } }; +template<class ConstantClass, class TypeClass> +struct ConstantArrayCreator { + static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) { + return new(V.size()) ConstantClass(Ty, V); + } +}; + template<class ConstantClass> struct ConstantKeyData { typedef void ValType; @@ -447,7 +467,6 @@ struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata, V.operands[0], V.operands[1]); llvm_unreachable("Invalid ConstantExpr!"); - return 0; } }; @@ -467,90 +486,6 @@ struct ConstantKeyData<ConstantExpr> { } }; -// ConstantAggregateZero does not take extra "value" argument... -template<class ValType> -struct ConstantCreator<ConstantAggregateZero, Type, ValType> { - static ConstantAggregateZero *create(Type *Ty, const ValType &V){ - return new ConstantAggregateZero(Ty); - } -}; - -template<> -struct ConstantKeyData<ConstantVector> { - typedef std::vector<Constant*> ValType; - static ValType getValType(ConstantVector *CP) { - std::vector<Constant*> Elements; - Elements.reserve(CP->getNumOperands()); - for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) - Elements.push_back(CP->getOperand(i)); - return Elements; - } -}; - -template<> -struct ConstantKeyData<ConstantAggregateZero> { - typedef char ValType; - static ValType getValType(ConstantAggregateZero *C) { - return 0; - } -}; - -template<> -struct ConstantKeyData<ConstantArray> { - typedef std::vector<Constant*> ValType; - static ValType getValType(ConstantArray *CA) { - std::vector<Constant*> Elements; - Elements.reserve(CA->getNumOperands()); - for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) - Elements.push_back(cast<Constant>(CA->getOperand(i))); - return Elements; - } -}; - -template<> -struct ConstantKeyData<ConstantStruct> { - typedef std::vector<Constant*> ValType; - static ValType getValType(ConstantStruct *CS) { - std::vector<Constant*> Elements; - Elements.reserve(CS->getNumOperands()); - for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) - Elements.push_back(cast<Constant>(CS->getOperand(i))); - return Elements; - } -}; - -// ConstantPointerNull does not take extra "value" argument... -template<class ValType> -struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { - static ConstantPointerNull *create(PointerType *Ty, const ValType &V){ - return new ConstantPointerNull(Ty); - } -}; - -template<> -struct ConstantKeyData<ConstantPointerNull> { - typedef char ValType; - static ValType getValType(ConstantPointerNull *C) { - return 0; - } -}; - -// UndefValue does not take extra "value" argument... -template<class ValType> -struct ConstantCreator<UndefValue, Type, ValType> { - static UndefValue *create(Type *Ty, const ValType &V) { - return new UndefValue(Ty); - } -}; - -template<> -struct ConstantKeyData<UndefValue> { - typedef char ValType; - static ValType getValType(UndefValue *C) { - return 0; - } -}; - template<> struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> { static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) { @@ -704,6 +639,129 @@ public: } }; +// Unique map for aggregate constants +template<class TypeClass, class ConstantClass> +class ConstantAggrUniqueMap { +public: + typedef ArrayRef<Constant*> Operands; + typedef std::pair<TypeClass*, Operands> LookupKey; +private: + struct MapInfo { + typedef DenseMapInfo<ConstantClass*> ConstantClassInfo; + typedef DenseMapInfo<Constant*> ConstantInfo; + typedef DenseMapInfo<TypeClass*> TypeClassInfo; + static inline ConstantClass* getEmptyKey() { + return ConstantClassInfo::getEmptyKey(); + } + static inline ConstantClass* getTombstoneKey() { + return ConstantClassInfo::getTombstoneKey(); + } + static unsigned getHashValue(const ConstantClass *CP) { + SmallVector<Constant*, 8> CPOperands; + CPOperands.reserve(CP->getNumOperands()); + for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I) + CPOperands.push_back(CP->getOperand(I)); + return getHashValue(LookupKey(CP->getType(), CPOperands)); + } + static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) { + return LHS == RHS; + } + static unsigned getHashValue(const LookupKey &Val) { + return hash_combine(Val.first, hash_combine_range(Val.second.begin(), + Val.second.end())); + } + static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + if (LHS.first != RHS->getType() + || LHS.second.size() != RHS->getNumOperands()) + return false; + for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) { + if (LHS.second[I] != RHS->getOperand(I)) + return false; + } + return true; + } + }; +public: + typedef DenseMap<ConstantClass *, char, MapInfo> MapTy; + +private: + /// Map - This is the main map from the element descriptor to the Constants. + /// This is the primary way we avoid creating two of the same shape + /// constant. + MapTy Map; + +public: + typename MapTy::iterator map_begin() { return Map.begin(); } + typename MapTy::iterator map_end() { return Map.end(); } + + void freeConstants() { + for (typename MapTy::iterator I=Map.begin(), E=Map.end(); + I != E; ++I) { + // Asserts that use_empty(). + delete I->first; + } + } + +private: + typename MapTy::iterator findExistingElement(ConstantClass *CP) { + return Map.find(CP); + } + + ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) { + ConstantClass* Result = + ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V); + + assert(Result->getType() == Ty && "Type specified is not correct!"); + Map[Result] = '\0'; + + return Result; + } +public: + + /// getOrCreate - Return the specified constant from the map, creating it if + /// necessary. + ConstantClass *getOrCreate(TypeClass *Ty, Operands V) { + LookupKey Lookup(Ty, V); + ConstantClass* Result = 0; + + typename MapTy::iterator I = Map.find_as(Lookup); + // Is it in the map? + if (I != Map.end()) + Result = I->first; + + if (!Result) { + // If no preexisting value, create one now... + Result = Create(Ty, V, I); + } + + return Result; + } + + /// Find the constant by lookup key. + typename MapTy::iterator find(LookupKey Lookup) { + return Map.find_as(Lookup); + } + + /// Insert the constant into its proper slot. + void insert(ConstantClass *CP) { + Map[CP] = '\0'; + } + + /// Remove this constant from the map + void remove(ConstantClass *CP) { + typename MapTy::iterator I = findExistingElement(CP); + assert(I != Map.end() && "Constant not found in constant table!"); + assert(I->first == CP && "Didn't find correct element?"); + Map.erase(I); + } + + void dump() const { + DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); + } +}; + } #endif diff --git a/contrib/llvm/lib/VMCore/Core.cpp b/contrib/llvm/lib/VMCore/Core.cpp index a505e4b..a9cca22 100644 --- a/contrib/llvm/lib/VMCore/Core.cpp +++ b/contrib/llvm/lib/VMCore/Core.cpp @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "llvm-c/Core.h" +#include "llvm/Attributes.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" @@ -132,10 +133,11 @@ LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { switch (unwrap(Ty)->getTypeID()) { - default: - assert(false && "Unhandled TypeID."); + default: llvm_unreachable("Unhandled TypeID."); case Type::VoidTyID: return LLVMVoidTypeKind; + case Type::HalfTyID: + return LLVMHalfTypeKind; case Type::FloatTyID: return LLVMFloatTypeKind; case Type::DoubleTyID: @@ -222,6 +224,9 @@ unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { /*--.. Operations on real types ............................................--*/ +LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) { + return (LLVMTypeRef) Type::getHalfTy(*unwrap(C)); +} LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); } @@ -241,6 +246,9 @@ LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) { return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C)); } +LLVMTypeRef LLVMHalfType(void) { + return LLVMHalfTypeInContext(LLVMGetGlobalContext()); +} LLVMTypeRef LLVMFloatType(void) { return LLVMFloatTypeInContext(LLVMGetGlobalContext()); } @@ -558,6 +566,17 @@ void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRe Dest[i] = wrap(N->getOperand(i)); } +void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, + LLVMValueRef Val) +{ + NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); + if (!N) + return; + MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL; + if (Op) + N->addOperand(Op); +} + /*--.. Operations on scalar constants ......................................--*/ LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, @@ -614,8 +633,8 @@ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, LLVMBool DontNullTerminate) { /* Inverted the sense of AddNull because ', 0)' is a better mnemonic for null termination than ', 1)'. */ - return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length), - DontNullTerminate == 0)); + return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), + DontNullTerminate == 0)); } LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, LLVMValueRef *ConstantVals, @@ -660,8 +679,7 @@ LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { static LLVMOpcode map_to_llvmopcode(int opcode) { switch (opcode) { - default: - assert(0 && "Unhandled Opcode."); + default: llvm_unreachable("Unhandled Opcode."); #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc; #include "llvm/Instruction.def" #undef HANDLE_INST @@ -671,12 +689,11 @@ static LLVMOpcode map_to_llvmopcode(int opcode) static int map_from_llvmopcode(LLVMOpcode code) { switch (code) { - default: - assert(0 && "Unhandled Opcode."); #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num; #include "llvm/Instruction.def" #undef HANDLE_INST } + llvm_unreachable("Unhandled Opcode."); } /*--.. Constant expressions ................................................--*/ @@ -1040,8 +1057,6 @@ LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { switch (unwrap<GlobalValue>(Global)->getLinkage()) { - default: - assert(false && "Unhandled Linkage Type."); case GlobalValue::ExternalLinkage: return LLVMExternalLinkage; case GlobalValue::AvailableExternallyLinkage: @@ -1076,16 +1091,13 @@ LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { return LLVMCommonLinkage; } - // Should never get here. - return static_cast<LLVMLinkage>(0); + llvm_unreachable("Invalid GlobalValue linkage!"); } void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { GlobalValue *GV = unwrap<GlobalValue>(Global); switch (Linkage) { - default: - assert(false && "Unhandled Linkage Type."); case LLVMExternalLinkage: GV->setLinkage(GlobalValue::ExternalLinkage); break; @@ -1337,14 +1349,14 @@ void LLVMSetGC(LLVMValueRef Fn, const char *GC) { void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { Function *Func = unwrap<Function>(Fn); const AttrListPtr PAL = Func->getAttributes(); - const AttrListPtr PALnew = PAL.addAttr(~0U, PA); + const AttrListPtr PALnew = PAL.addAttr(~0U, Attributes(PA)); Func->setAttributes(PALnew); } void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { Function *Func = unwrap<Function>(Fn); const AttrListPtr PAL = Func->getAttributes(); - const AttrListPtr PALnew = PAL.removeAttr(~0U, PA); + const AttrListPtr PALnew = PAL.removeAttr(~0U, Attributes(PA)); Func->setAttributes(PALnew); } @@ -1352,7 +1364,7 @@ LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) { Function *Func = unwrap<Function>(Fn); const AttrListPtr PAL = Func->getAttributes(); Attributes attr = PAL.getFnAttributes(); - return (LLVMAttribute)attr; + return (LLVMAttribute)attr.Raw(); } /*--.. Operations on parameters ............................................--*/ @@ -1414,18 +1426,18 @@ LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { } void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { - unwrap<Argument>(Arg)->addAttr(PA); + unwrap<Argument>(Arg)->addAttr(Attributes(PA)); } void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { - unwrap<Argument>(Arg)->removeAttr(PA); + unwrap<Argument>(Arg)->removeAttr(Attributes(PA)); } LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { Argument *A = unwrap<Argument>(Arg); Attributes attr = A->getParent()->getAttributes().getParamAttributes( A->getArgNo()+1); - return (LLVMAttribute)attr; + return (LLVMAttribute)attr.Raw(); } @@ -1603,10 +1615,9 @@ unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { Value *V = unwrap(Instr); if (CallInst *CI = dyn_cast<CallInst>(V)) return CI->getCallingConv(); - else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) + if (InvokeInst *II = dyn_cast<InvokeInst>(V)) return II->getCallingConv(); llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); - return 0; } void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { @@ -1622,14 +1633,14 @@ void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute PA) { CallSite Call = CallSite(unwrap<Instruction>(Instr)); Call.setAttributes( - Call.getAttributes().addAttr(index, PA)); + Call.getAttributes().addAttr(index, Attributes(PA))); } void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute PA) { CallSite Call = CallSite(unwrap<Instruction>(Instr)); Call.setAttributes( - Call.getAttributes().removeAttr(index, PA)); + Call.getAttributes().removeAttr(index, Attributes(PA))); } void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, @@ -2055,6 +2066,20 @@ LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); } +LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { + Value *P = unwrap<Value>(MemAccessInst); + if (LoadInst *LI = dyn_cast<LoadInst>(P)) + return LI->isVolatile(); + return cast<StoreInst>(P)->isVolatile(); +} + +void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { + Value *P = unwrap<Value>(MemAccessInst); + if (LoadInst *LI = dyn_cast<LoadInst>(P)) + return LI->setVolatile(isVolatile); + return cast<StoreInst>(P)->setVolatile(isVolatile); +} + /*--.. Casts ...............................................................--*/ LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, diff --git a/contrib/llvm/lib/VMCore/DebugInfoProbe.cpp b/contrib/llvm/lib/VMCore/DebugInfoProbe.cpp deleted file mode 100644 index d1275ff..0000000 --- a/contrib/llvm/lib/VMCore/DebugInfoProbe.cpp +++ /dev/null @@ -1,225 +0,0 @@ -//===-- DebugInfoProbe.cpp - DebugInfo Probe ------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements DebugInfoProbe. This probe can be used by a pass -// manager to analyze how optimizer is treating debugging information. -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "debuginfoprobe" -#include "llvm/DebugInfoProbe.h" -#include "llvm/Function.h" -#include "llvm/IntrinsicInst.h" -#include "llvm/Metadata.h" -#include "llvm/PassManager.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/Debug.h" -#include "llvm/Support/DebugLoc.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/ADT/StringRef.h" -#include <set> -#include <string> - -using namespace llvm; - -static cl::opt<bool> -EnableDebugInfoProbe("enable-debug-info-probe", cl::Hidden, - cl::desc("Enable debug info probe")); - -// CreateInfoOutputFile - Return a file stream to print our output on. -namespace llvm { extern raw_ostream *CreateInfoOutputFile(); } - -//===----------------------------------------------------------------------===// -// DebugInfoProbeImpl - This class implements a interface to monitor -// how an optimization pass is preserving debugging information. - -namespace llvm { - - class DebugInfoProbeImpl { - public: - DebugInfoProbeImpl() : NumDbgLineLost(0),NumDbgValueLost(0) {} - void initialize(StringRef PName, Function &F); - void finalize(Function &F); - void report(); - private: - unsigned NumDbgLineLost, NumDbgValueLost; - std::string PassName; - Function *TheFn; - std::set<MDNode *> DbgVariables; - std::set<Instruction *> MissingDebugLoc; - }; -} - -//===----------------------------------------------------------------------===// -// DebugInfoProbeImpl - -/// initialize - Collect information before running an optimization pass. -void DebugInfoProbeImpl::initialize(StringRef PName, Function &F) { - if (!EnableDebugInfoProbe) return; - PassName = PName; - - DbgVariables.clear(); - MissingDebugLoc.clear(); - TheFn = &F; - - for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) - for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); - BI != BE; ++BI) { - if (!isa<PHINode>(BI) && BI->getDebugLoc().isUnknown()) - MissingDebugLoc.insert(BI); - if (!isa<DbgInfoIntrinsic>(BI)) continue; - Value *Addr = NULL; - MDNode *Node = NULL; - if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) { - Addr = DDI->getAddress(); - Node = DDI->getVariable(); - } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) { - Addr = DVI->getValue(); - Node = DVI->getVariable(); - } - if (Addr) - DbgVariables.insert(Node); - } -} - -/// report - Report findings. This should be invoked after finalize. -void DebugInfoProbeImpl::report() { - if (!EnableDebugInfoProbe) return; - if (NumDbgLineLost || NumDbgValueLost) { - raw_ostream *OutStream = CreateInfoOutputFile(); - if (NumDbgLineLost) - *OutStream << NumDbgLineLost - << "\t times line number info lost by " - << PassName << "\n"; - if (NumDbgValueLost) - *OutStream << NumDbgValueLost - << "\t times variable info lost by " - << PassName << "\n"; - delete OutStream; - } - NumDbgLineLost = 0; - NumDbgValueLost = 0; -} - -/// finalize - Collect information after running an optimization pass. This -/// must be used after initialization. -void DebugInfoProbeImpl::finalize(Function &F) { - if (!EnableDebugInfoProbe) return; - assert (TheFn == &F && "Invalid function to measure!"); - - std::set<MDNode *>DbgVariables2; - for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) - for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); - BI != BE; ++BI) { - if (!isa<PHINode>(BI) && BI->getDebugLoc().isUnknown() && - MissingDebugLoc.count(BI) == 0) { - ++NumDbgLineLost; - DEBUG(dbgs() << "DebugInfoProbe (" << PassName << "): --- "); - DEBUG(BI->print(dbgs())); - DEBUG(dbgs() << "\n"); - } - if (!isa<DbgInfoIntrinsic>(BI)) continue; - Value *Addr = NULL; - MDNode *Node = NULL; - if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) { - Addr = DDI->getAddress(); - Node = DDI->getVariable(); - } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) { - Addr = DVI->getValue(); - Node = DVI->getVariable(); - } - if (Addr) - DbgVariables2.insert(Node); - } - - for (std::set<MDNode *>::iterator I = DbgVariables.begin(), - E = DbgVariables.end(); I != E; ++I) { - if (DbgVariables2.count(*I) == 0 && (*I)->getNumOperands() >= 2) { - DEBUG(dbgs() - << "DebugInfoProbe(" - << PassName - << "): Losing dbg info for variable: "; - if (MDString *MDS = dyn_cast_or_null<MDString>( - (*I)->getOperand(2))) - dbgs() << MDS->getString(); - else - dbgs() << "..."; - dbgs() << "\n"); - ++NumDbgValueLost; - } - } -} - -//===----------------------------------------------------------------------===// -// DebugInfoProbe - -DebugInfoProbe::DebugInfoProbe() { - pImpl = new DebugInfoProbeImpl(); -} - -DebugInfoProbe::~DebugInfoProbe() { - delete pImpl; -} - -/// initialize - Collect information before running an optimization pass. -void DebugInfoProbe::initialize(StringRef PName, Function &F) { - pImpl->initialize(PName, F); -} - -/// finalize - Collect information after running an optimization pass. This -/// must be used after initialization. -void DebugInfoProbe::finalize(Function &F) { - pImpl->finalize(F); -} - -/// report - Report findings. This should be invoked after finalize. -void DebugInfoProbe::report() { - pImpl->report(); -} - -//===----------------------------------------------------------------------===// -// DebugInfoProbeInfo - -/// ~DebugInfoProbeInfo - Report data collected by all probes before deleting -/// them. -DebugInfoProbeInfo::~DebugInfoProbeInfo() { - if (!EnableDebugInfoProbe) return; - for (StringMap<DebugInfoProbe*>::iterator I = Probes.begin(), - E = Probes.end(); I != E; ++I) { - I->second->report(); - delete I->second; - } - } - -/// initialize - Collect information before running an optimization pass. -void DebugInfoProbeInfo::initialize(Pass *P, Function &F) { - if (!EnableDebugInfoProbe) return; - if (P->getAsPMDataManager()) - return; - - StringMapEntry<DebugInfoProbe *> &Entry = - Probes.GetOrCreateValue(P->getPassName()); - DebugInfoProbe *&Probe = Entry.getValue(); - if (!Probe) - Probe = new DebugInfoProbe(); - Probe->initialize(P->getPassName(), F); -} - -/// finalize - Collect information after running an optimization pass. This -/// must be used after initialization. -void DebugInfoProbeInfo::finalize(Pass *P, Function &F) { - if (!EnableDebugInfoProbe) return; - if (P->getAsPMDataManager()) - return; - StringMapEntry<DebugInfoProbe *> &Entry = - Probes.GetOrCreateValue(P->getPassName()); - DebugInfoProbe *&Probe = Entry.getValue(); - assert (Probe && "DebugInfoProbe is not initialized!"); - Probe->finalize(F); -} diff --git a/contrib/llvm/lib/VMCore/DebugLoc.cpp b/contrib/llvm/lib/VMCore/DebugLoc.cpp index 328244f..9013d28 100644 --- a/contrib/llvm/lib/VMCore/DebugLoc.cpp +++ b/contrib/llvm/lib/VMCore/DebugLoc.cpp @@ -173,10 +173,7 @@ DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() { } unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) { - FoldingSetNodeID ID; - ID.AddInteger(Key.LineCol); - ID.AddInteger(Key.ScopeIdx); - return ID.ComputeHash(); + return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx)); } bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) { diff --git a/contrib/llvm/lib/VMCore/Dominators.cpp b/contrib/llvm/lib/VMCore/Dominators.cpp index 08b845e..219e631 100644 --- a/contrib/llvm/lib/VMCore/Dominators.cpp +++ b/contrib/llvm/lib/VMCore/Dominators.cpp @@ -80,27 +80,187 @@ void DominatorTree::print(raw_ostream &OS, const Module *) const { DT->print(OS); } -// dominates - Return true if A dominates a use in B. This performs the -// special checks necessary if A and B are in the same basic block. -bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{ - const BasicBlock *BBA = A->getParent(), *BBB = B->getParent(); - - // If A is an invoke instruction, its value is only available in this normal - // successor block. - if (const InvokeInst *II = dyn_cast<InvokeInst>(A)) - BBA = II->getNormalDest(); - - if (BBA != BBB) return dominates(BBA, BBB); - - // It is not possible to determine dominance between two PHI nodes - // based on their ordering. - if (isa<PHINode>(A) && isa<PHINode>(B)) +// dominates - Return true if Def dominates a use in User. This performs +// the special checks necessary if Def and User are in the same basic block. +// Note that Def doesn't dominate a use in Def itself! +bool DominatorTree::dominates(const Instruction *Def, + const Instruction *User) const { + const BasicBlock *UseBB = User->getParent(); + const BasicBlock *DefBB = Def->getParent(); + + // Any unreachable use is dominated, even if Def == User. + if (!isReachableFromEntry(UseBB)) + return true; + + // Unreachable definitions don't dominate anything. + if (!isReachableFromEntry(DefBB)) + return false; + + // An instruction doesn't dominate a use in itself. + if (Def == User) return false; - - // Loop through the basic block until we find A or B. - BasicBlock::const_iterator I = BBA->begin(); - for (; &*I != A && &*I != B; ++I) + + // The value defined by an invoke dominates an instruction only if + // it dominates every instruction in UseBB. + // A PHI is dominated only if the instruction dominates every possible use + // in the UseBB. + if (isa<InvokeInst>(Def) || isa<PHINode>(User)) + return dominates(Def, UseBB); + + if (DefBB != UseBB) + return dominates(DefBB, UseBB); + + // Loop through the basic block until we find Def or User. + BasicBlock::const_iterator I = DefBB->begin(); + for (; &*I != Def && &*I != User; ++I) /*empty*/; - - return &*I == A; + + return &*I == Def; +} + +// true if Def would dominate a use in any instruction in UseBB. +// note that dominates(Def, Def->getParent()) is false. +bool DominatorTree::dominates(const Instruction *Def, + const BasicBlock *UseBB) const { + const BasicBlock *DefBB = Def->getParent(); + + // Any unreachable use is dominated, even if DefBB == UseBB. + if (!isReachableFromEntry(UseBB)) + return true; + + // Unreachable definitions don't dominate anything. + if (!isReachableFromEntry(DefBB)) + return false; + + if (DefBB == UseBB) + return false; + + const InvokeInst *II = dyn_cast<InvokeInst>(Def); + if (!II) + return dominates(DefBB, UseBB); + + // Invoke results are only usable in the normal destination, not in the + // exceptional destination. + BasicBlock *NormalDest = II->getNormalDest(); + if (!dominates(NormalDest, UseBB)) + return false; + + // Simple case: if the normal destination has a single predecessor, the + // fact that it dominates the use block implies that we also do. + if (NormalDest->getSinglePredecessor()) + return true; + + // The normal edge from the invoke is critical. Conceptually, what we would + // like to do is split it and check if the new block dominates the use. + // With X being the new block, the graph would look like: + // + // DefBB + // /\ . . + // / \ . . + // / \ . . + // / \ | | + // A X B C + // | \ | / + // . \|/ + // . NormalDest + // . + // + // Given the definition of dominance, NormalDest is dominated by X iff X + // dominates all of NormalDest's predecessors (X, B, C in the example). X + // trivially dominates itself, so we only have to find if it dominates the + // other predecessors. Since the only way out of X is via NormalDest, X can + // only properly dominate a node if NormalDest dominates that node too. + for (pred_iterator PI = pred_begin(NormalDest), + E = pred_end(NormalDest); PI != E; ++PI) { + const BasicBlock *BB = *PI; + if (BB == DefBB) + continue; + + if (!DT->isReachableFromEntry(BB)) + continue; + + if (!dominates(NormalDest, BB)) + return false; + } + return true; +} + +bool DominatorTree::dominates(const Instruction *Def, + const Use &U) const { + Instruction *UserInst = dyn_cast<Instruction>(U.getUser()); + + // Instructions do not dominate non-instructions. + if (!UserInst) + return false; + + const BasicBlock *DefBB = Def->getParent(); + + // Determine the block in which the use happens. PHI nodes use + // their operands on edges; simulate this by thinking of the use + // happening at the end of the predecessor block. + const BasicBlock *UseBB; + if (PHINode *PN = dyn_cast<PHINode>(UserInst)) + UseBB = PN->getIncomingBlock(U); + else + UseBB = UserInst->getParent(); + + // Any unreachable use is dominated, even if Def == User. + if (!isReachableFromEntry(UseBB)) + return true; + + // Unreachable definitions don't dominate anything. + if (!isReachableFromEntry(DefBB)) + return false; + + // Invoke instructions define their return values on the edges + // to their normal successors, so we have to handle them specially. + // Among other things, this means they don't dominate anything in + // their own block, except possibly a phi, so we don't need to + // walk the block in any case. + if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { + // A PHI in the normal successor using the invoke's return value is + // dominated by the invoke's return value. + if (isa<PHINode>(UserInst) && + UserInst->getParent() == II->getNormalDest() && + cast<PHINode>(UserInst)->getIncomingBlock(U) == DefBB) + return true; + + // Otherwise use the instruction-dominates-block query, which + // handles the crazy case of an invoke with a critical edge + // properly. + return dominates(Def, UseBB); + } + + // If the def and use are in different blocks, do a simple CFG dominator + // tree query. + if (DefBB != UseBB) + return dominates(DefBB, UseBB); + + // Ok, def and use are in the same block. If the def is an invoke, it + // doesn't dominate anything in the block. If it's a PHI, it dominates + // everything in the block. + if (isa<PHINode>(UserInst)) + return true; + + // Otherwise, just loop through the basic block until we find Def or User. + BasicBlock::const_iterator I = DefBB->begin(); + for (; &*I != Def && &*I != UserInst; ++I) + /*empty*/; + + return &*I != UserInst; +} + +bool DominatorTree::isReachableFromEntry(const Use &U) const { + Instruction *I = dyn_cast<Instruction>(U.getUser()); + + // ConstantExprs aren't really reachable from the entry block, but they + // don't need to be treated like unreachable code either. + if (!I) return true; + + // PHI nodes use their operands on their incoming edges. + if (PHINode *PN = dyn_cast<PHINode>(I)) + return isReachableFromEntry(PN->getIncomingBlock(U)); + + // Everything else uses their operands in their own block. + return isReachableFromEntry(I->getParent()); } diff --git a/contrib/llvm/lib/VMCore/Function.cpp b/contrib/llvm/lib/VMCore/Function.cpp index 1215e6a..af6344e 100644 --- a/contrib/llvm/lib/VMCore/Function.cpp +++ b/contrib/llvm/lib/VMCore/Function.cpp @@ -39,6 +39,8 @@ template class llvm::SymbolTableListTraits<BasicBlock, Function>; // Argument Implementation //===----------------------------------------------------------------------===// +void Argument::anchor() { } + Argument::Argument(Type *Ty, const Twine &Name, Function *Par) : Value(Ty, Value::ArgumentVal) { Parent = 0; @@ -359,7 +361,7 @@ std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { FunctionType *Intrinsic::getType(LLVMContext &Context, ID id, ArrayRef<Type*> Tys) { Type *ResultTy = NULL; - std::vector<Type*> ArgTys; + SmallVector<Type*, 8> ArgTys; bool IsVarArg = false; #define GET_INTRINSIC_GENERATOR @@ -370,13 +372,9 @@ FunctionType *Intrinsic::getType(LLVMContext &Context, } bool Intrinsic::isOverloaded(ID id) { - static const bool OTable[] = { - false, #define GET_INTRINSIC_OVERLOAD_TABLE #include "llvm/Intrinsics.gen" #undef GET_INTRINSIC_OVERLOAD_TABLE - }; - return OTable[id]; } /// This defines the "Intrinsic::getAttributes(ID id)" method. @@ -402,6 +400,7 @@ Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { bool Function::hasAddressTaken(const User* *PutOffender) const { for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { const User *U = *I; + // FIXME: Check for blockaddress, which does not take the address. if (!isa<CallInst>(U) && !isa<InvokeInst>(U)) return PutOffender ? (*PutOffender = U, true) : true; ImmutableCallSite CS(cast<Instruction>(U)); @@ -411,41 +410,30 @@ bool Function::hasAddressTaken(const User* *PutOffender) const { return false; } +bool Function::isDefTriviallyDead() const { + // Check the linkage + if (!hasLinkOnceLinkage() && !hasLocalLinkage() && + !hasAvailableExternallyLinkage()) + return false; + + // Check if the function is used by anything other than a blockaddress. + for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) + if (!isa<BlockAddress>(*I)) + return false; + + return true; +} + /// callsFunctionThatReturnsTwice - Return true if the function has a call to /// setjmp or other function that gcc recognizes as "returning twice". -/// -/// FIXME: Remove after <rdar://problem/8031714> is fixed. -/// FIXME: Is the above FIXME valid? bool Function::callsFunctionThatReturnsTwice() const { - static const char *const ReturnsTwiceFns[] = { - "_setjmp", - "setjmp", - "sigsetjmp", - "setjmp_syscall", - "savectx", - "qsetjmp", - "vfork", - "getcontext" - }; - - for (const_inst_iterator I = inst_begin(this), E = inst_end(this); I != E; - ++I) { + for (const_inst_iterator + I = inst_begin(this), E = inst_end(this); I != E; ++I) { const CallInst* callInst = dyn_cast<CallInst>(&*I); if (!callInst) continue; if (callInst->canReturnTwice()) return true; - - // check for known function names. - // FIXME: move this to clang. - Function *F = callInst->getCalledFunction(); - if (!F) - continue; - StringRef Name = F->getName(); - for (unsigned J = 0, e = array_lengthof(ReturnsTwiceFns); J != e; ++J) { - if (Name == ReturnsTwiceFns[J]) - return true; - } } return false; diff --git a/contrib/llvm/lib/VMCore/GCOV.cpp b/contrib/llvm/lib/VMCore/GCOV.cpp index fc7f96f..595c452 100644 --- a/contrib/llvm/lib/VMCore/GCOV.cpp +++ b/contrib/llvm/lib/VMCore/GCOV.cpp @@ -107,7 +107,7 @@ bool GCOVFunction::read(GCOVBuffer &Buff, GCOVFormat Format) { for (unsigned i = 0, e = Count; i != e; ++i) { Blocks[i]->addCount(Buff.readInt64()); } - return true;; + return true; } LineNumber = Buff.readInt(); diff --git a/contrib/llvm/lib/VMCore/IRBuilder.cpp b/contrib/llvm/lib/VMCore/IRBuilder.cpp index 5114e2d..b459234 100644 --- a/contrib/llvm/lib/VMCore/IRBuilder.cpp +++ b/contrib/llvm/lib/VMCore/IRBuilder.cpp @@ -24,10 +24,10 @@ using namespace llvm; /// specified. If Name is specified, it is the name of the global variable /// created. Value *IRBuilderBase::CreateGlobalString(StringRef Str, const Twine &Name) { - Constant *StrConstant = ConstantArray::get(Context, Str, true); + Constant *StrConstant = ConstantDataArray::getString(Context, Str); Module &M = *BB->getParent()->getParent(); GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(), - true, GlobalValue::InternalLinkage, + true, GlobalValue::PrivateLinkage, StrConstant, "", 0, false); GV->setName(Name); GV->setUnnamedAddr(true); diff --git a/contrib/llvm/lib/VMCore/Instruction.cpp b/contrib/llvm/lib/VMCore/Instruction.cpp index 73191c1..5449714 100644 --- a/contrib/llvm/lib/VMCore/Instruction.cpp +++ b/contrib/llvm/lib/VMCore/Instruction.cpp @@ -102,7 +102,6 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case IndirectBr: return "indirectbr"; case Invoke: return "invoke"; case Resume: return "resume"; - case Unwind: return "unwind"; case Unreachable: return "unreachable"; // Standard binary operators... @@ -166,8 +165,6 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { default: return "<Invalid operator> "; } - - return 0; } /// isIdenticalTo - Return true if the specified instruction is exactly @@ -391,59 +388,6 @@ bool Instruction::isCommutative(unsigned op) { } } -bool Instruction::isSafeToSpeculativelyExecute() const { - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (Constant *C = dyn_cast<Constant>(getOperand(i))) - if (C->canTrap()) - return false; - - switch (getOpcode()) { - default: - return true; - case UDiv: - case URem: { - // x / y is undefined if y == 0, but calcuations like x / 3 are safe. - ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1)); - return Op && !Op->isNullValue(); - } - case SDiv: - case SRem: { - // x / y is undefined if y == 0, and might be undefined if y == -1, - // but calcuations like x / 3 are safe. - ConstantInt *Op = dyn_cast<ConstantInt>(getOperand(1)); - return Op && !Op->isNullValue() && !Op->isAllOnesValue(); - } - case Load: { - const LoadInst *LI = cast<LoadInst>(this); - if (!LI->isUnordered()) - return false; - return LI->getPointerOperand()->isDereferenceablePointer(); - } - case Call: - return false; // The called function could have undefined behavior or - // side-effects. - // FIXME: We should special-case some intrinsics (bswap, - // overflow-checking arithmetic, etc.) - case VAArg: - case Alloca: - case Invoke: - case PHI: - case Store: - case Ret: - case Br: - case IndirectBr: - case Switch: - case Unwind: - case Unreachable: - case Fence: - case LandingPad: - case AtomicRMW: - case AtomicCmpXchg: - case Resume: - return false; // Misc instructions which have effects - } -} - Instruction *Instruction::clone() const { Instruction *New = clone_impl(); New->SubclassOptionalData = SubclassOptionalData; diff --git a/contrib/llvm/lib/VMCore/Instructions.cpp b/contrib/llvm/lib/VMCore/Instructions.cpp index b3a7205..8db6ac9 100644 --- a/contrib/llvm/lib/VMCore/Instructions.cpp +++ b/contrib/llvm/lib/VMCore/Instructions.cpp @@ -625,40 +625,12 @@ void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { llvm_unreachable("ReturnInst has no successors!"); - return 0; } ReturnInst::~ReturnInst() { } //===----------------------------------------------------------------------===// -// UnwindInst Implementation -//===----------------------------------------------------------------------===// - -UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore) - : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, - 0, 0, InsertBefore) { -} -UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd) - : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, - 0, 0, InsertAtEnd) { -} - - -unsigned UnwindInst::getNumSuccessorsV() const { - return getNumSuccessors(); -} - -void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { - llvm_unreachable("UnwindInst has no successors!"); -} - -BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { - llvm_unreachable("UnwindInst has no successors!"); - return 0; -} - -//===----------------------------------------------------------------------===// // ResumeInst Implementation //===----------------------------------------------------------------------===// @@ -690,7 +662,6 @@ void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { llvm_unreachable("ResumeInst has no successors!"); - return 0; } //===----------------------------------------------------------------------===// @@ -712,12 +683,11 @@ unsigned UnreachableInst::getNumSuccessorsV() const { } void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { - llvm_unreachable("UnwindInst has no successors!"); + llvm_unreachable("UnreachableInst has no successors!"); } BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { - llvm_unreachable("UnwindInst has no successors!"); - return 0; + llvm_unreachable("UnreachableInst has no successors!"); } //===----------------------------------------------------------------------===// @@ -1359,6 +1329,15 @@ GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) /// template <typename IndexTy> static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { + if (Ptr->isVectorTy()) { + assert(IdxList.size() == 1 && + "GEP with vector pointers must have a single index"); + PointerType *PTy = dyn_cast<PointerType>( + cast<VectorType>(Ptr)->getElementType()); + assert(PTy && "Gep with invalid vector pointer found"); + return PTy->getElementType(); + } + PointerType *PTy = dyn_cast<PointerType>(Ptr); if (!PTy) return 0; // Type isn't a pointer type! Type *Agg = PTy->getElementType(); @@ -1366,7 +1345,7 @@ static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { // Handle the special case of the empty set index set, which is always valid. if (IdxList.empty()) return Agg; - + // If there is at least one index, the top level type must be sized, otherwise // it cannot be 'stepped over'. if (!Agg->isSized()) @@ -1396,6 +1375,18 @@ Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) { return getIndexedTypeInternal(Ptr, IdxList); } +unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) { + Type *Ty = Ptr->getType(); + + if (VectorType *VTy = dyn_cast<VectorType>(Ty)) + Ty = VTy->getElementType(); + + if (PointerType *PTy = dyn_cast<PointerType>(Ty)) + return PTy->getAddressSpace(); + + llvm_unreachable("Invalid GEP pointer type"); +} + /// hasAllZeroIndices - Return true if all of the indices of this GEP are /// zeros. If so, the result pointer and the first operand have the same /// value, just potentially different types. @@ -1558,46 +1549,84 @@ ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, const Value *Mask) { + // V1 and V2 must be vectors of the same type. if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) return false; + // Mask must be vector of i32. VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32)) return false; // Check to see if Mask is valid. + if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) + return true; + if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { - VectorType *VTy = cast<VectorType>(V1->getType()); + unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { - if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { - if (CI->uge(VTy->getNumElements()*2)) + if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { + if (CI->uge(V1Size*2)) return false; } else if (!isa<UndefValue>(MV->getOperand(i))) { return false; } } + return true; } - else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask)) - return false; - return true; + if (const ConstantDataSequential *CDS = + dyn_cast<ConstantDataSequential>(Mask)) { + unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); + for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) + if (CDS->getElementAsInteger(i) >= V1Size*2) + return false; + return true; + } + + // The bitcode reader can create a place holder for a forward reference + // used as the shuffle mask. When this occurs, the shuffle mask will + // fall into this case and fail. To avoid this error, do this bit of + // ugliness to allow such a mask pass. + if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask)) + if (CE->getOpcode() == Instruction::UserOp1) + return true; + + return false; } /// getMaskValue - Return the index from the shuffle mask for the specified /// output result. This is either -1 if the element is undef or a number less /// than 2*numelements. -int ShuffleVectorInst::getMaskValue(unsigned i) const { - const Constant *Mask = cast<Constant>(getOperand(2)); - if (isa<UndefValue>(Mask)) return -1; - if (isa<ConstantAggregateZero>(Mask)) return 0; - const ConstantVector *MaskCV = cast<ConstantVector>(Mask); - assert(i < MaskCV->getNumOperands() && "Index out of range"); - - if (isa<UndefValue>(MaskCV->getOperand(i))) +int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { + assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); + if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask)) + return CDS->getElementAsInteger(i); + Constant *C = Mask->getAggregateElement(i); + if (isa<UndefValue>(C)) return -1; - return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue(); + return cast<ConstantInt>(C)->getZExtValue(); +} + +/// getShuffleMask - Return the full mask for this instruction, where each +/// element is the element number and undef's are returned as -1. +void ShuffleVectorInst::getShuffleMask(Constant *Mask, + SmallVectorImpl<int> &Result) { + unsigned NumElts = Mask->getType()->getVectorNumElements(); + + if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) { + for (unsigned i = 0; i != NumElts; ++i) + Result.push_back(CDS->getElementAsInteger(i)); + return; + } + for (unsigned i = 0; i != NumElts; ++i) { + Constant *C = Mask->getAggregateElement(i); + Result.push_back(isa<UndefValue>(C) ? -1 : + cast<ConstantInt>(C)->getZExtValue()); + } } + //===----------------------------------------------------------------------===// // InsertValueInst Class //===----------------------------------------------------------------------===// @@ -1848,46 +1877,27 @@ BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, Instruction *InsertBefore) { Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); - return new BinaryOperator(Instruction::FSub, - zero, Op, + return new BinaryOperator(Instruction::FSub, zero, Op, Op->getType(), Name, InsertBefore); } BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, BasicBlock *InsertAtEnd) { Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); - return new BinaryOperator(Instruction::FSub, - zero, Op, + return new BinaryOperator(Instruction::FSub, zero, Op, Op->getType(), Name, InsertAtEnd); } BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, Instruction *InsertBefore) { - Constant *C; - if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { - C = Constant::getAllOnesValue(PTy->getElementType()); - C = ConstantVector::get( - std::vector<Constant*>(PTy->getNumElements(), C)); - } else { - C = Constant::getAllOnesValue(Op->getType()); - } - + Constant *C = Constant::getAllOnesValue(Op->getType()); return new BinaryOperator(Instruction::Xor, Op, C, Op->getType(), Name, InsertBefore); } BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, BasicBlock *InsertAtEnd) { - Constant *AllOnes; - if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { - // Create a vector of all ones values. - Constant *Elt = Constant::getAllOnesValue(PTy->getElementType()); - AllOnes = ConstantVector::get( - std::vector<Constant*>(PTy->getNumElements(), Elt)); - } else { - AllOnes = Constant::getAllOnesValue(Op->getType()); - } - + Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); return new BinaryOperator(Instruction::Xor, Op, AllOnes, Op->getType(), Name, InsertAtEnd); } @@ -1895,10 +1905,8 @@ BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, // isConstantAllOnes - Helper function for several functions below static inline bool isConstantAllOnes(const Value *V) { - if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) - return CI->isAllOnesValue(); - if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) - return CV->isAllOnesValue(); + if (const Constant *C = dyn_cast<Constant>(V)) + return C->isAllOnesValue(); return false; } @@ -1998,6 +2006,8 @@ bool BinaryOperator::isExact() const { // CastInst Class //===----------------------------------------------------------------------===// +void CastInst::anchor() {} + // Just determine if this cast only deals with integral->integral conversion. bool CastInst::isIntegerCast() const { switch (getOpcode()) { @@ -2042,8 +2052,7 @@ bool CastInst::isNoopCast(Instruction::CastOps Opcode, Type *DestTy, Type *IntPtrTy) { switch (Opcode) { - default: - assert(0 && "Invalid CastOp"); + default: llvm_unreachable("Invalid CastOp"); case Instruction::Trunc: case Instruction::ZExt: case Instruction::SExt: @@ -2236,13 +2245,10 @@ unsigned CastInst::isEliminableCastPair( case 99: // cast combination can't happen (error in input). This is for all cases // where the MidTy is not the same for the two cast instructions. - assert(0 && "Invalid Cast Combination"); - return 0; + llvm_unreachable("Invalid Cast Combination"); default: - assert(0 && "Error in CastResults table!!!"); - return 0; + llvm_unreachable("Error in CastResults table!!!"); } - return 0; } CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, @@ -2262,10 +2268,8 @@ CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); - default: - assert(0 && "Invalid opcode provided"); + default: llvm_unreachable("Invalid opcode provided"); } - return 0; } CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, @@ -2285,10 +2289,8 @@ CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); - default: - assert(0 && "Invalid opcode provided"); + default: llvm_unreachable("Invalid opcode provided"); } - return 0; } CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, @@ -2557,9 +2559,8 @@ CastInst::getCastOpcode( assert(DestBits == SrcBits && "Casting vector to floating point of different width"); return BitCast; // same size, no-op cast - } else { - llvm_unreachable("Casting pointer or non-first class to float"); } + llvm_unreachable("Casting pointer or non-first class to float"); } else if (DestTy->isVectorTy()) { assert(DestBits == SrcBits && "Illegal cast to vector (wrong type or size)"); @@ -2569,24 +2570,16 @@ CastInst::getCastOpcode( return BitCast; // ptr -> ptr } else if (SrcTy->isIntegerTy()) { return IntToPtr; // int -> ptr - } else { - assert(0 && "Casting pointer to other than pointer or int"); } + llvm_unreachable("Casting pointer to other than pointer or int"); } else if (DestTy->isX86_MMXTy()) { if (SrcTy->isVectorTy()) { assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); return BitCast; // 64-bit vector to MMX - } else { - assert(0 && "Illegal cast to X86_MMX"); } - } else { - assert(0 && "Casting to type that is not first-class"); + llvm_unreachable("Illegal cast to X86_MMX"); } - - // If we fall through to here we probably hit an assertion cast above - // and assertions are not turned on. Anything we return is an error, so - // BitCast is as good a choice as any. - return BitCast; + llvm_unreachable("Casting to type that is not first-class"); } //===----------------------------------------------------------------------===// @@ -2645,9 +2638,21 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && SrcLength == DstLength; case Instruction::PtrToInt: - return SrcTy->isPointerTy() && DstTy->isIntegerTy(); + if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) + return false; + if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) + if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) + return false; + return SrcTy->getScalarType()->isPointerTy() && + DstTy->getScalarType()->isIntegerTy(); case Instruction::IntToPtr: - return SrcTy->isIntegerTy() && DstTy->isPointerTy(); + if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) + return false; + if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) + if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) + return false; + return SrcTy->getScalarType()->isIntegerTy() && + DstTy->getScalarType()->isPointerTy(); case Instruction::BitCast: // BitCast implies a no-op cast of type only. No bits change. // However, you can't cast pointers to anything but pointers. @@ -2890,7 +2895,7 @@ bool CmpInst::isEquality() const { CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { switch (pred) { - default: assert(0 && "Unknown cmp predicate!"); + default: llvm_unreachable("Unknown cmp predicate!"); case ICMP_EQ: return ICMP_NE; case ICMP_NE: return ICMP_EQ; case ICMP_UGT: return ICMP_ULE; @@ -2923,7 +2928,7 @@ CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { switch (pred) { - default: assert(0 && "Unknown icmp predicate!"); + default: llvm_unreachable("Unknown icmp predicate!"); case ICMP_EQ: case ICMP_NE: case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: return pred; @@ -2936,7 +2941,7 @@ ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { switch (pred) { - default: assert(0 && "Unknown icmp predicate!"); + default: llvm_unreachable("Unknown icmp predicate!"); case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: return pred; @@ -3012,7 +3017,7 @@ ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { switch (pred) { - default: assert(0 && "Unknown cmp predicate!"); + default: llvm_unreachable("Unknown cmp predicate!"); case ICMP_EQ: case ICMP_NE: return pred; case ICMP_SGT: return ICMP_SLT; @@ -3147,31 +3152,32 @@ SwitchInst::~SwitchInst() { /// addCase - Add an entry to the switch instruction... /// void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { + unsigned NewCaseIdx = getNumCases(); unsigned OpNo = NumOperands; if (OpNo+2 > ReservedSpace) growOperands(); // Get more space! // Initialize some new operands. assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); NumOperands = OpNo+2; - OperandList[OpNo] = OnVal; - OperandList[OpNo+1] = Dest; + CaseIt Case(this, NewCaseIdx); + Case.setValue(OnVal); + Case.setSuccessor(Dest); } -/// removeCase - This method removes the specified successor from the switch -/// instruction. Note that this cannot be used to remove the default -/// destination (successor #0). -/// -void SwitchInst::removeCase(unsigned idx) { - assert(idx != 0 && "Cannot remove the default case!"); - assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); +/// removeCase - This method removes the specified case and its successor +/// from the switch instruction. +void SwitchInst::removeCase(CaseIt i) { + unsigned idx = i.getCaseIndex(); + + assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); unsigned NumOps = getNumOperands(); Use *OL = OperandList; // Overwrite this case with the end of the list. - if ((idx + 1) * 2 != NumOps) { - OL[idx * 2] = OL[NumOps - 2]; - OL[idx * 2 + 1] = OL[NumOps - 1]; + if (2 + (idx + 1) * 2 != NumOps) { + OL[2 + idx * 2] = OL[NumOps - 2]; + OL[2 + idx * 2 + 1] = OL[NumOps - 1]; } // Nuke the last value. @@ -3438,15 +3444,11 @@ ExtractElementInst *ExtractElementInst::clone_impl() const { } InsertElementInst *InsertElementInst::clone_impl() const { - return InsertElementInst::Create(getOperand(0), - getOperand(1), - getOperand(2)); + return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); } ShuffleVectorInst *ShuffleVectorInst::clone_impl() const { - return new ShuffleVectorInst(getOperand(0), - getOperand(1), - getOperand(2)); + return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); } PHINode *PHINode::clone_impl() const { @@ -3482,11 +3484,6 @@ ResumeInst *ResumeInst::clone_impl() const { return new(1) ResumeInst(*this); } -UnwindInst *UnwindInst::clone_impl() const { - LLVMContext &Context = getContext(); - return new UnwindInst(Context); -} - UnreachableInst *UnreachableInst::clone_impl() const { LLVMContext &Context = getContext(); return new UnreachableInst(Context); diff --git a/contrib/llvm/lib/VMCore/LLVMContext.cpp b/contrib/llvm/lib/VMCore/LLVMContext.cpp index ebd1e0a..68c5621 100644 --- a/contrib/llvm/lib/VMCore/LLVMContext.cpp +++ b/contrib/llvm/lib/VMCore/LLVMContext.cpp @@ -43,6 +43,16 @@ LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { // Create the 'prof' metadata kind. unsigned ProfID = getMDKindID("prof"); assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; + + // Create the 'fpaccuracy' metadata kind. + unsigned FPAccuracyID = getMDKindID("fpaccuracy"); + assert(FPAccuracyID == MD_fpaccuracy && "fpaccuracy kind id drifted"); + (void)FPAccuracyID; + + // Create the 'range' metadata kind. + unsigned RangeID = getMDKindID("range"); + assert(RangeID == MD_range && "range kind id drifted"); + (void)RangeID; } LLVMContext::~LLVMContext() { delete pImpl; } @@ -78,11 +88,11 @@ void *LLVMContext::getInlineAsmDiagnosticContext() const { return pImpl->InlineAsmDiagContext; } -void LLVMContext::emitError(StringRef ErrorStr) { +void LLVMContext::emitError(const Twine &ErrorStr) { emitError(0U, ErrorStr); } -void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) { +void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { unsigned LocCookie = 0; if (const MDNode *SrcLoc = I->getMetadata("srcloc")) { if (SrcLoc->getNumOperands() != 0) @@ -92,7 +102,7 @@ void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) { return emitError(LocCookie, ErrorStr); } -void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) { +void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { // If there is no error handler installed, just print the error and exit. if (pImpl->InlineAsmDiagHandler == 0) { errs() << "error: " << ErrorStr << "\n"; @@ -100,7 +110,7 @@ void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) { } // If we do have an error handler, we can report the error and keep going. - SMDiagnostic Diag("", "error: " + ErrorStr.str()); + SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str()); pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie); } diff --git a/contrib/llvm/lib/VMCore/LLVMContextImpl.cpp b/contrib/llvm/lib/VMCore/LLVMContextImpl.cpp index 504b372..6279bb8 100644 --- a/contrib/llvm/lib/VMCore/LLVMContextImpl.cpp +++ b/contrib/llvm/lib/VMCore/LLVMContextImpl.cpp @@ -21,6 +21,7 @@ LLVMContextImpl::LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID), + HalfTy(C, Type::HalfTyID), FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID), MetadataTy(C, Type::MetadataTyID), @@ -47,6 +48,16 @@ struct DropReferences { P.second->dropAllReferences(); } }; + +// Temporary - drops pair.first instead of second. +struct DropFirst { + // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' + // is a Constant*. + template<typename PairT> + void operator()(const PairT &P) { + P.first->dropAllReferences(); + } +}; } LLVMContextImpl::~LLVMContextImpl() { @@ -57,25 +68,32 @@ LLVMContextImpl::~LLVMContextImpl() { std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end()); DeleteContainerPointers(Modules); + // Free the constants. This is important to do here to ensure that they are + // freed before the LeakDetector is torn down. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), DropReferences()); std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), - DropReferences()); + DropFirst()); std::for_each(StructConstants.map_begin(), StructConstants.map_end(), - DropReferences()); + DropFirst()); std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), - DropReferences()); + DropFirst()); ExprConstants.freeConstants(); ArrayConstants.freeConstants(); StructConstants.freeConstants(); VectorConstants.freeConstants(); - AggZeroConstants.freeConstants(); - NullPtrConstants.freeConstants(); - UndefValueConstants.freeConstants(); + DeleteContainerSeconds(CAZConstants); + DeleteContainerSeconds(CPNConstants); + DeleteContainerSeconds(UVConstants); InlineAsms.freeConstants(); DeleteContainerSeconds(IntConstants); DeleteContainerSeconds(FPConstants); + for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), + E = CDSConstants.end(); I != E; ++I) + delete I->second; + CDSConstants.clear(); + // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet // and the NonUniquedMDNodes sets, so copy the values out first. SmallVector<MDNode*, 8> MDNodes; @@ -92,3 +110,24 @@ LLVMContextImpl::~LLVMContextImpl() { // Destroy MDStrings. DeleteContainerSeconds(MDStringCache); } + +// ConstantsContext anchors +void UnaryConstantExpr::anchor() { } + +void BinaryConstantExpr::anchor() { } + +void SelectConstantExpr::anchor() { } + +void ExtractElementConstantExpr::anchor() { } + +void InsertElementConstantExpr::anchor() { } + +void ShuffleVectorConstantExpr::anchor() { } + +void ExtractValueConstantExpr::anchor() { } + +void InsertValueConstantExpr::anchor() { } + +void GetElementPtrConstantExpr::anchor() { } + +void CompareConstantExpr::anchor() { } diff --git a/contrib/llvm/lib/VMCore/LLVMContextImpl.h b/contrib/llvm/lib/VMCore/LLVMContextImpl.h index a3f68fe..2252028 100644 --- a/contrib/llvm/lib/VMCore/LLVMContextImpl.h +++ b/contrib/llvm/lib/VMCore/LLVMContextImpl.h @@ -29,6 +29,7 @@ #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/StringMap.h" +#include "llvm/ADT/Hashing.h" #include <vector> namespace llvm { @@ -51,12 +52,14 @@ struct DenseMapAPIntKeyInfo { bool operator!=(const KeyTy& that) const { return !this->operator==(that); } + friend hash_code hash_value(const KeyTy &Key) { + return hash_combine(Key.type, Key.val); + } }; static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } static unsigned getHashValue(const KeyTy &Key) { - return DenseMapInfo<void*>::getHashValue(Key.type) ^ - Key.val.getHashValue(); + return static_cast<unsigned>(hash_value(Key)); } static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { return LHS == RHS; @@ -74,6 +77,9 @@ struct DenseMapAPFloatKeyInfo { bool operator!=(const KeyTy& that) const { return !this->operator==(that); } + friend hash_code hash_value(const KeyTy &Key) { + return hash_combine(Key.val); + } }; static inline KeyTy getEmptyKey() { return KeyTy(APFloat(APFloat::Bogus,1)); @@ -82,13 +88,132 @@ struct DenseMapAPFloatKeyInfo { return KeyTy(APFloat(APFloat::Bogus,2)); } static unsigned getHashValue(const KeyTy &Key) { - return Key.val.getHashValue(); + return static_cast<unsigned>(hash_value(Key)); } static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { return LHS == RHS; } }; +struct AnonStructTypeKeyInfo { + struct KeyTy { + ArrayRef<Type*> ETypes; + bool isPacked; + KeyTy(const ArrayRef<Type*>& E, bool P) : + ETypes(E), isPacked(P) {} + KeyTy(const KeyTy& that) : + ETypes(that.ETypes), isPacked(that.isPacked) {} + KeyTy(const StructType* ST) : + ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())), + isPacked(ST->isPacked()) {} + bool operator==(const KeyTy& that) const { + if (isPacked != that.isPacked) + return false; + if (ETypes != that.ETypes) + return false; + return true; + } + bool operator!=(const KeyTy& that) const { + return !this->operator==(that); + } + }; + static inline StructType* getEmptyKey() { + return DenseMapInfo<StructType*>::getEmptyKey(); + } + static inline StructType* getTombstoneKey() { + return DenseMapInfo<StructType*>::getTombstoneKey(); + } + static unsigned getHashValue(const KeyTy& Key) { + return hash_combine(hash_combine_range(Key.ETypes.begin(), + Key.ETypes.end()), + Key.isPacked); + } + static unsigned getHashValue(const StructType *ST) { + return getHashValue(KeyTy(ST)); + } + static bool isEqual(const KeyTy& LHS, const StructType *RHS) { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + return LHS == KeyTy(RHS); + } + static bool isEqual(const StructType *LHS, const StructType *RHS) { + return LHS == RHS; + } +}; + +struct FunctionTypeKeyInfo { + struct KeyTy { + const Type *ReturnType; + ArrayRef<Type*> Params; + bool isVarArg; + KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : + ReturnType(R), Params(P), isVarArg(V) {} + KeyTy(const KeyTy& that) : + ReturnType(that.ReturnType), + Params(that.Params), + isVarArg(that.isVarArg) {} + KeyTy(const FunctionType* FT) : + ReturnType(FT->getReturnType()), + Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())), + isVarArg(FT->isVarArg()) {} + bool operator==(const KeyTy& that) const { + if (ReturnType != that.ReturnType) + return false; + if (isVarArg != that.isVarArg) + return false; + if (Params != that.Params) + return false; + return true; + } + bool operator!=(const KeyTy& that) const { + return !this->operator==(that); + } + }; + static inline FunctionType* getEmptyKey() { + return DenseMapInfo<FunctionType*>::getEmptyKey(); + } + static inline FunctionType* getTombstoneKey() { + return DenseMapInfo<FunctionType*>::getTombstoneKey(); + } + static unsigned getHashValue(const KeyTy& Key) { + return hash_combine(Key.ReturnType, + hash_combine_range(Key.Params.begin(), + Key.Params.end()), + Key.isVarArg); + } + static unsigned getHashValue(const FunctionType *FT) { + return getHashValue(KeyTy(FT)); + } + static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + return LHS == KeyTy(RHS); + } + static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { + return LHS == RHS; + } +}; + +// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a +// shortcut to avoid comparing all operands. +template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> { + static bool Equals(const MDNode &X, const FoldingSetNodeID &ID, + unsigned IDHash, FoldingSetNodeID &TempID) { + assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?"); + // First, check if the cached hashes match. If they don't we can skip the + // expensive operand walk. + if (X.Hash != IDHash) + return false; + + // If they match we have to compare the operands. + X.Profile(TempID); + return TempID == ID; + } + static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) { + return X.Hash; // Return cached hash. + } +}; + /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp. class DebugRecVH : public CallbackVH { @@ -129,7 +254,7 @@ public: DenseMapAPFloatKeyInfo> FPMapTy; FPMapTy FPConstants; - StringMap<MDString*> MDStringCache; + StringMap<Value*> MDStringCache; FoldingSet<MDNode> MDNodeSet; // MDNodes may be uniqued or not uniqued. When they're not uniqued, they @@ -138,23 +263,23 @@ public: // on Context destruction. SmallPtrSet<MDNode*, 1> NonUniquedMDNodes; - ConstantUniqueMap<char, char, Type, ConstantAggregateZero> AggZeroConstants; + DenseMap<Type*, ConstantAggregateZero*> CAZConstants; - typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, - ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy; + typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy; ArrayConstantsTy ArrayConstants; - typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, - StructType, ConstantStruct, true /*largekey*/> StructConstantsTy; + typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy; StructConstantsTy StructConstants; - typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, - VectorType, ConstantVector> VectorConstantsTy; + typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy; VectorConstantsTy VectorConstants; - ConstantUniqueMap<char, char, PointerType, ConstantPointerNull> - NullPtrConstants; - ConstantUniqueMap<char, char, Type, UndefValue> UndefValueConstants; + DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; + + DenseMap<Type*, UndefValue*> UVConstants; + + StringMap<ConstantDataSequential*> CDSConstants; + DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses; ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> @@ -169,7 +294,7 @@ public: LeakDetectorImpl<Value> LLVMObjects; // Basic type instances. - Type VoidTy, LabelTy, FloatTy, DoubleTy, MetadataTy; + Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty; @@ -180,9 +305,10 @@ public: DenseMap<unsigned, IntegerType*> IntegerTypes; - // TODO: Optimize FunctionTypes/AnonStructTypes! - std::map<std::vector<Type*>, FunctionType*> FunctionTypes; - std::map<std::vector<Type*>, StructType*> AnonStructTypes; + typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap; + FunctionTypeMap FunctionTypes; + typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap; + StructTypeMap AnonStructTypes; StringMap<StructType*> NamedStructTypes; unsigned NamedStructTypesUniqueID; diff --git a/contrib/llvm/lib/VMCore/Metadata.cpp b/contrib/llvm/lib/VMCore/Metadata.cpp index ace4dc2..090b09a 100644 --- a/contrib/llvm/lib/VMCore/Metadata.cpp +++ b/contrib/llvm/lib/VMCore/Metadata.cpp @@ -29,16 +29,19 @@ using namespace llvm; // MDString implementation. // -MDString::MDString(LLVMContext &C, StringRef S) - : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {} +void MDString::anchor() { } + +MDString::MDString(LLVMContext &C) + : Value(Type::getMetadataTy(C), Value::MDStringVal) {} MDString *MDString::get(LLVMContext &Context, StringRef Str) { LLVMContextImpl *pImpl = Context.pImpl; - StringMapEntry<MDString *> &Entry = + StringMapEntry<Value*> &Entry = pImpl->MDStringCache.GetOrCreateValue(Str); - MDString *&S = Entry.getValue(); - if (!S) S = new MDString(Context, Entry.getKey()); - return S; + Value *&S = Entry.getValue(); + if (!S) S = new MDString(Context); + S->setValueName(&Entry); + return cast<MDString>(S); } //===----------------------------------------------------------------------===// @@ -48,14 +51,26 @@ MDString *MDString::get(LLVMContext &Context, StringRef Str) { // Use CallbackVH to hold MDNode operands. namespace llvm { class MDNodeOperand : public CallbackVH { - MDNode *Parent; + MDNode *getParent() { + MDNodeOperand *Cur = this; + + while (Cur->getValPtrInt() != 1) + --Cur; + + assert(Cur->getValPtrInt() == 1 && + "Couldn't find the beginning of the operand list!"); + return reinterpret_cast<MDNode*>(Cur) - 1; + } + public: - MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {} + MDNodeOperand(Value *V) : CallbackVH(V) {} ~MDNodeOperand() {} - void set(Value *V) { - setValPtr(V); - } + void set(Value *V) { this->setValPtr(V); } + + /// setAsFirstOperand - Accessor method to mark the operand as the first in + /// the list. + void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); } virtual void deleted(); virtual void allUsesReplacedWith(Value *NV); @@ -64,15 +79,13 @@ public: void MDNodeOperand::deleted() { - Parent->replaceOperand(this, 0); + getParent()->replaceOperand(this, 0); } void MDNodeOperand::allUsesReplacedWith(Value *NV) { - Parent->replaceOperand(this, NV); + getParent()->replaceOperand(this, NV); } - - //===----------------------------------------------------------------------===// // MDNode implementation. // @@ -85,6 +98,11 @@ static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { return reinterpret_cast<MDNodeOperand*>(N+1)+Op; } +void MDNode::replaceOperandWith(unsigned i, Value *Val) { + MDNodeOperand *Op = getOperandPtr(this, i); + replaceOperand(Op, Val); +} + MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) : Value(Type::getMetadataTy(C), Value::MDNodeVal) { NumOperands = Vals.size(); @@ -95,8 +113,13 @@ MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) // Initialize the operand list, which is co-allocated on the end of the node. unsigned i = 0; for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; - Op != E; ++Op, ++i) - new (Op) MDNodeOperand(Vals[i], this); + Op != E; ++Op, ++i) { + new (Op) MDNodeOperand(Vals[i]); + + // Mark the first MDNodeOperand as being the first in the list of operands. + if (i == 0) + Op->setAsFirstOperand(1); + } } @@ -161,12 +184,13 @@ static const Function *assertLocalFunction(const MDNode *N) { const Function *MDNode::getFunction() const { #ifndef NDEBUG return assertLocalFunction(this); -#endif +#else if (!isFunctionLocal()) return NULL; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) if (const Function *F = getFunctionForValue(getOperand(i))) return F; return NULL; +#endif } // destroy - Delete this node. Only when there are no uses. @@ -197,11 +221,11 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, ID.AddPointer(Vals[i]); void *InsertPoint; - MDNode *N = NULL; - - if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint))) + MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); + + if (N || !Insert) return N; - + bool isFunctionLocal = false; switch (FL) { case FL_Unknown: @@ -226,6 +250,9 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand)); N = new (Ptr) MDNode(Context, Vals, isFunctionLocal); + // Cache the operand hash. + N->Hash = ID.ComputeHash(); + // InsertPoint will have been set by the FindNodeOrInsertPos call. pImpl->MDNodeSet.InsertNode(N, InsertPoint); @@ -349,6 +376,8 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { return; } + // Cache the operand hash. + Hash = ID.ComputeHash(); // InsertPoint will have been set by the FindNodeOrInsertPos call. pImpl->MDNodeSet.InsertNode(this, InsertPoint); @@ -425,12 +454,12 @@ StringRef NamedMDNode::getName() const { // Instruction Metadata method implementations. // -void Instruction::setMetadata(const char *Kind, MDNode *Node) { +void Instruction::setMetadata(StringRef Kind, MDNode *Node) { if (Node == 0 && !hasMetadata()) return; setMetadata(getContext().getMDKindID(Kind), Node); } -MDNode *Instruction::getMetadataImpl(const char *Kind) const { +MDNode *Instruction::getMetadataImpl(StringRef Kind) const { return getMetadataImpl(getContext().getMDKindID(Kind)); } @@ -468,9 +497,11 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { } // Otherwise, we're removing metadata from an instruction. - assert(hasMetadataHashEntry() && - getContext().pImpl->MetadataStore.count(this) && + assert((hasMetadataHashEntry() == + getContext().pImpl->MetadataStore.count(this)) && "HasMetadata bit out of date!"); + if (!hasMetadataHashEntry()) + return; // Nothing to remove! LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; // Common case is removing the only entry. @@ -541,17 +572,15 @@ getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, getContext().pImpl->MetadataStore.count(this) && "Shouldn't have called this"); const LLVMContextImpl::MDMapTy &Info = - getContext().pImpl->MetadataStore.find(this)->second; + getContext().pImpl->MetadataStore.find(this)->second; assert(!Info.empty() && "Shouldn't have called this"); - Result.append(Info.begin(), Info.end()); - + // Sort the resulting array so it is stable. if (Result.size() > 1) array_pod_sort(Result.begin(), Result.end()); } - /// clearMetadataHashEntries - Clear all hashtable-based metadata from /// this instruction. void Instruction::clearMetadataHashEntries() { diff --git a/contrib/llvm/lib/VMCore/Module.cpp b/contrib/llvm/lib/VMCore/Module.cpp index c29029b..e8bc6db 100644 --- a/contrib/llvm/lib/VMCore/Module.cpp +++ b/contrib/llvm/lib/VMCore/Module.cpp @@ -321,11 +321,67 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { return NMD; } +/// eraseNamedMetadata - Remove the given NamedMDNode from this module and +/// delete it. void Module::eraseNamedMetadata(NamedMDNode *NMD) { static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); NamedMDList.erase(NMD); } +/// getModuleFlagsMetadata - Returns the module flags in the provided vector. +void Module:: +getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { + const NamedMDNode *ModFlags = getModuleFlagsMetadata(); + if (!ModFlags) return; + + for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) { + MDNode *Flag = ModFlags->getOperand(i); + ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); + MDString *Key = cast<MDString>(Flag->getOperand(1)); + Value *Val = Flag->getOperand(2); + Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), + Key, Val)); + } +} + +/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that +/// represents module-level flags. This method returns null if there are no +/// module-level flags. +NamedMDNode *Module::getModuleFlagsMetadata() const { + return getNamedMetadata("llvm.module.flags"); +} + +/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that +/// represents module-level flags. If module-level flags aren't found, it +/// creates the named metadata that contains them. +NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { + return getOrInsertNamedMetadata("llvm.module.flags"); +} + +/// addModuleFlag - Add a module-level flag to the module-level flags +/// metadata. It will create the module-level flags named metadata if it doesn't +/// already exist. +void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, + Value *Val) { + Type *Int32Ty = Type::getInt32Ty(Context); + Value *Ops[3] = { + ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val + }; + getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); +} +void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, + uint32_t Val) { + Type *Int32Ty = Type::getInt32Ty(Context); + addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); +} +void Module::addModuleFlag(MDNode *Node) { + assert(Node->getNumOperands() == 3 && + "Invalid number of operands for module flag!"); + assert(isa<ConstantInt>(Node->getOperand(0)) && + isa<MDString>(Node->getOperand(1)) && + "Invalid operand types for module flag!"); + getOrInsertModuleFlagsMetadata()->addOperand(Node); +} //===----------------------------------------------------------------------===// // Methods to control the materialization of GlobalValues in the Module. diff --git a/contrib/llvm/lib/VMCore/Pass.cpp b/contrib/llvm/lib/VMCore/Pass.cpp index 9afc540..994a7ff 100644 --- a/contrib/llvm/lib/VMCore/Pass.cpp +++ b/contrib/llvm/lib/VMCore/Pass.cpp @@ -25,11 +25,9 @@ using namespace llvm; // Pass Implementation // -Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { } - // Force out-of-line virtual method. -Pass::~Pass() { - delete Resolver; +Pass::~Pass() { + delete Resolver; } // Force out-of-line virtual method. @@ -48,7 +46,7 @@ bool Pass::mustPreserveAnalysisID(char &AID) const { return Resolver->getAnalysisIfAvailable(&AID, true) != 0; } -// dumpPassStructure - Implement the -debug-passes=Structure option +// dumpPassStructure - Implement the -debug-pass=Structure option void Pass::dumpPassStructure(unsigned Offset) { dbgs().indent(Offset*2) << getPassName() << "\n"; } @@ -71,7 +69,7 @@ void Pass::preparePassManager(PMStack &) { PassManagerType Pass::getPotentialPassManagerType() const { // Default implementation. - return PMT_Unknown; + return PMT_Unknown; } void Pass::getAnalysisUsage(AnalysisUsage &) const { @@ -155,9 +153,8 @@ PassManagerType FunctionPass::getPotentialPassManagerType() const { Pass *BasicBlockPass::createPrinterPass(raw_ostream &O, const std::string &Banner) const { - + llvm_unreachable("BasicBlockPass printing unsupported."); - return 0; } bool BasicBlockPass::doInitialization(Module &) { @@ -181,7 +178,7 @@ bool BasicBlockPass::doFinalization(Module &) { } PassManagerType BasicBlockPass::getPotentialPassManagerType() const { - return PMT_BasicBlockPassManager; + return PMT_BasicBlockPassManager; } const PassInfo *Pass::lookupPassInfo(const void *TI) { @@ -192,6 +189,13 @@ const PassInfo *Pass::lookupPassInfo(StringRef Arg) { return PassRegistry::getPassRegistry()->getPassInfo(Arg); } +Pass *Pass::createPass(AnalysisID ID) { + const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); + if (!PI) + return NULL; + return PI->createPass(); +} + Pass *PassInfo::createPass() const { assert((!isAnalysisGroup() || NormalCtor) && "No default implementation found for analysis group!"); @@ -246,7 +250,7 @@ namespace { typedef AnalysisUsage::VectorType VectorType; VectorType &CFGOnlyList; GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} - + void passEnumerate(const PassInfo *P) { if (P->isCFGOnlyPass()) CFGOnlyList.push_back(P->getTypeInfo()); diff --git a/contrib/llvm/lib/VMCore/PassManager.cpp b/contrib/llvm/lib/VMCore/PassManager.cpp index ecedb1d..28fbaa6 100644 --- a/contrib/llvm/lib/VMCore/PassManager.cpp +++ b/contrib/llvm/lib/VMCore/PassManager.cpp @@ -14,7 +14,6 @@ #include "llvm/PassManagers.h" #include "llvm/PassManager.h" -#include "llvm/DebugInfoProbe.h" #include "llvm/Assembly/PrintModulePass.h" #include "llvm/Assembly/Writer.h" #include "llvm/Support/CommandLine.h" @@ -26,7 +25,6 @@ #include "llvm/Support/PassNameParser.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/Mutex.h" -#include "llvm/ADT/StringMap.h" #include <algorithm> #include <map> using namespace llvm; @@ -84,32 +82,28 @@ PrintAfterAll("print-after-all", /// This is a helper to determine whether to print IR before or /// after a pass. -static bool ShouldPrintBeforeOrAfterPass(const void *PassID, +static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI, PassOptionList &PassesToPrint) { - if (const llvm::PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo(PassID)) { - for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) { - const llvm::PassInfo *PassInf = PassesToPrint[i]; - if (PassInf) - if (PassInf->getPassArgument() == PI->getPassArgument()) { - return true; - } - } + for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) { + const llvm::PassInfo *PassInf = PassesToPrint[i]; + if (PassInf) + if (PassInf->getPassArgument() == PI->getPassArgument()) { + return true; + } } return false; } - /// This is a utility to check whether a pass should have IR dumped /// before it. -static bool ShouldPrintBeforePass(const void *PassID) { - return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore); +static bool ShouldPrintBeforePass(const PassInfo *PI) { + return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore); } /// This is a utility to check whether a pass should have IR dumped /// after it. -static bool ShouldPrintAfterPass(const void *PassID) { - return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter); +static bool ShouldPrintAfterPass(const PassInfo *PI) { + return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter); } } // End of llvm namespace @@ -223,6 +217,7 @@ namespace llvm { class FunctionPassManagerImpl : public Pass, public PMDataManager, public PMTopLevelManager { + virtual void anchor(); private: bool wasRun; public: @@ -263,27 +258,15 @@ public: virtual PMDataManager *getAsPMDataManager() { return this; } virtual Pass *getAsPass() { return this; } + virtual PassManagerType getTopLevelPassManagerType() { + return PMT_FunctionPassManager; + } /// Pass Manager itself does not invalidate any analysis info. void getAnalysisUsage(AnalysisUsage &Info) const { Info.setPreservesAll(); } - void addTopLevelPass(Pass *P) { - if (ImmutablePass *IP = P->getAsImmutablePass()) { - // P is a immutable pass and it will be managed by this - // top level manager. Set up analysis resolver to connect them. - AnalysisResolver *AR = new AnalysisResolver(*this); - P->setResolver(AR); - initializeAnalysisImpl(P); - addImmutablePass(IP); - recordAvailableAnalysis(IP); - } else { - P->assignPassManager(activeStack, PMT_FunctionPassManager); - } - - } - FPPassManager *getContainedManager(unsigned N) { assert(N < PassManagers.size() && "Pass number out of range!"); FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); @@ -291,6 +274,8 @@ public: } }; +void FunctionPassManagerImpl::anchor() {} + char FunctionPassManagerImpl::ID = 0; //===----------------------------------------------------------------------===// @@ -384,6 +369,7 @@ char MPPassManager::ID = 0; class PassManagerImpl : public Pass, public PMDataManager, public PMTopLevelManager { + virtual void anchor(); public: static char ID; @@ -413,22 +399,11 @@ public: Info.setPreservesAll(); } - void addTopLevelPass(Pass *P) { - if (ImmutablePass *IP = P->getAsImmutablePass()) { - // P is a immutable pass and it will be managed by this - // top level manager. Set up analysis resolver to connect them. - AnalysisResolver *AR = new AnalysisResolver(*this); - P->setResolver(AR); - initializeAnalysisImpl(P); - addImmutablePass(IP); - recordAvailableAnalysis(IP); - } else { - P->assignPassManager(activeStack, PMT_ModulePassManager); - } - } - virtual PMDataManager *getAsPMDataManager() { return this; } virtual Pass *getAsPass() { return this; } + virtual PassManagerType getTopLevelPassManagerType() { + return PMT_ModulePassManager; + } MPPassManager *getContainedManager(unsigned N) { assert(N < PassManagers.size() && "Pass number out of range!"); @@ -437,26 +412,14 @@ public: } }; +void PassManagerImpl::anchor() {} + char PassManagerImpl::ID = 0; } // End of llvm namespace namespace { //===----------------------------------------------------------------------===// -// DebugInfoProbe - -static DebugInfoProbeInfo *TheDebugProbe; -static void createDebugInfoProbe() { - if (TheDebugProbe) return; - - // Constructed the first time this is called. This guarantees that the - // object will be constructed, if -enable-debug-info-probe is set, - // before static globals, thus it will be destroyed before them. - static ManagedStatic<DebugInfoProbeInfo> DIP; - TheDebugProbe = &*DIP; -} - -//===----------------------------------------------------------------------===// /// TimingInfo Class - This class is used to calculate information about the /// amount of time each pass takes to execute. This only happens when /// -time-passes is enabled on the command line. @@ -654,7 +617,32 @@ void PMTopLevelManager::schedulePass(Pass *P) { } // Now all required passes are available. - addTopLevelPass(P); + if (ImmutablePass *IP = P->getAsImmutablePass()) { + // P is a immutable pass and it will be managed by this + // top level manager. Set up analysis resolver to connect them. + PMDataManager *DM = getAsPMDataManager(); + AnalysisResolver *AR = new AnalysisResolver(*DM); + P->setResolver(AR); + DM->initializeAnalysisImpl(P); + addImmutablePass(IP); + DM->recordAvailableAnalysis(IP); + return; + } + + if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) { + Pass *PP = P->createPrinterPass( + dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***"); + PP->assignPassManager(activeStack, getTopLevelPassManagerType()); + } + + // Add the requested pass to the best available pass manager. + P->assignPassManager(activeStack, getTopLevelPassManagerType()); + + if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) { + Pass *PP = P->createPrinterPass( + dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***"); + PP->assignPassManager(activeStack, getTopLevelPassManagerType()); + } } /// Find the pass that implements Analysis AID. Search immutable @@ -1224,8 +1212,7 @@ void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { } Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) { - assert(0 && "Unable to find on the fly pass"); - return NULL; + llvm_unreachable("Unable to find on the fly pass"); } // Destructor @@ -1351,31 +1338,13 @@ FunctionPassManager::~FunctionPassManager() { delete FPM; } -/// addImpl - Add a pass to the queue of passes to run, without -/// checking whether to add a printer pass. -void FunctionPassManager::addImpl(Pass *P) { - FPM->add(P); -} - /// add - Add a pass to the queue of passes to run. This passes /// ownership of the Pass to the PassManager. When the /// PassManager_X is destroyed, the pass will be destroyed as well, so /// there is no need to delete the pass. (TODO delete passes.) /// This implies that all passes MUST be allocated with 'new'. void FunctionPassManager::add(Pass *P) { - // If this is a not a function pass, don't add a printer for it. - const void *PassID = P->getPassID(); - if (P->getPassKind() == PT_Function) - if (ShouldPrintBeforePass(PassID)) - addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ") - + P->getPassName() + " ***")); - - addImpl(P); - - if (P->getPassKind() == PT_Function) - if (ShouldPrintAfterPass(PassID)) - addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ") - + P->getPassName() + " ***")); + FPM->add(P); } /// run - Execute all of the passes scheduled for execution. Keep @@ -1455,7 +1424,6 @@ void FunctionPassManagerImpl::releaseMemoryOnTheFly() { bool FunctionPassManagerImpl::run(Function &F) { bool Changed = false; TimingInfo::createTheTimeInfo(); - createDebugInfoProbe(); initializeAllAnalysisInfo(); for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) @@ -1474,7 +1442,7 @@ bool FunctionPassManagerImpl::run(Function &F) { char FPPassManager::ID = 0; /// Print passes managed by this manager void FPPassManager::dumpPassStructure(unsigned Offset) { - llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; + dbgs().indent(Offset*2) << "FunctionPass Manager\n"; for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { FunctionPass *FP = getContainedPass(Index); FP->dumpPassStructure(Offset + 1); @@ -1503,16 +1471,13 @@ bool FPPassManager::runOnFunction(Function &F) { dumpRequiredSet(FP); initializeAnalysisImpl(FP); - if (TheDebugProbe) - TheDebugProbe->initialize(FP, F); + { PassManagerPrettyStackEntry X(FP, F); TimeRegion PassTimer(getPassTimer(FP)); LocalChanged |= FP->runOnFunction(F); } - if (TheDebugProbe) - TheDebugProbe->finalize(FP, F); Changed |= LocalChanged; if (LocalChanged) @@ -1662,7 +1627,6 @@ Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){ bool PassManagerImpl::run(Module &M) { bool Changed = false; TimingInfo::createTheTimeInfo(); - createDebugInfoProbe(); dumpArguments(); dumpPasses(); @@ -1687,27 +1651,12 @@ PassManager::~PassManager() { delete PM; } -/// addImpl - Add a pass to the queue of passes to run, without -/// checking whether to add a printer pass. -void PassManager::addImpl(Pass *P) { - PM->add(P); -} - /// add - Add a pass to the queue of passes to run. This passes ownership of /// the Pass to the PassManager. When the PassManager is destroyed, the pass /// will be destroyed as well, so there is no need to delete the pass. This /// implies that all passes MUST be allocated with 'new'. void PassManager::add(Pass *P) { - const void* PassID = P->getPassID(); - if (ShouldPrintBeforePass(PassID)) - addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ") - + P->getPassName() + " ***")); - - addImpl(P); - - if (ShouldPrintAfterPass(PassID)) - addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ") - + P->getPassName() + " ***")); + PM->add(P); } /// run - Execute all of the passes scheduled for execution. Keep track of @@ -1817,7 +1766,7 @@ void ModulePass::assignPassManager(PMStack &PMS, void FunctionPass::assignPassManager(PMStack &PMS, PassManagerType PreferredType) { - // Find Module Pass Manager + // Find Function Pass Manager while (!PMS.empty()) { if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) PMS.pop(); diff --git a/contrib/llvm/lib/VMCore/Type.cpp b/contrib/llvm/lib/VMCore/Type.cpp index 10184bc..c6f3558 100644 --- a/contrib/llvm/lib/VMCore/Type.cpp +++ b/contrib/llvm/lib/VMCore/Type.cpp @@ -25,6 +25,7 @@ using namespace llvm; Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) { switch (IDNumber) { case VoidTyID : return getVoidTy(C); + case HalfTyID : return getHalfTy(C); case FloatTyID : return getFloatTy(C); case DoubleTyID : return getDoubleTy(C); case X86_FP80TyID : return getX86_FP80Ty(C); @@ -57,7 +58,7 @@ bool Type::isIntegerTy(unsigned Bitwidth) const { bool Type::isIntOrIntVectorTy() const { if (isIntegerTy()) return true; - if (ID != Type::VectorTyID) return false; + if (getTypeID() != Type::VectorTyID) return false; return cast<VectorType>(this)->getElementType()->isIntegerTy(); } @@ -65,11 +66,12 @@ bool Type::isIntOrIntVectorTy() const { /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP types. /// bool Type::isFPOrFPVectorTy() const { - if (ID == Type::FloatTyID || ID == Type::DoubleTyID || - ID == Type::FP128TyID || ID == Type::X86_FP80TyID || - ID == Type::PPC_FP128TyID) + if (getTypeID() == Type::HalfTyID || getTypeID() == Type::FloatTyID || + getTypeID() == Type::DoubleTyID || + getTypeID() == Type::FP128TyID || getTypeID() == Type::X86_FP80TyID || + getTypeID() == Type::PPC_FP128TyID) return true; - if (ID != Type::VectorTyID) return false; + if (getTypeID() != Type::VectorTyID) return false; return cast<VectorType>(this)->getElementType()->isFloatingPointTy(); } @@ -131,6 +133,7 @@ bool Type::isEmptyTy() const { unsigned Type::getPrimitiveSizeInBits() const { switch (getTypeID()) { + case Type::HalfTyID: return 16; case Type::FloatTyID: return 32; case Type::DoubleTyID: return 64; case Type::X86_FP80TyID: return 80; @@ -157,11 +160,12 @@ int Type::getFPMantissaWidth() const { if (const VectorType *VTy = dyn_cast<VectorType>(this)) return VTy->getElementType()->getFPMantissaWidth(); assert(isFloatingPointTy() && "Not a floating point type!"); - if (ID == FloatTyID) return 24; - if (ID == DoubleTyID) return 53; - if (ID == X86_FP80TyID) return 64; - if (ID == FP128TyID) return 113; - assert(ID == PPC_FP128TyID && "unknown fp type"); + if (getTypeID() == HalfTyID) return 11; + if (getTypeID() == FloatTyID) return 24; + if (getTypeID() == DoubleTyID) return 53; + if (getTypeID() == X86_FP80TyID) return 64; + if (getTypeID() == FP128TyID) return 113; + assert(getTypeID() == PPC_FP128TyID && "unknown fp type"); return -1; } @@ -181,24 +185,69 @@ bool Type::isSizedDerivedType() const { if (!this->isStructTy()) return false; - // Opaque structs have no size. - if (cast<StructType>(this)->isOpaque()) - return false; - - // Okay, our struct is sized if all of the elements are. - for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I) - if (!(*I)->isSized()) - return false; + return cast<StructType>(this)->isSized(); +} - return true; +//===----------------------------------------------------------------------===// +// Subclass Helper Methods +//===----------------------------------------------------------------------===// + +unsigned Type::getIntegerBitWidth() const { + return cast<IntegerType>(this)->getBitWidth(); +} + +bool Type::isFunctionVarArg() const { + return cast<FunctionType>(this)->isVarArg(); +} + +Type *Type::getFunctionParamType(unsigned i) const { + return cast<FunctionType>(this)->getParamType(i); +} + +unsigned Type::getFunctionNumParams() const { + return cast<FunctionType>(this)->getNumParams(); +} + +StringRef Type::getStructName() const { + return cast<StructType>(this)->getName(); +} + +unsigned Type::getStructNumElements() const { + return cast<StructType>(this)->getNumElements(); +} + +Type *Type::getStructElementType(unsigned N) const { + return cast<StructType>(this)->getElementType(N); +} + + + +Type *Type::getSequentialElementType() const { + return cast<SequentialType>(this)->getElementType(); +} + +uint64_t Type::getArrayNumElements() const { + return cast<ArrayType>(this)->getNumElements(); +} + +unsigned Type::getVectorNumElements() const { + return cast<VectorType>(this)->getNumElements(); } +unsigned Type::getPointerAddressSpace() const { + return cast<PointerType>(this)->getAddressSpace(); +} + + + + //===----------------------------------------------------------------------===// // Primitive 'Type' data //===----------------------------------------------------------------------===// Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; } Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; } +Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; } Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; } Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; } Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; } @@ -217,6 +266,10 @@ IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) { return IntegerType::get(C, N); } +PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) { + return getHalfTy(C)->getPointerTo(AS); +} + PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) { return getFloatTy(C)->getPointerTo(AS); } @@ -328,23 +381,20 @@ FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params, // FunctionType::get - The factory function for the FunctionType class. FunctionType *FunctionType::get(Type *ReturnType, ArrayRef<Type*> Params, bool isVarArg) { - // TODO: This is brutally slow. - std::vector<Type*> Key; - Key.reserve(Params.size()+2); - Key.push_back(const_cast<Type*>(ReturnType)); - for (unsigned i = 0, e = Params.size(); i != e; ++i) - Key.push_back(const_cast<Type*>(Params[i])); - if (isVarArg) - Key.push_back(0); - LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; - FunctionType *&FT = pImpl->FunctionTypes[Key]; - - if (FT == 0) { + FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); + LLVMContextImpl::FunctionTypeMap::iterator I = + pImpl->FunctionTypes.find_as(Key); + FunctionType *FT; + + if (I == pImpl->FunctionTypes.end()) { FT = (FunctionType*) pImpl->TypeAllocator. - Allocate(sizeof(FunctionType) + sizeof(Type*)*(Params.size()+1), + Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1), AlignOf<FunctionType>::Alignment); new (FT) FunctionType(ReturnType, Params, isVarArg); + pImpl->FunctionTypes[FT] = true; + } else { + FT = I->first; } return FT; @@ -377,23 +427,22 @@ bool FunctionType::isValidArgumentType(Type *ArgTy) { StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, bool isPacked) { - // FIXME: std::vector is horribly inefficient for this probe. - std::vector<Type*> Key; - for (unsigned i = 0, e = ETypes.size(); i != e; ++i) { - assert(isValidElementType(ETypes[i]) && - "Invalid type for structure element!"); - Key.push_back(ETypes[i]); + LLVMContextImpl *pImpl = Context.pImpl; + AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); + LLVMContextImpl::StructTypeMap::iterator I = + pImpl->AnonStructTypes.find_as(Key); + StructType *ST; + + if (I == pImpl->AnonStructTypes.end()) { + // Value not found. Create a new type! + ST = new (Context.pImpl->TypeAllocator) StructType(Context); + ST->setSubclassData(SCDB_IsLiteral); // Literal struct. + ST->setBody(ETypes, isPacked); + Context.pImpl->AnonStructTypes[ST] = true; + } else { + ST = I->first; } - if (isPacked) - Key.push_back(0); - - StructType *&ST = Context.pImpl->AnonStructTypes[Key]; - if (ST) return ST; - - // Value not found. Create a new type! - ST = new (Context.pImpl->TypeAllocator) StructType(Context); - ST->setSubclassData(SCDB_IsLiteral); // Literal struct. - ST->setBody(ETypes, isPacked); + return ST; } @@ -403,13 +452,13 @@ void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) { setSubclassData(getSubclassData() | SCDB_HasBody); if (isPacked) setSubclassData(getSubclassData() | SCDB_Packed); - - Type **Elts = getContext().pImpl-> - TypeAllocator.Allocate<Type*>(Elements.size()); - memcpy(Elts, Elements.data(), sizeof(Elements[0])*Elements.size()); + + unsigned NumElements = Elements.size(); + Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements); + memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements); ContainedTys = Elts; - NumContainedTys = Elements.size(); + NumContainedTys = NumElements; } void StructType::setName(StringRef Name) { @@ -434,9 +483,10 @@ void StructType::setName(StringRef Name) { SmallString<64> TempStr(Name); TempStr.push_back('.'); raw_svector_ostream TmpStream(TempStr); + unsigned NameSize = Name.size(); do { - TempStr.resize(Name.size()+1); + TempStr.resize(NameSize + 1); TmpStream.resync(); TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; @@ -520,6 +570,26 @@ StructType *StructType::create(StringRef Name, Type *type, ...) { return llvm::StructType::create(Ctx, StructFields, Name); } +bool StructType::isSized() const { + if ((getSubclassData() & SCDB_IsSized) != 0) + return true; + if (isOpaque()) + return false; + + // Okay, our struct is sized if all of the elements are, but if one of the + // elements is opaque, the struct isn't sized *yet*, but may become sized in + // the future, so just bail out without caching. + for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) + if (!(*I)->isSized()) + return false; + + // Here we cheat a bit and cast away const-ness. The goal is to memoize when + // we find a sized type, as types can only move from opaque to sized, not the + // other way. + const_cast<StructType*>(this)->setSubclassData( + getSubclassData() | SCDB_IsSized); + return true; +} StringRef StructType::getName() const { assert(!isLiteral() && "Literal structs never have names"); @@ -664,6 +734,8 @@ VectorType *VectorType::get(Type *elementType, unsigned NumElements) { } bool VectorType::isValidElementType(Type *ElemTy) { + if (PointerType *PTy = dyn_cast<PointerType>(ElemTy)) + ElemTy = PTy->getElementType(); return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy(); } @@ -689,7 +761,12 @@ PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { PointerType::PointerType(Type *E, unsigned AddrSpace) : SequentialType(PointerTyID, E) { +#ifndef NDEBUG + const unsigned oldNCT = NumContainedTys; +#endif setSubclassData(AddrSpace); + // Check for miscompile. PR11652. + assert(oldNCT == NumContainedTys && "bitfield written out of bounds?"); } PointerType *Type::getPointerTo(unsigned addrs) { diff --git a/contrib/llvm/lib/VMCore/Use.cpp b/contrib/llvm/lib/VMCore/Use.cpp index 359a151..0128adc 100644 --- a/contrib/llvm/lib/VMCore/Use.cpp +++ b/contrib/llvm/lib/VMCore/Use.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Value.h" +#include <new> namespace llvm { diff --git a/contrib/llvm/lib/VMCore/User.cpp b/contrib/llvm/lib/VMCore/User.cpp index f01fa34..5f35ce4 100644 --- a/contrib/llvm/lib/VMCore/User.cpp +++ b/contrib/llvm/lib/VMCore/User.cpp @@ -17,6 +17,8 @@ namespace llvm { // User Class //===----------------------------------------------------------------------===// +void User::anchor() {} + // replaceUsesOfWith - Replaces all references to the "From" definition with // references to the "To" definition. // diff --git a/contrib/llvm/lib/VMCore/Value.cpp b/contrib/llvm/lib/VMCore/Value.cpp index 2fa5f08..4006b2c 100644 --- a/contrib/llvm/lib/VMCore/Value.cpp +++ b/contrib/llvm/lib/VMCore/Value.cpp @@ -66,7 +66,7 @@ Value::~Value() { // a <badref> // if (!use_empty()) { - dbgs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n"; + dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) dbgs() << "Use still stuck around after Def is destroyed:" << **I << "\n"; @@ -76,7 +76,7 @@ Value::~Value() { // If this value is named, destroy the name. This should not be in a symtab // at this point. - if (Name) + if (Name && SubclassID != MDStringVal) Name->Destroy(); // There should be no uses of this object anymore, remove it. @@ -108,6 +108,19 @@ bool Value::hasNUsesOrMore(unsigned N) const { /// isUsedInBasicBlock - Return true if this value is used in the specified /// basic block. bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { + // Start by scanning over the instructions looking for a use before we start + // the expensive use iteration. + unsigned MaxBlockSize = 3; + for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { + if (std::find(I->op_begin(), I->op_end(), this) != I->op_end()) + return true; + if (MaxBlockSize-- == 0) // If the block is larger fall back to use_iterator + break; + } + + if (MaxBlockSize != 0) // We scanned the entire block and found no use. + return false; + for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { const Instruction *User = dyn_cast<Instruction>(*I); if (User && User->getParent() == BB) @@ -156,11 +169,10 @@ StringRef Value::getName() const { return Name->getKey(); } -std::string Value::getNameStr() const { - return getName().str(); -} - void Value::setName(const Twine &NewName) { + assert(SubclassID != MDStringVal && + "Cannot set the name of MDString with this method!"); + // Fast path for common IRBuilder case of setName("") when there is no name. if (NewName.isTriviallyEmpty() && !hasName()) return; @@ -219,6 +231,8 @@ void Value::setName(const Twine &NewName) { /// takeName - transfer the name from V to this value, setting V's name to /// empty. It is an error to call V->takeName(V). void Value::takeName(Value *V) { + assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); + ValueSymbolTable *ST = 0; // If this value has a name, drop it. if (hasName()) { @@ -308,20 +322,40 @@ void Value::replaceAllUsesWith(Value *New) { BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); } -Value *Value::stripPointerCasts() { - if (!getType()->isPointerTy()) - return this; +namespace { +// Various metrics for how much to strip off of pointers. +enum PointerStripKind { + PSK_ZeroIndices, + PSK_InBoundsConstantIndices, + PSK_InBounds +}; + +template <PointerStripKind StripKind> +static Value *stripPointerCastsAndOffsets(Value *V) { + if (!V->getType()->isPointerTy()) + return V; // Even though we don't look through PHI nodes, we could be called on an // instruction in an unreachable block, which may be on a cycle. SmallPtrSet<Value *, 4> Visited; - Value *V = this; Visited.insert(V); do { if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { - if (!GEP->hasAllZeroIndices()) - return V; + switch (StripKind) { + case PSK_ZeroIndices: + if (!GEP->hasAllZeroIndices()) + return V; + break; + case PSK_InBoundsConstantIndices: + if (!GEP->hasAllConstantIndices()) + return V; + // fallthrough + case PSK_InBounds: + if (!GEP->isInBounds()) + return V; + break; + } V = GEP->getPointerOperand(); } else if (Operator::getOpcode(V) == Instruction::BitCast) { V = cast<Operator>(V)->getOperand(0); @@ -337,10 +371,24 @@ Value *Value::stripPointerCasts() { return V; } +} // namespace + +Value *Value::stripPointerCasts() { + return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); +} + +Value *Value::stripInBoundsConstantOffsets() { + return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); +} + +Value *Value::stripInBoundsOffsets() { + return stripPointerCastsAndOffsets<PSK_InBounds>(this); +} /// isDereferenceablePointer - Test if this value is always a pointer to /// allocated and suitably aligned memory for a simple load or store. -bool Value::isDereferenceablePointer() const { +static bool isDereferenceablePointer(const Value *V, + SmallPtrSet<const Value *, 32> &Visited) { // Note that it is not safe to speculate into a malloc'd region because // malloc may return null. // It's also not always safe to follow a bitcast, for example: @@ -349,20 +397,22 @@ bool Value::isDereferenceablePointer() const { // be handled using TargetData to check sizes and alignments though. // These are obviously ok. - if (isa<AllocaInst>(this)) return true; + if (isa<AllocaInst>(V)) return true; // Global variables which can't collapse to null are ok. - if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) + if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) return !GV->hasExternalWeakLinkage(); // byval arguments are ok. - if (const Argument *A = dyn_cast<Argument>(this)) + if (const Argument *A = dyn_cast<Argument>(V)) return A->hasByValAttr(); - + // For GEPs, determine if the indexing lands within the allocated object. - if (const GEPOperator *GEP = dyn_cast<GEPOperator>(this)) { + if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { // Conservatively require that the base pointer be fully dereferenceable. - if (!GEP->getOperand(0)->isDereferenceablePointer()) + if (!Visited.insert(GEP->getOperand(0))) + return false; + if (!isDereferenceablePointer(GEP->getOperand(0), Visited)) return false; // Check the indices. gep_type_iterator GTI = gep_type_begin(GEP); @@ -396,6 +446,13 @@ bool Value::isDereferenceablePointer() const { return false; } +/// isDereferenceablePointer - Test if this value is always a pointer to +/// allocated and suitably aligned memory for a simple load or store. +bool Value::isDereferenceablePointer() const { + SmallPtrSet<const Value *, 32> Visited; + return ::isDereferenceablePointer(this, Visited); +} + /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, /// return the value in the PHI node corresponding to PredBB. If not, return /// ourself. This is useful if you want to know the value something has in a @@ -425,7 +482,7 @@ void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { setPrevPtr(List); if (Next) { Next->setPrevPtr(&Next); - assert(VP == Next->VP && "Added to wrong list?"); + assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); } } @@ -441,14 +498,14 @@ void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { /// AddToUseList - Add this ValueHandle to the use list for VP. void ValueHandleBase::AddToUseList() { - assert(VP && "Null pointer doesn't have a use list!"); + assert(VP.getPointer() && "Null pointer doesn't have a use list!"); - LLVMContextImpl *pImpl = VP->getContext().pImpl; + LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; - if (VP->HasValueHandle) { + if (VP.getPointer()->HasValueHandle) { // If this value already has a ValueHandle, then it must be in the // ValueHandles map already. - ValueHandleBase *&Entry = pImpl->ValueHandles[VP]; + ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; assert(Entry != 0 && "Value doesn't have any handles?"); AddToExistingUseList(&Entry); return; @@ -462,10 +519,10 @@ void ValueHandleBase::AddToUseList() { DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); - ValueHandleBase *&Entry = Handles[VP]; + ValueHandleBase *&Entry = Handles[VP.getPointer()]; assert(Entry == 0 && "Value really did already have handles?"); AddToExistingUseList(&Entry); - VP->HasValueHandle = true; + VP.getPointer()->HasValueHandle = true; // If reallocation didn't happen or if this was the first insertion, don't // walk the table. @@ -477,14 +534,16 @@ void ValueHandleBase::AddToUseList() { // Okay, reallocation did happen. Fix the Prev Pointers. for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), E = Handles.end(); I != E; ++I) { - assert(I->second && I->first == I->second->VP && "List invariant broken!"); + assert(I->second && I->first == I->second->VP.getPointer() && + "List invariant broken!"); I->second->setPrevPtr(&I->second); } } /// RemoveFromUseList - Remove this ValueHandle from its current use list. void ValueHandleBase::RemoveFromUseList() { - assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!"); + assert(VP.getPointer() && VP.getPointer()->HasValueHandle && + "Pointer doesn't have a use list!"); // Unlink this from its use list. ValueHandleBase **PrevPtr = getPrevPtr(); @@ -500,11 +559,11 @@ void ValueHandleBase::RemoveFromUseList() { // If the Next pointer was null, then it is possible that this was the last // ValueHandle watching VP. If so, delete its entry from the ValueHandles // map. - LLVMContextImpl *pImpl = VP->getContext().pImpl; + LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; if (Handles.isPointerIntoBucketsArray(PrevPtr)) { - Handles.erase(VP); - VP->HasValueHandle = false; + Handles.erase(VP.getPointer()); + VP.getPointer()->HasValueHandle = false; } } @@ -554,7 +613,7 @@ void ValueHandleBase::ValueIsDeleted(Value *V) { // All callbacks, weak references, and assertingVHs should be dropped by now. if (V->HasValueHandle) { #ifndef NDEBUG // Only in +Asserts mode... - dbgs() << "While deleting: " << *V->getType() << " %" << V->getNameStr() + dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() << "\n"; if (pImpl->ValueHandles[V]->getKind() == Assert) llvm_unreachable("An asserting value handle still pointed to this" @@ -617,8 +676,8 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { case Tracking: case Weak: dbgs() << "After RAUW from " << *Old->getType() << " %" - << Old->getNameStr() << " to " << *New->getType() << " %" - << New->getNameStr() << "\n"; + << Old->getName() << " to " << *New->getType() << " %" + << New->getName() << "\n"; llvm_unreachable("A tracking or weak value handle still pointed to the" " old value!\n"); default: diff --git a/contrib/llvm/lib/VMCore/ValueTypes.cpp b/contrib/llvm/lib/VMCore/ValueTypes.cpp index e13bd7d..9a8e185 100644 --- a/contrib/llvm/lib/VMCore/ValueTypes.cpp +++ b/contrib/llvm/lib/VMCore/ValueTypes.cpp @@ -87,8 +87,7 @@ unsigned EVT::getExtendedSizeInBits() const { return ITy->getBitWidth(); if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy)) return VTy->getBitWidth(); - assert(false && "Unrecognized extended type!"); - return 0; // Suppress warnings. + llvm_unreachable("Unrecognized extended type!"); } /// getEVTString - This function returns value type as a string, e.g. "i32". @@ -101,13 +100,13 @@ std::string EVT::getEVTString() const { if (isInteger()) return "i" + utostr(getSizeInBits()); llvm_unreachable("Invalid EVT!"); - return "?"; case MVT::i1: return "i1"; case MVT::i8: return "i8"; case MVT::i16: return "i16"; case MVT::i32: return "i32"; case MVT::i64: return "i64"; case MVT::i128: return "i128"; + case MVT::f16: return "f16"; case MVT::f32: return "f32"; case MVT::f64: return "f64"; case MVT::f80: return "f80"; @@ -134,12 +133,13 @@ std::string EVT::getEVTString() const { case MVT::v4i64: return "v4i64"; case MVT::v8i64: return "v8i64"; case MVT::v2f32: return "v2f32"; + case MVT::v2f16: return "v2f16"; case MVT::v4f32: return "v4f32"; case MVT::v8f32: return "v8f32"; case MVT::v2f64: return "v2f64"; case MVT::v4f64: return "v4f64"; case MVT::Metadata:return "Metadata"; - case MVT::untyped: return "untyped"; + case MVT::Untyped: return "Untyped"; } } @@ -158,6 +158,7 @@ Type *EVT::getTypeForEVT(LLVMContext &Context) const { case MVT::i32: return Type::getInt32Ty(Context); case MVT::i64: return Type::getInt64Ty(Context); case MVT::i128: return IntegerType::get(Context, 128); + case MVT::f16: return Type::getHalfTy(Context); case MVT::f32: return Type::getFloatTy(Context); case MVT::f64: return Type::getDoubleTy(Context); case MVT::f80: return Type::getX86_FP80Ty(Context); @@ -180,6 +181,7 @@ Type *EVT::getTypeForEVT(LLVMContext &Context) const { case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2); case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4); case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8); + case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2); case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2); case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4); case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8); @@ -197,11 +199,11 @@ EVT EVT::getEVT(Type *Ty, bool HandleUnknown){ default: if (HandleUnknown) return MVT(MVT::Other); llvm_unreachable("Unknown type!"); - return MVT::isVoid; case Type::VoidTyID: return MVT::isVoid; case Type::IntegerTyID: return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth()); + case Type::HalfTyID: return MVT(MVT::f16); case Type::FloatTyID: return MVT(MVT::f32); case Type::DoubleTyID: return MVT(MVT::f64); case Type::X86_FP80TyID: return MVT(MVT::f80); diff --git a/contrib/llvm/lib/VMCore/Verifier.cpp b/contrib/llvm/lib/VMCore/Verifier.cpp index 9564b7d..96492e4 100644 --- a/contrib/llvm/lib/VMCore/Verifier.cpp +++ b/contrib/llvm/lib/VMCore/Verifier.cpp @@ -1,4 +1,4 @@ -//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// +//===-- Verifier.cpp - Implement the Module Verifier -----------------------==// // // The LLVM Compiler Infrastructure // @@ -51,6 +51,7 @@ #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" #include "llvm/IntrinsicInst.h" +#include "llvm/LLVMContext.h" #include "llvm/Metadata.h" #include "llvm/Module.h" #include "llvm/Pass.h" @@ -117,7 +118,6 @@ namespace { struct Verifier : public FunctionPass, public InstVisitor<Verifier> { static char ID; // Pass ID, replacement for typeid bool Broken; // Is this module found to be broken? - bool RealPass; // Are we not being run by a PassManager? VerifierFailureAction action; // What to do if verification fails. Module *Mod; // Module we are verifying right now @@ -143,13 +143,13 @@ namespace { const Value *PersonalityFn; Verifier() - : FunctionPass(ID), Broken(false), RealPass(true), + : FunctionPass(ID), Broken(false), action(AbortProcessAction), Mod(0), Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) { initializeVerifierPass(*PassRegistry::getPassRegistry()); } explicit Verifier(VerifierFailureAction ctn) - : FunctionPass(ID), Broken(false), RealPass(true), action(ctn), Mod(0), + : FunctionPass(ID), Broken(false), action(ctn), Mod(0), Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) { initializeVerifierPass(*PassRegistry::getPassRegistry()); } @@ -158,17 +158,14 @@ namespace { Mod = &M; Context = &M.getContext(); - // If this is a real pass, in a pass manager, we must abort before - // returning back to the pass manager, or else the pass manager may try to - // run other passes on the broken module. - if (RealPass) - return abortIfBroken(); - return false; + // We must abort before returning back to the pass manager, or else the + // pass manager may try to run other passes on the broken module. + return abortIfBroken(); } bool runOnFunction(Function &F) { // Get dominator information if we are being run by PassManager - if (RealPass) DT = &getAnalysis<DominatorTree>(); + DT = &getAnalysis<DominatorTree>(); Mod = F.getParent(); if (!Context) Context = &F.getContext(); @@ -177,13 +174,9 @@ namespace { InstsInThisBlock.clear(); PersonalityFn = 0; - // If this is a real pass, in a pass manager, we must abort before - // returning back to the pass manager, or else the pass manager may try to - // run other passes on the broken module. - if (RealPass) - return abortIfBroken(); - - return false; + // We must abort before returning back to the pass manager, or else the + // pass manager may try to run other passes on the broken module. + return abortIfBroken(); } bool doFinalization(Module &M) { @@ -214,8 +207,7 @@ namespace { virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequiredID(PreVerifyID); - if (RealPass) - AU.addRequired<DominatorTree>(); + AU.addRequired<DominatorTree>(); } /// abortIfBroken - If the module is broken and we are supposed to abort on @@ -225,7 +217,6 @@ namespace { if (!Broken) return false; MessagesStr << "Broken module found, "; switch (action) { - default: llvm_unreachable("Unknown action"); case AbortProcessAction: MessagesStr << "compilation aborted!\n"; dbgs() << MessagesStr.str(); @@ -239,6 +230,7 @@ namespace { MessagesStr << "compilation terminated.\n"; return true; } + llvm_unreachable("Invalid action"); } @@ -279,6 +271,7 @@ namespace { void visitGetElementPtrInst(GetElementPtrInst &GEP); void visitLoadInst(LoadInst &LI); void visitStoreInst(StoreInst &SI); + void verifyDominatesUse(Instruction &I, unsigned i); void visitInstruction(Instruction &I); void visitTerminatorInst(TerminatorInst &I); void visitBranchInst(BranchInst &BI); @@ -547,7 +540,7 @@ void Verifier::VerifyParameterAttrs(Attributes Attrs, Type *Ty, for (unsigned i = 0; i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { Attributes MutI = Attrs & Attribute::MutuallyIncompatible[i]; - Assert1(!(MutI & (MutI - 1)), "Attributes " + + Assert1(MutI.isEmptyOrSingleton(), "Attributes " + Attribute::getAsString(MutI) + " are incompatible!", V); } @@ -607,7 +600,7 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, for (unsigned i = 0; i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { Attributes MutI = FAttrs & Attribute::MutuallyIncompatible[i]; - Assert1(!(MutI & (MutI - 1)), "Attributes " + + Assert1(MutI.isEmptyOrSingleton(), "Attributes " + Attribute::getAsString(MutI) + " are incompatible!", V); } } @@ -812,11 +805,11 @@ void Verifier::visitSwitchInst(SwitchInst &SI) { // have the same type as the switched-on value. Type *SwitchTy = SI.getCondition()->getType(); SmallPtrSet<ConstantInt*, 32> Constants; - for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) { - Assert1(SI.getCaseValue(i)->getType() == SwitchTy, + for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) { + Assert1(i.getCaseValue()->getType() == SwitchTy, "Switch constants must all be same type as switch value!", &SI); - Assert2(Constants.insert(SI.getCaseValue(i)), - "Duplicate integer as switch case", &SI, SI.getCaseValue(i)); + Assert2(Constants.insert(i.getCaseValue()), + "Duplicate integer as switch case", &SI, i.getCaseValue()); } visitTerminatorInst(SI); @@ -1035,8 +1028,19 @@ void Verifier::visitPtrToIntInst(PtrToIntInst &I) { Type *SrcTy = I.getOperand(0)->getType(); Type *DestTy = I.getType(); - Assert1(SrcTy->isPointerTy(), "PtrToInt source must be pointer", &I); - Assert1(DestTy->isIntegerTy(), "PtrToInt result must be integral", &I); + Assert1(SrcTy->getScalarType()->isPointerTy(), + "PtrToInt source must be pointer", &I); + Assert1(DestTy->getScalarType()->isIntegerTy(), + "PtrToInt result must be integral", &I); + Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), + "PtrToInt type mismatch", &I); + + if (SrcTy->isVectorTy()) { + VectorType *VSrc = dyn_cast<VectorType>(SrcTy); + VectorType *VDest = dyn_cast<VectorType>(DestTy); + Assert1(VSrc->getNumElements() == VDest->getNumElements(), + "PtrToInt Vector width mismatch", &I); + } visitInstruction(I); } @@ -1046,9 +1050,18 @@ void Verifier::visitIntToPtrInst(IntToPtrInst &I) { Type *SrcTy = I.getOperand(0)->getType(); Type *DestTy = I.getType(); - Assert1(SrcTy->isIntegerTy(), "IntToPtr source must be an integral", &I); - Assert1(DestTy->isPointerTy(), "IntToPtr result must be a pointer",&I); - + Assert1(SrcTy->getScalarType()->isIntegerTy(), + "IntToPtr source must be an integral", &I); + Assert1(DestTy->getScalarType()->isPointerTy(), + "IntToPtr result must be a pointer",&I); + Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), + "IntToPtr type mismatch", &I); + if (SrcTy->isVectorTy()) { + VectorType *VSrc = dyn_cast<VectorType>(SrcTy); + VectorType *VDest = dyn_cast<VectorType>(DestTy); + Assert1(VSrc->getNumElements() == VDest->getNumElements(), + "IntToPtr Vector width mismatch", &I); + } visitInstruction(I); } @@ -1245,7 +1258,7 @@ void Verifier::visitICmpInst(ICmpInst &IC) { Assert1(Op0Ty == Op1Ty, "Both operands to ICmp instruction are not of the same type!", &IC); // Check that the operands are the right type - Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPointerTy(), + Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(), "Invalid operand types for ICmp instruction", &IC); // Check that the predicate is valid. Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE && @@ -1295,17 +1308,41 @@ void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) { } void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { - Assert1(cast<PointerType>(GEP.getOperand(0)->getType()) - ->getElementType()->isSized(), + Type *TargetTy = GEP.getPointerOperandType()->getScalarType(); + + Assert1(isa<PointerType>(TargetTy), + "GEP base pointer is not a vector or a vector of pointers", &GEP); + Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(), "GEP into unsized type!", &GEP); - + SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); Type *ElTy = - GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), Idxs); + GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs); Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); - Assert2(GEP.getType()->isPointerTy() && - cast<PointerType>(GEP.getType())->getElementType() == ElTy, - "GEP is not of right type for indices!", &GEP, ElTy); + + if (GEP.getPointerOperandType()->isPointerTy()) { + // Validate GEPs with scalar indices. + Assert2(GEP.getType()->isPointerTy() && + cast<PointerType>(GEP.getType())->getElementType() == ElTy, + "GEP is not of right type for indices!", &GEP, ElTy); + } else { + // Validate GEPs with a vector index. + Assert1(Idxs.size() == 1, "Invalid number of indices!", &GEP); + Value *Index = Idxs[0]; + Type *IndexTy = Index->getType(); + Assert1(IndexTy->isVectorTy(), + "Vector GEP must have vector indices!", &GEP); + Assert1(GEP.getType()->isVectorTy(), + "Vector GEP must return a vector value", &GEP); + Type *ElemPtr = cast<VectorType>(GEP.getType())->getElementType(); + Assert1(ElemPtr->isPointerTy(), + "Vector GEP pointer operand is not a pointer!", &GEP); + unsigned IndexWidth = cast<VectorType>(IndexTy)->getNumElements(); + unsigned GepWidth = cast<VectorType>(GEP.getType())->getNumElements(); + Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP); + Assert1(ElTy == cast<PointerType>(ElemPtr)->getElementType(), + "Vector GEP type does not match pointer type!", &GEP); + } visitInstruction(GEP); } @@ -1324,6 +1361,25 @@ void Verifier::visitLoadInst(LoadInst &LI) { Assert1(LI.getSynchScope() == CrossThread, "Non-atomic load cannot have SynchronizationScope specified", &LI); } + + if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) { + unsigned NumOperands = Range->getNumOperands(); + Assert1(NumOperands % 2 == 0, "Unfinished range!", Range); + unsigned NumRanges = NumOperands / 2; + Assert1(NumRanges >= 1, "It should have at least one range!", Range); + for (unsigned i = 0; i < NumRanges; ++i) { + ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i)); + Assert1(Low, "The lower limit must be an integer!", Low); + ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1)); + Assert1(High, "The upper limit must be an integer!", High); + Assert1(High->getType() == Low->getType() && + High->getType() == ElTy, "Range types must match load type!", + &LI); + Assert1(High->getValue() != Low->getValue(), "Range must not be empty!", + Range); + } + } + visitInstruction(LI); } @@ -1468,6 +1524,58 @@ void Verifier::visitLandingPadInst(LandingPadInst &LPI) { visitInstruction(LPI); } +void Verifier::verifyDominatesUse(Instruction &I, unsigned i) { + Instruction *Op = cast<Instruction>(I.getOperand(i)); + BasicBlock *BB = I.getParent(); + BasicBlock *OpBlock = Op->getParent(); + PHINode *PN = dyn_cast<PHINode>(&I); + + // DT can handle non phi instructions for us. + if (!PN) { + // Definition must dominate use unless use is unreachable! + Assert2(InstsInThisBlock.count(Op) || !DT->isReachableFromEntry(BB) || + DT->dominates(Op, &I), + "Instruction does not dominate all uses!", Op, &I); + return; + } + + // Check that a definition dominates all of its uses. + if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { + // Invoke results are only usable in the normal destination, not in the + // exceptional destination. + BasicBlock *NormalDest = II->getNormalDest(); + + + // PHI nodes differ from other nodes because they actually "use" the + // value in the predecessor basic blocks they correspond to. + BasicBlock *UseBlock = BB; + unsigned j = PHINode::getIncomingValueNumForOperand(i); + UseBlock = PN->getIncomingBlock(j); + Assert2(UseBlock, "Invoke operand is PHI node with bad incoming-BB", + Op, &I); + + if (UseBlock == OpBlock) { + // Special case of a phi node in the normal destination or the unwind + // destination. + Assert2(BB == NormalDest || !DT->isReachableFromEntry(UseBlock), + "Invoke result not available in the unwind destination!", + Op, &I); + } else { + Assert2(DT->dominates(II, UseBlock) || + !DT->isReachableFromEntry(UseBlock), + "Invoke result does not dominate all uses!", Op, &I); + } + } + + // PHI nodes are more difficult than other nodes because they actually + // "use" the value in the predecessor basic blocks they correspond to. + unsigned j = PHINode::getIncomingValueNumForOperand(i); + BasicBlock *PredBB = PN->getIncomingBlock(j); + Assert2(PredBB && (DT->dominates(OpBlock, PredBB) || + !DT->isReachableFromEntry(PredBB)), + "Instruction does not dominate all uses!", Op, &I); +} + /// verifyInstruction - Verify that an instruction is well formed. /// void Verifier::visitInstruction(Instruction &I) { @@ -1536,84 +1644,30 @@ void Verifier::visitInstruction(Instruction &I) { } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { Assert1(GV->getParent() == Mod, "Referencing global in another module!", &I); - } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { - BasicBlock *OpBlock = Op->getParent(); - - // Check that a definition dominates all of its uses. - if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { - // Invoke results are only usable in the normal destination, not in the - // exceptional destination. - BasicBlock *NormalDest = II->getNormalDest(); - - Assert2(NormalDest != II->getUnwindDest(), - "No uses of invoke possible due to dominance structure!", - Op, &I); - - // PHI nodes differ from other nodes because they actually "use" the - // value in the predecessor basic blocks they correspond to. - BasicBlock *UseBlock = BB; - if (PHINode *PN = dyn_cast<PHINode>(&I)) { - unsigned j = PHINode::getIncomingValueNumForOperand(i); - UseBlock = PN->getIncomingBlock(j); - } - Assert2(UseBlock, "Invoke operand is PHI node with bad incoming-BB", - Op, &I); - - if (isa<PHINode>(I) && UseBlock == OpBlock) { - // Special case of a phi node in the normal destination or the unwind - // destination. - Assert2(BB == NormalDest || !DT->isReachableFromEntry(UseBlock), - "Invoke result not available in the unwind destination!", - Op, &I); - } else { - Assert2(DT->dominates(NormalDest, UseBlock) || - !DT->isReachableFromEntry(UseBlock), - "Invoke result does not dominate all uses!", Op, &I); - - // If the normal successor of an invoke instruction has multiple - // predecessors, then the normal edge from the invoke is critical, - // so the invoke value can only be live if the destination block - // dominates all of it's predecessors (other than the invoke). - if (!NormalDest->getSinglePredecessor() && - DT->isReachableFromEntry(UseBlock)) - // If it is used by something non-phi, then the other case is that - // 'NormalDest' dominates all of its predecessors other than the - // invoke. In this case, the invoke value can still be used. - for (pred_iterator PI = pred_begin(NormalDest), - E = pred_end(NormalDest); PI != E; ++PI) - if (*PI != II->getParent() && !DT->dominates(NormalDest, *PI) && - DT->isReachableFromEntry(*PI)) { - CheckFailed("Invoke result does not dominate all uses!", Op,&I); - return; - } - } - } else if (PHINode *PN = dyn_cast<PHINode>(&I)) { - // PHI nodes are more difficult than other nodes because they actually - // "use" the value in the predecessor basic blocks they correspond to. - unsigned j = PHINode::getIncomingValueNumForOperand(i); - BasicBlock *PredBB = PN->getIncomingBlock(j); - Assert2(PredBB && (DT->dominates(OpBlock, PredBB) || - !DT->isReachableFromEntry(PredBB)), - "Instruction does not dominate all uses!", Op, &I); - } else { - if (OpBlock == BB) { - // If they are in the same basic block, make sure that the definition - // comes before the use. - Assert2(InstsInThisBlock.count(Op) || !DT->isReachableFromEntry(BB), - "Instruction does not dominate all uses!", Op, &I); - } - - // Definition must dominate use unless use is unreachable! - Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, &I) || - !DT->isReachableFromEntry(BB), - "Instruction does not dominate all uses!", Op, &I); - } + } else if (isa<Instruction>(I.getOperand(i))) { + verifyDominatesUse(I, i); } else if (isa<InlineAsm>(I.getOperand(i))) { Assert1((i + 1 == e && isa<CallInst>(I)) || (i + 3 == e && isa<InvokeInst>(I)), "Cannot take the address of an inline asm!", &I); } } + + if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpaccuracy)) { + Assert1(I.getType()->isFPOrFPVectorTy(), + "fpaccuracy requires a floating point result!", &I); + Assert1(MD->getNumOperands() == 1, "fpaccuracy takes one operand!", &I); + ConstantFP *Op = dyn_cast_or_null<ConstantFP>(MD->getOperand(0)); + Assert1(Op, "fpaccuracy ULPs not a floating point number!", &I); + APFloat ULPs = Op->getValueAPF(); + Assert1(ULPs.isNormal() || ULPs.isZero(), + "fpaccuracy ULPs not a normal number!", &I); + Assert1(!ULPs.isNegative(), "fpaccuracy ULPs is negative!", &I); + } + + MDNode *MD = I.getMetadata(LLVMContext::MD_range); + Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I); + InstsInThisBlock.insert(&I); } @@ -1642,6 +1696,12 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { switch (ID) { default: break; + case Intrinsic::ctlz: // llvm.ctlz + case Intrinsic::cttz: // llvm.cttz + Assert1(isa<ConstantInt>(CI.getArgOperand(1)), + "is_zero_undef argument of bit counting intrinsics must be a " + "constant int", &CI); + break; case Intrinsic::dbg_declare: { // llvm.dbg.declare Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)), "invalid llvm.dbg.declare intrinsic call 1", &CI); |