[llvm] [SimplifyCFG] Simplify switch instruction that has duplicate arms (PR #114262)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 14 09:01:49 PST 2024


================
@@ -7436,6 +7437,185 @@ static bool simplifySwitchOfCmpIntrinsic(SwitchInst *SI, IRBuilderBase &Builder,
   return true;
 }
 
+/// Checking whether two cases of SI are equal depends on the contents of the
+/// BasicBlock and the incoming values of their successor PHINodes.
+/// PHINode::getIncomingValueForBlock is O(|Preds|), so we'd like to avoid
+/// calling this function on each BasicBlock every time isEqual is called,
+/// especially since the same BasicBlock may be passed as an argument multiple
+/// times. To do this, we can precompute a map of PHINode -> Pred BasicBlock ->
+/// IncomingValue and add it in the Wrapper so isEqual can do O(1) checking
+/// of the incoming values.
+struct SwitchSuccWrapper {
+  // Keep so we can use SwitchInst::setSuccessor to do the replacement. It won't
+  // be important to equality though.
+  unsigned SuccNum;
+  BasicBlock *Dest;
+  DenseMap<PHINode *, DenseMap<BasicBlock *, Value *>> *PhiPredIVs;
----------------
antoniofrighetto wrote:

Maybe the inner mapping can be a SmallDenseMap? I glanced over, but could you confirm the inner map is actually needed? It seems to be prepopulated with values that you can just retrieve from the PN?

https://github.com/llvm/llvm-project/pull/114262


More information about the llvm-commits mailing list