use Constant::getAggregateElement to simplify a bunch of code.

llvm-svn: 148934
This commit is contained in:
Chris Lattner 2012-01-25 06:48:06 +00:00
parent 7834900950
commit 6705883ad8
5 changed files with 68 additions and 189 deletions

View File

@ -311,65 +311,37 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
// not reached.
}
if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType());
if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
isa<ConstantDataSequential>(C)) {
Type *EltTy = cast<SequentialType>(C->getType())->getElementType();
uint64_t EltSize = TD.getTypeAllocSize(EltTy);
uint64_t Index = ByteOffset / EltSize;
uint64_t Offset = ByteOffset - Index * EltSize;
for (; Index != CA->getType()->getNumElements(); ++Index) {
if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr,
BytesLeft, TD))
return false;
if (EltSize >= BytesLeft)
return true;
Offset = 0;
BytesLeft -= EltSize;
CurPtr += EltSize;
}
return true;
}
if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType());
uint64_t Index = ByteOffset / EltSize;
uint64_t Offset = ByteOffset - Index * EltSize;
for (; Index != CV->getType()->getNumElements(); ++Index) {
if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr,
BytesLeft, TD))
return false;
if (EltSize >= BytesLeft)
return true;
Offset = 0;
BytesLeft -= EltSize;
CurPtr += EltSize;
}
return true;
}
if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
uint64_t EltSize = CDS->getElementByteSize();
uint64_t Index = ByteOffset / EltSize;
uint64_t Offset = ByteOffset - Index * EltSize;
for (unsigned e = CDS->getNumElements(); Index != e; ++Index) {
if (!ReadDataFromGlobal(CDS->getElementAsConstant(Index), Offset, CurPtr,
BytesLeft, TD))
return false;
if (EltSize >= BytesLeft)
return true;
Offset = 0;
BytesLeft -= EltSize;
CurPtr += EltSize;
}
return true;
}
uint64_t NumElts;
if (ArrayType *AT = dyn_cast<ArrayType>(C->getType()))
NumElts = AT->getNumElements();
else
NumElts = cast<VectorType>(C->getType())->getNumElements();
for (; Index != NumElts; ++Index) {
if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
BytesLeft, TD))
return false;
if (EltSize >= BytesLeft)
return true;
Offset = 0;
BytesLeft -= EltSize;
CurPtr += EltSize;
}
return true;
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
if (CE->getOpcode() == Instruction::IntToPtr &&
CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
BytesLeft, TD);
return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
BytesLeft, TD);
}
// Otherwise, unknown initializer type.
@ -1010,11 +982,14 @@ Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
ConstantExpr *CE) {
if (!CE->getOperand(1)->isNullValue())
return 0; // Do not allow stepping over the value!
SmallVector<Constant*, 8> Indices(CE->getNumOperands()-2);
for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i)
Indices[i-2] = CE->getOperand(i);
return ConstantFoldLoadThroughGEPIndices(C, Indices);
// Loop over all of the operands, tracking down which value we are
// addressing.
for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
C = C->getAggregateElement(CE->getOperand(i));
if (C == 0) return 0;
}
return C;
}
/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
@ -1026,32 +1001,8 @@ Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
// Loop over all of the operands, tracking down which value we are
// addressing.
for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
ConstantInt *Idx = dyn_cast<ConstantInt>(Indices[i]);
if (Idx == 0) return 0;
uint64_t IdxVal = Idx->getZExtValue();
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
C = CS->getOperand(IdxVal);
} else if (ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C)){
C = CAZ->getElementValue(Idx);
} else if (UndefValue *UV = dyn_cast<UndefValue>(C)) {
C = UV->getElementValue(Idx);
} else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
if (IdxVal >= CA->getType()->getNumElements())
return 0;
C = CA->getOperand(IdxVal);
} else if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(C)){
if (IdxVal >= CDS->getNumElements())
return 0;
C = CDS->getElementAsConstant(IdxVal);
} else if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
if (IdxVal >= CV->getType()->getNumElements())
return 0;
C = CV->getOperand(IdxVal);
} else {
return 0;
}
C = C->getAggregateElement(Indices[i]);
if (C == 0) return 0;
}
return C;
}

View File

@ -1493,19 +1493,12 @@ Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
"Not looking at a struct or array?");
assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
"Invalid indices for type?");
CompositeType *PTy = cast<CompositeType>(V->getType());
if (isa<UndefValue>(V))
return UndefValue::get(ExtractValueInst::getIndexedType(PTy, idx_range));
if (isa<ConstantAggregateZero>(V))
return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
idx_range));
if (isa<ConstantArray>(V) || isa<ConstantStruct>(V))
// Recursively process this constant
return FindInsertedValue(cast<Constant>(V)->getOperand(idx_range[0]),
idx_range.slice(1), InsertBefore);
if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
return CDS->getElementAsConstant(idx_range[0]);
if (Constant *C = dyn_cast<Constant>(V)) {
C = C->getAggregateElement(idx_range[0]);
if (C == 0) return 0;
return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
}
if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
// Loop the indices for the insertvalue instruction in parallel with the

View File

