コード例 #1
0
void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
{
  const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();

  // Emit the PTX .version and .target attributes
  OutStreamer.EmitRawText(Twine("\t.version ") + ST.getPTXVersionString());
  OutStreamer.EmitRawText(Twine("\t.target ") + ST.getTargetString() +
                                (ST.supportsDouble() ? ""
                                                     : ", map_f64_to_f32"));
  // .address_size directive is optional, but it must immediately follow
  // the .target directive if present within a module
  if (ST.supportsPTX23()) {
    const char *addrSize = ST.is64Bit() ? "64" : "32";
    OutStreamer.EmitRawText(Twine("\t.address_size ") + addrSize);
  }

  OutStreamer.AddBlankLine();

  // Define any .file directives
  DebugInfoFinder DbgFinder;
  DbgFinder.processModule(M);

  for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
       E = DbgFinder.compile_unit_end(); I != E; ++I) {
    DICompileUnit DIUnit(*I);
    StringRef FN = DIUnit.getFilename();
    StringRef Dir = DIUnit.getDirectory();
    GetOrCreateSourceID(FN, Dir);
  }

  OutStreamer.AddBlankLine();

  // declare external functions
  for (Module::const_iterator i = M.begin(), e = M.end();
       i != e; ++i)
    EmitFunctionDeclaration(i);
  
  // declare global variables
  for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
       i != e; ++i)
    EmitVariableDeclaration(i);
}
コード例 #2
0
ファイル: StripSymbols.cpp プロジェクト: QuentinFiard/llvm
/// Remove any debug info for global variables/functions in the given module for
/// which said global variable/function no longer exists (i.e. is null).
///
/// Debugging information is encoded in llvm IR using metadata. This is designed
/// such a way that debug info for symbols preserved even if symbols are
/// optimized away by the optimizer. This special pass removes debug info for
/// such symbols.
bool StripDeadDebugInfo::runOnModule(Module &M) {
  bool Changed = false;

  LLVMContext &C = M.getContext();

  // Find all debug info in F. This is actually overkill in terms of what we
  // want to do, but we want to try and be as resilient as possible in the face
  // of potential debug info changes by using the formal interfaces given to us
  // as much as possible.
  DebugInfoFinder F;
  F.processModule(M);

  // For each compile unit, find the live set of global variables/functions and
  // replace the current list of potentially dead global variables/functions
  // with the live list.
  SmallVector<Value *, 64> LiveGlobalVariables;
  SmallVector<Value *, 64> LiveSubprograms;
  DenseSet<const MDNode *> VisitedSet;

  for (DebugInfoFinder::iterator CI = F.compile_unit_begin(),
         CE = F.compile_unit_end(); CI != CE; ++CI) {
    // Create our compile unit.
    DICompileUnit DIC(*CI);
    assert(DIC.Verify() && "DIC must verify as a DICompileUnit.");

    // Create our live subprogram list.
    DIArray SPs = DIC.getSubprograms();
    bool SubprogramChange = false;
    for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
      DISubprogram DISP(SPs.getElement(i));
      assert(DISP.Verify() && "DISP must verify as a DISubprogram.");

      // Make sure we visit each subprogram only once.
      if (!VisitedSet.insert(DISP).second)
        continue;

      // If the function referenced by DISP is not null, the function is live.
      if (DISP.getFunction())
        LiveSubprograms.push_back(DISP);
      else
        SubprogramChange = true;
    }

    // Create our live global variable list.
    DIArray GVs = DIC.getGlobalVariables();
    bool GlobalVariableChange = false;
    for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
      DIGlobalVariable DIG(GVs.getElement(i));
      assert(DIG.Verify() && "DIG must verify as DIGlobalVariable.");

      // Make sure we only visit each global variable only once.
      if (!VisitedSet.insert(DIG).second)
        continue;

      // If the global variable referenced by DIG is not null, the global
      // variable is live.
      if (DIG.getGlobal())
        LiveGlobalVariables.push_back(DIG);
      else
        GlobalVariableChange = true;
    }

    // If we found dead subprograms or global variables, replace the current
    // subprogram list/global variable list with our new live subprogram/global
    // variable list.
    if (SubprogramChange) {
      // Make sure that 9 is still the index of the subprograms. This is to make
      // sure that an assert is hit if the location of the subprogram array
      // changes. This is just to make sure that this is updated if such an
      // event occurs.
      assert(DIC->getNumOperands() >= 10 &&
             SPs == DIC->getOperand(9) &&
             "DICompileUnits is expected to store Subprograms in operand "
             "9.");
      DIC->replaceOperandWith(9, MDNode::get(C, LiveSubprograms));
      Changed = true;
    }

    if (GlobalVariableChange) {
      // Make sure that 10 is still the index of global variables. This is to
      // make sure that an assert is hit if the location of the subprogram array
      // changes. This is just to make sure that this index is updated if such
      // an event occurs.
      assert(DIC->getNumOperands() >= 11 &&
             GVs == DIC->getOperand(10) &&
             "DICompileUnits is expected to store Global Variables in operand "
             "10.");
      DIC->replaceOperandWith(10, MDNode::get(C, LiveGlobalVariables));
      Changed = true;
    }

    // Reset lists for the next iteration.
    LiveSubprograms.clear();
    LiveGlobalVariables.clear();
  }

  return Changed;
}