diff options
author | Nikita Popov <npopov@redhat.com> | 2024-09-19 10:19:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-19 10:19:15 +0200 |
commit | 30cdf1e959d2e4dee1c871ff37470dcdb7e8d099 (patch) | |
tree | 79aff7c06626c3f9862d12a27100909ad5b69e43 /llvm/lib/Transforms/Utils/SimplifyCFG.cpp | |
parent | 752e10379c2ffb4f6eebf490f1fab7eb769dfbf6 (diff) | |
download | llvm-30cdf1e959d2e4dee1c871ff37470dcdb7e8d099.zip llvm-30cdf1e959d2e4dee1c871ff37470dcdb7e8d099.tar.gz llvm-30cdf1e959d2e4dee1c871ff37470dcdb7e8d099.tar.bz2 |
[SimplifyCFG] Pass context instruction to isSafeToSpeculativelyExecute() (#109132)
Pass speculation target and assumption cache to
isSafeToSpeculativelyExecute() calls.
This allows speculating based on dereferenceable/align assumptions, but
the primary motivation here is to avoid regressions from planned changes
to fix https://github.com/llvm/llvm-project/issues/108854.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 09461e6..69c4475 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -398,9 +398,6 @@ static void addPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, /// expensive. static InstructionCost computeSpeculationCost(const User *I, const TargetTransformInfo &TTI) { - assert((!isa<Instruction>(I) || - isSafeToSpeculativelyExecute(cast<Instruction>(I))) && - "Instruction is not safe to speculatively execute!"); return TTI.getInstructionCost(I, TargetTransformInfo::TCK_SizeAndLatency); } @@ -421,12 +418,11 @@ static InstructionCost computeSpeculationCost(const User *I, /// After this function returns, Cost is increased by the cost of /// V plus its non-dominating operands. If that cost is greater than /// Budget, false is returned and Cost is undefined. -static bool dominatesMergePoint(Value *V, BasicBlock *BB, +static bool dominatesMergePoint(Value *V, BasicBlock *BB, Instruction *InsertPt, SmallPtrSetImpl<Instruction *> &AggressiveInsts, - InstructionCost &Cost, - InstructionCost Budget, + InstructionCost &Cost, InstructionCost Budget, const TargetTransformInfo &TTI, - unsigned Depth = 0) { + AssumptionCache *AC, unsigned Depth = 0) { // It is possible to hit a zero-cost cycle (phi/gep instructions for example), // so limit the recursion depth. // TODO: While this recursion limit does prevent pathological behavior, it @@ -461,7 +457,7 @@ static bool dominatesMergePoint(Value *V, BasicBlock *BB, // Okay, it looks like the instruction IS in the "condition". Check to // see if it's a cheap instruction to unconditionally compute, and if it // only uses stuff defined outside of the condition. If so, hoist it out. - if (!isSafeToSpeculativelyExecute(I)) + if (!isSafeToSpeculativelyExecute(I, InsertPt, AC)) return false; Cost += computeSpeculationCost(I, TTI); @@ -480,8 +476,8 @@ static bool dominatesMergePoint(Value *V, BasicBlock *BB, // Okay, we can only really hoist these out if their operands do // not take us over the cost threshold. for (Use &Op : I->operands()) - if (!dominatesMergePoint(Op, BB, AggressiveInsts, Cost, Budget, TTI, - Depth + 1)) + if (!dominatesMergePoint(Op, BB, InsertPt, AggressiveInsts, Cost, Budget, + TTI, AC, Depth + 1)) return false; // Okay, it's safe to do this! Remember this instruction. AggressiveInsts.insert(I); @@ -3140,7 +3136,8 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI, return false; // Don't hoist the instruction if it's unsafe or expensive. - if (!IsSafeCheapLoadStore && !isSafeToSpeculativelyExecute(&I) && + if (!IsSafeCheapLoadStore && + !isSafeToSpeculativelyExecute(&I, BI, Options.AC) && !(HoistCondStores && !SpeculatedStoreValue && (SpeculatedStoreValue = isSafeToSpeculateStore(&I, BB, ThenBB, EndBB)))) @@ -3651,7 +3648,8 @@ static bool foldCondBranchOnValueKnownInPredecessor(BranchInst *BI, /// Given a BB that starts with the specified two-entry PHI node, /// see if we can eliminate it. static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, - DomTreeUpdater *DTU, const DataLayout &DL, + DomTreeUpdater *DTU, AssumptionCache *AC, + const DataLayout &DL, bool SpeculateUnpredictables) { // Ok, this is a two entry PHI node. Check to see if this is a simple "if // statement", which has a very simple dominance structure. Basically, we @@ -3741,10 +3739,10 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, continue; } - if (!dominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts, - Cost, Budget, TTI) || - !dominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts, - Cost, Budget, TTI)) + if (!dominatesMergePoint(PN->getIncomingValue(0), BB, DomBI, + AggressiveInsts, Cost, Budget, TTI, AC) || + !dominatesMergePoint(PN->getIncomingValue(1), BB, DomBI, + AggressiveInsts, Cost, Budget, TTI, AC)) return Changed; } @@ -8116,7 +8114,7 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) { // eliminate it, do so now. if (auto *PN = dyn_cast<PHINode>(BB->begin())) if (PN->getNumIncomingValues() == 2) - if (foldTwoEntryPHINode(PN, TTI, DTU, DL, + if (foldTwoEntryPHINode(PN, TTI, DTU, Options.AC, DL, Options.SpeculateUnpredictables)) return true; } |