About user of bitcast/GEP instruction

Hi,

In PromoteMemoryToRegister.cpp, it seems to rely on the fact that the only users of bitcast/GEP instruction are lifetime intrinsics (llvm.lifetime.start/end). I did some searching in llvm/test folder, it seems to be true.

However, by reading LLVM IR manual, I don’t see any restriction stated on the possible user of bitcast/GEP instruction. So my question is who impose the restriction ? Is it Clang ?

Regards
-guoqing

If a stack pointer is passed to another function it can’t be promoted to a register, as that function will need to load or store through the pointer. But while these intrinsics look like functions, they aren’t really.

From: "guoqing zhang" <gqzhang81@gmail.com>
To: llvmdev@cs.uiuc.edu
Sent: Friday, January 30, 2015 4:29:16 AM
Subject: [LLVMdev] About user of bitcast/GEP instruction

Hi,

In PromoteMemoryToRegister.cpp, it seems to rely on the fact that the
only users of bitcast/GEP instruction are lifetime intrinsics
(llvm.lifetime.start/end).

You're misreading that code. There is special handling is that pass for looking through those instructions to find lifetime intrinsics, but that's all.

I did some searching in llvm/test folder,
it seems to be true.

bitcasts and GEPs are general-purpose instructions. GEPs are most often used by loads/stores. Look at test/Transforms/LoopReroll/basic.ll for an example with lots of them. test/Analysis/BasicAA/gep-alias.ll is another place to look.

However, by reading LLVM IR manual, I don't see any restriction
stated on the possible user of bitcast/GEP instruction. So my
question is who impose the restriction ? Is it Clang ?

There is definitely no such restriction.

-Hal

Hi,

If the special handling in the meg2reg pass is to look for lifetime intrinsics, shouldn’t it cast to and then use getInstrinsicID to check for lifetime_start and lifetime_end ?

The thing that I don’t understand is the following piece of code, which finds all the users and cast it to then eraseFromParent(). How can this guarantee that it only erase lifetime instrinsics ?

-guoqing

// The only users of this bitcast/GEP instruction are lifetime intrinsics.
// Follow the use/def chain to erase them now instead of leaving it for
// dead code elimination later.
for (auto UUI = I->user_begin(), UUE = I->user_end(); UUI != UUE;) {
Instruction *Inst = cast(*UUI);
++UUI;
Inst->eraseFromParent();
}

From: "guoqing zhang" <gqzhang81@gmail.com>
To: "Hal Finkel" <hfinkel@anl.gov>
Cc: llvmdev@cs.uiuc.edu
Sent: Friday, January 30, 2015 9:17:13 AM
Subject: Re: [LLVMdev] About user of bitcast/GEP instruction

Hi,

If the special handling in the meg2reg pass is to look for lifetime
intrinsics, shouldn't it cast to <IntrisicInst> and then use
getInstrinsicID to check for lifetime_start and lifetime_end ?

The thing that I don't understand is the following piece of code,
which finds all the users and cast it to <Instruction> then
eraseFromParent(). How can this guarantee that it only erase
lifetime instrinsics ?

I think that Chandler (cc'd) is the best person to explain the overall algorithm here. Please note, however, that this is an internal routine that has implicit preconditions. Why are you looking at it?

The comment at the top of the function says:

  // Knowing that this alloca is promotable, we know that it's safe to kill all
  // instructions except for load and store.

and so we already know that the alloca is not used in any non-trivial way except by loads or stores (or perhaps some other kinds of intrinsics). There are explicit checks for lifetime intrinsics elsewhere, and if the alloca were used by anything that returned a value (like a bitcase or a GEP) that was used by anything other than a lifetime intrinsic, then it would not be promotable and this routine should not be called.

-Hal