+// InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing'
+// is already in, and put it right before 'Existing'. This instruction should
+// only be used when there is no iterator to Existing already around. The
+// returned iterator points to the new instruction.
+//
+BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst,
+ Instruction *Existing) {
+ BasicBlock *BB = Existing->getParent();
+ BasicBlock::InstListType &BIL = BB->getInstList();
+ BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), Existing);
+ assert(BI != BIL.end() && "Inst not in it's parents BB!");
+ return BIL.insert(BI, NewInst);
+}
+
+
+
+static const Type *getStructOffsetStep(const StructType *STy, unsigned &Offset,
+ std::vector<Value*> &Indices) {
+ assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
+ const StructLayout *SL = TD.getStructLayout(STy);
+
+ // This loop terminates always on a 0 <= i < MemberOffsets.size()
+ unsigned i;
+ for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
+ if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
+ break;
+
+ assert(Offset >= SL->MemberOffsets[i] &&
+ (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
+
+ // Make sure to save the current index...
+ Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
+ Offset = SL->MemberOffsets[i];
+ return STy->getContainedType(i);
+}