[PATCH] D98114: [Loads] Forward constant vector store to load of first element

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 6 08:20:12 PST 2021


nikic created this revision.
nikic added reviewers: jdoerfert, reames, spatel.
Herald added a subscriber: hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

InstCombine performs simple forwarding from stores to loads, but currently only handles the case where the load and store have the same size. This extends it to also handle a store of a constant with a larger size followed by a load with a smaller size.

This is implemented through `ConstantFoldLoadThroughBitcast()` which is fairly primitive (e.g. does not allow storing a large integer and then loading a small one), but at least can forward the first element of a vector store. Unfortunately it seems that we currently don't have a generic helper for "read a constant value as a different type", it's all tangled up with other logic in either ConstantFolding or VNCoercion.

Of course, GVN is the pass that implements this in full generality, but store to load forwarding in InstCombine is fairly important to sidestep phase ordering issues.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98114

Files:
  llvm/lib/Analysis/Loads.cpp
  llvm/test/Transforms/InstCombine/load-store-forward.ll


Index: llvm/test/Transforms/InstCombine/load-store-forward.ll
===================================================================
--- llvm/test/Transforms/InstCombine/load-store-forward.ll
+++ llvm/test/Transforms/InstCombine/load-store-forward.ll
@@ -22,8 +22,7 @@
 ; CHECK-LABEL: @vec_store_load_first(
 ; CHECK-NEXT:    [[P2:%.*]] = bitcast i32* [[P:%.*]] to <2 x i32>*
 ; CHECK-NEXT:    store <2 x i32> <i32 1, i32 2>, <2 x i32>* [[P2]], align 8
-; CHECK-NEXT:    [[LOAD:%.*]] = load i32, i32* [[P]], align 4
-; CHECK-NEXT:    ret i32 [[LOAD]]
+; CHECK-NEXT:    ret i32 1
 ;
   %p2 = bitcast i32* %p to <2 x i32>*
   store <2 x i32> <i32 1, i32 2>, <2 x i32>* %p2
Index: llvm/lib/Analysis/Loads.cpp
===================================================================
--- llvm/lib/Analysis/Loads.cpp
+++ llvm/lib/Analysis/Loads.cpp
@@ -498,12 +498,15 @@
     if (!AreEquivalentAddressValues(StorePtr, Ptr))
       return nullptr;
 
+    if (IsLoadCSE)
+      *IsLoadCSE = false;
+
     Value *Val = SI->getValueOperand();
-    if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL)) {
-      if (IsLoadCSE)
-        *IsLoadCSE = false;
+    if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL))
       return Val;
-    }
+
+    if (auto *C = dyn_cast<Constant>(Val))
+      return ConstantFoldLoadThroughBitcast(C, AccessTy, DL);
   }
 
   return nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98114.328763.patch
Type: text/x-patch
Size: 1393 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210306/79be4244/attachment.bin>


More information about the llvm-commits mailing list