Bram Adams
2006-Oct-01 19:28 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
Hi, I need to find a way to extract all BasicBlocks of a Function (no clones!) into a new Function that has the exact same signature, and adding a call to the new Function in the old one. I tried out lib/ Transforms/Utils/CodeExtractor::ExtractCodeRegion(...), but this one unfortunately checks first to see whether there are any allocas and/ or va_starts and returns a null pointer in that case. Could this check be omitted? If not, is there another way to do the extraction? One of the obstacles I face when trying to do the complement (creating new Function and adding call to original in it), is to find out how to pass the varargs argument of the new Function into the call to the old Function. Will passing the sbyte** passed to llvm.va_start do the trick? Kind regards, Bram Adams GH-SEL, INTEC, Ghent University (Belgium)
Chris Lattner
2006-Oct-02 19:35 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
On Sun, 1 Oct 2006, Bram Adams wrote:> One of the obstacles I face when trying to do the complement > (creating new Function and adding call to original in it), is to find > out how to pass the varargs argument of the new Function into the > call to the old Function. Will passing the sbyte** passed to > llvm.va_start do the trick?I think this is the better way to go. If you're concerned about varargs, you need to call va_start in the varargs one, then pass the valist down. I thin kthe easiest way to do this is to do the 'complement' as you describe, but specially handle the varargs case. Basically you need to call va_start in the original function, and pass the valist pointer down. This shouldn't be too hard. -Chris -- http://nondot.org/sabre/ http://llvm.org/
Bram Adams
2006-Oct-02 20:49 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
Hi, Op 2-okt-06, om 21:35 heeft Chris Lattner het volgende geschreven:> I think the easiest way to do this is to do the 'complement' as you > describe, but specially handle the varargs case. Basically you > need to > call va_start in the original function, and pass the valist pointer > down. > This shouldn't be too hard.OK. I've been rethinking my use of lib/Transforms/Utils/ CodeExtractor::ExtractCodeRegion(...) to obtain my first goal, but I don't think I need such a complex algorithm after all. Basically, I only need to clone a Function's signature, move the full body to the clone and call the clone from the original Function instead. Determining in- and outgoing variables is unnecessary, as the original Function's signature is the same as the clone's one. Also, both here as in the "complement" case a CallInst needs to be synthesized, but the users of the original Function don't need to change in the former case. That's why I'd like this approach more instead of the latter one (e.g. in the case of function pointers or of external libraries probably not all uses can be found). It seems that all I'd need would be the following naïve approach: 1) clone the original Function's signature (clone has empty body) 2) make mapping of the original Function's Arguments to the clone's ones 3) iterate over the original Function's BasicBlocks, modifying their parent one by one; the BasicBlocks' mutual connections remain intact 4) move the symbol table to the clone 5) replace all uses of the original Arguments by the clone's ones in the clone's body 6) synthesize a CallInst to the clone So, I wonder what problems may show up in this algorithm (steps 4, 5 and 6 seem to be crucial). The vararg-case could be a problem in step 5, although the va_start intrinsic isn't tied to the surrounding Function at first sight. Kind regards, Bram Adams GH-SEL, INTEC, Ghent University (Belgium)
Maybe Matching Threads
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function