[llvm] [MergeICmps] Perform dereferenceability check with context (PR #202884)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 12 02:47:15 PDT 2026
================
@@ -734,6 +737,37 @@ static BasicBlock *mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
return BB;
}
+// The transform may change the order in which the comparison is performed,
+// in which case we may perform loads that were not performed by the original
+// program. As such, we need to ensure that all the accessed memory is
+// dereferenceable.
+bool BCECmpChain::isDereferenceable() {
+ // We know that there can be no frees inside the merged blocks, so it's
+ // sufficient for dereferenceability to hold at the entry block. One
+ // exception to this is if the entry block performs "other work" and will
+ // get split. In that case, we need to consider frees prior to the splitting
+ // point.
+ Instruction *CxtI = SplitAt ? SplitAt : &EntryBlock_->front();
+
+ for (const auto &Blocks : MergedBlocks_) {
+ const BCECmpBlock &LowestBlock = Blocks.front();
+ const Value *Lhs = LowestBlock.Lhs().LoadI->getPointerOperand();
+ const Value *Rhs = LowestBlock.Rhs().LoadI->getPointerOperand();
+ const DataLayout &DL = LowestBlock.Lhs().LoadI->getDataLayout();
+
+ unsigned SizeInBits = 0;
+ for (const BCECmpBlock &Block : Blocks)
+ SizeInBits += Block.SizeBits();
+
+ APInt Size(64, SizeInBits / 8);
+ SimplifyQuery SQ(DL, CxtI);
----------------
nikic wrote:
In theory AC/DT could be useful to support this transform based on assume_dereferenceable. The pass doesn't use AC/DT currently though (DT is only preserved), and I don't think it's worth it to request them just for this.
https://github.com/llvm/llvm-project/pull/202884
More information about the llvm-commits
mailing list