CS255/455 Spring 2008 Questions and Answers

This page is intended for anyone to post questions and everyone to answer them. Please insert questions at the beginning and separate each question with a horizontal rule separator. For other formatting instructions read the answer to this question.
Q: How can we identify a node with type casting? e.g. D.1570 = (double) D.1569; or a.1 = (unsigned int) a.0;. I want to identify the rhs is a type casting and want to extract the variables as well.

A: I would think you'll see a special node representing the cast. A quick look at tree.def found two suspects, each has a unary child, which should be the variable or expression being converted. ChenDing

/* Represents a conversion of type of a value.
All conversions, including implicit ones, must be
represented by CONVERT_EXPR or NOP_EXPR nodes. */
DEFTREECODE (CONVERT_EXPR, "convert_expr", tcc_unary, 1)
/* Represents a conversion expected to require no code to be generated. */
DEFTREECODE (NOP_EXPR, "nop_expr", tcc_unary, 1)

Q: How can we identify definitions and uses of global variables like "int array[32]" in fibonacci.c?

A: Depending on the purpose, usually a compiler traverses the code, examines each node, and stores information as needed.
Can you iterate the nodes and record all the pointers to the defs and uses of a variable of your interest? Then you can traverse
your special data structure for these defs and uses. Please elaborate if this does not answer the question.

I guess the expected answer would be is_global_var(var);

Q: Is there a standard way to handle function calls like printf, scanf or in general var-arg functions ? TREE_CHAIN can lead to variable declarations instead of a chain of arguments, and unless explicit care of those functions is taken (through string matching == printf/scanf), I think we have to resolve to SSA graph to find relevant information (name of operands of functions, especially var-arg functions). What do we do : handle them explicitly, or is there a way to do it I cannot track down (except for SSA)?

A: For the project you don't need to handle external functions other than printf/scanf. The usual solution is through special treatment. In addition, there is no use of pointer other than in scanf. Your compiler can take advantage of the property that different variables in the test programs do not alias.

Actually, from your discussion, I remember my another concern about function call: they could define global variables or arrays. I guess we need to handle them carefully??????

Q: For dead code elimination, can we assume all function calls are essential or do we need to be able to distinguish the difference between calls like "foo(a)" and "bar(&a)"?

A: Function calls are a form of control flow and should be preserved in basic dead code elimination, because they may have needed side effects even though the return value has no use. To remove dead calls, one needs an interprocedural form of the transformation, which would be interesting to develop.
Q: how can we debug our gcc, more specifically should I use the option -v with the make , make install , or ... ?

A: Use -v with gcc when you do the compilation. See How to debug your compiler.
Q: I followed the advice giving in the project website about using the node to accelerate gcc make and install instructions and it wasn't fun at all waiting in the queue in order to get a free node, especially when I was competing with people whom allocated like 20 or more nodes in order to run a huge giant project, so I decided to switch back to my machine, so should I run: 1) make clean. 2) make. 3) make install. from the obj directory or something else is required ?

A: If you are building the compiler on a different machine, you may need to reconfigure the Makefile by removing the object directory completely and run configure again. See Gcc installation notes.

Q: I can't understanding the Lattice part and all the proofs we had in the lectures, also I can't find any explanation for it in our two text books, is there any other recommended resource to relay on?

A: For lattice, you may read some books about discrete mathematics or Lattice on Wiki.
Q: If z has two dominators then one dominates the other. We didn't end up proving this in the class. Can we get the proof?

A: Scroll down !
Q: Is there any way to convert a string to tree? e.g. I want to convert an expression "1-2" to a tree.

A: I don't know any magic function for this. But You may build the tree step by step.
Q: Is there a way to get a string representation of a tree, particularly a tree that represents a single variable? get_name() does not work for variables like D.####

A: I think it does not work for variables that look like D.#### since they are temporary compiler variables and do not actually have a name associated with them. Check out print_node_brief in print-tree.c to see how that function deals with getting a string representation of it.

Actually, #### is a unique ID for each declaration, people can use DECL_UID(node) to get that unique number.

Another solution, suggested by Tivadar, is to use the address of the tree node representing the temporary variable.
Q: Are there functions to change operands of a tree or do we have to build EXPR trees ourselves with some other function?

A: There are some macroes named "build0", "build1", .... in tree.h which can be used to build gimple trees. For changing operands, there are 3 steps: build new operands, remove old operands and place new operands.
Q: It seems that there are couple of hash table related functions in tree-vn.c, why can't we use that? Or can we write similar ones? (I mean similar hash_f and eq_f, etc).

A: Either is OK for the project.

Q: How much of tree-vn.c can we use ? For project 3, we need VEC and hash from data structs I guess, but there 's a whole lot of functionality there that might not be considered ssa stuff.

A: The name "vn" looks suspicious. You can read the code but you must code your own implementation. You can use the control flow analysis in Gcc but not the value numbering in Gcc.

