liberty/lib/Utilities/InlineFunctionWithVmap.cpp
// Clone OldFunc into NewFunc, transforming the old arguments into references to
// VMap values.
//
Clone OldFunc into NewFunc, transforming the old arguments into references to
VMap values.
Clone OldFunc into NewFunc transforming the old arguments into references to VMap values .
void CloneFunctionIntoWithVMap(Function *NewFunc, const Function *OldFunc,
                             ValueToValueMapTy &VMap,
                             bool ModuleLevelChanges,
                             SmallVectorImpl<ReturnInst*> &Returns,
                             StringRef NameSuffix, ClonedCodeInfo *CodeInfo=0,
                             ValueMapTypeRemapper *TypeMapper=0) {
  assert(!NameSuffix.empty() && "NameSuffix cannot be null!");
#ifndef NDEBUG
  for (Function::const_arg_iterator I = OldFunc->arg_begin(),
       E = OldFunc->arg_end(); I != E; ++I)
    assert(VMap.count(I) && "No mapping from source argument specified!");
#endif
  for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
       BI != BE; ++BI) {
    const BasicBlock &BB = *BI;
     Create a new basic block and copy instructions into it!
    BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
     Add basic block mapping.
    VMap[&BB] = CBB;
     It is only legal to clone a function if a block address within that
     function is never referenced outside of the function.  Given that, we
     want to map block addresses from the old function to block addresses in
     the clone. (This is different from the generic ValueMapper
     implementation, which generates an invalid blockaddress when
     cloning a function.)
    if (BB.hasAddressTaken()) {
      Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
                                              const_cast<BasicBlock*>(&BB));
      VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
    }
     Note return instructions for the caller.
    if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
      Returns.push_back(RI);
  }
   Loop over all of the instructions in the function, fixing up operand
   references as we go.  This uses VMap to do all the hard work.
  Function::iterator BB (cast<BasicBlock>(VMap[&*(OldFunc->begin())]));
  Function::iterator BE;
  for (BE = NewFunc->end(); BB != BE; ++BB)
     Loop over all instructions, fixing each one as we find it...
    for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
      RemapInstruction(&*II, VMap,
                       ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
                       TypeMapper);
}
void CloneFunctionIntoWithVMap ( Function * NewFunc , const Function * OldFunc , ValueToValueMapTy & VMap , bool ModuleLevelChanges , SmallVectorImpl < ReturnInst * > & Returns , StringRef NameSuffix , ClonedCodeInfo * CodeInfo = 0 , ValueMapTypeRemapper * TypeMapper = 0 ) { assert ( ! NameSuffix . empty ( ) && " " ) ; #ifndef NDEBUG for ( Function :: const_arg_iterator I = OldFunc -> arg_begin ( ) , E = OldFunc -> arg_end ( ) ; I != E ; ++ I ) assert ( VMap . count ( I ) && " " ) ; #endif for ( Function :: const_iterator BI = OldFunc -> begin ( ) , BE = OldFunc -> end ( ) ; BI != BE ; ++ BI ) { const BasicBlock & BB = * BI ; BasicBlock * CBB = CloneBasicBlock ( & BB , VMap , NameSuffix , NewFunc , CodeInfo ) ; VMap [ & BB ] = CBB ; if ( BB . hasAddressTaken ( ) ) { Constant * OldBBAddr = BlockAddress :: get ( const_cast < Function * > ( OldFunc ) , const_cast < BasicBlock * > ( & BB ) ) ; VMap [ OldBBAddr ] = BlockAddress :: get ( NewFunc , CBB ) ; } if ( ReturnInst * RI = dyn_cast < ReturnInst > ( CBB -> getTerminator ( ) ) ) Returns . push_back ( RI ) ; } Function :: iterator BB ( cast < BasicBlock > ( VMap [ & * ( OldFunc -> begin ( ) ) ] ) ) ; Function :: iterator BE ; for ( BE = NewFunc -> end ( ) ; BB != BE ; ++ BB ) for ( BasicBlock :: iterator II = BB -> begin ( ) ; II != BB -> end ( ) ; ++ II ) RemapInstruction ( & * II , VMap , ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges , TypeMapper ) ; }
Clone OldFunc into NewFunc, transforming the old arguments into references to
VMap values.
Clone OldFunc into NewFunc transforming the old arguments into references to VMap values .
/* DON'T clone any attributes
  // Clone any attributes.
  if (NewFunc->arg_size() == OldFunc->arg_size())
    NewFunc->copyAttributesFrom(OldFunc);
  else {
    //Some arguments were deleted with the VMap. Copy arguments one by one
    for (Function::const_arg_iterator I = OldFunc->arg_begin(),
           E = OldFunc->arg_end(); I != E; ++I)
      if (Argument* Anew = dyn_cast<Argument>(VMap[I]))
        Anew->addAttr( OldFunc->getAttributes()
                       .getParamAttributes(I->getArgNo() + 1));
    NewFunc->setAttributes(NewFunc->getAttributes()
                           .addAttr(0, OldFunc->getAttributes()
                                     .getRetAttributes()));
    NewFunc->setAttributes(NewFunc->getAttributes()
                           .addAttr(~0, OldFunc->getAttributes()
                                     .getFnAttributes()));

  }
*/ // Loop over all of the basic blocks in the function, cloning them as // appropriate.  Note that we save BE this way in order to handle cloning of // recursive functions into themselves. // // Create a new basic block and copy instructions into it! // Add basic block mapping. // It is only legal to clone a function if a block address within that // function is never referenced outside of the function.  Given that, we // want to map block addresses from the old function to block addresses in // the clone. (This is different from the generic ValueMapper // implementation, which generates an invalid blockaddress when // cloning a function.) // Note return instructions for the caller. // Loop over all of the instructions in the function, fixing up operand // references as we go.  This uses VMap to do all the hard work. // Loop over all instructions, fixing each one as we find it...