Skip to content
IndVarSimplify.cpp 44.6 KiB
Newer Older
  // Find new predicate for integer comparison.
  CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
  switch (EC->getPredicate()) {
  case CmpInst::FCMP_OEQ:
  case CmpInst::FCMP_UEQ:
    NewPred = CmpInst::ICMP_EQ;
    break;
  case CmpInst::FCMP_OGT:
  case CmpInst::FCMP_UGT:
    NewPred = CmpInst::ICMP_UGT;
    break;
  case CmpInst::FCMP_OGE:
  case CmpInst::FCMP_UGE:
    NewPred = CmpInst::ICMP_UGE;
    break;
  case CmpInst::FCMP_OLT:
  case CmpInst::FCMP_ULT:
    NewPred = CmpInst::ICMP_ULT;
    break;
  case CmpInst::FCMP_OLE:
  case CmpInst::FCMP_ULE:
    NewPred = CmpInst::ICMP_ULE;
    break;
  default:
    break;
  if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
  // Insert new integer induction variable.
  PHINode *NewPHI = PHINode::Create(Type::Int32Ty,
                                    PH->getName()+".int", PH);
  NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue),
                      PH->getIncomingBlock(IncomingEdge));

Dan Gohman's avatar
Dan Gohman committed
  Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
                                            ConstantInt::get(Type::Int32Ty,
                                            Incr->getName()+".int", Incr);
  NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));

  ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV);
  Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(BackEdge) : NewEV);
  Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(BackEdge));
Dan Gohman's avatar
Dan Gohman committed
  ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(),
                                 EC->getParent()->getTerminator());
  // Delete old, floating point, exit comparision instruction.
  EC->replaceAllUsesWith(NewEC);
  DeadInsts.insert(EC);
  // Delete old, floating point, increment instruction.
  Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
  DeadInsts.insert(Incr);
Devang Patel's avatar
 
Devang Patel committed
  // Replace floating induction variable. Give SIToFPInst preference over
  // UIToFPInst because it is faster on platforms that are widely used.
  if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) {
Dan Gohman's avatar
Dan Gohman committed
    SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv",
                                      PH->getParent()->getFirstNonPHI());
    PH->replaceAllUsesWith(Conv);
  } else {
Dan Gohman's avatar
Dan Gohman committed
    UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv",
                                      PH->getParent()->getFirstNonPHI());
    PH->replaceAllUsesWith(Conv);
  }
  DeadInsts.insert(PH);