From: Chris Lattner Date: Wed, 25 Jan 2012 06:48:06 +0000 (+0000) Subject: use Constant::getAggregateElement to simplify a bunch of code. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=a1f00f4d488eb5daff52faaf99c62ee652fd3b85;p=oota-llvm.git use Constant::getAggregateElement to simplify a bunch of code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148934 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 43b3af2ac52..6a49e6d98ab 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -311,30 +311,20 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, // not reached. } - if (ConstantArray *CA = dyn_cast(C)) { - uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType()); + if (isa(C) || isa(C) || + isa(C)) { + Type *EltTy = cast(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(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, + uint64_t NumElts; + if (ArrayType *AT = dyn_cast(C->getType())) + NumElts = AT->getNumElements(); + else + NumElts = cast(C->getType())->getNumElements(); + + for (; Index != NumElts; ++Index) { + if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr, BytesLeft, TD)) return false; if (EltSize >= BytesLeft) @@ -346,30 +336,12 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, } return true; } - - if (ConstantDataSequential *CDS = dyn_cast(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; - } - if (ConstantExpr *CE = dyn_cast(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 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(Indices[i]); - if (Idx == 0) return 0; - - uint64_t IdxVal = Idx->getZExtValue(); - - if (ConstantStruct *CS = dyn_cast(C)) { - C = CS->getOperand(IdxVal); - } else if (ConstantAggregateZero *CAZ = dyn_cast(C)){ - C = CAZ->getElementValue(Idx); - } else if (UndefValue *UV = dyn_cast(C)) { - C = UV->getElementValue(Idx); - } else if (ConstantArray *CA = dyn_cast(C)) { - if (IdxVal >= CA->getType()->getNumElements()) - return 0; - C = CA->getOperand(IdxVal); - } else if (ConstantDataSequential *CDS=dyn_cast(C)){ - if (IdxVal >= CDS->getNumElements()) - return 0; - C = CDS->getElementAsConstant(IdxVal); - } else if (ConstantVector *CV = dyn_cast(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; } diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index ca55fcbc6ed..21008a14678 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -1493,19 +1493,12 @@ Value *llvm::FindInsertedValue(Value *V, ArrayRef idx_range, "Not looking at a struct or array?"); assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && "Invalid indices for type?"); - CompositeType *PTy = cast(V->getType()); - - if (isa(V)) - return UndefValue::get(ExtractValueInst::getIndexedType(PTy, idx_range)); - if (isa(V)) - return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy, - idx_range)); - if (isa(V) || isa(V)) - // Recursively process this constant - return FindInsertedValue(cast(V)->getOperand(idx_range[0]), - idx_range.slice(1), InsertBefore); - if (ConstantDataSequential *CDS = dyn_cast(V)) - return CDS->getElementAsConstant(idx_range[0]); + + if (Constant *C = dyn_cast(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(V)) { // Loop the indices for the insertvalue instruction in parallel with the diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 563aed5daa0..60fa9a76e2a 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -844,21 +844,10 @@ bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { } static void getArrayElements(Constant *C, SmallVectorImpl &Dest) { - if (ConstantArray *I = dyn_cast(C)) { - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - Dest.push_back(I->getOperand(i)); - return; - } - - if (ConstantDataSequential *CDS = dyn_cast(C)) { - for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) - Dest.push_back(CDS->getElementAsConstant(i)); - return; - } - - ConstantAggregateZero *CAZ = cast(C); - Dest.append(cast(C->getType())->getNumElements(), - CAZ->getSequentialElement()); + unsigned NumElements = cast(C->getType())->getNumElements(); + + for (unsigned i = 0; i != NumElements; ++i) + Dest.push_back(C->getAggregateElement(i)); } void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 2926508a428..6e8b434ce3d 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -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(Idx); - if (!CI) return 0; - unsigned IdxV = CI->getZExtValue(); - - if (ConstantStruct *CS = dyn_cast(Agg)) { - if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV); - } else if (ConstantArray *CA = dyn_cast(Agg)) { - if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV); - } else if (ConstantVector *CP = dyn_cast(Agg)) { - if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV); - } else if (isa(Agg)) { - if (StructType *STy = dyn_cast(Agg->getType())) { - if (IdxV < STy->getNumElements()) - return Constant::getNullValue(STy->getElementType(IdxV)); - } else if (SequentialType *STy = - dyn_cast(Agg->getType())) { - return Constant::getNullValue(STy->getElementType()); - } - } else if (isa(Agg)) { - if (StructType *STy = dyn_cast(Agg->getType())) { - if (IdxV < STy->getNumElements()) - return UndefValue::get(STy->getElementType(IdxV)); - } else if (SequentialType *STy = - dyn_cast(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, diff --git a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index 7b1617d7789..6873d15baba 100644 --- a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -834,59 +834,39 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, } UndefElts = 0; - if (ConstantVector *CV = dyn_cast(V)) { + + // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential. + if (Constant *C = dyn_cast(V)) { + // Check if this is identity. If so, return 0 since we are not simplifying + // anything. + if (DemandedElts.isAllOnesValue()) + return 0; + Type *EltTy = cast(V->getType())->getElementType(); Constant *Undef = UndefValue::get(EltTy); - - std::vector Elts; - for (unsigned i = 0; i != VWidth; ++i) + + SmallVector 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(CV->getOperand(i))) { // Already undef. + continue; + } + + Constant *Elt = C->getAggregateElement(i); + if (Elt == 0) return 0; + + if (isa(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(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 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(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 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.