llvm.org GIT mirror llvm / e56a94e
Remove the API for creating ConstantExprs with the nsw, nuw, inbounds, and exact flags. Because ConstantExprs are uniqued, creating an expression with this flag causes all expressions with the same operands to have the same flag, which may not be safe. Add, sub, mul, and sdiv ConstantExprs are usually folded anyway, so the main interesting flag here is inbounds, and the constant folder already knows how to set the inbounds flag automatically in most cases, so there isn't an urgent need for the API support. This can be reconsidered in the future, but for now just removing these API bits eliminates a source of potential trouble with little downside. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80959 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
9 changed file(s) with 22 addition(s) and 92 deletion(s). Raw diff Collapse all Expand all
647647 static Constant *getIntToPtr(Constant *C, const Type *Ty);
648648 static Constant *getBitCast (Constant *C, const Type *Ty);
649649
650 static Constant* getNSWAdd(Constant* C1, Constant* C2);
651 static Constant* getExactSDiv(Constant* C1, Constant* C2);
652
653650 /// Transparently provide more efficient getOperand methods.
654651 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
655652
735732 Constant* const *IdxList, unsigned NumIdx);
736733 static Constant *getGetElementPtr(Constant *C,
737734 Value* const *IdxList, unsigned NumIdx);
738
739 /// Create an "inbounds" getelementptr. See the documentation for the
740 /// "inbounds" flag in LangRef.html for details.
741 static Constant *getInBoundsGetElementPtr(Constant *C,
742 Constant* const *IdxList,
743 unsigned NumIdx);
744 static Constant *getInBoundsGetElementPtr(Constant *C,
745 Value* const *IdxList,
746 unsigned NumIdx);
747735
748736 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
749737 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
3535 return ConstantExpr::getAdd(LHS, RHS);
3636 }
3737 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
38 return ConstantExpr::getNSWAdd(LHS, RHS);
38 return ConstantExpr::getAdd(LHS, RHS);
3939 }
4040 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
4141 return ConstantExpr::getFAdd(LHS, RHS);
5959 return ConstantExpr::getSDiv(LHS, RHS);
6060 }
6161 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
62 return ConstantExpr::getExactSDiv(LHS, RHS);
62 return ConstantExpr::getSDiv(LHS, RHS);
6363 }
6464 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
6565 return ConstantExpr::getFDiv(LHS, RHS);
126126
127127 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
128128 unsigned NumIdx) const {
129 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
129 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
130130 }
131131 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
132132 unsigned NumIdx) const {
133 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
133 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
134134 }
135135
136136 //===--------------------------------------------------------------------===//
130130
131131 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
132132 unsigned NumIdx) const {
133 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
133 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
134134 }
135135 Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
136136 unsigned NumIdx) const {
5151 return Fold(ConstantExpr::getAdd(LHS, RHS));
5252 }
5353 Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
54 return Fold(ConstantExpr::getNSWAdd(LHS, RHS));
54 return Fold(ConstantExpr::getAdd(LHS, RHS));
5555 }
5656 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
5757 return Fold(ConstantExpr::getFAdd(LHS, RHS));
7575 return Fold(ConstantExpr::getSDiv(LHS, RHS));
7676 }
7777 Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
78 return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
78 return Fold(ConstantExpr::getSDiv(LHS, RHS));
7979 }
8080 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
8181 return Fold(ConstantExpr::getFDiv(LHS, RHS));
142142
143143 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
144144 unsigned NumIdx) const {
145 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
145 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
146146 }
147147 Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
148148 unsigned NumIdx) const {
149 return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
149 return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
150150 }
151151
152152 //===--------------------------------------------------------------------===//
469469 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal);
470470 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal);
471471 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
472 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
473472 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
474473 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
475474 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
477476 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
478477 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
479478 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
480 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
481479 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
482480 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
483481 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
494492 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
495493 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
496494 LLVMValueRef *ConstantIndices, unsigned NumIndices);
497 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
498 LLVMValueRef *ConstantIndices,
499 unsigned NumIndices);
500495 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
501496 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
502497 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
202202 if (Offset != 0)
203203 return 0;
204204
205 // Create the GEP constant expr.
206 Constant *C = ConstantExpr::getGetElementPtr(Ptr,
207 &NewIdxs[0], NewIdxs.size());
208 assert(cast(C->getType())->getElementType() == Ty &&
209 "Computed GetElementPtr has unexpected type!");
210
205211 // If the base is the start of a GlobalVariable and all the array indices
206212 // remain in their static bounds, the GEP is inbounds. We can check that
207213 // all indices are in bounds by just checking the first index only
208 // because we've just normalized all the indices.
209 Constant *C = isa(Ptr) && NewIdxs[0]->isNullValue() ?
210 ConstantExpr::getInBoundsGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()) :
211 ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
212 assert(cast(C->getType())->getElementType() == Ty &&
213 "Computed GetElementPtr has unexpected type!");
214 // because we've just normalized all the indices. We can mutate the
215 // Constant in place because we've proven that the indices are in bounds,
216 // so they'll always be in bounds.
217 if (isa(Ptr) && NewIdxs[0]->isNullValue())
218 if (GEPOperator *GEP = dyn_cast(C))
219 GEP->setIsInBounds(true);
214220
215221 // If we ended up indexing a member with a type that doesn't match
216222 // the type of what the original indices indexed, add a cast.
121121 }
122122
123123 if (ElTy == DPTy->getElementType())
124 // This GEP is inbounds because all indices are zero.
125 return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0],
126 IdxList.size());
124 return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
127125 }
128126
129127 // Handle casts from one vector constant to another. We know that the src
628628 Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
629629 // FIXME: make this the primary ctor method.
630630 return get(std::vector(Vals, Vals+NumVals));
631 }
632
633 Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) {
634 Constant *C = getAdd(C1, C2);
635 // Set nsw attribute, assuming constant folding didn't eliminate the
636 // Add.
637 if (AddOperator *Add = dyn_cast(C))
638 Add->setHasNoSignedWrap(true);
639 return C;
640 }
641
642 Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
643 Constant *C = getSDiv(C1, C2);
644 // Set exact attribute, assuming constant folding didn't eliminate the
645 // SDiv.
646 if (SDivOperator *SDiv = dyn_cast(C))
647 SDiv->setIsExact(true);
648 return C;
649631 }
650632
651633 // Utility function for determining if a ConstantExpr is a CastOp or not. This
14901472 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
14911473 }
14921474
1493 Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1494 Value* const *Idxs,
1495 unsigned NumIdx) {
1496 Constant *Result = getGetElementPtr(C, Idxs, NumIdx);
1497 // Set in bounds attribute, assuming constant folding didn't eliminate the
1498 // GEP.
1499 if (GEPOperator *GEP = dyn_cast(Result))
1500 GEP->setIsInBounds(true);
1501 return Result;
1502 }
1503
15041475 Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
15051476 unsigned NumIdx) {
15061477 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
1507 }
1508
1509 Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1510 Constant* const *Idxs,
1511 unsigned NumIdx) {
1512 return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx);
15131478 }
15141479
15151480 Constant *
534534 unwrap(RHSConstant)));
535535 }
536536
537 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
538 LLVMValueRef RHSConstant) {
539 return wrap(ConstantExpr::getNSWAdd(
540 unwrap(LHSConstant),
541 unwrap(RHSConstant)));
542 }
543
544537 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
545538 return wrap(ConstantExpr::getFAdd(
546539 unwrap(LHSConstant),
580573 return wrap(ConstantExpr::getSDiv(
581574 unwrap(LHSConstant),
582575 unwrap(RHSConstant)));
583 }
584
585 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
586 LLVMValueRef RHSConstant) {
587 return wrap(ConstantExpr::getExactSDiv(
588 unwrap(LHSConstant),
589 unwrap(RHSConstant)));
590576 }
591577
592578 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
670656 unwrap(ConstantIndices,
671657 NumIndices),
672658 NumIndices));
673 }
674
675 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
676 LLVMValueRef *ConstantIndices,
677 unsigned NumIndices) {
678 Constant* Val = unwrap(ConstantVal);
679 Constant** Idxs = unwrap(ConstantIndices, NumIndices);
680 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
681659 }
682660
683661 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {