diff options
author | Chris Lattner <sabre@nondot.org> | 2012-01-25 06:48:06 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2012-01-25 06:48:06 +0000 |
commit | a1f00f4d488eb5daff52faaf99c62ee652fd3b85 (patch) | |
tree | 0eae19e36e8626cb494b38c88596988b44299120 /lib | |
parent | 969ba287cd2be9b2d6843db6fa5337585f84283b (diff) | |
download | external_llvm-a1f00f4d488eb5daff52faaf99c62ee652fd3b85.zip external_llvm-a1f00f4d488eb5daff52faaf99c62ee652fd3b85.tar.gz external_llvm-a1f00f4d488eb5daff52faaf99c62ee652fd3b85.tar.bz2 |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Analysis/ConstantFolding.cpp | 97 | ||||
-rw-r--r-- | lib/Analysis/ValueTracking.cpp | 19 | ||||
-rw-r--r-- | lib/Linker/LinkModules.cpp | 19 | ||||
-rw-r--r-- | lib/Transforms/IPO/GlobalOpt.cpp | 38 | ||||
-rw-r--r-- | lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp | 64 |
5 files changed, 58 insertions, 179 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 43b3af2..6a49e6d 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<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, + 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) @@ -346,30 +336,12 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, } 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; - } - 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; } diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index ca55fcb..21008a1 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -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 diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 563aed5..60fa9a7 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<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) { diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 2926508..6e8b434 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<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, diff --git a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index 7b1617d..6873d15 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<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. |