<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal>Hi All,<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>I am trying to write code for simple instrumentation. What I
want to do is to insert a call to an external function for result of each
conditional branch instruction. This external function simply print true or
false based on the result of condition. The modified code is then written into
new file. However when I try to link that file with another bitcode file
(containing external function), it results “<i>llvm-link: error loading
file 'newfile.bc'</i>” error. This error is inconsistent. It appears for
some programs and not for some other. If it links with other file, during
execution it produces following error -  <i>Assertion `Addr &&
"Code generation didn't add function to GlobalAddress table!"' failed</i><o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>Below is the code of my pass. It inherits ModulePass and
does not have any other method except runOnModule. Am I missing something
during call insertion or bitcode modification? I am using LLVM-2.6. <b><i>The
same code in LLVM-2.5 works correctly</i></b>. (except Type::getInt32Ty, llvm-2.5
has Type::Int32Ty and no LLVMContext object)<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>Thanks,<o:p></o:p></p>

<p class=MsoNormal>Nehal<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>    bool ConditionProfile::runOnModule(Module
&M)<o:p></o:p></p>

<p class=MsoNormal>    {<o:p></o:p></p>

<p class=MsoNormal>        // Iterates
through all functions of the module<o:p></o:p></p>

<p class=MsoNormal>       
for(Module::iterator mi = M.begin(), me = M.end(); mi!=me; mi++)<o:p></o:p></p>

<p class=MsoNormal>        {<o:p></o:p></p>

<p class=MsoNormal>           
// Iterates through all basic blocks of the function            <o:p></o:p></p>

<p class=MsoNormal>           
for(Function::iterator fi = mi->begin(), fe = mi->end(); fi!=fe; fi++)<o:p></o:p></p>

<p class=MsoNormal>           
{<o:p></o:p></p>

<p class=MsoNormal>                   
BasicBlock::iterator bi = fi->end();<o:p></o:p></p>

<p class=MsoNormal>                   
bi--;                 //
Getting the terminator/last instruction of BasicBlock<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                   
if(isa<BranchInst>(bi))<o:p></o:p></p>

<p class=MsoNormal>                   
{<o:p></o:p></p>

<p class=MsoNormal>                       
BranchInst *brInst = cast<BranchInst>(bi);<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                       
// We are interested in conditional branch only<o:p></o:p></p>

<p class=MsoNormal>                       
if(brInst->isUnconditional()) continue;<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                       
Value *condRes = brInst->getCondition();<o:p></o:p></p>

<p class=MsoNormal>                       
errs()<<"Type:"<<condRes->getType()->getDescription()<<"\n";<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                      
// Looking for a function in Module Symbol table            
<o:p></o:p></p>

<p class=MsoNormal>                       
Constant *PrintFn = M.getOrInsertFunction("_Z12PrintCondResb",
Type::getInt32Ty(context), condRes->getType(), (Type *)0);<o:p></o:p></p>

<p class=MsoNormal>                       
if(!PrintFn) {<o:p></o:p></p>

<p class=MsoNormal>           
                errs()<<"GetOrInsertFailed\n";<o:p></o:p></p>

<p class=MsoNormal>                           
continue;<o:p></o:p></p>

<p class=MsoNormal>                       
}<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                       
std::vector<Value *> args(1);<o:p></o:p></p>

<p class=MsoNormal>                           
args[0] = condRes;<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                       
// Creating a call instruction to above function.<o:p></o:p></p>

<p class=MsoNormal>                        CallInst
*callInst = <o:p></o:p></p>

<p class=MsoNormal>                          
CallInst::Create(PrintFn, args.begin(), args.end(), "", bi);<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>                       
callInst->setCallingConv(CallingConv::Fast);<o:p></o:p></p>

<p class=MsoNormal>                   
}<o:p></o:p></p>

<p class=MsoNormal>           
}<o:p></o:p></p>

<p class=MsoNormal>        }<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>       // Writing modified
module to new file.<o:p></o:p></p>

<p class=MsoNormal>        std::ostream *os
= new std::ofstream("newfile.bc");<o:p></o:p></p>

<p class=MsoNormal>       
WriteBitcodeToFile(&M, *os);<o:p></o:p></p>

<p class=MsoNormal><o:p> </o:p></p>

<p class=MsoNormal>        return true;<o:p></o:p></p>

<p class=MsoNormal>    }<o:p></o:p></p>

</div>

</body>

</html>