#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/StatisticReporter.h"
+#include <algorithm>
static Statistic<> NumCombined("instcombine\t- Number of insts combined");
Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
// Eliminate 'add int %X, 0'
- if (I->getType()->isIntegral() &&
- RHS == Constant::getNullValue(I->getType())) {
+ if (RHS == Constant::getNullValue(I->getType())) {
AddUsesToWorkList(I); // Add all modified instrs to worklist
I->replaceAllUsesWith(LHS);
return I;
// not used by anyone else...
//
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
- if (Op1I->use_size() == 1) {
+ if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
// Swap the two operands of the subexpr...
Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
Op1I->setOperand(0, IIOp1);
if (Result) {
++NumCombined;
// Should we replace the old instruction with a new one?
- if (Result != I)
+ if (Result != I) {
+ // Instructions can end up on the worklist more than once. Make sure
+ // we do not process an instruction that has been deleted.
+ std::vector<Instruction*>::iterator It = std::find(WorkList.begin(),
+ WorkList.end(), I);
+ while (It != WorkList.end()) {
+ It = WorkList.erase(It);
+ It = std::find(It, WorkList.end(), I);
+ }
+
ReplaceInstWithInst(I, Result);
+ }
WorkList.push_back(Result);
AddUsesToWorkList(Result);