From: Benjamin Kramer Date: Mon, 10 Mar 2014 15:03:06 +0000 (+0000) Subject: [C++11] Modernize the IR library a bit. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6e9eeab69f0ce496d0120f9718e026f319d2ab01;p=oota-llvm.git [C++11] Modernize the IR library a bit. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203465 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/DataLayout.h b/include/llvm/IR/DataLayout.h index d32b8402d64..8ad9bd0f4ad 100644 --- a/include/llvm/IR/DataLayout.h +++ b/include/llvm/IR/DataLayout.h @@ -219,8 +219,8 @@ public: /// The width is specified in bits. /// bool isLegalInteger(unsigned Width) const { - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - if (LegalIntWidths[i] == Width) + for (unsigned LegalIntWidth : LegalIntWidths) + if (LegalIntWidth == Width) return true; return false; } @@ -283,8 +283,8 @@ public: /// only supports i32 as a native integer type, then i27 fits in a legal // integer type but i45 does not. bool fitsInLegalInteger(unsigned Width) const { - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - if (Width <= LegalIntWidths[i]) + for (unsigned LegalIntWidth : LegalIntWidths) + if (Width <= LegalIntWidth) return true; return false; } diff --git a/include/llvm/IR/MDBuilder.h b/include/llvm/IR/MDBuilder.h index ce81b5498f5..c07b2bdc17e 100644 --- a/include/llvm/IR/MDBuilder.h +++ b/include/llvm/IR/MDBuilder.h @@ -174,11 +174,8 @@ public: /// given name, an offset and a parent in the TBAA type DAG. MDNode *createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset = 0) { - SmallVector Ops(3); - Type *Int64 = IntegerType::get(Context, 64); - Ops[0] = createString(Name); - Ops[1] = Parent; - Ops[2] = ConstantInt::get(Int64, Offset); + ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); + Value *Ops[3] = { createString(Name), Parent, Off }; return MDNode::get(Context, Ops); } diff --git a/include/llvm/IR/User.h b/include/llvm/IR/User.h index d288e1908c2..061bc91d312 100644 --- a/include/llvm/IR/User.h +++ b/include/llvm/IR/User.h @@ -178,8 +178,8 @@ public: // delete. // void dropAllReferences() { - for (op_iterator i = op_begin(), e = op_end(); i != e; ++i) - i->set(0); + for (Use &U : operands()) + U.set(0); } /// replaceUsesOfWith - Replaces all references to the "From" definition with diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index e6ca17a6ebf..5654e15ae30 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -185,8 +185,7 @@ void DataLayout::reset(StringRef Desc) { ManglingMode = MM_None; // Default alignments - for (int I = 0, N = array_lengthof(DefaultAlignments); I < N; ++I) { - const LayoutAlignElem &E = DefaultAlignments[I]; + for (const LayoutAlignElem &E : DefaultAlignments) { setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign, E.TypeBitWidth); } @@ -370,12 +369,12 @@ DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); - for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { - if (Alignments[i].AlignType == (unsigned)align_type && - Alignments[i].TypeBitWidth == bit_width) { + for (LayoutAlignElem &Elem : Alignments) { + if (Elem.AlignType == (unsigned)align_type && + Elem.TypeBitWidth == bit_width) { // Update the abi, preferred alignments. - Alignments[i].ABIAlign = abi_align; - Alignments[i].PrefAlign = pref_align; + Elem.ABIAlign = abi_align; + Elem.PrefAlign = pref_align; return; } } @@ -384,15 +383,12 @@ DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, pref_align, bit_width)); } -static bool comparePointerAlignElem(const PointerAlignElem &A, - uint32_t AddressSpace) { - return A.AddressSpace < AddressSpace; -} - DataLayout::PointersTy::iterator DataLayout::findPointerLowerBound(uint32_t AddressSpace) { return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace, - comparePointerAlignElem); + [](const PointerAlignElem &A, uint32_t AddressSpace) { + return A.AddressSpace < AddressSpace; + }); } void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, @@ -472,11 +468,10 @@ class StructLayoutMap { LayoutInfoTy LayoutInfo; public: - virtual ~StructLayoutMap() { + ~StructLayoutMap() { // Remove any layouts. - for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end(); - I != E; ++I) { - StructLayout *Value = I->second; + for (const auto &I : LayoutInfo) { + StructLayout *Value = I.second; Value->~StructLayout(); free(Value); } @@ -485,9 +480,6 @@ public: StructLayout *&operator[](StructType *STy) { return LayoutInfo[STy]; } - - // for debugging... - virtual void dump() const {} }; } // end anonymous namespace @@ -550,10 +542,7 @@ std::string DataLayout::getStringRepresentation() const { break; } - for (PointersTy::const_iterator I = Pointers.begin(), E = Pointers.end(); - I != E; ++I) { - const PointerAlignElem &PI = *I; - + for (const PointerAlignElem &PI : Pointers) { // Skip default. if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 && PI.TypeByteWidth == 8) @@ -568,12 +557,9 @@ std::string DataLayout::getStringRepresentation() const { OS << ':' << PI.PrefAlign*8; } - const LayoutAlignElem *DefaultStart = DefaultAlignments; - const LayoutAlignElem *DefaultEnd = - DefaultStart + array_lengthof(DefaultAlignments); - for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { - const LayoutAlignElem &AI = Alignments[i]; - if (std::find(DefaultStart, DefaultEnd, AI) != DefaultEnd) + for (const LayoutAlignElem &AI : Alignments) { + if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments), + AI) != std::end(DefaultAlignments)) continue; OS << '-' << (char)AI.AlignType; if (AI.TypeBitWidth) @@ -731,17 +717,15 @@ Type *DataLayout::getIntPtrType(Type *Ty) const { } Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - if (Width <= LegalIntWidths[i]) - return Type::getIntNTy(C, LegalIntWidths[i]); + for (unsigned LegalIntWidth : LegalIntWidths) + if (Width <= LegalIntWidth) + return Type::getIntNTy(C, LegalIntWidth); return 0; } unsigned DataLayout::getLargestLegalIntTypeSize() const { - unsigned MaxWidth = 0; - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - MaxWidth = std::max(MaxWidth, LegalIntWidths[i]); - return MaxWidth; + auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end()); + return Max != LegalIntWidths.end() ? *Max : 0; } uint64_t DataLayout::getIndexedOffset(Type *ptrTy, diff --git a/lib/IR/Instruction.cpp b/lib/IR/Instruction.cpp index 67aa8b28183..bd7a62e83d9 100644 --- a/lib/IR/Instruction.cpp +++ b/lib/IR/Instruction.cpp @@ -281,9 +281,8 @@ bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { // We have two instructions of identical opcode and #operands. Check to see // if all operands are the same. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i) != I->getOperand(i)) - return false; + if (!std::equal(op_begin(), op_end(), I->op_begin())) + return false; // Check special state that is a part of some instructions. if (const LoadInst *LI = dyn_cast(this)) @@ -323,11 +322,8 @@ bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { RMWI->getSynchScope() == cast(I)->getSynchScope(); if (const PHINode *thisPHI = dyn_cast(this)) { const PHINode *otherPHI = cast(I); - for (unsigned i = 0, e = thisPHI->getNumOperands(); i != e; ++i) { - if (thisPHI->getIncomingBlock(i) != otherPHI->getIncomingBlock(i)) - return false; - } - return true; + return std::equal(thisPHI->block_begin(), thisPHI->block_end(), + otherPHI->block_begin()); } return true; } @@ -552,8 +548,8 @@ Instruction *Instruction::clone() const { // new one. SmallVector, 4> TheMDs; getAllMetadataOtherThanDebugLoc(TheMDs); - for (unsigned i = 0, e = TheMDs.size(); i != e; ++i) - New->setMetadata(TheMDs[i].first, TheMDs[i].second); + for (const auto &MD : TheMDs) + New->setMetadata(MD.first, MD.second); New->setDebugLoc(getDebugLoc()); return New; diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index c3a34a2ccd7..d874411ccd3 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -1578,11 +1578,11 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, if (const ConstantVector *MV = dyn_cast(Mask)) { unsigned V1Size = cast(V1->getType())->getNumElements(); - for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { - if (ConstantInt *CI = dyn_cast(MV->getOperand(i))) { + for (Value *Op : MV->operands()) { + if (ConstantInt *CI = dyn_cast(Op)) { if (CI->uge(V1Size*2)) return false; - } else if (!isa(MV->getOperand(i))) { + } else if (!isa(Op)) { return false; } } @@ -1702,8 +1702,7 @@ ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) // Type *ExtractValueInst::getIndexedType(Type *Agg, ArrayRef Idxs) { - for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) { - unsigned Index = Idxs[CurIdx]; + for (unsigned Index : Idxs) { // We can't use CompositeType::indexValid(Index) here. // indexValid() always returns true for arrays because getelementptr allows // out-of-bounds indices. Since we don't allow those for extractvalue and diff --git a/lib/IR/Metadata.cpp b/lib/IR/Metadata.cpp index c6107f5d804..ba393345db8 100644 --- a/lib/IR/Metadata.cpp +++ b/lib/IR/Metadata.cpp @@ -224,8 +224,8 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef Vals, // Note that if the operands are later nulled out, the node will be // removed from the uniquing map. FoldingSetNodeID ID; - for (unsigned i = 0; i != Vals.size(); ++i) - ID.AddPointer(Vals[i]); + for (Value *V : Vals) + ID.AddPointer(V); void *InsertPoint; MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); @@ -236,8 +236,7 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef Vals, bool isFunctionLocal = false; switch (FL) { case FL_Unknown: - for (unsigned i = 0; i != Vals.size(); ++i) { - Value *V = Vals[i]; + for (Value *V : Vals) { if (!V) continue; if (isFunctionLocalValue(V)) { isFunctionLocal = true; @@ -649,9 +648,9 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { setHasMetadataHashEntry(true); } else { // Handle replacement of an existing value. - for (unsigned i = 0, e = Info.size(); i != e; ++i) - if (Info[i].first == KindID) { - Info[i].second = Node; + for (auto &P : Info) + if (P.first == KindID) { + P.second = Node; return; } } @@ -697,10 +696,9 @@ MDNode *Instruction::getMetadataImpl(unsigned KindID) const { LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; assert(!Info.empty() && "bit out of sync with hash table"); - for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); - I != E; ++I) - if (I->first == KindID) - return I->second; + for (const auto &I : Info) + if (I.first == KindID) + return I.second; return 0; } diff --git a/lib/IR/Module.cpp b/lib/IR/Module.cpp index 6a3a38f60b9..c8c07f27a00 100644 --- a/lib/IR/Module.cpp +++ b/lib/IR/Module.cpp @@ -271,8 +271,7 @@ getModuleFlagsMetadata(SmallVectorImpl &Flags) const { const NamedMDNode *ModFlags = getModuleFlagsMetadata(); if (!ModFlags) return; - for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) { - MDNode *Flag = ModFlags->getOperand(i); + for (const MDNode *Flag : ModFlags->operands()) { if (Flag->getNumOperands() >= 3 && isa(Flag->getOperand(0)) && isa(Flag->getOperand(1))) { // Check the operands of the MDNode before accessing the operands. @@ -291,8 +290,7 @@ getModuleFlagsMetadata(SmallVectorImpl &Flags) const { Value *Module::getModuleFlag(StringRef Key) const { SmallVector ModuleFlags; getModuleFlagsMetadata(ModuleFlags); - for (unsigned I = 0, E = ModuleFlags.size(); I < E; ++I) { - const ModuleFlagEntry &MFE = ModuleFlags[I]; + for (const ModuleFlagEntry &MFE : ModuleFlags) { if (Key == MFE.Key->getString()) return MFE.Val; }