teach valuetracking about ConstantDataSequential

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148790 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2012-01-24 07:54:10 +00:00
parent dada586f16
commit df39028607
3 changed files with 75 additions and 33 deletions

View File

@ -325,7 +325,11 @@ public:
/// getElementValue - Return a zero of the right value for the specified GEP /// getElementValue - Return a zero of the right value for the specified GEP
/// index. /// index.
Constant *getElementValue(Constant *C); Constant *getElementValue(Constant *C);
/// getElementValue - Return a zero of the right value for the specified GEP
/// index.
Constant *getElementValue(unsigned Idx);
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// Methods for support type inquiry through isa, cast, and dyn_cast:
/// ///
static bool classof(const ConstantAggregateZero *) { return true; } static bool classof(const ConstantAggregateZero *) { return true; }
@ -1106,7 +1110,11 @@ public:
/// getElementValue - Return an undef of the right value for the specified GEP /// getElementValue - Return an undef of the right value for the specified GEP
/// index. /// index.
UndefValue *getElementValue(Constant *C); UndefValue *getElementValue(Constant *C);
/// getElementValue - Return an undef of the right value for the specified GEP
/// index.
UndefValue *getElementValue(unsigned Idx);
virtual void destroyConstant(); virtual void destroyConstant();
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// Methods for support type inquiry through isa, cast, and dyn_cast:

View File

@ -100,6 +100,19 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
} }
return; return;
} }
if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
// We know that CDS must be a vector of integers. Take the intersection of
// each element.
KnownZero.setAllBits(); KnownOne.setAllBits();
APInt Elt(KnownZero.getBitWidth(), 0);
for (unsigned i = 0, e = CDS->getType()->getNumElements(); i != e; ++i) {
Elt = CDS->getElementAsInteger(i);
KnownZero &= ~Elt;
KnownOne &= Elt;
}
return;
}
// The address of an aligned GlobalValue has trailing zeros. // The address of an aligned GlobalValue has trailing zeros.
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
unsigned Align = GV->getAlignment(); unsigned Align = GV->getAlignment();
@ -1472,50 +1485,51 @@ static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
Instruction *InsertBefore) { Instruction *InsertBefore) {
// Nothing to index? Just return V then (this is useful at the end of our // Nothing to index? Just return V then (this is useful at the end of our
// recursion) // recursion).
if (idx_range.empty()) if (idx_range.empty())
return V; return V;
// We have indices, so V should have an indexable type // We have indices, so V should have an indexable type.
assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
&& "Not looking at a struct or array?"); "Not looking at a struct or array?");
assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
&& "Invalid indices for type?"); "Invalid indices for type?");
CompositeType *PTy = cast<CompositeType>(V->getType()); CompositeType *PTy = cast<CompositeType>(V->getType());
if (isa<UndefValue>(V)) if (isa<UndefValue>(V))
return UndefValue::get(ExtractValueInst::getIndexedType(PTy, return UndefValue::get(ExtractValueInst::getIndexedType(PTy, idx_range));
idx_range)); if (isa<ConstantAggregateZero>(V))
else if (isa<ConstantAggregateZero>(V))
return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy, return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
idx_range)); idx_range));
else if (Constant *C = dyn_cast<Constant>(V)) { if (isa<ConstantArray>(V) || isa<ConstantStruct>(V))
if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) // Recursively process this constant
// Recursively process this constant return FindInsertedValue(cast<Constant>(V)->getOperand(idx_range[0]),
return FindInsertedValue(C->getOperand(idx_range[0]), idx_range.slice(1), idx_range.slice(1), InsertBefore);
InsertBefore); if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
} else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { return CDS->getElementAsConstant(idx_range[0]);
if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
// Loop the indices for the insertvalue instruction in parallel with the // Loop the indices for the insertvalue instruction in parallel with the
// requested indices // requested indices
const unsigned *req_idx = idx_range.begin(); const unsigned *req_idx = idx_range.begin();
for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
i != e; ++i, ++req_idx) { i != e; ++i, ++req_idx) {
if (req_idx == idx_range.end()) { if (req_idx == idx_range.end()) {
if (InsertBefore) // We can't handle this without inserting insertvalues
// The requested index identifies a part of a nested aggregate. Handle if (!InsertBefore)
// this specially. For example,
// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
// %C = extractvalue {i32, { i32, i32 } } %B, 1
// This can be changed into
// %A = insertvalue {i32, i32 } undef, i32 10, 0
// %C = insertvalue {i32, i32 } %A, i32 11, 1
// which allows the unused 0,0 element from the nested struct to be
// removed.
return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
InsertBefore);
else
// We can't handle this without inserting insertvalues
return 0; return 0;
// The requested index identifies a part of a nested aggregate. Handle
// this specially. For example,
// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
// %C = extractvalue {i32, { i32, i32 } } %B, 1
// This can be changed into
// %A = insertvalue {i32, i32 } undef, i32 10, 0
// %C = insertvalue {i32, i32 } %A, i32 11, 1
// which allows the unused 0,0 element from the nested struct to be
// removed.
return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
InsertBefore);
} }
// This insert value inserts something else than what we are looking for. // This insert value inserts something else than what we are looking for.
@ -1531,7 +1545,9 @@ Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
return FindInsertedValue(I->getInsertedValueOperand(), return FindInsertedValue(I->getInsertedValueOperand(),
makeArrayRef(req_idx, idx_range.end()), makeArrayRef(req_idx, idx_range.end()),
InsertBefore); InsertBefore);
} else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { }
if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
// If we're extracting a value from an aggregrate that was extracted from // If we're extracting a value from an aggregrate that was extracted from
// something else, we can extract from that something else directly instead. // something else, we can extract from that something else directly instead.
// However, we will need to chain I's indices with the requested indices. // However, we will need to chain I's indices with the requested indices.

View File

@ -624,6 +624,15 @@ Constant *ConstantAggregateZero::getElementValue(Constant *C) {
return getStructElement(cast<ConstantInt>(C)->getZExtValue()); return getStructElement(cast<ConstantInt>(C)->getZExtValue());
} }
/// getElementValue - Return a zero of the right value for the specified GEP
/// index.
Constant *ConstantAggregateZero::getElementValue(unsigned Idx) {
if (isa<SequentialType>(getType()))
return getSequentialElement();
return getStructElement(Idx);
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// UndefValue Implementation // UndefValue Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -648,6 +657,15 @@ UndefValue *UndefValue::getElementValue(Constant *C) {
return getStructElement(cast<ConstantInt>(C)->getZExtValue()); return getStructElement(cast<ConstantInt>(C)->getZExtValue());
} }
/// getElementValue - Return an undef of the right value for the specified GEP
/// index.
UndefValue *UndefValue::getElementValue(unsigned Idx) {
if (isa<SequentialType>(getType()))
return getSequentialElement();
return getStructElement(Idx);
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// ConstantXXX Classes // ConstantXXX Classes