Q: For the scanf function, are the temporary variable statements that we need to move those that have the variables on the right hand side (instead of the left hand side as in the printf)?

A: Since scanf reads values to variables directly, there is no need to move any temporary variable assignment with scanf. The project description has been adjusted. Thanks for the question. (ChenDing)

Q: I am trying to build a list of statements linked through NEXT CHAIN, but I cannot get it to work. Any ideas about how this process actually works ? In the excerpt below I just call apend_to_statement_list for my two statements. By append's source code, a new statement list should be built bellow mylist if mylist is initially NULL. This partly works, i.e. if I print mylist I can see stmt1 and stmt, but it keeps saying I cannot retrieve them using NEXT CHAIN...

A: This is interesting. I would think the result might be a bit strange since it may contain three statements, the first being the NULL. Another way is to append stmt2 to stmt1 to create the list, as below

mylist = stmt1
append_to_statement_list(stmt2, & mylist)

I am not familiar with NEXT CHAIN (the name seems a macro) but I wonder whether its problem has something to do with the NULL stmt. In addition, if you can print mylist through some function, then I would check how that function iterates through the list in spite of the problem with NEXT CHAIN. Please post what you find. (ChenDing)

A NEXT CHAIN is a very useful macro which is used to build an easy to handle list from any tree. However, I have not been able to build such a list of my own, rather I did manage to manipulate, for example, the number of arguments of a var-arg function like printf with its iternal wacky representation. This is what Xiaoming suggests, I hadn't noticed and I found it on my own. I guess your suggestion about the problem explains what I experienced, but I cannot remember. The statement list carries some more information used in some certain occasions (I remember so), but at least I did manage to use it to build a list:

At c-semantics they have some push/pop functions, I didn't use it though. In all, I highly doubt it might be of any use to me again, NEXT CHAIN is important, if somebody can make a concrete build/use-list example with it please post it.

Q: Given a tree node, i.e. call expr, how do you extract the function name from the node?

A: I have already got this one. Please refer the following code which already exists in cs255.c

Since the two printf share one copy stmt of j, how should we move it? I think this is not quite clearly defined in the project description.

A: This is a good example, which exposes several finer points of the requirement. First is what we consider as copies to temporaries. In this example, "i.11=i" is a copy but "j=2" is not because the latter copies from a constant, not a variable. Needless to say that "i=i+1" is not a copy either. The reordering would yield

It is fine if you consider constant assignments such as "j=2" a copy. The requirement does not say what to do if two scanf/printf statements have or copy from the same variable as in the following example (slightly changed from the original one)

j = i;
printf("%d", j);
printf("%d", j);

Since the requirement did not address this conflict, you can in principle do whatever you want to without losing any points. I would recommend replicating the copy, "j=i" in this example, for each related statement. The solution is equivalent of keeping the copy with the earliest statement if there are no intervening changes to the right-hand operand of the copy statement.

Another problem that the original example shows is what to do when we use complex expressions as arguments to scanf/printf. We can certainly do something more elaborate to make the compiler not only weird but also insistently weird but we don't have to. You don't need to go beyond the requirement. When demonstrating your compiler, do not use complex expressions and instead perhaps explain that yours is a weird compiler, not a weird and wicked compiler. (ChenDing)

Q: How to dump c-like code for a gimple node?

A: Suppose node is the node, use debug_tree(node); to do the dump.
I think another useful dump function is dump_node(node, TDF_SLIM, stderr), which prints detailed information about a node.

If you want to print out the string representation of the node type, you can use the following trick due to TongxinBai. As an example cute code trick in Gcc, while the tree types are defined in enum tree_code in tree.h, the actual definition is not in tree.h but in tree.def, which includes a list of all node types and their (string) symbols. Looking at the code you'll quickly see how the trick works, and borrowing it you can easily construct an array of type strings (in your program file) as follows:

Q: More of remarks and vague ideas than questions: in class we discussed about Chris' idea to perform the a=b-c <=> b=c+1 analysis. I had doubts about the idea because I was thinking "=" more of as an assignment operator, rather than the algebraic equality alternative, which pretty much is what it is in this analysis step. I guess thus, that at this level we can perform an even wider variety of symbolic analysis (something that would resemble what Maple does for the common mathematical notations, but in our case solely for Algebra) and generate an even wider range of possible optimizations one cannot possible expect at first sight. I was wondering though to what extent this actually happens, since the way we program brakes down the simplest algebraic expressions into difficult to track simplified steps. I was also wondering whether one can run any optimizations as (syntax) tree-data structure operations, i.e whether instead of a hash there exists an advanced data structure that keeps the tree-like structure of the syntax tree and optimizations are realized as (for example) rotations, insertions/deletions/pruning etc. After all, after the optimizations we should be able to build a new syntax tree from an optimized version of the code ; does there exist a possible repetitive transformation that does not need intermediate hash-based analysis (maybe I 'm making a hard problem impossible here)?

