<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">This surprises me too. Here is the real code from my module pass:<div><br></div><div><div> 89 bool SymbolicDataflow::runOnModule(llvm::Module &M) {</div><div> 90     // Init per module goes here</div><div> 91     AA = &getAnalysis<llvm::AliasAnalysis>();</div><div> 92     LIs.clear();</div><div> 93     DTs.clear();</div><div> 94     for (llvm::Module::iterator it = M.begin(); it != M.end(); ++it) {</div><div> 95         llvm::Function *F = &*it;</div><div> 96         if (!F->isDeclaration()) {</div><div> 97             llvm::LoopInfo *LI = &getAnalysis<llvm::LoopInfo>(*F);</div><div> 98             llvm::DominatorTree *DT = &getAnalysis<llvm::DominatorTree>(*F);</div><div> 99             LIs[F] = LI;</div><div>100             DTs[F] = DT;</div><div>101             DEBUG(llvm::errs() << "PASS INIT " << LI << " " << DT << " " << F->getName() << "\n");</div><div>102         }</div><div>103     }</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>……</div><div><br></div><div>It prints out the poiner value of each instance, and it is same for all Function… At least on my machine...</div><div><br></div><div>Best,</div><div>Fan</div><div><br></div><div><div>On Mar 9, 2012, at 5:34 PM, John Criswell wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
  
    <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
  
  <div bgcolor="#FFFFFF" text="#000000">
    On 3/9/12 4:28 PM, Fan Long wrote:
    <blockquote cite="mid:7DB7911A-4FBD-4601-A117-D6FF5441636A@csail.mit.edu" type="cite">Thank you for your quick reply.
      <div><br>
      </div>
      <div>Actually I am using a std::map to map Function* to LoopInfo*,
        but that does not help in this case. Each time I call
        getAnalysis<llvm::LoopInfo>(*F), it returns the same
        instance of llvm::LoopInfo, so the std::map is just mapping
        every function into the same instance. It seems only the
        analysis result for the last function is valid, because all the
        result for all previous functions are erased.</div>
    </blockquote>
    <br>
    Just to make sure I understand: you are saying that every time you
    call getAnalysis<LoopInfo>(), you get the *same* LoopInfo *
    regardless of whether you call it on the same function or on a
    different function.  Is that correct?<br>
    <br>
    Getting the same LoopInfo * when you call getAnalysis<> on the
    same function twice would not surprise me.  Getting the same
    LoopInfo * when you call getAnalysis on F1 and F2 where F1 and F2
    are different functions would surprise me greatly.<br>
    <br>
    <blockquote cite="mid:7DB7911A-4FBD-4601-A117-D6FF5441636A@csail.mit.edu" type="cite">
      <div><br>
      </div>
      <div>The only workaround solution I have now is to copy all
        analysis result out of the data structure of LoopInfo before I
        call next &getAnalysis(). Because llvm::LoopInfo does not
        provide copy method, this will be very dirty to do so.</div>
    </blockquote>
    <br>
    Yes, that may be what you have to do.<br>
    <br>
    -- John T.<br>
    <br>
    <blockquote cite="mid:7DB7911A-4FBD-4601-A117-D6FF5441636A@csail.mit.edu" type="cite">
      <div><br>
      </div>
      <div>Best,</div>
      <div>Fan</div>
      <div><br>
        <div>
          <div>On Mar 9, 2012, at 5:20 PM, John Criswell wrote:</div>
          <br class="Apple-interchange-newline">
          <blockquote type="cite">
            <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
            <div bgcolor="#FFFFFF" text="#000000"> On 3/9/12 4:10 PM,
              Fan Long wrote:
              <blockquote cite="mid:3EDC7AC1-478A-41CD-A970-7D75B5BD0E67@csail.mit.edu" type="cite">
                <pre wrap="">Hello,
        I am trying to write a new ModulePass using LoopInfo analysis result, but it seems I misunderstand some concept about PassManager. Basically I want to keep LoopInfo analysis result alive. Here is an example showing the problem I encountered, assuming I already addRequired<llvm::LoopInfo>() in getAnalysisUsage:
        
        void foo(llvm::Function *F1, llvm::Function *F2) {
                llvm::LoopInfo *LI1, LI2;
                LI1 = &getAnalysis<llvm::LoopInfo>(*F1);
                llvm::Loop* L1 = LI1->getLoopFor(F1->begin());
                LI2 = &getAnalysis<llvm::LoopInfo>(*F2);
                llvm::Loop* L2 = LI2->getLoopFor(F2->begin());
                L1->dump();  // crash
                L2->dump();
        }

        I checked why this program crashes. It is because the getAnalysis returns same LoopInfo instance. Each time it clears previous results and run it on the new function. Thus it invalidate the pointer L1 after calling &getAnalysis<llvm::LoopInfo>(*F2).</pre>
              </blockquote>
              <br>
              To the best of my knowledge, the LLVM pass manager never
              preserves a FunctionPass analysis that is requested by a
              ModulePass; every time you call getAnalysis for a
              function, the FunctionPass is re-run.<br>
              <blockquote cite="mid:3EDC7AC1-478A-41CD-A970-7D75B5BD0E67@csail.mit.edu" type="cite">
                <pre wrap="">   
        My questions is whether there is a way to get around this, and to keep the analysis result of Function Pass of all functions alive during my Module Pass? I am using LLVM-3.1-svn version. I would really appreciate your help!</pre>
              </blockquote>
              <br>
              The trick I've used is to structure the code so that
              getAnalysis<>() is only called once per function. 
              For example, your ModulePass can have a std::map that maps
              between Function * and LoopInfo *.  You then provide a
              method getLoopInfo(Function * F) that checks to see if F
              is in the map.  If it is, it returns what is in the map. 
              If it isn't, it calls getAnalysis on F, stores the result
              in the map, and returns the LoopInfo pointer.<br>
              <br>
              This is important not only for functionality (in your
              case) but also for performance; you don't want to
              calculate an analysis twice for the same function.<br>
              <br>
              -- John T.<br>
              <br>
              <blockquote cite="mid:3EDC7AC1-478A-41CD-A970-7D75B5BD0E67@csail.mit.edu" type="cite">
                <pre wrap="">Best,
Fan</pre>
                <br>
                <fieldset class="mimeAttachmentHeader"></fieldset>
                <br>
                <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu/">http://llvm.cs.uiuc.edu</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
              </blockquote>
              <br>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </div>

</blockquote></div><br></div></body></html>