[llvm-commits] [llvm] r122336 - /llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp

Chris Lattner clattner at apple.com
Tue Dec 21 09:10:43 PST 2010


On Dec 21, 2010, at 8:12 AM, Duncan Sands wrote:

> Author: baldrick
> Date: Tue Dec 21 10:12:03 2010
> New Revision: 122336
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=122336&view=rev
> Log:
> If an instruction simplifies, try again to simplify any uses of it.  This is
> not very important since the pass is only used for testing, but it does make
> it more realistic.  Suggested by Frits van Bommel.

Cool thanks.

> +      // Add all interesting instructions to the worklist.
> +      std::set<Instruction*> Worklist;

Using an std::set for this will cause the pass to work nondeterminstically (based on pointer addresses).  How about using an std::vector like instcombine?  The cost is that deleting an instruction requires scanning (linear time) the vector to see if an instruction is in the worklist multiple times.  If you want to get really crazy, you can use a smallptrset + vector to prevent that.

-Chris

>       for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
>         for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
>           Instruction *I = BI++;
> -          if (Value *V = SimplifyInstruction(I, TD, DT)) {
> -            I->replaceAllUsesWith(V);
> +          // Zap any dead instructions.
> +          if (isInstructionTriviallyDead(I)) {
>             I->eraseFromParent();
>             Changed = true;
> -            ++NumSimplified;
> +            continue;
>           }
> +          // Add all others to the worklist.
> +          Worklist.insert(I);
>         }
> +
> +      // Simplify everything in the worklist until the cows come home.
> +      while (!Worklist.empty()) {
> +        Instruction *I = *Worklist.begin();
> +        Worklist.erase(Worklist.begin());
> +        Value *V = SimplifyInstruction(I, TD, DT);
> +        if (!V) continue;
> +
> +        // This instruction simplifies!  Replace it with its simplification and
> +        // add all uses to the worklist, since they may now simplify.
> +        I->replaceAllUsesWith(V);
> +        for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
> +             UI != UE; ++UI)
> +          // In unreachable code an instruction can use itself, in which case
> +          // don't add it to the worklist since we are about to erase it.
> +          if (*UI != I) Worklist.insert(cast<Instruction>(*UI));
> +        if (isInstructionTriviallyDead(I))
> +          I->eraseFromParent();
> +        ++NumSimplified;
> +        Changed = true;
> +      }
> +
>       return Changed;
>     }
>   };
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list