A: Okay let's add a category for remarks and begin them with symbol R. You are right that the optimization of algebraic expressions is not different from the classic problem of mathematics. When we start talking about the equivalence between programs we need to understand the basic semantics of programming languages, which we'll discuss in the last two weeks of the course. (added by ChenDing, 10pm, 1/31)

Q: Is there only one immediate dominator for every block, or might it be the case that there are more?

A: The definitions as mentioned in class :

A block X is a Dominator of Y in a Control Flow Graph, if X appears on every path that enters Y. We write X>>Y and say X dominates Y. We also say that if X!=Y , then X "strictly" dominates Y.

The Immediate Dominator of Y is the strict dominator that is closest to Y in an execution path.

If a block X has two strict dominators, Y and Z, either they are the same or one strictly dominates the other. Here is a proof by contradiction. Assume Y and Z are different, and neither one strictly dominates the other. Take a path from entry to X, which must go through Y and Z. Without loss of generality assume Y appears closest to X on this path, which means that there is a path Y->X without going through Z. Since Z does not strictly dominate Y, there is a path entry->Y without going through Z. The combination of the above two paths, entry->Y->X, does not go through Z, contradicting our assumption that Z (strictly) dominates X.

Given the above lemma (due to Satyaki Mahalanabis), it is easy to show that if Y and Z are strict dominators of X and Y strictly dominates Z, then Z appears closer to X than Y does on every path from entry to X. Otherwise, one can construct a path entry->Y->X without going through Z.

In order to better understand the definition and proof and their implications, here is a graph where there is a single strict dominator, and here is another one where the problem described by Satyaki is depicted: one node "strictly dominates" the other. In fact, this graph is trying to draw a picture where nodes C and D dominate E , C strictly dominates D, D strictly dominates C, but C and D are equal distance from E, ending up with the contradiction mentioned in the lemma. Trying to draw a graph like that pretty much by itself shows why there is a problem with having two immediate dominators: you end up adding and removing nodes and edges, and cannot avoid the contradiction: there will exist a path from A to E where C and D can be bypassed, so they are not even dominators of E, and they are not strict dominators of each other because one does not reside in every path from root to the other. This is not the easiest thing to see, so you can trust your intuition that it's true by the examples above.

Notice the following from experimentation with GCC. One can create the following C program to trick the compiler into generating something very close to the second graph.Here is a simple program trying to have an one-to-one correspondence with the graph (it actually does not, due to practical reasons mentioned bellow) :

If you run GCC on it, you get a normal binary - no error, everything looks fine. The expected effect is that of while(1); Indeed, that is the case : the paths from B and C to E are actually never taken. If you try -fdump-tree-gimple though, the gimplified code has some unexpected annotations :

What happens is that we have a correct program, but there is this weird behavior : though there is a path from C to E and from D to E, the compiler can't take this path, because it sticks to jumping around between C and D. The compiler annotates this as an error during some part of its dominator graph analysis (or possibly later - you can find out yourself ; the labeled tree node has been marked with ERROR_MARK and there are quite some cases you can get this behavior from the compiler, just grep the source). As the Cooper-Torzcon book mentions in p. 480, "Structural Data-Flow Algorithms and Reducibility", there are a bunch of issues with allowing this code in the tree graph and that is why GCC marks it as an error. Use the index of the book and you might find more interesting things about the control flow graph. The problem arises from languages like C allowing jumps to basic blocks the way we did. Maybe one can do something like that with exceptions too. The problem is very interesting, because the classic data flow assumes that all branch directions are possible. If there are special relations between branch tests, for example, if test a is true than test b must be true, then the meet-over-all-path problem requires more precise analysis---type state for example.

As to oppose to the fact that this is indeed an error, versus a similar case with analogous effect but different behavior, we can see what GCC has to say when we try to compile while(1):

You can see that the compiler does not object to your intention to create an infinite loop. The reason it did object in the previous example, was an analysis issue.

Last remarks: Trying a top down approach to the proof (showing that one node will dominate the other) is kind of harder, because, like Tivadar did in class, you can see that you end up with two nodes, one dominating the other but don't grasp why this might be wrong. Graph drawing does help, but probably sticking strictly to the definitions is an ok proof also. Previous attempts, false-alarms and in all the discussion about this subject before being rewritten for the n-th time can be seen through history. This answer to the question has been rewritten by merging Q+A concerning this subject, including a talk with Dr. Ding and Satyaki. I hope the examples make an otherwise nice short and correct proof nicer and more intriguing.

Q: How do we format a question and an answer?

A: A question and an answer start with the bold letter Q and A respectively followed by a colon. Use italic text for the question and normal text for the answer. Use a separator (a horizontal rule) after each question. Add new questions to the beginning of the list.