arrays and multidim arrays
* Rewrite the llvm parser/lexer in http://www.antlr.org when time permits.
They actually do C++. Imagine that.
-* Need to implement constant propogation of cast instructions!
* Fix DCE to elminate br <c>, %L1, %L1 so that it can optimize the main of
fib.ll better. Currently I have to do this to get best results:
as < fib.ll | opt -inline -sccp -dce -sccp -dce |dis
// Implement higher level instruction folding type instructions
//===----------------------------------------------------------------------===//
+inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V,
+ const Type *DestTy) {
+ return ConstRules::get(*V)->castTo(V, DestTy);
+}
+
inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
ConstPoolVal *V) {
switch (Opcode) {
case Instruction::Not: return !*V;
+ // TODO: Handle get element ptr instruction here in the future? GEP null?
}
return 0;
}
return true;
}
+inline static bool
+ConstantFoldCast(Method *M, Method::inst_iterator &DI,
+ CastInst *CI, ConstPoolVal *D) {
+ ConstPoolVal *ReplaceWith =
+ opt::ConstantFoldCastInstruction(D, CI->getType());
+
+ if (!ReplaceWith) return false; // Nothing new to change...
+
+ // Replaces all of the uses of a variable with uses of the constant.
+ CI->replaceAllUsesWith(ReplaceWith);
+
+ // Remove the cast from the list of definitions...
+ CI->getParent()->getInstList().remove(DI.getInstructionIterator());
+
+ // The new constant inherits the old name of the cast...
+ if (CI->hasName())
+ ReplaceWith->setName(CI->getName(), M->getSymbolTableSure());
+
+ // Delete the cast now...
+ delete CI;
+ return true;
+}
+
inline static bool
ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI,
BinaryOperator *Op,
if (D1 && D2)
return ConstantFoldBinaryInst(M, II, cast<BinaryOperator>(Inst), D1, D2);
+ } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
+ ConstPoolVal *D = dyn_cast<ConstPoolVal>(CI->getOperand(0));
+ if (D) return ConstantFoldCast(M, II, CI, D);
+
} else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
if (D) return ConstantFoldUnaryInst(M, II, UInst, D);
if (VState.isOverdefined()) { // Inherit overdefinedness of operand
markOverdefined(I);
} else if (VState.isConstant()) { // Propogate constant value
- ConstPoolVal *Result =
- opt::ConstantFoldUnaryInstruction(I->getOpcode(),
- VState.getConstant());
+ ConstPoolVal *Result = isa<CastInst>(I)
+ ? opt::ConstantFoldCastInstruction(VState.getConstant(), I->getType())
+ : opt::ConstantFoldUnaryInstruction(I->getOpcode(),
+ VState.getConstant());
if (Result) {
// This instruction constant folds!
// Implement higher level instruction folding type instructions
//===----------------------------------------------------------------------===//
+inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V,
+ const Type *DestTy) {
+ return ConstRules::get(*V)->castTo(V, DestTy);
+}
+
inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
ConstPoolVal *V) {
switch (Opcode) {
case Instruction::Not: return !*V;
+ // TODO: Handle get element ptr instruction here in the future? GEP null?
}
return 0;
}
// Implement higher level instruction folding type instructions
//===----------------------------------------------------------------------===//
+inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V,
+ const Type *DestTy) {
+ return ConstRules::get(*V)->castTo(V, DestTy);
+}
+
inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode,
ConstPoolVal *V) {
switch (Opcode) {
case Instruction::Not: return !*V;
+ // TODO: Handle get element ptr instruction here in the future? GEP null?
}
return 0;
}