I am trying to write a Simple pass on basic block and the code is as follows:
struct SimplePass : BasicBlockPass, InstVisitor<SimplePass>
{
... some initialisation and some finalization code
virtual bool runOnBasicBlock(BasicBlock& B) {
std::cout << "---This is a block divider---" << B.size() << std::endl;
visit(B);
return false;
}
void visitInstruction(Instruction& I){
std::cout << "Visiting every single instruction:" << I.getOpcodeName(I.getOpcode()) << std::endl;
}
void visitBranchInst(BranchInst& I) {
if(I.isUnconditional()) {
std::cout << "Encountered an unconditional branch!" << std::endl;
}
}
}
and very strangely I get some output like this:
...
---This is a block divider---5
Visiting every single instruction:call
Visiting every single instruction:load
Visiting every single instruction:add
Visiting every single instruction:store
Encountered an unconditional branch!
---This is a block divider---7
Visiting every single instruction:phi
Visiting every single instruction:load
Visiting every single instruction:sub
Visiting every single instruction:call
Visiting every single instruction:load
Visiting every single instruction:icmp
---This is a block divider---3
......
It is easy to see that in both blocks above the actual number of instructions should be 5 and 7, however the visitInstrucion function sometimes do not visit the last instruction of a basic block, why does this happen? Is this supposed to happen?
In the first block:
Visiting every single instruction:call
Visiting every single instruction:load
Visiting every single instruction:add
Visiting every single instruction:store
Encountered an unconditional branch!
It is 5 though! The last line comes from your void visitBranchInst(BranchInst& I) which takes precedence over visitInstruction. More specific visitors take precedence over more generic ones. If you want visitInstruction to be called anyway, you have to do it explicitly from more specific visitors - it won't happen automatically.
As for the next block, maybe it ends with a branch that is conditional? Then your visitBranchInst doesn't print anything, and doesn't propagate to visitInstruction.
Related
I'm trying to find a TreeNode with a certain Name inside my Treeview.
For that, I want to use the Find function.
The problem is every time I try to access the Array I get the IndexOutOfRange exception.
When debugging, I can see that the index im accessing has a value in it.
if (!form->treeView1->Nodes->Find(gcnew String(primary.c_str()), false))
{
std::cout << "NoValue" << std::endl;
}
else
{
System::Windows::Forms::TreeNode^ prima = form->treeView1->Nodes->Find(gcnew String(primary.c_str()), true)[0];
prima->Nodes->Add(second);
}
Here you can see the Debug I did. I put the Breakpoint at the prima variable where the array that the Find function is returning is accessed.
I know this can look like a rookie question already asked a thousand time. But I searched for the exact answer and I haven't found one...
I'm working on a code that, to sum up, fill an XML with different data.
I'm trying to optimize a part of it. The "naïve" code is the following:
xml << "<Node>";
for(auto& input : object.m_vec)
{
if(input == "Something")
{
xml << input;
}
}
xml << "</Node>";
for(auto& input : object.m_vec)
{
if(input == "SomethingElse")
{
xml << "<OtherNode>";
xml << input;
xml << "</OtherNode>";
break;
}
}
The important thing is, while more than one input fit in <Node></Node>, only one fit in <OtherNode></OtherNode> (explaining the break;) and it may not exist either (explaining the xml << in-between the if statement).
I think I could optimize it such like:
std::vector<std::string>* VecPointer;
xml << "<Node>";
for(auto& input : object.m_vec)
{
if(input == "Something")
{
xml << input;
}
else if(input == "SomethingElse")
{
VecPointer = &input;
}
}
xml << "</Node>";
if(!VecPointer->empty())
{
xml << "<OtherNode>"
<< *VecPointer
<< "</OtherNode>";
}
The point for me here is that there is no extra memory needed and no extra loop. But the pointer to the local variable bothers me. With my beginner's eyes I can't see a case where it can lead to something wrong.
Is this okay? Why? Do you see a better way to do it?
You need to make sure your compairson also looks for an existing value within the VecPointer, since your original second loop only cares about the first value it comes across.
else if(VecPointer && input == "SomethingElse")
Don't look for ->empty(), as that's accessing the pointer and asking whether the pointed to vector is empty. If there's nothing to point to in the first place, you're going to have a bad time at the -> stage of the statement. Instead, if against it, since it's a pointer.
if(VecPointer)
Finally, you're using a Vector to save that one value from m_vec, which from other code I'm assuming is not a vector<vector<string>> but a vector<string> - in the latter case, your VecPointer should be std::string*
std::string* VecPointer = nullptr;
I'm trying to optimize a part of it.
...
Is this okay?
Maybe not! This may already be a poor use of your time. Are you sure that this is what's hurting your performance? Or that there's a performance problem at all?
Remember Don Knuth's old adage: Premature optimization is the root of all evil...
Do you see a better way to do it?
Consider profiling your program to see which parts actually take up the most time.
On an unrelated note, you could use standard library algorithms to simplify your (unoptimized) code. For example:
if (std::ranges::find(std::begin(object.m_vec) std::end(object.m_vec), "SomethingElse"s )
!= std::end(object.m_vec))
{
xml << "<OtherNode>" << whatever << "</OtherNode>";
}
I'm trying to code a counter for a class I made in c++.
I'm passing an amount of time, deltaT, to a method of the Wake class, which does simply adds it onto the value already stored by a variable in the class. The code is:
void checkPlayerWakes(int deltaT){
for(size_t i = 0; i < game.getPlayer().getWakes().size(); i++){
Wake& w = game.getPlayer().getWakes().at(i);
w.age(deltaT);
}
}
However, the timer which is meant to be increasing, is remaining at 0.
The code to change the timer is:
void Wake::age(int millis) {
cout << "Updating : " << currentLife;
this->currentLife += millis;
setAlpha(((double)currentLife)/((double)lifeTime));
cout << " " << currentLife << endl;
}
E.g.
first current life: 0
second current life: 16
I know that if I were to use
Wake w = something
w.age(deltaT)
it wouldn't work because "w" would just be a copy of the object. However, that's clearly NOT my problem here? Also game.getPlayer() also returns a reference, a PLAYERSHIP&.
Last time I had a similar problem, it was solved by calling and returning references instead of just the ClassName. Am I still doing something wrong?
getWakes() needs also to return a reference, and should return vector& as opposed to vector.
I'm adding this here to make sure that this question is marked as answered.
First, my code:
// Reads the index
bi::managed_mapped_file file(bi::open_or_create, indexFile.c_str(), bf::file_size(indexFile.c_str()));
allocator_t alloc(file.get_segment_manager());
rtree_t * rtree_ptr = file.find_or_construct<rtree_t>("rtree")(params_t(), indexable_t(), equal_to_t(), alloc);
std::cout << "The index contains " << rtree_ptr->size() << " entries." << std::endl;
std::ifstream inf(transFile.c_str());
std::string line;
while(getline(inf,line))
{
transition t = transition(line);
point A;
A.set<0>(t.getQ1mz()-1);
A.set<1>(t.getQ3mz()-1);
A.set<2>(0.3);
A.set<3>(0.2);
value_t v = std::make_pair(A,t);
rtree_ptr->insert(v);
rtree_ptr->remove(v);
}
std::cout << "Finished. The index now contains " << rtree_ptr->size() << " entries." << std::endl;
It reads the R-tree from a memory-mapped file. Then, it reads an input file, transFile, make ten so-called "transition" objects from it's content, and inserts them in the tree. Immediately after, it removes them. This is a useless case, but it illustrates well the problem that the removal steps don't work. The output I get is :
The index contains 339569462 entries.
Finished. The index now contains 339569472 entries.
So clearly, the size of the tree increases by ten, because the ten insertions worked like a charm ; but if the removals were working, in the end the tree should have the same size as before, which is not the case.
I have followed the syntax about removing values from an R-tree described here, and all compiles properly, but for some strange reason it just doesn't remove the value. My guess might be that since it deletes by value, it might just not find the value to delete, but how can it be since the value is the one just inserted one line ago?
I have the following code in C++ for a recursive function. I don't understand why everything above the basecase is used before basecase is met (stacking?), and then only the lines below the basecase are used as its unstacking.
#include <iostream>
using namespace std;
void printnum(int begin)
{
cout << "upanddown";
cout << begin << endl; //Why is everything this line and above cout'd as it builds the stack and ignored on the unstacking.
if (begin <= 9) { printnum(begin + 1); } // Once the program starts unstacking the functions, why doesn't it console out "upanddown"?
e
cout << begin << endl; //Why is everything below the base case only shown as it unstacks?
}
int main()
{
printnum(1); //First function call, so it starts at one
system("PAUSE");
}
Which results in:
upanddown1
upanddown2
upanddown3
upanddown4
upanddown5
upanddown6
upanddown7
upanddown8
upanddown9
upanddown10
10
9
8
7
6
5
4
3
2
1
Press any key to continue . . .
Why doesn't upanddown execute below the base case ase well?
int main()
{
printnum(1); //First function call, so it starts at one
system("PAUSE");
}
Here is your main function. It has two statements. system("PAUSE"), which is the tail call¸ executed after printnum(1) has returned. To know where to continue it uses the stack for both indicating where to execute next and it's used to give functions arguments. Every call uses the stack so a recursive function is nothing special. The only special thing about it is that it happens to call itself, but the call to itself is independent and the current function is suspended until it has ended just like main. Consider your recursive function:
void printnum(int begin)
{
cout << "upanddown"; // 1
cout << begin << endl; // 2
if (begin <= 9) // 3
{
printnum(begin + 1); // 4
}
cout << begin << endl; // 5
}
So what happens if begin is 10? It will do 1, 2, 3, not 4, then 5.
Output is "upanddowdn10\n10\n".
For begin = 9 it will do 1, 2 which is "upanddown9\n", then the result of printnum(10) is printed since it hits your default case, then "9\n" is printed last. Thus the printed result is "upanddown9\nupanddowdn10\n10\n9\n"
For begin = 8 it will do 1, 2 which is "upanddown8\n", then the result of printnum(9) is printed since it hits your default case, then "8\n" is printed last. Thus the printed result is "upanddown8\nupanddown9\nupanddowdn10\n10\n9\n8\n"
...
For begin = 1 it will do 1, 2 which is "upanddown1\n", then the result of printnum(2) is printed since it hits your default case, then "1\n" is printed last. Thus the printed result is "upanddown1\nupanddown2\nupanddown3\nupanddown4\nupanddown5\nupanddown6\nupanddown7\nupanddown8\nupanddown9\nupanddowdn10\n10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n"
So to answer your question. The "Upanddownx\n" output is done first because it is the first thing to do in your function. If begin is below 10 it will output the whole result of printnum(begin+1). Last it will print "x\n" in the end.
When the flow execution reaches a recursive function call, a function call is made and it starts from the top (That's why you don't see the numbers printed here). When a recursive call reaches the last statement of the function, the execution flow is transferred to the next statement below caller statement (in this case that statement is cout << begin << endl;) That's why you only see the number when backtracking.
Some references about recursion:
Forum
Tutorial
Just remove this line from your code.It is getting those outputs while unstacking.
cout << begin << endl; //Why is everything below the base case only shown as it unstacks?