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->setHasNoSignedOverflow(true);
+ return C;
+}
+
Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
Constant *C = getSDiv(C1, C2);
- cast<SDivOperator>(C)->setIsExact(true);
+ // Set exact attribute, assuming constant folding didn't eliminate the
+ // SDiv.
+ if (SDivOperator *SDiv = dyn_cast<SDivOperator>(C))
+ SDiv->setIsExact(true);
return C;
}
Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
Value* const *Idxs,
unsigned NumIdx) {
- // Get the result type of the getelementptr!
- const Type *Ty =
- GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
- assert(Ty && "GEP indices invalid!");
- unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
- Constant *Result = getGetElementPtrTy(PointerType::get(Ty, As), C,
- Idxs, NumIdx);
- cast<GEPOperator>(Result)->setIsInBounds(true);
+ 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;
}