RFH: Blockarguments

In LoopInfo.cpp you find the following code:

PHINode *Loop::getCanonicalInductionVariable() const {
  BasicBlock *H = getHeader();

  BasicBlock *Incoming = nullptr, *Backedge = nullptr;
  if (!getIncomingAndBackEdge(Incoming, Backedge))
    return nullptr;

  // Loop over all of the PHI nodes, looking for a canonical indvar.
  for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
    PHINode *PN = cast<PHINode>(I);
    if (ConstantInt *CI =
            dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
      if (CI->isZero())
        if (Instruction *Inc =
                dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
          if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
            if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
              if (CI->isOne())
                return PN;
  }
  return nullptr;
}

MLIR has Blockarguments instead of phis. Note the getIncomingValueForBlock on the phiNode. For Blockarguments; I could find a type but I couldn’t find the incoming blocks?!? Obviously I missed something. A block has predecessors and successors. Blockarguments do not?!?

The arguments of the terminator of the Block predecessors would be equivalent to what you’re looking for if I followed your question.

Indeed the terminator was the solution. The code above uses GraphTraits and templates to make it work for LLVM IR and MIR. The solution for MLIR looks completely different. It was a fun exercise.