diff options
-rw-r--r-- | include/llvm-c/Core.h | 5 | ||||
-rw-r--r-- | include/llvm/Constants.h | 12 | ||||
-rw-r--r-- | include/llvm/Support/ConstantFolder.h | 8 | ||||
-rw-r--r-- | include/llvm/Support/NoFolder.h | 2 | ||||
-rw-r--r-- | include/llvm/Support/TargetFolder.h | 8 | ||||
-rw-r--r-- | lib/Analysis/ConstantFolding.cpp | 18 | ||||
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 4 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 35 | ||||
-rw-r--r-- | lib/VMCore/Core.cpp | 22 |
9 files changed, 92 insertions, 22 deletions
diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 95f3474..40696e0 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -470,6 +470,7 @@ LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal); LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal); LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal); LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); @@ -477,6 +478,7 @@ LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); @@ -493,6 +495,9 @@ LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, LLVMValueRef *ConstantIndices, unsigned NumIndices); +LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, + unsigned NumIndices); LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType); LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 8566ba0..da6fe96 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -648,6 +648,9 @@ public: static Constant *getIntToPtr(Constant *C, const Type *Ty); static Constant *getBitCast (Constant *C, const Type *Ty); + static Constant* getNSWAdd(Constant* C1, Constant* C2); + static Constant* getExactSDiv(Constant* C1, Constant* C2); + /// Transparently provide more efficient getOperand methods. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); @@ -734,6 +737,15 @@ public: static Constant *getGetElementPtr(Constant *C, Value* const *IdxList, unsigned NumIdx); + /// Create an "inbounds" getelementptr. See the documentation for the + /// "inbounds" flag in LangRef.html for details. + static Constant *getInBoundsGetElementPtr(Constant *C, + Constant* const *IdxList, + unsigned NumIdx); + static Constant *getInBoundsGetElementPtr(Constant *C, + Value* const *IdxList, + unsigned NumIdx); + static Constant *getExtractElement(Constant *Vec, Constant *Idx); static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx); static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask); diff --git a/include/llvm/Support/ConstantFolder.h b/include/llvm/Support/ConstantFolder.h index 8ce5237..3c9278a 100644 --- a/include/llvm/Support/ConstantFolder.h +++ b/include/llvm/Support/ConstantFolder.h @@ -36,7 +36,7 @@ public: return ConstantExpr::getAdd(LHS, RHS); } Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const { - return ConstantExpr::getAdd(LHS, RHS); + return ConstantExpr::getNSWAdd(LHS, RHS); } Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { return ConstantExpr::getFAdd(LHS, RHS); @@ -60,7 +60,7 @@ public: return ConstantExpr::getSDiv(LHS, RHS); } Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const { - return ConstantExpr::getSDiv(LHS, RHS); + return ConstantExpr::getExactSDiv(LHS, RHS); } Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { return ConstantExpr::getFDiv(LHS, RHS); @@ -127,11 +127,11 @@ public: Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, unsigned NumIdx) const { - return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); + return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); } Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, unsigned NumIdx) const { - return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); + return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); } //===--------------------------------------------------------------------===// diff --git a/include/llvm/Support/NoFolder.h b/include/llvm/Support/NoFolder.h index 0072471..4540f02 100644 --- a/include/llvm/Support/NoFolder.h +++ b/include/llvm/Support/NoFolder.h @@ -131,7 +131,7 @@ public: Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, unsigned NumIdx) const { - return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); + return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx); } Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, unsigned NumIdx) const { diff --git a/include/llvm/Support/TargetFolder.h b/include/llvm/Support/TargetFolder.h index 80ab900..77533c0 100644 --- a/include/llvm/Support/TargetFolder.h +++ b/include/llvm/Support/TargetFolder.h @@ -52,7 +52,7 @@ public: return Fold(ConstantExpr::getAdd(LHS, RHS)); } Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const { - return Fold(ConstantExpr::getAdd(LHS, RHS)); + return Fold(ConstantExpr::getNSWAdd(LHS, RHS)); } Constant *CreateFAdd(Constant *LHS, Constant *RHS) const { return Fold(ConstantExpr::getFAdd(LHS, RHS)); @@ -76,7 +76,7 @@ public: return Fold(ConstantExpr::getSDiv(LHS, RHS)); } Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const { - return Fold(ConstantExpr::getSDiv(LHS, RHS)); + return Fold(ConstantExpr::getExactSDiv(LHS, RHS)); } Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { return Fold(ConstantExpr::getFDiv(LHS, RHS)); @@ -143,11 +143,11 @@ public: Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList, unsigned NumIdx) const { - return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); + return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); } Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList, unsigned NumIdx) const { - return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx)); + return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx)); } //===--------------------------------------------------------------------===// diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 9bc0093..109eaad 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -203,21 +203,15 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps, if (Offset != 0) return 0; - // Create the GEP constant expr. - Constant *C = ConstantExpr::getGetElementPtr(Ptr, - &NewIdxs[0], NewIdxs.size()); - assert(cast<PointerType>(C->getType())->getElementType() == Ty && - "Computed GetElementPtr has unexpected type!"); - // If the base is the start of a GlobalVariable and all the array indices // remain in their static bounds, the GEP is inbounds. We can check that // all indices are in bounds by just checking the first index only - // because we've just normalized all the indices. We can mutate the - // Constant in place because we've proven that the indices are in bounds, - // so they'll always be in bounds. - if (isa<GlobalVariable>(Ptr) && NewIdxs[0]->isNullValue()) - if (GEPOperator *GEP = dyn_cast<GEPOperator>(C)) - GEP->setIsInBounds(true); + // because we've just normalized all the indices. + Constant *C = isa<GlobalVariable>(Ptr) && NewIdxs[0]->isNullValue() ? + ConstantExpr::getInBoundsGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()) : + ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()); + assert(cast<PointerType>(C->getType())->getElementType() == Ty && + "Computed GetElementPtr has unexpected type!"); // If we ended up indexing a member with a type that doesn't match // the type of what the original indices indexed, add a cast. diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index a869467..701a195 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -122,7 +122,9 @@ static Constant *FoldBitCast(LLVMContext &Context, } if (ElTy == DPTy->getElementType()) - return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size()); + // This GEP is inbounds because all indices are zero. + return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0], + IdxList.size()); } // Handle casts from one vector constant to another. We know that the src diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 1ddc1e2..37efafc 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -631,6 +631,24 @@ Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) { return get(std::vector<Constant*>(Vals, Vals+NumVals)); } +Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) { + Constant *C = getAdd(C1, C2); + // Set nsw attribute, assuming constant folding didn't eliminate the + // Add. + if (AddOperator *Add = dyn_cast<AddOperator>(C)) + Add->setHasNoSignedWrap(true); + return C; +} + +Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) { + Constant *C = getSDiv(C1, C2); + // Set exact attribute, assuming constant folding didn't eliminate the + // SDiv. + if (SDivOperator *SDiv = dyn_cast<SDivOperator>(C)) + SDiv->setIsExact(true); + return C; +} + // Utility function for determining if a ConstantExpr is a CastOp or not. This // can't be inline because we don't want to #include Instruction.h into // Constant.h @@ -1473,11 +1491,28 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); } +Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, + Value* const *Idxs, + unsigned NumIdx) { + Constant *Result = getGetElementPtr(C, Idxs, NumIdx); + // Set in bounds attribute, assuming constant folding didn't eliminate the + // GEP. + if (GEPOperator *GEP = dyn_cast<GEPOperator>(Result)) + GEP->setIsInBounds(true); + return Result; +} + Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, unsigned NumIdx) { return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); } +Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C, + Constant* const *Idxs, + unsigned NumIdx) { + return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx); +} + Constant * ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { assert(LHS->getType() == RHS->getType()); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 240b27a..1dbf5c4 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -535,6 +535,13 @@ LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { unwrap<Constant>(RHSConstant))); } +LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, + LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getNSWAdd( + unwrap<Constant>(LHSConstant), + unwrap<Constant>(RHSConstant))); +} + LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { return wrap(ConstantExpr::getFAdd( unwrap<Constant>(LHSConstant), @@ -576,6 +583,13 @@ LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { unwrap<Constant>(RHSConstant))); } +LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, + LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getExactSDiv( + unwrap<Constant>(LHSConstant), + unwrap<Constant>(RHSConstant))); +} + LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { return wrap(ConstantExpr::getFDiv( unwrap<Constant>(LHSConstant), @@ -659,6 +673,14 @@ LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, NumIndices)); } +LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, + unsigned NumIndices) { + Constant* Val = unwrap<Constant>(ConstantVal); + Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices); + return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices)); +} + LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { return wrap(ConstantExpr::getTrunc( unwrap<Constant>(ConstantVal), |