Make better use of ConstantExpr::getGetElementPtr's InBounds parameter.

llvm-svn: 135676
This commit is contained in:
Jay Foad 2011-07-21 15:15:37 +00:00
parent 3aa2f0a064
commit 2f5fc8c67d
5 changed files with 25 additions and 31 deletions

View File

@ -2278,9 +2278,8 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
(Value**)(Elts.data() + 1), (Value**)(Elts.data() + 1),
Elts.size() - 1)) Elts.size() - 1))
return Error(ID.Loc, "invalid indices for getelementptr"); return Error(ID.Loc, "invalid indices for getelementptr");
ID.ConstantVal = InBounds ? ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
ConstantExpr::getInBoundsGetElementPtr(Elts[0], Indices) : InBounds);
ConstantExpr::getGetElementPtr(Elts[0], Indices);
} else if (Opc == Instruction::Select) { } else if (Opc == Instruction::Select) {
if (Elts.size() != 3) if (Elts.size() != 3)
return Error(ID.Loc, "expected three operands to select"); return Error(ID.Loc, "expected three operands to select");

View File

@ -1352,10 +1352,9 @@ bool BitcodeReader::ParseConstants() {
Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
} }
ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
V = ConstantExpr::getInBoundsGetElementPtr(Elts[0], Indices); BitCode ==
else bitc::CST_CODE_CE_INBOUNDS_GEP);
V = ConstantExpr::getGetElementPtr(Elts[0], Indices);
break; break;
} }
case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]

View File

@ -2407,9 +2407,9 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
i != e; ++i) i != e; ++i)
GEPOps.push_back(getVal(Values, *i)); GEPOps.push_back(getVal(Values, *i));
InstResult = cast<GEPOperator>(GEP)->isInBounds() ? InstResult =
ConstantExpr::getInBoundsGetElementPtr(P, GEPOps) : ConstantExpr::getGetElementPtr(P, GEPOps,
ConstantExpr::getGetElementPtr(P, GEPOps); cast<GEPOperator>(GEP)->isInBounds());
} else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) { } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
if (LI->isVolatile()) return false; // no volatile accesses. if (LI->isVolatile()) return false; // no volatile accesses.
InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)), InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)),

View File

@ -2231,10 +2231,10 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
NewIndices.push_back(Combined); NewIndices.push_back(Combined);
NewIndices.append(Idxs.begin() + 1, Idxs.end()); NewIndices.append(Idxs.begin() + 1, Idxs.end());
return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ? return
ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0), ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices,
NewIndices) : inBounds &&
ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); cast<GEPOperator>(CE)->isInBounds());
} }
} }
@ -2250,11 +2250,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
if (ArrayType *CAT = if (ArrayType *CAT =
dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
if (CAT->getElementType() == SAT->getElementType()) if (CAT->getElementType() == SAT->getElementType())
return inBounds ? return
ConstantExpr::getInBoundsGetElementPtr( ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0),
(Constant*)CE->getOperand(0), Idxs) : Idxs, inBounds);
ConstantExpr::getGetElementPtr(
(Constant*)CE->getOperand(0), Idxs);
} }
} }
@ -2309,9 +2307,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
if (!NewIdxs.empty()) { if (!NewIdxs.empty()) {
for (unsigned i = 0, e = Idxs.size(); i != e; ++i) for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]); if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
return inBounds ? return ConstantExpr::getGetElementPtr(C, NewIdxs, inBounds);
ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs) :
ConstantExpr::getGetElementPtr(C, NewIdxs);
} }
// If all indices are known integers and normalized, we can do a simple // If all indices are known integers and normalized, we can do a simple

View File

@ -839,13 +839,13 @@ ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
for (unsigned i = 1, e = getNumOperands(); i != e; ++i) for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Ops[i-1] = getOperand(i); Ops[i-1] = getOperand(i);
if (OpNo == 0) if (OpNo == 0)
return cast<GEPOperator>(this)->isInBounds() ? return
ConstantExpr::getInBoundsGetElementPtr(Op, Ops) : ConstantExpr::getGetElementPtr(Op, Ops,
ConstantExpr::getGetElementPtr(Op, Ops); cast<GEPOperator>(this)->isInBounds());
Ops[OpNo-1] = Op; Ops[OpNo-1] = Op;
return cast<GEPOperator>(this)->isInBounds() ? return
ConstantExpr::getInBoundsGetElementPtr(getOperand(0), Ops) : ConstantExpr::getGetElementPtr(getOperand(0), Ops,
ConstantExpr::getGetElementPtr(getOperand(0), Ops); cast<GEPOperator>(this)->isInBounds());
} }
default: default:
assert(getNumOperands() == 2 && "Must be binary operator?"); assert(getNumOperands() == 2 && "Must be binary operator?");
@ -891,9 +891,9 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const {
case Instruction::ShuffleVector: case Instruction::ShuffleVector:
return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
case Instruction::GetElementPtr: case Instruction::GetElementPtr:
return cast<GEPOperator>(this)->isInBounds() ? return
ConstantExpr::getInBoundsGetElementPtr(Ops[0], Ops.slice(1)) : ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1),
ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1)); cast<GEPOperator>(this)->isInBounds());
case Instruction::ICmp: case Instruction::ICmp:
case Instruction::FCmp: case Instruction::FCmp:
return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);