[llvm] r203465 - [C++11] Modernize the IR library a bit.
Benjamin Kramer
benny.kra at googlemail.com
Mon Mar 10 08:03:07 PDT 2014
Author: d0k
Date: Mon Mar 10 10:03:06 2014
New Revision: 203465
URL: http://llvm.org/viewvc/llvm-project?rev=203465&view=rev
Log:
[C++11] Modernize the IR library a bit.
No functionality change.
Modified:
llvm/trunk/include/llvm/IR/DataLayout.h
llvm/trunk/include/llvm/IR/MDBuilder.h
llvm/trunk/include/llvm/IR/User.h
llvm/trunk/lib/IR/DataLayout.cpp
llvm/trunk/lib/IR/Instruction.cpp
llvm/trunk/lib/IR/Instructions.cpp
llvm/trunk/lib/IR/Metadata.cpp
llvm/trunk/lib/IR/Module.cpp
Modified: llvm/trunk/include/llvm/IR/DataLayout.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DataLayout.h?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DataLayout.h (original)
+++ llvm/trunk/include/llvm/IR/DataLayout.h Mon Mar 10 10:03:06 2014
@@ -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;
}
Modified: llvm/trunk/include/llvm/IR/MDBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/MDBuilder.h?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/MDBuilder.h (original)
+++ llvm/trunk/include/llvm/IR/MDBuilder.h Mon Mar 10 10:03:06 2014
@@ -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<Value *, 4> 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);
}
Modified: llvm/trunk/include/llvm/IR/User.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/User.h?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/User.h (original)
+++ llvm/trunk/include/llvm/IR/User.h Mon Mar 10 10:03:06 2014
@@ -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
Modified: llvm/trunk/lib/IR/DataLayout.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DataLayout.cpp?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/lib/IR/DataLayout.cpp (original)
+++ llvm/trunk/lib/IR/DataLayout.cpp Mon Mar 10 10:03:06 2014
@@ -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 a
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 a
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::getStringReprese
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::getStringReprese
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
}
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<unsigned>(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,
Modified: llvm/trunk/lib/IR/Instruction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Instruction.cpp?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Instruction.cpp (original)
+++ llvm/trunk/lib/IR/Instruction.cpp Mon Mar 10 10:03:06 2014
@@ -281,9 +281,8 @@ bool Instruction::isIdenticalToWhenDefin
// 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<LoadInst>(this))
@@ -323,11 +322,8 @@ bool Instruction::isIdenticalToWhenDefin
RMWI->getSynchScope() == cast<AtomicRMWInst>(I)->getSynchScope();
if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
const PHINode *otherPHI = cast<PHINode>(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<std::pair<unsigned, MDNode*>, 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;
Modified: llvm/trunk/lib/IR/Instructions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Instructions.cpp?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Instructions.cpp (original)
+++ llvm/trunk/lib/IR/Instructions.cpp Mon Mar 10 10:03:06 2014
@@ -1578,11 +1578,11 @@ bool ShuffleVectorInst::isValidOperands(
if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
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))) {
+ for (Value *Op : MV->operands()) {
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
if (CI->uge(V1Size*2))
return false;
- } else if (!isa<UndefValue>(MV->getOperand(i))) {
+ } else if (!isa<UndefValue>(Op)) {
return false;
}
}
@@ -1702,8 +1702,7 @@ ExtractValueInst::ExtractValueInst(const
//
Type *ExtractValueInst::getIndexedType(Type *Agg,
ArrayRef<unsigned> 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
Modified: llvm/trunk/lib/IR/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Metadata.cpp?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Metadata.cpp (original)
+++ llvm/trunk/lib/IR/Metadata.cpp Mon Mar 10 10:03:06 2014
@@ -224,8 +224,8 @@ MDNode *MDNode::getMDNode(LLVMContext &C
// 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 &C
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 K
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(uns
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;
}
Modified: llvm/trunk/lib/IR/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Module.cpp?rev=203465&r1=203464&r2=203465&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Module.cpp (original)
+++ llvm/trunk/lib/IR/Module.cpp Mon Mar 10 10:03:06 2014
@@ -271,8 +271,7 @@ getModuleFlagsMetadata(SmallVectorImpl<M
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<ConstantInt>(Flag->getOperand(0)) &&
isa<MDString>(Flag->getOperand(1))) {
// Check the operands of the MDNode before accessing the operands.
@@ -291,8 +290,7 @@ getModuleFlagsMetadata(SmallVectorImpl<M
Value *Module::getModuleFlag(StringRef Key) const {
SmallVector<Module::ModuleFlagEntry, 8> 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;
}
More information about the llvm-commits
mailing list