@ -844,21 +844,10 @@ bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
}
static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
if (ConstantArray *I = dyn_cast<ConstantArray>(C)) {
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dest.push_back(I->getOperand(i));
return;
}
if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
Dest.push_back(CDS->getElementAsConstant(i));
return;
}
ConstantAggregateZero *CAZ = cast<ConstantAggregateZero>(C);
Dest.append(cast<ArrayType>(C->getType())->getNumElements(),
CAZ->getSequentialElement());
unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
for (unsigned i = 0; i != NumElements; ++i)
Dest.push_back(C->getAggregateElement(i));
}
void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {

View File

@ -276,38 +276,6 @@ static bool AnalyzeGlobal(const Value *V, GlobalStatus &GS,
return false;
}
static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
ConstantInt *CI = dyn_cast<ConstantInt>(Idx);
if (!CI) return 0;
unsigned IdxV = CI->getZExtValue();
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) {
if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV);
} else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) {
if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV);
} else if (ConstantVector *CP = dyn_cast<ConstantVector>(Agg)) {
if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV);
} else if (isa<ConstantAggregateZero>(Agg)) {
if (StructType *STy = dyn_cast<StructType>(Agg->getType())) {
if (IdxV < STy->getNumElements())
return Constant::getNullValue(STy->getElementType(IdxV));
} else if (SequentialType *STy =
dyn_cast<SequentialType>(Agg->getType())) {
return Constant::getNullValue(STy->getElementType());
}
} else if (isa<UndefValue>(Agg)) {
if (StructType *STy = dyn_cast<StructType>(Agg->getType())) {
if (IdxV < STy->getNumElements())
return UndefValue::get(STy->getElementType(IdxV));
} else if (SequentialType *STy =
dyn_cast<SequentialType>(Agg->getType())) {
return UndefValue::get(STy->getElementType());
}
}
return 0;
}
/// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
/// users of the global, cleaning up the obvious ones. This is largely just a
/// quick scan over the use list to clean up the easy and obvious cruft. This
@ -520,8 +488,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
NewGlobals.reserve(STy->getNumElements());
const StructLayout &Layout = *TD.getStructLayout(STy);
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::getInt32Ty(STy->getContext()), i));
Constant *In = Init->getAggregateElement(i);
assert(In && "Couldn't get element of initializer?");
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
GlobalVariable::InternalLinkage,
@ -553,8 +520,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType());
unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
for (unsigned i = 0, e = NumElements; i != e; ++i) {
Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::getInt32Ty(Init->getContext()), i));
Constant *In = Init->getAggregateElement(i);
assert(In && "Couldn't get element of initializer?");
GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,

View File

@ -834,59 +834,39 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
}
UndefElts = 0;
if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
// Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
if (Constant *C = dyn_cast<Constant>(V)) {
// Check if this is identity. If so, return 0 since we are not simplifying
// anything.
if (DemandedElts.isAllOnesValue())
return 0;
Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Constant *Undef = UndefValue::get(EltTy);
std::vector<Constant*> Elts;
for (unsigned i = 0; i != VWidth; ++i)
SmallVector<Constant*, 16> Elts;
for (unsigned i = 0; i != VWidth; ++i) {
if (!DemandedElts[i]) { // If not demanded, set to undef.
Elts.push_back(Undef);
UndefElts.setBit(i);
} else if (isa<UndefValue>(CV->getOperand(i))) { // Already undef.
continue;
}
Constant *Elt = C->getAggregateElement(i);
if (Elt == 0) return 0;
if (isa<UndefValue>(Elt)) { // Already undef.
Elts.push_back(Undef);
UndefElts.setBit(i);
} else { // Otherwise, defined.
Elts.push_back(CV->getOperand(i));
Elts.push_back(Elt);
}
}
// If we changed the constant, return it.
Constant *NewCV = ConstantVector::get(Elts);
return NewCV != CV ? NewCV : 0;
}
if (ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
// Check if this is identity. If so, return 0 since we are not simplifying
// anything.
if (DemandedElts.isAllOnesValue())
return 0;
// Simplify to a ConstantVector where the non-demanded elements are undef.
Constant *Undef = UndefValue::get(CDV->getElementType());
SmallVector<Constant*, 16> Elts;
for (unsigned i = 0; i != VWidth; ++i)
Elts.push_back(DemandedElts[i] ? CDV->getElementAsConstant(i) : Undef);
UndefElts = DemandedElts ^ EltMask;
return ConstantVector::get(Elts);
}
if (ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(V)) {
// Check if this is identity. If so, return 0 since we are not simplifying
// anything.
if (DemandedElts.isAllOnesValue())
return 0;
// Simplify the CAZ to a ConstantVector where the non-demanded elements are
// set to undef.
Constant *Zero = CAZ->getSequentialElement();
Constant *Undef = UndefValue::get(Zero->getType());
SmallVector<Constant*, 16> Elts;
for (unsigned i = 0; i != VWidth; ++i)
Elts.push_back(DemandedElts[i] ? Zero : Undef);
UndefElts = DemandedElts ^ EltMask;
return ConstantVector::get(Elts);
return NewCV != C ? NewCV : 0;
}
// Limit search depth.