1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 12:43:36 +01:00

use GEP::accumulateConstantOffset() to replace custom written code to compute GEP offset

llvm-svn: 174279
This commit is contained in:
Nuno Lopes 2013-02-03 13:17:11 +00:00
parent 5c9c5547e6
commit a59b3dcbfc

View File

@ -235,38 +235,13 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
// i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
if (CE->getOpcode() == Instruction::GetElementPtr) { if (GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
// Cannot compute this if the element type of the pointer is missing size
// info.
if (!cast<PointerType>(CE->getOperand(0)->getType())
->getElementType()->isSized())
return false;
// If the base isn't a global+constant, we aren't either. // If the base isn't a global+constant, we aren't either.
if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
return false; return false;
// Otherwise, add any offset that our operands provide. // Otherwise, add any offset that our operands provide.
gep_type_iterator GTI = gep_type_begin(CE); return GEP->accumulateConstantOffset(TD, Offset);
for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
i != e; ++i, ++GTI) {
ConstantInt *CI = dyn_cast<ConstantInt>(*i);
if (!CI) return false; // Index isn't a simple constant?
if (CI->isZero()) continue; // Not adding anything.
if (StructType *ST = dyn_cast<StructType>(*GTI)) {
// N = N + Offset
Offset +=
APInt(Offset.getBitWidth(),
TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()));
} else {
SequentialType *SQT = cast<SequentialType>(*GTI);
Offset += APInt(Offset.getBitWidth(),
TD.getTypeAllocSize(SQT->getElementType()) *
CI->getSExtValue());
}
}
return true;
} }
return false; return false;