[PATCH] D118727: [MemoryBuiltins][FIX] Adjust index type size properly wrt. AS casts

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 7 07:42:50 PST 2022


jdoerfert updated this revision to Diff 406464.
jdoerfert added a comment.

Add check lines


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118727/new/

https://reviews.llvm.org/D118727

Files:
  llvm/include/llvm/Analysis/MemoryBuiltins.h
  llvm/lib/Analysis/MemoryBuiltins.cpp
  llvm/test/Transforms/InstCombine/builtin-dynamic-object-size.ll


Index: llvm/test/Transforms/InstCombine/builtin-dynamic-object-size.ll
===================================================================
--- llvm/test/Transforms/InstCombine/builtin-dynamic-object-size.ll
+++ llvm/test/Transforms/InstCombine/builtin-dynamic-object-size.ll
@@ -1,6 +1,6 @@
 ; RUN: opt -instcombine -S < %s | FileCheck %s
 
-target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128-p7:32:32"
 target triple = "x86_64-apple-macosx10.14.0"
 
 ; Function Attrs: nounwind ssp uwtable
@@ -152,6 +152,27 @@
 ; CHECK-NEXT:    br i1 false, label %if.else, label %if.then
 ; CHECK:    call void @fortified_chk(i8* %obj, i64 [[SZ]])
 
+ at p7 = internal addrspace(7) global i8 0
+
+; Gracefully handle AS cast when the address spaces have different pointer widths.
+define i64 @as_cast(i1 %c) {
+; CHECK:  [[TMP0:%.*]] = select i1 %c, i64 64, i64 1
+; CHECK:  [[NOT:%.*]] = xor i1 %c, true
+; CHECK:  [[NEG:%.*]] = sext i1 [[NOT]] to i64
+; CHECK:  [[TMP1:%.*]] = add nsw i64 [[TMP0]], [[NEG]]
+; CHECK:  [[TMP2:%.*]] = icmp ne i64 [[TMP1]], -1
+; CHECK:  call void @llvm.assume(i1 [[TMP2]])
+; CHECK:  ret i64 [[TMP1]]
+;
+entry:
+  %p0 = tail call i8* @malloc(i64 64)
+  %gep = getelementptr i8, i8 addrspace(7)* @p7, i32 1
+  %as = addrspacecast i8 addrspace(7)* %gep to i8*
+  %select = select i1 %c, i8* %p0, i8* %as
+  %calc_size = tail call i64 @llvm.objectsize.i64.p0i8(i8* %select, i1 false, i1 true, i1 true)
+  ret i64 %calc_size
+}
+
 declare void @bury(i32) local_unnamed_addr #2
 
 ; Function Attrs: nounwind allocsize(0)
Index: llvm/lib/Analysis/MemoryBuiltins.cpp
===================================================================
--- llvm/lib/Analysis/MemoryBuiltins.cpp
+++ llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -573,10 +573,34 @@
 }
 
 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
+  unsigned InitialIntTyBits = DL.getIndexTypeSizeInBits(V->getType());
+
+  // Stripping pointer casts can strip address space casts which can change the
+  // index type size. The invariant is that we use the value type to determine
+  // the index type size. If we stripped address space casts we "repair" the
+  // APInt as we pass it upwards. For the caller, the APInt matches the type of
+  // the argument value V (or better the type of it).
+  V = V->stripPointerCasts();
+
+  // Later we use the index type size and zero but it will match the type of the
+  // value that is passed to computeImpl.
   IntTyBits = DL.getIndexTypeSizeInBits(V->getType());
   Zero = APInt::getZero(IntTyBits);
 
-  V = V->stripPointerCasts();
+  if (InitialIntTyBits == IntTyBits)
+    return computeImpl(V);
+
+  // We stripped an address space cast that changed the index type size.
+  // Readjust it to match the argument's index type size.
+  SizeOffsetType SOT = computeImpl(V);
+  if (knownSize(SOT) && !::CheckedZextOrTrunc(SOT.first, InitialIntTyBits))
+    return unknown();
+  if (knownOffset(SOT) && !::CheckedZextOrTrunc(SOT.second, InitialIntTyBits))
+    return unknown();
+  return SOT;
+}
+
+SizeOffsetType ObjectSizeOffsetVisitor::computeImpl(Value *V) {
   if (Instruction *I = dyn_cast<Instruction>(V)) {
     // If we have already seen this instruction, bail out. Cycles can happen in
     // unreachable code after constant propagation.
Index: llvm/include/llvm/Analysis/MemoryBuiltins.h
===================================================================
--- llvm/include/llvm/Analysis/MemoryBuiltins.h
+++ llvm/include/llvm/Analysis/MemoryBuiltins.h
@@ -221,6 +221,7 @@
   SizeOffsetType visitInstruction(Instruction &I);
 
 private:
+  SizeOffsetType computeImpl(Value *V);
   bool CheckedZextOrTrunc(APInt &I);
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118727.406464.patch
Type: text/x-patch
Size: 3717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220207/ca8d427a/attachment.bin>


More information about the llvm-commits mailing list