<!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 bgcolor="#ffffff" text="#000000">
On 1/24/11 12:05 PM, Douglas do Couto Teixeira wrote:
<blockquote
cite="mid:AANLkTikzDF2iJe4PqORmJ21JBxNfgWEzLEHfVfCYjGTD@mail.gmail.com"
type="cite"><br>
<br>
<div class="gmail_quote">On Mon, Jan 24, 2011 at 3:01 PM, Nick
Lewycky <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:nicholas@mxc.ca">nicholas@mxc.ca</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
<div class="im">On 01/24/2011 04:41 AM, Douglas do Couto
Teixeira wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
Hi,<br>
<br>
Nick, thanks for the reply.<br>
I still have a problem: I only need to "clone" an
Instruction, changing<br>
its type. That is, I would like to keep all
characteristics of the old<br>
Instruction and create a new one only with a different
type.<br>
</blockquote>
<br>
</div>
Sure, but what about its operands? An "add" instruction must
have the same type as its operands, what do you want to do
with them? </blockquote>
<div><br>
I also need to convert the type of the operands. But I want to
do this when they are created instead of inserting "trunc"
instructions before performing an operation. But it seems hard
to me.<br>
</div>
</div>
</blockquote>
<br>
Actually, I don't think it will be that difficult. You basically
need to do the following:<br>
<br>
1) Take the backwards, intra-procedural slice of the instruction
(i.e., find the instructions, operands, the operands' operands, the
operands' operands' operands, etc.).<br>
<br>
2) Visit all of the instructions in the slice and convert them. You
want to visit definitions before uses. To do that,<br>
a) Make new phi instructions for all phis in the slice. The
operands of the phis should be the Undef value.<br>
b) Use the dominator tree analysis and traverse basic blocks in
dominator tree order (i.e., start at the top of the dominator tree
and process each node breadth first). Convert all of the
instructions in each basic block (except phis).<br>
c) Revisit all the phis and plug in their new operands.<br>
<br>
3) Delete all the old instructions.<br>
<br>
This is a variation of the SSA construction algorithm in Zadeck et.
al.'s paper (Effeciently Computing Single Static Assignment Form and
the Control Dependence Graph).<br>
<br>
The only tricky part is handling non-instruction operands (e.g.,
function arguments, global variables, etc.). Some might be trivial
to convert. Others may be difficult. You should look over all the
classes derived from llvm::Value and decide how difficult it would
be to convert them.<br>
<br>
-- John T.<br>
<br>
<br>
<blockquote
cite="mid:AANLkTikzDF2iJe4PqORmJ21JBxNfgWEzLEHfVfCYjGTD@mail.gmail.com"
type="cite">
<div class="gmail_quote">
<div>
</div>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">Suppose you're going from a 32-bit add to
a 64-bit add, do the old operands get zero extended? Sign
extended? You'll need to insert instructions for that (unless
they're constants in which case you can use constant
expressions). Similarly, what if the old type is a float and
the new one is an int? float to signed int, float to unsigned
int, or bitcast (only legal sometimes)?</blockquote>
<div><br>
I believe I don't need worry about it because I only create
smaller instructions. So I never convert a 32-bit instruction
in a 64-bit instruction. <br>
</div>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
<div class="im"><br>
<br>
I am trying<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
create a new Instruction thus:<br>
<br>
%3 = add nsw i32 %1, %2 ; <i16> [#uses=2] //Old
Instruction<br>
<br>
Value* Op0 = I->getOperand(0);<br>
Value* Op1 = I->getOperand(1);<br>
Value* V0 = new
Value(Type::getInt16Ty(Op0->getContext()),<br>
Op0->getValueID());<br>
</blockquote>
<br>
</div>
Hunh, Value's constructor is protected.<br>
<br>
In any event, Value is pure base. Constructing one this way
will never get you what you want. If the ValueID indicates an
Instruction, go through Instruction to create one.
<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
Value* V1 = new
Value(Type::getInt16Ty(Op1->getContext()),<br>
Op1->getValueID());<br>
Instruction* newInst = BinaryOperator::CreateNSWAdd(V0,
V1, "test");<br>
errs() << "NewInst:\n" << *newInst <<
"\n";<br>
<br>
<br>
But I get something like this:<br>
<br>
%test = add nsw i16 <badref>, <badref> ;
<i16> [#uses=0]<br>
</blockquote>
<br>
</div>
The two instructions V0 and V1 you created were never inserted
into the BasicBlock so they can't be numbered, and also they
don't have names.
<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
What I am doing wrong?<br>
</blockquote>
<br>
</div>
Suppose that you're going from i32 to i16. Your only choice
with that particular pair of types is a truncate. So:<br>
<br>
IRBuilder builder(OldInst);<br>
Value *V0 = builder.CreateTrunc(Op0, Type::getInt16Ty());<br>
Value *V1 = builder.CreateTrunc(Op1, Type::getInt16Ty());<br>
Value *Add = builder.CreateNSWAdd(V0, V1, "test");<br>
<br>
The IRBuilder will take care of the distinction between
instructions and constants for you. Note that I have not
tested the above code, it may need some fixing before it
compiles.<br>
<font color="#888888">
<br>
Nick<br>
<br>
</font>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
<div class="im">
<br>
<br>
Best,<br>
<br>
Douglas<br>
<br>
On Fri, Jan 21, 2011 at 8:25 PM, Nick Lewycky <<a
moz-do-not-send="true" href="mailto:nlewycky@google.com"
target="_blank">nlewycky@google.com</a><br>
</div>
<div class="im">
<mailto:<a moz-do-not-send="true"
href="mailto:nlewycky@google.com" target="_blank">nlewycky@google.com</a>>>
wrote:<br>
<br>
On 21 January 2011 12:56, Douglas do Couto Teixeira<br>
</div>
<div class="im"> <<a moz-do-not-send="true"
href="mailto:douglasdocouto@gmail.com" target="_blank">douglasdocouto@gmail.com</a>
<mailto:<a moz-do-not-send="true"
href="mailto:douglasdocouto@gmail.com" target="_blank">douglasdocouto@gmail.com</a>>>
wrote:<br>
<br>
Hello guys,<br>
<br>
I wonder how I can change the type of an integer
variable. For<br>
instance, given the instruction "%3 = add i32 %1,
%2" I would<br>
like to alter the instruction to "%3 = add i16 %1,
%2". Is there<br>
any way to do this?<br>
<br>
<br>
No. Instead you create a new Instruction, in this case
with<br>
BinaryOperator::CreateAdd, then
OldInst->replaceAllUsesWith(NewInst)<br>
to update all the users, then
OldInst->eraseFromParent() since it's<br>
now dead code.<br>
<br>
Also, all values have types immutably assigned at
creation, so<br>
you'll need to insert casts (trunc instructions in your
case) to<br>
cast %1 and %2 from i32 to i16 for the smaller add.<br>
<br>
Nick<br>
<br>
<br>
<br>
<br>
</div>
<div class="im">
_______________________________________________<br>
LLVM Developers mailing list<br>
<a moz-do-not-send="true"
href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>
<a moz-do-not-send="true"
href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a moz-do-not-send="true"
href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev"
target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div>
</blockquote>
<br>
</blockquote>
</div>
<br>
</blockquote>
<br>
</body>
</html>