llvm.org GIT mirror llvm / c40f17b
Generalize ScalarEvolution to be able to analyze GEPs when TargetData is not present. It still uses TargetData when available. This generalization also fixed some limitations in the TargetData case; the attached testcase covers this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79344 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
6 changed file(s) with 567 addition(s) and 123 deletion(s). Raw diff Collapse all Expand all
432432 const SCEV *getUMaxExpr(SmallVectorImpl &Operands);
433433 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
434434 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
435 const SCEV *getFieldOffsetExpr(const StructType *STy, unsigned FieldNo);
436 const SCEV *getAllocSizeExpr(const Type *AllocTy);
435437 const SCEV *getUnknown(Value *V);
436438 const SCEV *getCouldNotCompute();
437439
114114
115115 Value *visitUMaxExpr(const SCEVUMaxExpr *S);
116116
117 Value *visitFieldOffsetExpr(const SCEVFieldOffsetExpr *S);
118
119 Value *visitAllocSizeExpr(const SCEVAllocSizeExpr *S);
120
117121 Value *visitUnknown(const SCEVUnknown *S) {
118122 return S->getValue();
119123 }
2525 // These should be ordered in terms of increasing complexity to make the
2626 // folders simpler.
2727 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
28 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, scUnknown,
29 scCouldNotCompute
28 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
29 scFieldOffset, scAllocSize, scUnknown, scCouldNotCompute
3030 };
3131
3232 //===--------------------------------------------------------------------===//
487487 }
488488 };
489489
490 //===--------------------------------------------------------------------===//
491 /// SCEVTargetDataConstant - This node is the base class for representing
492 /// target-dependent values in a target-independent way.
493 ///
494 class SCEVTargetDataConstant : public SCEV {
495 protected:
496 const Type *Ty;
497 SCEVTargetDataConstant(const FoldingSetNodeID &ID, enum SCEVTypes T,
498 const Type *ty) :
499 SCEV(ID, T), Ty(ty) {}
500
501 public:
502 virtual bool isLoopInvariant(const Loop *) const { return true; }
503 virtual bool hasComputableLoopEvolution(const Loop *) const {
504 return false; // not computable
505 }
506
507 virtual bool hasOperand(const SCEV *) const {
508 return false;
509 }
510
511 bool dominates(BasicBlock *, DominatorTree *) const {
512 return true;
513 }
514
515 virtual const Type *getType() const { return Ty; }
516
517 /// Methods for support type inquiry through isa, cast, and dyn_cast:
518 static inline bool classof(const SCEVTargetDataConstant *S) { return true; }
519 static inline bool classof(const SCEV *S) {
520 return S->getSCEVType() == scFieldOffset ||
521 S->getSCEVType() == scAllocSize;
522 }
523 };
524
525 //===--------------------------------------------------------------------===//
526 /// SCEVFieldOffsetExpr - This node represents an offsetof expression.
527 ///
528 class SCEVFieldOffsetExpr : public SCEVTargetDataConstant {
529 friend class ScalarEvolution;
530
531 const StructType *STy;
532 unsigned FieldNo;
533 SCEVFieldOffsetExpr(const FoldingSetNodeID &ID, const Type *ty,
534 const StructType *sty, unsigned fieldno) :
535 SCEVTargetDataConstant(ID, scFieldOffset, ty),
536 STy(sty), FieldNo(fieldno) {}
537
538 public:
539 const StructType *getStructType() const { return STy; }
540 unsigned getFieldNo() const { return FieldNo; }
541
542 virtual void print(raw_ostream &OS) const;
543
544 /// Methods for support type inquiry through isa, cast, and dyn_cast:
545 static inline bool classof(const SCEVFieldOffsetExpr *S) { return true; }
546 static inline bool classof(const SCEV *S) {
547 return S->getSCEVType() == scFieldOffset;
548 }
549 };
550
551 //===--------------------------------------------------------------------===//
552 /// SCEVAllocSize - This node represents a sizeof expression.
553 ///
554 class SCEVAllocSizeExpr : public SCEVTargetDataConstant {
555 friend class ScalarEvolution;
556
557 const Type *AllocTy;
558 SCEVAllocSizeExpr(const FoldingSetNodeID &ID,
559 const Type *ty, const Type *allocty) :
560 SCEVTargetDataConstant(ID, scAllocSize, ty),
561 AllocTy(allocty) {}
562
563 public:
564 const Type *getAllocType() const { return AllocTy; }
565
566 virtual void print(raw_ostream &OS) const;
567
568 /// Methods for support type inquiry through isa, cast, and dyn_cast:
569 static inline bool classof(const SCEVAllocSizeExpr *S) { return true; }
570 static inline bool classof(const SCEV *S) {
571 return S->getSCEVType() == scAllocSize;
572 }
573 };
490574
491575 //===--------------------------------------------------------------------===//
492576 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
551635 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
552636 case scUMaxExpr:
553637 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
638 case scFieldOffset:
639 return ((SC*)this)->visitFieldOffsetExpr((const SCEVFieldOffsetExpr*)S);
640 case scAllocSize:
641 return ((SC*)this)->visitAllocSizeExpr((const SCEVAllocSizeExpr*)S);
554642 case scUnknown:
555643 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
556644 case scCouldNotCompute:
306306 OS << "}<" << L->getHeader()->getName() + ">";
307307 }
308308
309 void SCEVFieldOffsetExpr::print(raw_ostream &OS) const {
310 // LLVM struct fields don't have names, so just print the field number.
311 OS << "offsetof(" << *STy << ", " << FieldNo << ")";
312 }
313
314 void SCEVAllocSizeExpr::print(raw_ostream &OS) const {
315 OS << "sizeof(" << *AllocTy << ")";
316 }
317
309318 bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
310319 // All non-instruction values are loop invariant. All instructions are loop
311320 // invariant if they are not contained in the specified loop.
333342 //===----------------------------------------------------------------------===//
334343 // SCEV Utilities
335344 //===----------------------------------------------------------------------===//
345
346 static bool CompareTypes(const Type *A, const Type *B) {
347 if (A->getTypeID() != B->getTypeID())
348 return A->getTypeID() < B->getTypeID();
349 if (const IntegerType *AI = dyn_cast(A)) {
350 const IntegerType *BI = cast(B);
351 return AI->getBitWidth() < BI->getBitWidth();
352 }
353 if (const PointerType *AI = dyn_cast(A)) {
354 const PointerType *BI = cast(B);
355 return CompareTypes(AI->getElementType(), BI->getElementType());
356 }
357 if (const ArrayType *AI = dyn_cast(A)) {
358 const ArrayType *BI = cast(B);
359 if (AI->getNumElements() != BI->getNumElements())
360 return AI->getNumElements() < BI->getNumElements();
361 return CompareTypes(AI->getElementType(), BI->getElementType());
362 }
363 if (const VectorType *AI = dyn_cast(A)) {
364 const VectorType *BI = cast(B);
365 if (AI->getNumElements() != BI->getNumElements())
366 return AI->getNumElements() < BI->getNumElements();
367 return CompareTypes(AI->getElementType(), BI->getElementType());
368 }
369 if (const StructType *AI = dyn_cast(A)) {
370 const StructType *BI = cast(B);
371 if (AI->getNumElements() != BI->getNumElements())
372 return AI->getNumElements() < BI->getNumElements();
373 for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i)
374 if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) ||
375 CompareTypes(BI->getElementType(i), AI->getElementType(i)))
376 return CompareTypes(AI->getElementType(i), BI->getElementType(i));
377 }
378 return false;
379 }
336380
337381 namespace {
338382 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
444488 if (const SCEVCastExpr *LC = dyn_cast(LHS)) {
445489 const SCEVCastExpr *RC = cast(RHS);
446490 return operator()(LC->getOperand(), RC->getOperand());
491 }
492
493 // Compare offsetof expressions.
494 if (const SCEVFieldOffsetExpr *LA = dyn_cast(LHS)) {
495 const SCEVFieldOffsetExpr *RA = cast(RHS);
496 if (CompareTypes(LA->getStructType(), RA->getStructType()) ||
497 CompareTypes(RA->getStructType(), LA->getStructType()))
498 return CompareTypes(LA->getStructType(), RA->getStructType());
499 return LA->getFieldNo() < RA->getFieldNo();
500 }
501
502 // Compare sizeof expressions by the allocation type.
503 if (const SCEVAllocSizeExpr *LA = dyn_cast(LHS)) {
504 const SCEVAllocSizeExpr *RA = cast(RHS);
505 return CompareTypes(LA->getAllocType(), RA->getAllocType());
447506 }
448507
449508 llvm_unreachable("Unknown SCEV kind!");
9751034 /// unspecified bits out to the given type.
9761035 ///
9771036 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
978 const Type *Ty) {
1037 const Type *Ty) {
9791038 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
9801039 "This is not an extending conversion!");
9811040 assert(isSCEVable(Ty) &&
20002059 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
20012060 }
20022061
2062 const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy,
2063 unsigned FieldNo) {
2064 // If we have TargetData we can determine the constant offset.
2065 if (TD) {
2066 const Type *IntPtrTy = TD->getIntPtrType(getContext());
2067 const StructLayout &SL = *TD->getStructLayout(STy);
2068 uint64_t Offset = SL.getElementOffset(FieldNo);
2069 return getIntegerSCEV(Offset, IntPtrTy);
2070 }
2071
2072 // Field 0 is always at offset 0.
2073 if (FieldNo == 0) {
2074 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2075 return getIntegerSCEV(0, Ty);
2076 }
2077
2078 // Okay, it looks like we really DO need an offsetof expr. Check to see if we
2079 // already have one, otherwise create a new one.
2080 FoldingSetNodeID ID;
2081 ID.AddInteger(scFieldOffset);
2082 ID.AddPointer(STy);
2083 ID.AddInteger(FieldNo);
2084 void *IP = 0;
2085 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2086 SCEV *S = SCEVAllocator.Allocate();
2087 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2088 new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo);
2089 UniqueSCEVs.InsertNode(S, IP);
2090 return S;
2091 }
2092
2093 const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) {
2094 // If we have TargetData we can determine the constant size.
2095 if (TD && AllocTy->isSized()) {
2096 const Type *IntPtrTy = TD->getIntPtrType(getContext());
2097 return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy);
2098 }
2099
2100 // Expand an array size into the element size times the number
2101 // of elements.
2102 if (const ArrayType *ATy = dyn_cast(AllocTy)) {
2103 const SCEV *E = getAllocSizeExpr(ATy->getElementType());
2104 return getMulExpr(
2105 E, getConstant(ConstantInt::get(cast(E->getType()),
2106 ATy->getNumElements())));
2107 }
2108
2109 // Expand a vector size into the element size times the number
2110 // of elements.
2111 if (const VectorType *VTy = dyn_cast(AllocTy)) {
2112 const SCEV *E = getAllocSizeExpr(VTy->getElementType());
2113 return getMulExpr(
2114 E, getConstant(ConstantInt::get(cast(E->getType()),
2115 VTy->getNumElements())));
2116 }
2117
2118 // Okay, it looks like we really DO need a sizeof expr. Check to see if we
2119 // already have one, otherwise create a new one.
2120 FoldingSetNodeID ID;
2121 ID.AddInteger(scAllocSize);
2122 ID.AddPointer(AllocTy);
2123 void *IP = 0;
2124 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2125 SCEV *S = SCEVAllocator.Allocate();
2126 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2127 new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy);
2128 UniqueSCEVs.InsertNode(S, IP);
2129 return S;
2130 }
2131
20032132 const SCEV *ScalarEvolution::getUnknown(Value *V) {
20042133 // Don't attempt to do anything other than create a SCEVUnknown object
20052134 // here. createSCEV only calls getUnknown after checking for all other
20262155 /// can optionally include pointer types if the ScalarEvolution class
20272156 /// has access to target-specific information.
20282157 bool ScalarEvolution::isSCEVable(const Type *Ty) const {
2029 // Integers are always SCEVable.
2030 if (Ty->isInteger())
2031 return true;
2032
2033 // Pointers are SCEVable if TargetData information is available
2034 // to provide pointer size information.
2035 if (isa(Ty))
2036 return TD != NULL;
2037
2038 // Otherwise it's not SCEVable.
2039 return false;
2158 // Integers and pointers are always SCEVable.
2159 return Ty->isInteger() || isa(Ty);
20402160 }
20412161
20422162 /// getTypeSizeInBits - Return the size in bits of the specified type,
20482168 if (TD)
20492169 return TD->getTypeSizeInBits(Ty);
20502170
2051 // Otherwise, we support only integer types.
2052 assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!");
2053 return Ty->getPrimitiveSizeInBits();
2171 // Integer types have fixed sizes.
2172 if (Ty->isInteger())
2173 return Ty->getPrimitiveSizeInBits();
2174
2175 // The only other support type is pointer. Without TargetData, conservatively
2176 // assume pointers are 64-bit.
2177 assert(isa(Ty) && "isSCEVable permitted a non-SCEVable type!");
2178 return 64;
20542179 }
20552180
20562181 /// getEffectiveSCEVType - Return a type with the same bitwidth as
20632188 if (Ty->isInteger())
20642189 return Ty;
20652190
2191 // The only other support type is pointer.
20662192 assert(isa(Ty) && "Unexpected non-pointer non-integer type!");
2067 return TD->getIntPtrType(getContext());
2193 if (TD) return TD->getIntPtrType(getContext());
2194
2195 // Without TargetData, conservatively assume pointers are 64-bit.
2196 return Type::getInt64Ty(getContext());
20682197 }
20692198
20702199 const SCEV *ScalarEvolution::getCouldNotCompute() {
21312260 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V,
21322261 const Type *Ty) {
21332262 const Type *SrcTy = V->getType();
2134 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2135 (Ty->isInteger() || (TD && isa(Ty))) &&
2263 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2264 (Ty->isInteger() || isa(Ty)) &&
21362265 "Cannot truncate or zero extend with non-integer arguments!");
21372266 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
21382267 return V; // No conversion
21482277 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
21492278 const Type *Ty) {
21502279 const Type *SrcTy = V->getType();
2151 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2152 (Ty->isInteger() || (TD && isa(Ty))) &&
2280 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2281 (Ty->isInteger() || isa(Ty)) &&
21532282 "Cannot truncate or zero extend with non-integer arguments!");
21542283 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
21552284 return V; // No conversion
21642293 const SCEV *
21652294 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) {
21662295 const Type *SrcTy = V->getType();
2167 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2168 (Ty->isInteger() || (TD && isa(Ty))) &&
2296 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2297 (Ty->isInteger() || isa(Ty)) &&
21692298 "Cannot noop or zero extend with non-integer arguments!");
21702299 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
21712300 "getNoopOrZeroExtend cannot truncate!");
21802309 const SCEV *
21812310 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) {
21822311 const Type *SrcTy = V->getType();
2183 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2184 (Ty->isInteger() || (TD && isa(Ty))) &&
2312 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2313 (Ty->isInteger() || isa(Ty)) &&
21852314 "Cannot noop or sign extend with non-integer arguments!");
21862315 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
21872316 "getNoopOrSignExtend cannot truncate!");
21972326 const SCEV *
21982327 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) {
21992328 const Type *SrcTy = V->getType();
2200 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2201 (Ty->isInteger() || (TD && isa(Ty))) &&
2329 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2330 (Ty->isInteger() || isa(Ty)) &&
22022331 "Cannot noop or any extend with non-integer arguments!");
22032332 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
22042333 "getNoopOrAnyExtend cannot truncate!");
22122341 const SCEV *
22132342 ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) {
22142343 const Type *SrcTy = V->getType();
2215 assert((SrcTy->isInteger() || (TD && isa(SrcTy))) &&
2216 (Ty->isInteger() || (TD && isa(Ty))) &&
2344 assert((SrcTy->isInteger() || isa(SrcTy)) &&
2345 (Ty->isInteger() || isa(Ty)) &&
22172346 "Cannot truncate or noop with non-integer arguments!");
22182347 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
22192348 "getTruncateOrNoop cannot extend!");
24322561 ///
24332562 const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) {
24342563
2435 const Type *IntPtrTy = TD->getIntPtrType(getContext());
2564 const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
24362565 Value *Base = GEP->getOperand(0);
24372566 // Don't attempt to analyze GEPs over unsized objects.
24382567 if (!cast(Base->getType())->getElementType()->isSized())
24462575 // Compute the (potentially symbolic) offset in bytes for this index.
24472576 if (const StructType *STy = dyn_cast(*GTI++)) {
24482577 // For a struct, add the member offset.
2449 const StructLayout &SL = *TD->getStructLayout(STy);
24502578 unsigned FieldNo = cast(Index)->getZExtValue();
2451 uint64_t Offset = SL.getElementOffset(FieldNo);
2452 TotalOffset = getAddExpr(TotalOffset, getIntegerSCEV(Offset, IntPtrTy));
2579 TotalOffset = getAddExpr(TotalOffset,
2580 getFieldOffsetExpr(STy, FieldNo));
24532581 } else {
24542582 // For an array, add the element offset, explicitly scaled.
24552583 const SCEV *LocalOffset = getSCEV(Index);
24562584 if (!isa(LocalOffset->getType()))
24572585 // Getelementptr indicies are signed.
24582586 LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy);
2459 LocalOffset =
2460 getMulExpr(LocalOffset,
2461 getIntegerSCEV(TD->getTypeAllocSize(*GTI), IntPtrTy));
2587 LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI));
24622588 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
24632589 }
24642590 }
29513077 // expressions we handle are GEPs and address literals.
29523078
29533079 case Instruction::GetElementPtr:
2954 if (!TD) break; // Without TD we can't analyze pointers.
29553080 return createNodeForGEP(U);
29563081
29573082 case Instruction::PHI:
39464071 return getTruncateExpr(Op, Cast->getType());
39474072 }
39484073
4074 if (isa(V))
4075 return V;
4076
39494077 llvm_unreachable("Unknown SCEV type!");
39504078 return 0;
39514079 }
157157 /// check to see if the divide was folded.
158158 static bool FactorOutConstant(const SCEV *&S,
159159 const SCEV *&Remainder,
160 const APInt &Factor,
161 ScalarEvolution &SE) {
160 const SCEV *Factor,
161 ScalarEvolution &SE,
162 const TargetData *TD) {
162163 // Everything is divisible by one.
163 if (Factor == 1)
164 if (Factor->isOne())
164165 return true;
166
167 // x/x == 1.
168 if (S == Factor) {
169 S = SE.getIntegerSCEV(1, S->getType());
170 return true;
171 }
165172
166173 // For a Constant, check for a multiple of the given factor.
167174 if (const SCEVConstant *C = dyn_cast(S)) {
168 ConstantInt *CI =
169 ConstantInt::get(SE.getContext(), C->getValue()->getValue().sdiv(Factor));
170 // If the quotient is zero and the remainder is non-zero, reject
171 // the value at this scale. It will be considered for subsequent
172 // smaller scales.
173 if (C->isZero() || !CI->isZero()) {
174 const SCEV *Div = SE.getConstant(CI);
175 S = Div;
176 Remainder =
177 SE.getAddExpr(Remainder,
178 SE.getConstant(C->getValue()->getValue().srem(Factor)));
175 // 0/x == 0.
176 if (C->isZero())
179177 return true;
178 // Check for divisibility.
179 if (const SCEVConstant *FC = dyn_cast(Factor)) {
180 ConstantInt *CI =
181 ConstantInt::get(SE.getContext(),
182 C->getValue()->getValue().sdiv(
183 FC->getValue()->getValue()));
184 // If the quotient is zero and the remainder is non-zero, reject
185 // the value at this scale. It will be considered for subsequent
186 // smaller scales.
187 if (!CI->isZero()) {
188 const SCEV *Div = SE.getConstant(CI);
189 S = Div;
190 Remainder =
191 SE.getAddExpr(Remainder,
192 SE.getConstant(C->getValue()->getValue().srem(
193 FC->getValue()->getValue())));
194 return true;
195 }
180196 }
181197 }
182198
183199 // In a Mul, check if there is a constant operand which is a multiple
184200 // of the given factor.
185 if (const SCEVMulExpr *M = dyn_cast(S))
186 if (const SCEVConstant *C = dyn_cast(M->getOperand(0)))
187 if (!C->getValue()->getValue().srem(Factor)) {
188 const SmallVectorImpl &MOperands = M->getOperands();
189 SmallVector NewMulOps(MOperands.begin(),
190 MOperands.end());
191 NewMulOps[0] =
192 SE.getConstant(C->getValue()->getValue().sdiv(Factor));
193 S = SE.getMulExpr(NewMulOps);
194 return true;
201 if (const SCEVMulExpr *M = dyn_cast(S)) {
202 if (TD) {
203 // With TargetData, the size is known. Check if there is a constant
204 // operand which is a multiple of the given factor. If so, we can
205 // factor it.
206 const SCEVConstant *FC = cast(Factor);
207 if (const SCEVConstant *C = dyn_cast(M->getOperand(0)))
208 if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) {
209 const SmallVectorImpl &MOperands = M->getOperands();
210 SmallVector NewMulOps(MOperands.begin(),
211 MOperands.end());
212 NewMulOps[0] =
213 SE.getConstant(C->getValue()->getValue().sdiv(
214 FC->getValue()->getValue()));
215 S = SE.getMulExpr(NewMulOps);
216 return true;
217 }
218 } else {
219 // Without TargetData, check if Factor can be factored out of any of the
220 // Mul's operands. If so, we can just remove it.
221 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
222 const SCEV *SOp = M->getOperand(i);
223 const SCEV *Remainder = SE.getIntegerSCEV(0, SOp->getType());
224 if (FactorOutConstant(SOp, Remainder, Factor, SE, TD) &&
225 Remainder->isZero()) {
226 const SmallVectorImpl &MOperands = M->getOperands();
227 SmallVector NewMulOps(MOperands.begin(),
228 MOperands.end());
229 NewMulOps[i] = SOp;
230 S = SE.getMulExpr(NewMulOps);
231 return true;
232 }
195233 }
234 }
235 }
196236
197237 // In an AddRec, check if both start and step are divisible.
198238 if (const SCEVAddRecExpr *A = dyn_cast(S)) {
199239 const SCEV *Step = A->getStepRecurrence(SE);
200240 const SCEV *StepRem = SE.getIntegerSCEV(0, Step->getType());
201 if (!FactorOutConstant(Step, StepRem, Factor, SE))
241 if (!FactorOutConstant(Step, StepRem, Factor, SE, TD))
202242 return false;
203243 if (!StepRem->isZero())
204244 return false;
205245 const SCEV *Start = A->getStart();
206 if (!FactorOutConstant(Start, Remainder, Factor, SE))
246 if (!FactorOutConstant(Start, Remainder, Factor, SE, TD))
207247 return false;
208248 S = SE.getAddRecExpr(Start, Step, A->getLoop());
209249 return true;
212252 return false;
213253 }
214254
255 /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs
256 /// is the number of SCEVAddRecExprs present, which are kept at the end of
257 /// the list.
258 ///
259 static void SimplifyAddOperands(SmallVectorImpl &Ops,
260 const Type *Ty,
261 ScalarEvolution &SE) {
262 unsigned NumAddRecs = 0;
263 for (unsigned i = Ops.size(); i > 0 && isa(Ops[i-1]); --i)
264 ++NumAddRecs;
265 // Group Ops into non-addrecs and addrecs.
266 SmallVector NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs);
267 SmallVector AddRecs(Ops.end() - NumAddRecs, Ops.end());
268 // Let ScalarEvolution sort and simplify the non-addrecs list.
269 const SCEV *Sum = NoAddRecs.empty() ?
270 SE.getIntegerSCEV(0, Ty) :
271 SE.getAddExpr(NoAddRecs);
272 // If it returned an add, use the operands. Otherwise it simplified
273 // the sum into a single value, so just use that.
274 if (const SCEVAddExpr *Add = dyn_cast(Sum))
275 Ops = Add->getOperands();
276 else {
277 Ops.clear();
278 if (!Sum->isZero())
279 Ops.push_back(Sum);
280 }
281 // Then append the addrecs.
282 Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end());
283 }
284
285 /// SplitAddRecs - Flatten a list of add operands, moving addrec start values
286 /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}.
287 /// This helps expose more opportunities for folding parts of the expressions
288 /// into GEP indices.
289 ///
290 static void SplitAddRecs(SmallVectorImpl &Ops,
291 const Type *Ty,
292 ScalarEvolution &SE) {
293 // Find the addrecs.
294 SmallVector AddRecs;
295 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
296 while (const SCEVAddRecExpr *A = dyn_cast(Ops[i])) {
297 const SCEV *Start = A->getStart();
298 if (Start->isZero()) break;
299 const SCEV *Zero = SE.getIntegerSCEV(0, Ty);
300 AddRecs.push_back(SE.getAddRecExpr(Zero,
301 A->getStepRecurrence(SE),
302 A->getLoop()));
303 if (const SCEVAddExpr *Add = dyn_cast(Start)) {
304 Ops[i] = Zero;
305 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
306 e += Add->getNumOperands();
307 } else {
308 Ops[i] = Start;
309 }
310 }
311 if (!AddRecs.empty()) {
312 // Add the addrecs onto the end of the list.
313 Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end());
314 // Resort the operand list, moving any constants to the front.
315 SimplifyAddOperands(Ops, Ty, SE);
316 }
317 }
318
215319 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
216320 /// instead of using ptrtoint+arithmetic+inttoptr. This helps
217 /// BasicAliasAnalysis analyze the result.
321 /// BasicAliasAnalysis and other passes analyze the result.
218322 ///
219323 /// Design note: This depends on ScalarEvolution not recognizing inttoptr
220324 /// and ptrtoint operators, as they may introduce pointer arithmetic
245349 SmallVector Ops(op_begin, op_end);
246350 bool AnyNonZeroIndices = false;
247351
352 // Split AddRecs up into parts as either of the parts may be usable
353 // without the other.
354 SplitAddRecs(Ops, Ty, SE);
355
248356 // Decend down the pointer's type and attempt to convert the other
249357 // operands into GEP indices, at each level. The first index in a GEP
250358 // indexes into the array implied by the pointer operand; the rest of
251359 // the indices index into the element or field type selected by the
252360 // preceding index.
253361 for (;;) {
254 APInt ElSize = APInt(SE.getTypeSizeInBits(Ty),
255 ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0);
256 SmallVector NewOps;
362 const SCEV *ElSize = SE.getAllocSizeExpr(ElTy);
363 // If the scale size is not 0, attempt to factor out a scale for
364 // array indexing.
257365 SmallVector ScaledOps;
258 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
259 // Split AddRecs up into parts as either of the parts may be usable
260 // without the other.
261 if (const SCEVAddRecExpr *A = dyn_cast(Ops[i]))
262 if (!A->getStart()->isZero()) {
263 const SCEV *Start = A->getStart();
264 Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
265 A->getStepRecurrence(SE),
266 A->getLoop()));
267 Ops[i] = Start;
268 ++e;
269 }
270 // If the scale size is not 0, attempt to factor out a scale.
271 if (ElSize != 0) {
366 if (ElTy->isSized() && !ElSize->isZero()) {
367 SmallVector NewOps;
368 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
272369 const SCEV *Op = Ops[i];
273 const SCEV *Remainder = SE.getIntegerSCEV(0, Op->getType());
274 if (FactorOutConstant(Op, Remainder, ElSize, SE)) {
275 ScaledOps.push_back(Op); // Op now has ElSize factored out.
276 NewOps.push_back(Remainder);
277 continue;
370 const SCEV *Remainder = SE.getIntegerSCEV(0, Ty);
371 if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.TD)) {
372 // Op now has ElSize factored out.
373 ScaledOps.push_back(Op);
374 if (!Remainder->isZero())
375 NewOps.push_back(Remainder);
376 AnyNonZeroIndices = true;
377 } else {
378 // The operand was not divisible, so add it to the list of operands
379 // we'll scan next iteration.
380 NewOps.push_back(Ops[i]);
278381 }
279382 }
280 // If the operand was not divisible, add it to the list of operands
281 // we'll scan next iteration.
282 NewOps.push_back(Ops[i]);
283 }
284 Ops = NewOps;
285 AnyNonZeroIndices |= !ScaledOps.empty();
383 // If we made any changes, update Ops.
384 if (!ScaledOps.empty()) {
385 Ops = NewOps;
386 SimplifyAddOperands(Ops, Ty, SE);
387 }
388 }
389
390 // Record the scaled array index for this level of the type. If
391 // we didn't find any operands that could be factored, tentatively
392 // assume that element zero was selected (since the zero offset
393 // would obviously be folded away).
286394 Value *Scaled = ScaledOps.empty() ?
287395 Constant::getNullValue(Ty) :
288396 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
289397 GepIndices.push_back(Scaled);
290398
291399 // Collect struct field index operands.
292 if (!Ops.empty())
293 while (const StructType *STy = dyn_cast(ElTy)) {
400 while (const StructType *STy = dyn_cast(ElTy)) {
401 bool FoundFieldNo = false;
402 // An empty struct has no fields.
403 if (STy->getNumElements() == 0) break;
404 if (SE.TD) {
405 // With TargetData, field offsets are known. See if a constant offset
406 // falls within any of the struct fields.
407 if (Ops.empty()) break;
294408 if (const SCEVConstant *C = dyn_cast(Ops[0]))
295409 if (SE.getTypeSizeInBits(C->getType()) <= 64) {
296410 const StructLayout &SL = *SE.TD->getStructLayout(STy);
303417 Ops[0] =
304418 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
305419 AnyNonZeroIndices = true;
306 continue;
420 FoundFieldNo = true;
307421 }
308422 }
309 break;
423 } else {
424 // Without TargetData, just check for a SCEVFieldOffsetExpr of the
425 // appropriate struct type.
426 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
427 if (const SCEVFieldOffsetExpr *FO =
428 dyn_cast(Ops[i]))
429 if (FO->getStructType() == STy) {
430 unsigned FieldNo = FO->getFieldNo();
431 GepIndices.push_back(
432 ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
433 FieldNo));
434 ElTy = STy->getTypeAtIndex(FieldNo);
435 Ops[i] = SE.getConstant(Ty, 0);
436 AnyNonZeroIndices = true;
437 FoundFieldNo = true;
438 break;
439 }
310440 }
311
312 if (const ArrayType *ATy = dyn_cast(ElTy)) {
441 // If no struct field offsets were found, tentatively assume that
442 // field zero was selected (since the zero offset would obviously
443 // be folded away).
444 if (!FoundFieldNo) {
445 ElTy = STy->getTypeAtIndex(0u);
446 GepIndices.push_back(
447 Constant::getNullValue(Type::getInt32Ty(Ty->getContext())));
448 }
449 }
450
451 if (const ArrayType *ATy = dyn_cast(ElTy))
313452 ElTy = ATy->getElementType();
314 continue;
315 }
316 break;
453 else
454 break;
317455 }
318456
319457 // If none of the operands were convertable to proper GEP indices, cast
320458 // the base to i8* and do an ugly getelementptr with that. It's still
321459 // better than ptrtoint+arithmetic+inttoptr at least.
322460 if (!AnyNonZeroIndices) {
461 // Cast the base to i8*.
323462 V = InsertNoopCastOfTo(V,
324463 Type::getInt8Ty(Ty->getContext())->getPointerTo(PTy->getAddressSpace()));
464
465 // Expand the operands for a plain byte offset.
325466 Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
326467
327468 // Fold a GEP with constant operands.
344485 }
345486 }
346487
347 Value *GEP = Builder.CreateGEP(V, Idx, "scevgep");
488 // Emit a GEP.
489 Value *GEP = Builder.CreateGEP(V, Idx, "uglygep");
348490 InsertedValues.insert(GEP);
349491 return GEP;
350492 }
367509
368510 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
369511 // comments on expandAddToGEP for details.
370 if (SE.TD)
371 if (const PointerType *PTy = dyn_cast(V->getType())) {
372 const SmallVectorImpl &Ops = S->getOperands();
373 return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], PTy, Ty, V);
374 }
512 if (const PointerType *PTy = dyn_cast(V->getType())) {
513 const SmallVectorImpl &Ops = S->getOperands();
514 return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], PTy, Ty, V);
515 }
375516
376517 V = InsertNoopCastOfTo(V, Ty);
377518
483624
484625 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
485626 // comments on expandAddToGEP for details.
486 if (SE.TD) {
487 const SCEV *Base = S->getStart();
488 const SCEV *RestArray[1] = { Rest };
489 // Dig into the expression to find the pointer base for a GEP.
490 ExposePointerBase(Base, RestArray[0], SE);
491 // If we found a pointer, expand the AddRec with a GEP.
492 if (const PointerType *PTy = dyn_cast(Base->getType())) {
493 // Make sure the Base isn't something exotic, such as a multiplied
494 // or divided pointer value. In those cases, the result type isn't
495 // actually a pointer type.
496 if (!isa(Base) && !isa(Base)) {
497 Value *StartV = expand(Base);
498 assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
499 return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
500 }
627 const SCEV *Base = S->getStart();
628 const SCEV *RestArray[1] = { Rest };
629 // Dig into the expression to find the pointer base for a GEP.
630 ExposePointerBase(Base, RestArray[0], SE);
631 // If we found a pointer, expand the AddRec with a GEP.
632 if (const PointerType *PTy = dyn_cast(Base->getType())) {
633 // Make sure the Base isn't something exotic, such as a multiplied
634 // or divided pointer value. In those cases, the result type isn't
635 // actually a pointer type.
636 if (!isa(Base) && !isa(Base)) {
637 Value *StartV = expand(Base);
638 assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
639 return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
501640 }
502641 }
503642
653792 if (LHS->getType() != S->getType())
654793 LHS = InsertNoopCastOfTo(LHS, S->getType());
655794 return LHS;
795 }
796
797 Value *SCEVExpander::visitFieldOffsetExpr(const SCEVFieldOffsetExpr *S) {
798 return ConstantExpr::getOffsetOf(S->getStructType(), S->getFieldNo());
799 }
800
801 Value *SCEVExpander::visitAllocSizeExpr(const SCEVAllocSizeExpr *S) {
802 return ConstantExpr::getSizeOf(S->getAllocType());
656803 }
657804
658805 Value *SCEVExpander::expandCodeFor(const SCEV *SH, const Type *Ty) {
0 ; RUN: llvm-as < %s | opt -indvars | llvm-dis > %t
1 ; Exactly one getelementptr for each load+store.
2 ; RUN: grep getelementptr %t | count 6
3 ; Each getelementptr using %struct.Q* %s as a base and not i8*.
4 ; RUN: grep {getelementptr \[%\]struct\\.Q\\* \[%\]s,} %t | count 6
5 ; No explicit integer multiplications!
6 ; RUN: not grep {= mul} %t
7 ; No i8* arithmetic or pointer casting anywhere!
8 ; RUN: not grep {i8\\*} %t
9 ; RUN: not grep bitcast %t
10 ; RUN: not grep inttoptr %t
11 ; RUN: not grep ptrtoint %t
12
13 ; FIXME: This test should pass with or without TargetData. Until opt
14 ; supports running tests without targetdata, just hardware this in.
15 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
16
17 %struct.Q = type { [10 x %struct.N] }
18 %struct.N = type { %struct.S }
19 %struct.S = type { [100 x double], [100 x double] }
20
21 define void @foo(%struct.Q* %s, i64 %n) nounwind {
22 entry:
23 br label %bb1
24
25 bb1:
26 %i = phi i64 [ 2, %entry ], [ %i.next, %bb ]
27 %j = phi i64 [ 0, %entry ], [ %j.next, %bb ]
28 %t5 = icmp slt i64 %i, %n
29 br i1 %t5, label %bb, label %return
30
31 bb:
32 %t0 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 0, i64 %i
33 %t1 = load double* %t0, align 8
34 %t2 = fmul double %t1, 3.200000e+00
35 %t3 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 0, i64 %i
36 store double %t2, double* %t3, align 8
37
38 %s0 = getelementptr inbounds %struct.Q* %s, i64 13, i32 0, i64 7, i32 0, i32 1, i64 %i
39 %s1 = load double* %s0, align 8
40 %s2 = fmul double %s1, 3.200000e+00
41 %s3 = getelementptr inbounds %struct.Q* %s, i64 13, i32 0, i64 7, i32 0, i32 1, i64 %i
42 store double %s2, double* %s3, align 8
43
44 %u0 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 7, i32 0, i32 1, i64 %j
45 %u1 = load double* %u0, align 8
46 %u2 = fmul double %u1, 3.200000e+00
47 %u3 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 7, i32 0, i32 1, i64 %j
48 store double %u2, double* %u3, align 8
49
50 %v0 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 1, i64 %i
51 %v1 = load double* %v0, align 8
52 %v2 = fmul double %v1, 3.200000e+00
53 %v3 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 1, i64 %i
54 store double %v2, double* %v3, align 8
55
56 %w0 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 0, i64 %j
57 %w1 = load double* %w0, align 8
58 %w2 = fmul double %w1, 3.200000e+00
59 %w3 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 0, i32 0, i32 0, i64 %j
60 store double %w2, double* %w3, align 8
61
62 %x0 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 3, i32 0, i32 0, i64 %i
63 %x1 = load double* %x0, align 8
64 %x2 = fmul double %x1, 3.200000e+00
65 %x3 = getelementptr inbounds %struct.Q* %s, i64 0, i32 0, i64 3, i32 0, i32 0, i64 %i
66 store double %x2, double* %x3, align 8
67
68 %i.next = add i64 %i, 1
69 %j.next = add i64 %j, 1
70 br label %bb1
71
72 return:
73 ret void
74 }