-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[X86,SimplifyCFG] Support hoisting load/store with conditional faulting (Part II) #108812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
1077be1
9d73fdd
15e25fb
40e2130
d6d50c4
de28ed9
8ec9409
fc7df9a
dfe6cc6
1ad8714
5a5de39
b1bda56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1660,21 +1660,37 @@ static bool areIdenticalUpToCommutativity(const Instruction *I1, | |
| /// \endcode | ||
| /// | ||
| /// So we need to turn hoisted load/store into cload/cstore. | ||
| static void hoistConditionalLoadsStores( | ||
| static bool hoistConditionalLoadsStores( | ||
| BranchInst *BI, | ||
| SmallVectorImpl<Instruction *> &SpeculatedConditionalLoadsStores, | ||
| bool Invert) { | ||
| std::optional<bool> Invert) { | ||
| auto &Context = BI->getParent()->getContext(); | ||
| auto *VCondTy = FixedVectorType::get(Type::getInt1Ty(Context), 1); | ||
| auto *Cond = BI->getOperand(0); | ||
| // Construct the condition if needed. | ||
| BasicBlock *BB = BI->getParent(); | ||
| IRBuilder<> Builder(SpeculatedConditionalLoadsStores.back()); | ||
| Value *Mask = Builder.CreateBitCast( | ||
| Invert ? Builder.CreateXor(Cond, ConstantInt::getTrue(Context)) : Cond, | ||
| VCondTy); | ||
| IRBuilder<> Builder(Invert ? SpeculatedConditionalLoadsStores.back() : BI); | ||
|
||
| Value *Mask = nullptr; | ||
| Value *MaskFalse = nullptr; | ||
| Value *MaskTrue = nullptr; | ||
| if (Invert) { | ||
| Mask = Builder.CreateBitCast( | ||
| *Invert ? Builder.CreateXor(Cond, ConstantInt::getTrue(Context)) : Cond, | ||
| VCondTy); | ||
| } else { | ||
| MaskFalse = Builder.CreateBitCast( | ||
| Builder.CreateXor(Cond, ConstantInt::getTrue(Context)), VCondTy); | ||
| MaskTrue = Builder.CreateBitCast(Cond, VCondTy); | ||
| } | ||
| auto PeekThroughBitcasts = [](Value *V) { | ||
| while (auto *BitCast = dyn_cast<BitCastInst>(V)) | ||
| V = BitCast->getOperand(0); | ||
| return V; | ||
| }; | ||
| for (auto *I : SpeculatedConditionalLoadsStores) { | ||
| IRBuilder<> Builder(I); | ||
| IRBuilder<> Builder(Invert ? I : BI); | ||
| if (!Invert) | ||
|
||
| Mask = I->getParent() == BI->getSuccessor(0) ? MaskTrue : MaskFalse; | ||
| // We currently assume conditional faulting load/store is supported for | ||
| // scalar types only when creating new instructions. This can be easily | ||
| // extended for vector types in the future. | ||
|
|
@@ -1686,12 +1702,14 @@ static void hoistConditionalLoadsStores( | |
| auto *Ty = I->getType(); | ||
| PHINode *PN = nullptr; | ||
| Value *PassThru = nullptr; | ||
| for (User *U : I->users()) | ||
| if ((PN = dyn_cast<PHINode>(U))) { | ||
| PassThru = Builder.CreateBitCast(PN->getIncomingValueForBlock(BB), | ||
| FixedVectorType::get(Ty, 1)); | ||
| break; | ||
| } | ||
| if (Invert) | ||
| for (User *U : I->users()) | ||
| if ((PN = dyn_cast<PHINode>(U))) { | ||
| PassThru = Builder.CreateBitCast( | ||
| PeekThroughBitcasts(PN->getIncomingValueForBlock(BB)), | ||
| FixedVectorType::get(Ty, 1)); | ||
| break; | ||
| } | ||
| MaskedLoadStore = Builder.CreateMaskedLoad( | ||
| FixedVectorType::get(Ty, 1), Op0, LI->getAlign(), Mask, PassThru); | ||
| Value *NewLoadStore = Builder.CreateBitCast(MaskedLoadStore, Ty); | ||
|
|
@@ -1700,8 +1718,8 @@ static void hoistConditionalLoadsStores( | |
| I->replaceAllUsesWith(NewLoadStore); | ||
| } else { | ||
| // Handle Store. | ||
| auto *StoredVal = | ||
| Builder.CreateBitCast(Op0, FixedVectorType::get(Op0->getType(), 1)); | ||
| auto *StoredVal = Builder.CreateBitCast( | ||
| PeekThroughBitcasts(Op0), FixedVectorType::get(Op0->getType(), 1)); | ||
| MaskedLoadStore = Builder.CreateMaskedStore( | ||
| StoredVal, I->getOperand(1), cast<StoreInst>(I)->getAlign(), Mask); | ||
| } | ||
|
|
@@ -1725,6 +1743,7 @@ static void hoistConditionalLoadsStores( | |
| MaskedLoadStore->copyMetadata(*I); | ||
| I->eraseFromParent(); | ||
| } | ||
| return true; | ||
|
||
| } | ||
|
|
||
| static bool isSafeCheapLoadStore(const Instruction *I, | ||
RKSimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -1771,6 +1790,30 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI, | |
| if (Succ->hasAddressTaken() || !Succ->getSinglePredecessor()) | ||
| return false; | ||
|
|
||
| auto *BI = dyn_cast<BranchInst>(TI); | ||
| if (BI && HoistLoadsStoresWithCondFaulting && | ||
| Options.HoistLoadsStoresWithCondFaulting) { | ||
| SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores; | ||
| for (auto *Succ : successors(BB)) { | ||
| for (Instruction &I : *Succ) { | ||
| if (I.isTerminator()) { | ||
| if (I.getNumSuccessors() > 1) | ||
| return false; | ||
| continue; | ||
| } else if (!isSafeCheapLoadStore(&I, TTI) || | ||
| SpeculatedConditionalLoadsStores.size() == | ||
| HoistLoadsStoresWithCondFaultingThreshold) { | ||
| return false; | ||
| } | ||
| SpeculatedConditionalLoadsStores.push_back(&I); | ||
| } | ||
| } | ||
|
|
||
| if (!SpeculatedConditionalLoadsStores.empty()) | ||
|
||
| return hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores, | ||
| std::nullopt); | ||
| } | ||
|
|
||
| // The second of pair is a SkipFlags bitmask. | ||
| using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>; | ||
| SmallVector<SuccIterPair, 8> SuccIterPairs; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment like
?
It's a little hard to know when it's nullopt w/o searching for the caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.