- MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
-
- // Variables to hold the index vector and ptr value.
- // The major work here is to extract these for all 3 instruction types
- // and to try to fold chains of constant indices into a single offset.
- // After that, we call SetMemOperands_Internal(), which creates the
- // appropriate operands for the machine instruction.
- vector<Value*> idxVec;
- bool allConstantIndices = true;
- Value* ptrVal = memInst->getPointerOperand();
-
- // If there is a GetElemPtr instruction to fold in to this instr,
- // it must be in the left child for Load and GetElemPtr, and in the
- // right child for Store instructions.
- InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
- ? vmInstrNode->rightChild()
- : vmInstrNode->leftChild());
-
- // Check if all indices are constant for this instruction
- for (MemAccessInst::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
- allConstantIndices && OI != OE; ++OI)
- if (! isa<Constant>(*OI))
- allConstantIndices = false;
-
- // If we have only constant indices, fold chains of constant indices
- // in this and any preceding GetElemPtr instructions.
- bool foldedGEPs = false;
- if (allConstantIndices &&
- (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
- ptrChild->getOpLabel() == GetElemPtrIdx))
- if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
- ptrVal = newPtr;
- foldedGEPs = true;
- }
-
- // Append the index vector of the current instruction, if any.
- // Skip the leading [0] index if preceding GEPs were folded into this.
- if (memInst->getNumIndices() > 0) {
- assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
- idxVec.insert(idxVec.end(),
- memInst->idx_begin() + foldedGEPs, memInst->idx_end());
- }
-
- // Now create the appropriate operands for the machine instruction
- SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
- ptrVal, idxVec, allConstantIndices, target);
-}