<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Thank you, Duncan.<br>
    <br>
     I rewrote the code, please help check why it still does not work:<br>
    <br>
    //declare global variable<br>
    const Type *IntTy = Type::getInt32Ty(M.getContext());<br>
    const Type *ATyC = ArrayType::get(Type::getInt64Ty(M.getContext()),
    1);<br>
    <font color="#000000">GlobalVariable *CounterSize = new
      GlobalVariable(M, ATyC, false, GlobalValue::InternalLinkage,<br>
                                                             
      Constant::getNullValue(ATyC), "MemTraceCounterSize");</font><br>
    <br>
    const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
    3000);<br>
    <font color="#000000">GlobalVariable *Counters = new
      GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,<br>
                      Constant::getNullValue(ATy), "MemTraceCounters");</font><br>
    <br>
    //get the index<br>
    std::vector<Constant*>IndicesC(2);<br>
    IndicesC[0] = Constant::getNullValue(Type::getInt32Ty(Context));<br>
    IndicesC[1] = ConstantInt::get(Type::getInt32Ty(Context),0);<br>
    Constant *ElementPtrC = 
ConstantExpr::getGetElementPtr(CounterSize,&IndicesC[0],IndicesC.size());<br>
                                                     Value
    *OldCounterSize =new LoadInst(ElementPtrC, "OldCounterSize",
    InsertPos);<br>
    Value *OldCounterSize =new LoadInst(ElementPtrC, "", InsertPos);<br>
    <br>
    //create a getelementptr instruction: we want get 
    &Counters[OldCounterSize]<br>
     std::vector<Value*>new_idx;<br>
     <font color="#ff0000">new_idx.push_back(OldCounterSize); // ERROR?</font><br>
    Value *nextaddr = GetElementPtrInst::Create(Counters,
    new_idx.begin(), new_idx.end(), "", InsertPos);<br>
    <br>
    Thanks!<br>
    于 2011/5/18 16:04, Duncan Sands 写道:
    <blockquote cite="mid:4DD37D8F.1030503@free.fr" type="cite">
      <pre wrap="">Hi Guangming Tan,

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">GlobalVariable:
int *counter; //counter the number of load/store operations in run-time
int *counterArray; //record the load/store addresses
</pre>
          </blockquote>
          <pre wrap="">strictly speaking these are not arrays, they are pointers.  Also, you have
written them in some kind of C-style idiom.  What are the declarations in
LLVM IR?
</pre>
        </blockquote>
        <pre wrap="">const Type *IntTy = Type::getInt32Ty(M.getContext());
const Type *ATyC = ArrayType::get(Type::getInt64Ty(M.getContext()), 1);

GlobalVariable *CounterSize = new GlobalVariable(M, ATyC, false,
GlobalValue::InternalLinkage,
Constant::getNullValue(ATyC), "MemTraceCounterSize");
</pre>
      </blockquote>
      <pre wrap="">
so CounterSize is an array of length 1, containing a single 64 bit integer.
Using an array seems rather pointless here.

</pre>
      <blockquote type="cite">
        <pre wrap="">
const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), 3000);
GlobalVariable *Counters = new GlobalVariable(M, ATy, false,
GlobalValue::InternalLinkage,
Constant::getNullValue(ATy), "MemTraceCounters");
</pre>
      </blockquote>
      <pre wrap="">
OK, so Counters is an array of 3000 32 bit integers.

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">Constant *ElementPtr = ConstantExpr::getGetElementPtr(counter,
&index[0], index.size());
</pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">
What is "counter", the same things as CounterSize?

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">//store the memory address to counterArray[oldcounter]
std::vector<Constant*>    indexC(2);
indexC[0] =  Constant::getNullvalue(Type:getInt32Ty(Context));
indexC[1] = dync_cast(llvm::ConstantInt>(oldcounter);
</pre>
          </blockquote>
          <pre wrap="">Since oldcounter is not a constant (its value is not known at compile time...)
this is never going to work.  Declare the vector to be of Value* not Constant*.
Then you don't need the dynamic cast.
</pre>
        </blockquote>
        <pre wrap="">So, do you mean that we have no way to use the "oldcounter" as an index
to access an array?
</pre>
      </blockquote>
      <pre wrap="">
No, this is perfectly possible.  I think you are confusing constants and
instructions.  Instructions are executed at runtime.  Constants are known
at compile time.  You clearly need an instruction here but you are trying
to create a constant.

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">Constant *ElmentPtr = ConstantExpr::getGetElementPtr(counterArray,
&indexC[0], indexC.size());
</pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">
Here you should be using GetElementPtrInst::Create to make a getelementptr
instruction rather than using ConstantExpr::getGetElementPtr which creates a
getelementptr constant (which represents a constant address - the address you
are computing is clearly not constant since it changes every time counter is
incremented).

Ciao, Duncan.

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre wrap="">This line can then not be a ConstantExpr, it has to be an instruction.  Again,
it cannot be a constant since the address computed isn't constant (it is not
known at compile time...).

Ciao, Duncan.

</pre>
          <blockquote type="cite">
            <pre wrap="">......// other codes

Unfortunately, the oldcounter of Value type can not be cast to
ConstantInt, dync_cast returns NULL.
Is there any way to retrieve the integer value from oldcounter?

Thanks!

</pre>
          </blockquote>
          <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a 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>
        <pre wrap="">
_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a 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>
      <pre wrap="">
_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a 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>
  </body>
</html>