[llvm-commits] [llvm] r138618 - in /llvm/trunk: lib/Transforms/Utils/SimplifyCFG.cpp test/Transforms/SimplifyCFG/phi-undef-loadstore.ll

Benjamin Kramer benny.kra at googlemail.com
Thu Aug 25 19:27:11 PDT 2011


On 25.08.2011, at 18:37, Eli Friedman wrote:

> On Thu, Aug 25, 2011 at 6:22 PM, Benjamin Kramer
> <benny.kra at googlemail.com> wrote:
>> Author: d0k
>> Date: Thu Aug 25 20:22:29 2011
>> New Revision: 138618
[…]
>> +/// Check if passing a value to an instruction will cause undefined behavior.
>> +static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
>> +  Constant *C = dyn_cast<Constant>(V);
>> +  if (!C)
>> +    return false;
>> +
>> +  if (!I->hasOneUse()) // FIXME: There is no reason to limit this to one use.
>> +    return false;
> 
> I assume this is a performance optimization?

Yup, I'll reword the comment.

>> +  if (C->isNullValue()) {
>> +    Instruction *Use = I->use_back();
>> +
>> +    // Now make sure that there are no instructions in between that can alter
>> +    // control flow (eg. calls)
>> +    for (BasicBlock::iterator i = ++BasicBlock::iterator(I); &*i != Use; ++i)
>> +      if (i == I->getParent()->end() ||
>> +          !i->isSafeToSpeculativelyExecute())
>> +        return false;
> 
> I->hasSideEffects() should be sufficient here... you don't care if
> there's an instruction with undefined behavior before the instruction
> with undefined behavior. :)

OK :)

> 
>> +    // Look through GEPs. A load from a GEP derived from NULL is still undefined
>> +    if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
>> +      if (GEP->getPointerOperand() == I)
>> +        return passingValueIsAlwaysUndefined(V, GEP);
>> +
>> +    // Look through bitcasts.
>> +    if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
>> +      return passingValueIsAlwaysUndefined(V, BC);
>> +
>> +    // load from null is undefined
>> +    if (isa<LoadInst>(Use))
>> +      return true;
>> +
>> +    // store to null is undef
>> +    if (isa<StoreInst>(Use) && Use->getOperand(1) == I)
>> +      return true;
> 
> Missing address space checks?

I always forget about that :(

Thank you for the review!

- Ben



More information about the llvm-commits mailing list