return 0;
}
+Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
+ const Constant *Elt,
+ const Constant *Idx) {
+ const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx);
+ if (!CIdx) return 0;
+ unsigned idxVal = CIdx->getValue();
+ if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) {
+ // Insertion of scalar constant into packed undef
+ // Optimize away insertion of undef
+ if (isa<UndefValue>(Elt))
+ return const_cast<Constant*>(Val);
+ // Otherwise break the aggregate undef into multiple undefs and do
+ // the insertion
+ unsigned numOps =
+ cast<PackedType>(Val->getType())->getNumElements();
+ std::vector<Constant*> Ops;
+ Ops.reserve(numOps);
+ for (unsigned i = 0; i < numOps; ++i) {
+ const Constant *Op =
+ (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
+ Ops.push_back(const_cast<Constant*>(Op));
+ }
+ return ConstantPacked::get(Ops);
+ }
+ if (const ConstantAggregateZero *CVal =
+ dyn_cast<ConstantAggregateZero>(Val)) {
+ // Insertion of scalar constant into packed aggregate zero
+ // Optimize away insertion of zero
+ if (Elt->isNullValue())
+ return const_cast<Constant*>(Val);
+ // Otherwise break the aggregate zero into multiple zeros and do
+ // the insertion
+ unsigned numOps =
+ cast<PackedType>(Val->getType())->getNumElements();
+ std::vector<Constant*> Ops;
+ Ops.reserve(numOps);
+ for (unsigned i = 0; i < numOps; ++i) {
+ const Constant *Op =
+ (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
+ Ops.push_back(const_cast<Constant*>(Op));
+ }
+ return ConstantPacked::get(Ops);
+ }
+ if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
+ // Insertion of scalar constant into packed constant
+ std::vector<Constant*> Ops;
+ Ops.reserve(CVal->getNumOperands());
+ for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
+ const Constant *Op =
+ (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
+ Ops.push_back(const_cast<Constant*>(Op));
+ }
+ return ConstantPacked::get(Ops);
+ }
+ return 0;
+}
+
/// isZeroSizedType - This type is zero sized if its an array or structure of
/// zero sized types. The only leaf zero sized type is an empty structure.
static bool isMaybeZeroSizedType(const Type *Ty) {
const Constant *V2);
Constant *ConstantFoldExtractElementInstruction(const Constant *Val,
const Constant *Idx);
+ Constant *ConstantFoldInsertElementInstruction(const Constant *Val,
+ const Constant *Elt,
+ const Constant *Idx);
Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
const Constant *V2);
Constant *ConstantFoldGetElementPtr(const Constant *C,
const Constant *V2);
Constant *ConstantFoldExtractElementInstruction(const Constant *Val,
const Constant *Idx);
+ Constant *ConstantFoldInsertElementInstruction(const Constant *Val,
+ const Constant *Elt,
+ const Constant *Idx);
Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
const Constant *V2);
Constant *ConstantFoldGetElementPtr(const Constant *C,
}
};
-/// ExtractElementConstantExpr - This class is private to Constants.cpp, and is used
-/// behind the scenes to implement extractelement constant exprs.
+/// ExtractElementConstantExpr - This class is private to
+/// Constants.cpp, and is used behind the scenes to implement
+/// extractelement constant exprs.
class ExtractElementConstantExpr : public ConstantExpr {
Use Ops[2];
public:
}
};
+/// InsertElementConstantExpr - This class is private to
+/// Constants.cpp, and is used behind the scenes to implement
+/// insertelement constant exprs.
+class InsertElementConstantExpr : public ConstantExpr {
+ Use Ops[3];
+public:
+ InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
+ : ConstantExpr(C1->getType(), Instruction::InsertElement,
+ Ops, 3) {
+ Ops[0].init(C1, this);
+ Ops[1].init(C2, this);
+ Ops[2].init(C3, this);
+ }
+};
+
/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
/// used behind the scenes to implement getelementpr constant exprs.
struct GetElementPtrConstantExpr : public ConstantExpr {
return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
if (V.first == Instruction::ExtractElement)
return new ExtractElementConstantExpr(V.second[0], V.second[1]);
+ if (V.first == Instruction::InsertElement)
+ return new InsertElementConstantExpr(V.second[0], V.second[1],
+ V.second[2]);
assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
assert(isa<PackedType>(Val->getType()) &&
"Tried to create extractelement operation on non-packed type!");
assert(Idx->getType() == Type::UIntTy &&
- "Index must be uint type!");
+ "Extractelement index must be uint type!");
return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
Val, Idx);
}
+Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
+ Constant *Elt, Constant *Idx) {
+ if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
+ return FC; // Fold a few common cases...
+ // Look up the constant in the table first to ensure uniqueness
+ std::vector<Constant*> ArgVec(1, Val);
+ ArgVec.push_back(Elt);
+ ArgVec.push_back(Idx);
+ const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
+ return ExprConstants.getOrCreate(ReqTy, Key);
+}
+
+Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
+ Constant *Idx) {
+ assert(isa<PackedType>(Val->getType()) &&
+ "Tried to create insertelement operation on non-packed type!");
+ assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
+ && "Insertelement types must match!");
+ assert(Idx->getType() == Type::UIntTy &&
+ "Insertelement index must be uint type!");
+ return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
+ Val, Elt, Idx);
+}
+
// destroyConstant - Remove the constant from the constant table...
//
void ConstantExpr::destroyConstant() {
case Shr: return "shr";
case VAArg: return "va_arg";
case ExtractElement: return "extractelement";
+ case InsertElement: return "insertelement";
default: return "<Invalid operator> ";
}
//===----------------------------------------------------------------------===//
ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
- const std::string &Name, Instruction *InsertBef)
+ const std::string &Name, Instruction *InsertBef)
: Instruction(cast<PackedType>(Val->getType())->getElementType(),
ExtractElement, Ops, 2, Name, InsertBef) {
Ops[0].init(Val, this);
}
ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
- const std::string &Name, BasicBlock *InsertAE)
+ const std::string &Name, BasicBlock *InsertAE)
: Instruction(cast<PackedType>(Val->getType())->getElementType(),
ExtractElement, Ops, 2, Name, InsertAE) {
Ops[0].init(Val, this);
Ops[1].init(Index, this);
}
+//===----------------------------------------------------------------------===//
+// InsertElementInst Implementation
+//===----------------------------------------------------------------------===//
+
+InsertElementInst::InsertElementInst(Value *Val, Value *Elt, Value *Index,
+ const std::string &Name, Instruction *InsertBef)
+ : Instruction(Val->getType(), InsertElement, Ops, 3, Name, InsertBef) {
+ Ops[0].init(Val, this);
+ Ops[1].init(Elt, this);
+ Ops[2].init(Index, this);
+}
+
+InsertElementInst::InsertElementInst(Value *Val, Value *Elt, Value *Index,
+ const std::string &Name, BasicBlock *InsertAE)
+ : Instruction(Val->getType(), InsertElement, Ops, 3, Name, InsertAE) {
+ Ops[0].init(Val, this);
+ Ops[1].init(Elt, this);
+ Ops[2].init(Index, this);
+}
+
//===----------------------------------------------------------------------===//
// BinaryOperator Class
//===----------------------------------------------------------------------===//
SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
ExtractElementInst *ExtractElementInst::clone() const {return new ExtractElementInst(*this); }
+InsertElementInst *InsertElementInst::clone() const {return new InsertElementInst(*this); }
PHINode *PHINode::clone() const { return new PHINode(*this); }
ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
if (RealPass)
- AU.addRequired<ETForest>();
+ AU.addRequired<ETForest>();
}
/// abortIfBroken - If the module is broken and we are supposed to abort on
void visitBinaryOperator(BinaryOperator &B);
void visitShiftInst(ShiftInst &SI);
void visitExtractElementInst(ExtractElementInst &EI);
+ void visitInsertElementInst(InsertElementInst &EI);
void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
void visitCallInst(CallInst &CI);
void visitGetElementPtrInst(GetElementPtrInst &GEP);
Assert1(EI.getOperand(1)->getType() == Type::UIntTy,
"Second operand to extractelement must be uint type!", &EI);
Assert1(EI.getType() ==
- cast<PackedType>(EI.getOperand(0)->getType())->getElementType(),
- "Extractelement return type must be same as "
- "first operand element type!", &EI);
+ cast<PackedType>(EI.getOperand(0)->getType())->getElementType(),
+ "Extractelement return type must match "
+ "first operand element type!", &EI);
visitInstruction(EI);
}
+void Verifier::visitInsertElementInst(InsertElementInst &IE) {
+ Assert1(isa<PackedType>(IE.getOperand(0)->getType()),
+ "First operand to insertelement must be packed type!", &IE);
+ Assert1(IE.getOperand(1)->getType() ==
+ cast<PackedType>(IE.getOperand(0)->getType())->getElementType(),
+ "Second operand to insertelement must match "
+ "first operand element type!", &IE);
+ Assert1(IE.getOperand(2)->getType() == Type::UIntTy,
+ "Third operand to insertelement must be uint type!", &IE);
+ visitInstruction(IE);
+}
+
void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
const Type *ElTy =
GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),