LLVM  15.0.0git
Macros | Functions
CallPromotionUtils.cpp File Reference
#include "llvm/Transforms/Utils/CallPromotionUtils.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/TypeMetadataUtils.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Include dependency graph for CallPromotionUtils.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "call-promotion-utils"
 

Functions

static void fixupPHINodeForNormalDest (InvokeInst *Invoke, BasicBlock *OrigBlock, BasicBlock *MergeBlock)
 Fix-up phi nodes in an invoke instruction's normal destination. More...
 
static void fixupPHINodeForUnwindDest (InvokeInst *Invoke, BasicBlock *OrigBlock, BasicBlock *ThenBlock, BasicBlock *ElseBlock)
 Fix-up phi nodes in an invoke instruction's unwind destination. More...
 
static void createRetPHINode (Instruction *OrigInst, Instruction *NewInst, BasicBlock *MergeBlock, IRBuilder<> &Builder)
 Create a phi node for the returned value of a call or invoke instruction. More...
 
static void createRetBitCast (CallBase &CB, Type *RetTy, CastInst **RetBitCast)
 Cast a call or invoke instruction to the given type. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "call-promotion-utils"

Definition at line 23 of file CallPromotionUtils.cpp.

Function Documentation

◆ createRetBitCast()

static void createRetBitCast ( CallBase CB,
Type RetTy,
CastInst **  RetBitCast 
)
static

Cast a call or invoke instruction to the given type.

When promoting a call site, the return type of the call site might not match that of the callee. If this is the case, we have to cast the returned value to the correct type. The location of the cast depends on if we have a call or invoke instruction.

For example, if the call instruction below requires a bitcast after promotion:

orig_bb: t0 = call i32 @func() ...

The bitcast is placed after the call instruction:

orig_bb: ; Uses of the original return value are replaced by uses of the bitcast. t0 = call i32 @func() t1 = bitcast i32 t0 to ... ...

A similar transformation is performed for invoke instructions. However, since invokes are terminating, a new block is created for the bitcast. For example, if the invoke instruction below requires a bitcast after promotion:

orig_bb: t0 = invoke i32 @func() to label normal_dst unwind label unwind_dst

The edge between the original block and the invoke's normal destination is split, and the bitcast is placed there:

orig_bb: t0 = invoke i32 @func() to label split_bb unwind label unwind_dst

split_bb: ; Uses of the original return value are replaced by uses of the bitcast. t1 = bitcast i32 t0 to ... br label normal_dst

Definition at line 162 of file CallPromotionUtils.cpp.

References llvm::CastInst::CreateBitOrPointerCast(), llvm::BasicBlock::front(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::User::replaceUsesOfWith(), llvm::SplitEdge(), and llvm::Value::users().

Referenced by llvm::promoteCall().

◆ createRetPHINode()

static void createRetPHINode ( Instruction OrigInst,
Instruction NewInst,
BasicBlock MergeBlock,
IRBuilder<> &  Builder 
)
static

Create a phi node for the returned value of a call or invoke instruction.

After versioning a call or invoke instruction that returns a value, we have to merge the value of the original and new instructions. We do this by creating a phi node and replacing uses of the original instruction with this phi node.

For example, if OrigInst is defined in "else_bb" and NewInst is defined in "then_bb", we create the following phi node:

; Uses of the original instruction are replaced by uses of the phi node. t0 = phi i32 [ orig_inst, else_bb ], [ new_inst, then_bb ],

Definition at line 107 of file CallPromotionUtils.cpp.

References llvm::PHINode::addIncoming(), Builder, llvm::BasicBlock::front(), llvm::Instruction::getParent(), llvm::Value::getType(), llvm::Type::isVoidTy(), llvm::Value::use_empty(), and llvm::Value::users().

Referenced by llvm::versionCallSite().

◆ fixupPHINodeForNormalDest()

static void fixupPHINodeForNormalDest ( InvokeInst Invoke,
BasicBlock OrigBlock,
BasicBlock MergeBlock 
)
static

Fix-up phi nodes in an invoke instruction's normal destination.

After versioning an invoke instruction, values coming from the original block will now be coming from the "merge" block. For example, in the code below:

then_bb: t0 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

else_bb: t1 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

merge_bb: t2 = phi i32 [ t0, then_bb ], [ t1, else_bb ] br normal_dst

normal_dst: t3 = phi i32 [ x, orig_bb ], ...

"orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in "normal_dst" must be fixed to refer to "merge_bb":

normal_dst: t3 = phi i32 [ x, merge_bb ], ...

Definition at line 50 of file CallPromotionUtils.cpp.

References llvm::InvokeInst::getNormalDest(), and llvm::BasicBlock::phis().

Referenced by llvm::versionCallSite().

◆ fixupPHINodeForUnwindDest()

static void fixupPHINodeForUnwindDest ( InvokeInst Invoke,
BasicBlock OrigBlock,
BasicBlock ThenBlock,
BasicBlock ElseBlock 
)
static

Fix-up phi nodes in an invoke instruction's unwind destination.

After versioning an invoke instruction, values coming from the original block will now be coming from either the "then" block or the "else" block. For example, in the code below:

then_bb: t0 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

else_bb: t1 = invoke i32 ptr() to label merge_bb unwind label unwind_dst

unwind_dst: t3 = phi i32 [ x, orig_bb ], ...

"orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":

unwind_dst: t3 = phi i32 [ x, then_bb ], [ x, else_bb ], ...

Definition at line 81 of file CallPromotionUtils.cpp.

References llvm::InvokeInst::getUnwindDest(), and llvm::BasicBlock::phis().

Referenced by llvm::versionCallSite().