[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
This commit is contained in:
Benjamin Kramer 2014-03-10 15:03:06 +00:00
parent e1820a6a4e
commit 6e9eeab69f
8 changed files with 50 additions and 78 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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

View File

@ -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<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,

View File

@ -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<LoadInst>(this))
@ -323,11 +322,8 @@ bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
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;

View File

@ -1578,11 +1578,11 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
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 ExtractValueInst &EVI)
//
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

View File

@ -224,8 +224,8 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> 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<Value*> 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;
}

View File

@ -271,8 +271,7 @@ 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);
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<ModuleFlagEntry> &Flags) const {
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;
}