llvm.org GIT mirror llvm / a1f00f4
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 Chris Lattner 7 years ago
5 changed file(s) with 61 addition(s) and 182 deletion(s). Raw diff Collapse all Expand all
310310 // not reached.
311311 }
312312
313 if (ConstantArray *CA = dyn_cast(C)) {
314 uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType());
313 if (isa(C) || isa(C) ||
314 isa(C)) {
315 Type *EltTy = cast(C->getType())->getElementType();
316 uint64_t EltSize = TD.getTypeAllocSize(EltTy);
315317 uint64_t Index = ByteOffset / EltSize;
316318 uint64_t Offset = ByteOffset - Index * EltSize;
317 for (; Index != CA->getType()->getNumElements(); ++Index) {
318 if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr,
319 uint64_t NumElts;
320 if (ArrayType *AT = dyn_cast(C->getType()))
321 NumElts = AT->getNumElements();
322 else
323 NumElts = cast(C->getType())->getNumElements();
324
325 for (; Index != NumElts; ++Index) {
326 if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
319327 BytesLeft, TD))
320328 return false;
321329 if (EltSize >= BytesLeft)
327335 }
328336 return true;
329337 }
330
331 if (ConstantVector *CV = dyn_cast(C)) {
332 uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType());
333 uint64_t Index = ByteOffset / EltSize;
334 uint64_t Offset = ByteOffset - Index * EltSize;
335 for (; Index != CV->getType()->getNumElements(); ++Index) {
336 if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr,
337 BytesLeft, TD))
338 return false;
339 if (EltSize >= BytesLeft)
340 return true;
341338
342 Offset = 0;
343 BytesLeft -= EltSize;
344 CurPtr += EltSize;
345 }
346 return true;
347 }
348
349 if (ConstantDataSequential *CDS = dyn_cast(C)) {
350 uint64_t EltSize = CDS->getElementByteSize();
351 uint64_t Index = ByteOffset / EltSize;
352 uint64_t Offset = ByteOffset - Index * EltSize;
353 for (unsigned e = CDS->getNumElements(); Index != e; ++Index) {
354 if (!ReadDataFromGlobal(CDS->getElementAsConstant(Index), Offset, CurPtr,
355 BytesLeft, TD))
356 return false;
357 if (EltSize >= BytesLeft)
358 return true;
359
360 Offset = 0;
361 BytesLeft -= EltSize;
362 CurPtr += EltSize;
363 }
364 return true;
365 }
366
367339 if (ConstantExpr *CE = dyn_cast(C)) {
368340 if (CE->getOpcode() == Instruction::IntToPtr &&
369341 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
370 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
371 BytesLeft, TD);
342 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
343 BytesLeft, TD);
372344 }
373345
374346 // Otherwise, unknown initializer type.
1009981 ConstantExpr *CE) {
1010982 if (!CE->getOperand(1)->isNullValue())
1011983 return 0; // Do not allow stepping over the value!
1012
1013 SmallVector Indices(CE->getNumOperands()-2);
1014 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i)
1015 Indices[i-2] = CE->getOperand(i);
1016 return ConstantFoldLoadThroughGEPIndices(C, Indices);
984
985 // Loop over all of the operands, tracking down which value we are
986 // addressing.
987 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
988 C = C->getAggregateElement(CE->getOperand(i));
989 if (C == 0) return 0;
990 }
991 return C;
1017992 }
1018993
1019994 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
10251000 // Loop over all of the operands, tracking down which value we are
10261001 // addressing.
10271002 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1028 ConstantInt *Idx = dyn_cast(Indices[i]);
1029 if (Idx == 0) return 0;
1030
1031 uint64_t IdxVal = Idx->getZExtValue();
1032
1033 if (ConstantStruct *CS = dyn_cast(C)) {
1034 C = CS->getOperand(IdxVal);
1035 } else if (ConstantAggregateZero *CAZ = dyn_cast(C)){
1036 C = CAZ->getElementValue(Idx);
1037 } else if (UndefValue *UV = dyn_cast(C)) {
1038 C = UV->getElementValue(Idx);
1039 } else if (ConstantArray *CA = dyn_cast(C)) {
1040 if (IdxVal >= CA->getType()->getNumElements())
1041 return 0;
1042 C = CA->getOperand(IdxVal);
1043 } else if (ConstantDataSequential *CDS=dyn_cast(C)){
1044 if (IdxVal >= CDS->getNumElements())
1045 return 0;
1046 C = CDS->getElementAsConstant(IdxVal);
1047 } else if (ConstantVector *CV = dyn_cast(C)) {
1048 if (IdxVal >= CV->getType()->getNumElements())
1049 return 0;
1050 C = CV->getOperand(IdxVal);
1051 } else {
1052 return 0;
1053 }
1003 C = C->getAggregateElement(Indices[i]);
1004 if (C == 0) return 0;
10541005 }
10551006 return C;
10561007 }
14921492 "Not looking at a struct or array?");
14931493 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
14941494 "Invalid indices for type?");
1495 CompositeType *PTy = cast(V->getType());
1496
1497 if (isa(V))
1498 return UndefValue::get(ExtractValueInst::getIndexedType(PTy, idx_range));
1499 if (isa(V))
1500 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
1501 idx_range));
1502 if (isa(V) || isa(V))
1503 // Recursively process this constant
1504 return FindInsertedValue(cast(V)->getOperand(idx_range[0]),
1505 idx_range.slice(1), InsertBefore);
1506 if (ConstantDataSequential *CDS = dyn_cast(V))
1507 return CDS->getElementAsConstant(idx_range[0]);
1495
1496 if (Constant *C = dyn_cast(V)) {
1497 C = C->getAggregateElement(idx_range[0]);
1498 if (C == 0) return 0;
1499 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
1500 }
15081501
15091502 if (InsertValueInst *I = dyn_cast(V)) {
15101503 // Loop the indices for the insertvalue instruction in parallel with the
843843 }
844844
845845 static void getArrayElements(Constant *C, SmallVectorImpl &Dest) {
846 if (ConstantArray *I = dyn_cast(C)) {
847 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
848 Dest.push_back(I->getOperand(i));
849 return;
850 }
851
852 if (ConstantDataSequential *CDS = dyn_cast(C)) {
853 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
854 Dest.push_back(CDS->getElementAsConstant(i));
855 return;
856 }
857
858 ConstantAggregateZero *CAZ = cast(C);
859 Dest.append(cast(C->getType())->getNumElements(),
860 CAZ->getSequentialElement());
846 unsigned NumElements = cast(C->getType())->getNumElements();
847
848 for (unsigned i = 0; i != NumElements; ++i)
849 Dest.push_back(C->getAggregateElement(i));
861850 }
862851
863852 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
275275 return false;
276276 }
277277
278 static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
279 ConstantInt *CI = dyn_cast(Idx);
280 if (!CI) return 0;
281 unsigned IdxV = CI->getZExtValue();
282
283 if (ConstantStruct *CS = dyn_cast(Agg)) {
284 if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV);
285 } else if (ConstantArray *CA = dyn_cast(Agg)) {
286 if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV);
287 } else if (ConstantVector *CP = dyn_cast(Agg)) {
288 if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV);
289 } else if (isa(Agg)) {
290 if (StructType *STy = dyn_cast(Agg->getType())) {
291 if (IdxV < STy->getNumElements())
292 return Constant::getNullValue(STy->getElementType(IdxV));
293 } else if (SequentialType *STy =
294 dyn_cast(Agg->getType())) {
295 return Constant::getNullValue(STy->getElementType());
296 }
297 } else if (isa(Agg)) {
298 if (StructType *STy = dyn_cast(Agg->getType())) {
299 if (IdxV < STy->getNumElements())
300 return UndefValue::get(STy->getElementType(IdxV));
301 } else if (SequentialType *STy =
302 dyn_cast(Agg->getType())) {
303 return UndefValue::get(STy->getElementType());
304 }
305 }
306 return 0;
307 }
308
309
310278 /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
311279 /// users of the global, cleaning up the obvious ones. This is largely just a
312280 /// quick scan over the use list to clean up the easy and obvious cruft. This
519487 NewGlobals.reserve(STy->getNumElements());
520488 const StructLayout &Layout = *TD.getStructLayout(STy);
521489 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
522 Constant *In = getAggregateConstantElement(Init,
523 ConstantInt::get(Type::getInt32Ty(STy->getContext()), i));
490 Constant *In = Init->getAggregateElement(i);
524491 assert(In && "Couldn't get element of initializer?");
525492 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
526493 GlobalVariable::InternalLinkage,
552519 uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType());
553520 unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
554521 for (unsigned i = 0, e = NumElements; i != e; ++i) {
555 Constant *In = getAggregateConstantElement(Init,
556 ConstantInt::get(Type::getInt32Ty(Init->getContext()), i));
522 Constant *In = Init->getAggregateElement(i);
557523 assert(In && "Couldn't get element of initializer?");
558524
559525 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
833833 }
834834
835835 UndefElts = 0;
836 if (ConstantVector *CV = dyn_cast(V)) {
837 Type *EltTy = cast(V->getType())->getElementType();
838 Constant *Undef = UndefValue::get(EltTy);
839
840 std::vector Elts;
841 for (unsigned i = 0; i != VWidth; ++i)
842 if (!DemandedElts[i]) { // If not demanded, set to undef.
843 Elts.push_back(Undef);
844 UndefElts.setBit(i);
845 } else if (isa(CV->getOperand(i))) { // Already undef.
846 Elts.push_back(Undef);
847 UndefElts.setBit(i);
848 } else { // Otherwise, defined.
849 Elts.push_back(CV->getOperand(i));
850 }
851
852 // If we changed the constant, return it.
853 Constant *NewCV = ConstantVector::get(Elts);
854 return NewCV != CV ? NewCV : 0;
855 }
856836
857 if (ConstantDataVector *CDV = dyn_cast(V)) {
837 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
838 if (Constant *C = dyn_cast(V)) {
858839 // Check if this is identity. If so, return 0 since we are not simplifying
859840 // anything.
860841 if (DemandedElts.isAllOnesValue())
861842 return 0;
862843
863 // Simplify to a ConstantVector where the non-demanded elements are undef.
864 Constant *Undef = UndefValue::get(CDV->getElementType());
844 Type *EltTy = cast(V->getType())->getElementType();
845 Constant *Undef = UndefValue::get(EltTy);
865846
866847 SmallVector Elts;
867 for (unsigned i = 0; i != VWidth; ++i)
868 Elts.push_back(DemandedElts[i] ? CDV->getElementAsConstant(i) : Undef);
869 UndefElts = DemandedElts ^ EltMask;
870 return ConstantVector::get(Elts);
871
872 }
873
874 if (ConstantAggregateZero *CAZ = dyn_cast(V)) {
875 // Check if this is identity. If so, return 0 since we are not simplifying
876 // anything.
877 if (DemandedElts.isAllOnesValue())
878 return 0;
879
880 // Simplify the CAZ to a ConstantVector where the non-demanded elements are
881 // set to undef.
882 Constant *Zero = CAZ->getSequentialElement();
883 Constant *Undef = UndefValue::get(Zero->getType());
884 SmallVector Elts;
885 for (unsigned i = 0; i != VWidth; ++i)
886 Elts.push_back(DemandedElts[i] ? Zero : Undef);
887 UndefElts = DemandedElts ^ EltMask;
888 return ConstantVector::get(Elts);
848 for (unsigned i = 0; i != VWidth; ++i) {
849 if (!DemandedElts[i]) { // If not demanded, set to undef.
850 Elts.push_back(Undef);
851 UndefElts.setBit(i);
852 continue;
853 }
854
855 Constant *Elt = C->getAggregateElement(i);
856 if (Elt == 0) return 0;
857
858 if (isa(Elt)) { // Already undef.
859 Elts.push_back(Undef);
860 UndefElts.setBit(i);
861 } else { // Otherwise, defined.
862 Elts.push_back(Elt);
863 }
864 }
865
866 // If we changed the constant, return it.
867 Constant *NewCV = ConstantVector::get(Elts);
868 return NewCV != C ? NewCV : 0;
889869 }
890870
891871 // Limit search depth.