[C++11] Modernize the IR library a bit.
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 10 Mar 2014 15:03:06 +0000 (15:03 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 10 Mar 2014 15:03:06 +0000 (15:03 +0000)
No functionality change.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203465 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/DataLayout.h
include/llvm/IR/MDBuilder.h
include/llvm/IR/User.h
lib/IR/DataLayout.cpp
lib/IR/Instruction.cpp
lib/IR/Instructions.cpp
lib/IR/Metadata.cpp
lib/IR/Module.cpp

index d32b8402d64d3e376c5c1bfed4dbea7f32b18acc..8ad9bd0f4ad33bf2ec54efee4713d9fe65c165a7 100644 (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;
   }
index ce81b5498f5295377beb80998e97d34d8cbe86b6..c07b2bdc17ead8b5bcdbb6dfd22d369a76eff731 100644 (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);
   }
 
index d288e1908c2f6581d204eedf0f6bd1fe03a19a96..061bc91d31217ef3234b4500e99ac1bc1e49f525 100644 (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
index e6ca17a6ebfb0bd249a9bcad913e057fb089f2ad..5654e15ae30266826b199e68727eb99169942457 100644 (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,
index 67aa8b281835fa291fa54e0ad1a2d3f99968bb62..bd7a62e83d9b66411dba87ba5680a114811d9ae4 100644 (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;
index c3a34a2ccd793b09964f0c1200997152e64dbfc6..d874411ccd39be55143270c09591dc63d23005e5 100644 (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
index c6107f5d8042cde1b8cadf1b8a712f077f8f3536..ba393345db8f53aa801f0e84be5384137e466c89 100644 (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;
 }
 
index 6a3a38f60b903c53ee508e318e6429da2001e675..c8c07f27a0057ace0da4b4bf62b56cbf4d690ae0 100644 (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;
   }