21#define DEBUG_TYPE "control-flow-hub"
37 "Only support branch terminator.");
39 auto *Condition = Branch->isConditional() ? Branch->getCondition() :
nullptr;
43 if (Branch->isUnconditional()) {
44 assert(Succ0 == Branch->getSuccessor(0));
46 Branch->setSuccessor(0, FirstGuardBlock);
48 assert(!Succ1 || Succ1 == Branch->getSuccessor(1));
49 if (Succ0 && !Succ1) {
50 Branch->setSuccessor(0, FirstGuardBlock);
51 }
else if (Succ1 && !Succ0) {
52 Branch->setSuccessor(1, FirstGuardBlock);
54 Branch->eraseFromParent();
73 for (
int E = GuardBlocks.
size() - 1;
I != E; ++
I) {
96 for (
auto [BB, Succ0, Succ1] : Branches) {
98 Value *IncomingId =
nullptr;
100 auto Succ0Iter =
find(Outgoing, Succ0);
101 auto Succ1Iter =
find(Outgoing, Succ1);
103 ConstantInt::get(Int32Ty, std::distance(Outgoing.
begin(), Succ0Iter));
105 ConstantInt::get(Int32Ty, std::distance(Outgoing.
begin(), Succ1Iter));
107 BB->getTerminator()->getIterator());
110 auto SuccIter = Succ0 ?
find(Outgoing, Succ0) :
find(Outgoing, Succ1);
112 ConstantInt::get(Int32Ty, std::distance(Outgoing.
begin(), SuccIter));
114 Phi->addIncoming(IncomingId, BB);
117 for (
int I = 0, E = Outgoing.
size() - 1;
I != E; ++
I) {
121 auto *Cmp = ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, Phi,
122 ConstantInt::get(Int32Ty,
I),
123 Out->
getName() +
".predicate", GuardBlocks[
I]);
124 GuardPredicates[Out] = Cmp;
141 for (
int I = 0, E = Outgoing.
size() - 1;
I != E; ++
I) {
149 GuardPredicates[Out] = Phi;
152 for (
auto [BB, Succ0, Succ1] : Branches) {
162 bool OneSuccessorDone =
false;
163 for (
int I = 0, E = Outgoing.
size() - 1;
I != E; ++
I) {
165 PHINode *Phi = cast<PHINode>(GuardPredicates[Out]);
166 if (Out != Succ0 && Out != Succ1) {
167 Phi->addIncoming(BoolFalse, BB);
168 }
else if (!Succ0 || !Succ1 || OneSuccessorDone) {
171 Phi->addIncoming(BoolTrue, BB);
175 Phi->addIncoming(Condition, BB);
179 Phi->addIncoming(Inverted, BB);
181 OneSuccessorDone =
true;
205 std::optional<unsigned> MaxControlFlowBooleans) {
209 for (
int I = 0, E = Outgoing.
size() - 1;
I != E; ++
I)
218 if (!MaxControlFlowBooleans || Outgoing.
size() <= *MaxControlFlowBooleans)
240 while (
I != Out->
end() && isa<PHINode>(
I)) {
241 auto *Phi = cast<PHINode>(
I);
244 Phi->getName() +
".moved", FirstGuardBlock->
begin());
245 bool AllUndef =
true;
246 for (
auto [BB, Succ0, Succ1] :
Incoming) {
248 if (Phi->getBasicBlockIndex(BB) != -1) {
249 V = Phi->removeIncomingValue(BB,
false);
253 AllUndef &= isa<UndefValue>(V);
256 NewPhi->addIncoming(V, BB);
259 Value *NewV = NewPhi;
261 NewPhi->eraseFromParent();
264 if (Phi->getNumOperands() == 0) {
265 Phi->replaceAllUsesWith(NewV);
266 I = Phi->eraseFromParent();
269 Phi->addIncoming(NewV, GuardBlock);
276 const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {
282 for (
auto [BB, Succ0, Succ1] :
Branches) {
284 assert(
Incoming.insert(BB).second &&
"Duplicate entry for incoming block.");
292 if (Outgoing.
size() < 2)
293 return {Outgoing.
front(),
false};
297 for (
auto [BB, Succ0, Succ1] :
Branches) {
307 DeletionCandidates, Prefix, MaxControlFlowBooleans);
311 for (
int I = 0, E = GuardBlocks.
size();
I != E; ++
I)
317 int NumGuards = GuardBlocks.
size();
319 for (
auto [BB, Succ0, Succ1] :
Branches)
322 for (
int I = 0;
I != NumGuards - 1; ++
I) {
330 Outgoing[NumGuards - 1]});
332 Outgoing[NumGuards]});
336 for (
auto I : DeletionCandidates) {
338 if (
auto *Inst = dyn_cast_or_null<Instruction>(
I))
339 Inst->eraseFromParent();
342 return {FirstGuardBlock,
true};
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void calcPredicateUsingBooleans(ArrayRef< EdgeDescriptor > Branches, ArrayRef< BasicBlock * > Outgoing, SmallVectorImpl< BasicBlock * > &GuardBlocks, BBPredicates &GuardPredicates, SmallVectorImpl< WeakVH > &DeletionCandidates)
static void setupBranchForGuard(ArrayRef< BasicBlock * > GuardBlocks, ArrayRef< BasicBlock * > Outgoing, BBPredicates &GuardPredicates)
static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock, ArrayRef< EdgeDescriptor > Incoming, BasicBlock *FirstGuardBlock)
static void calcPredicateUsingInteger(ArrayRef< EdgeDescriptor > Branches, ArrayRef< BasicBlock * > Outgoing, ArrayRef< BasicBlock * > GuardBlocks, BBPredicates &GuardPredicates)
static Value * redirectToHub(BasicBlock *BB, BasicBlock *Succ0, BasicBlock *Succ1, BasicBlock *FirstGuardBlock)
static void convertToGuardPredicates(ArrayRef< EdgeDescriptor > Branches, ArrayRef< BasicBlock * > Outgoing, SmallVectorImpl< BasicBlock * > &GuardBlocks, SmallVectorImpl< WeakVH > &DeletionCandidates, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans)
This file implements a set that has insertion order iteration characteristics.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & front() const
front - Get the first element.
size_t size() const
size - Get the array size.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static constexpr UpdateKind Delete
static constexpr UpdateKind Insert
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
This is an important class for using LLVM in a threaded context.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
ArrayRef< value_type > getArrayRef() const
size_type size() const
Determine the number of elements in the SetVector.
const value_type & front() const
Return the first element of the SetVector.
const value_type & back() const
Return the last element of the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
LLVM Value Representation.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI Value * invertCondition(Value *Condition)
Invert the given true/false value, possibly reusing an existing copy.
std::pair< BasicBlock *, bool > finalize(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans=std::nullopt)
Return the unified loop exit block and a flag indicating if the CFG was changed at all.
SmallVector< BranchDescriptor > Branches
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...