[PATCH] D54588: [llvm][IRBuilder] Introspection for CreateAlignmentAssumption*() functions

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 15 10:25:55 PST 2018


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, dneilson, craig.topper, dblaikie.

Clang calls these functions to produce IR for assume-aligned attributes.
I would like to teach UBSAN to verify these assumptions.
For that, i need to access the final pointer on which the check is performed,
and the actual `icmp` that does the check.

The alternative to this would be to fully re-implement this in clang.


Repository:
  rL LLVM

https://reviews.llvm.org/D54588

Files:
  include/llvm/IR/IRBuilder.h


Index: include/llvm/IR/IRBuilder.h
===================================================================
--- include/llvm/IR/IRBuilder.h
+++ include/llvm/IR/IRBuilder.h
@@ -2123,14 +2123,19 @@
     return V;
   }
 
+  struct AlignmentAssumptionIntrospection {
+    Value *PtrIntValue = nullptr;
+    Value *InvCond = nullptr;
+  };
+
 private:
   /// Helper function that creates an assume intrinsic call that
   /// represents an alignment assumption on the provided Ptr, Mask, Type
-  /// and Offset.
-  CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
-                                            Value *PtrValue, Value *Mask,
-                                            Type *IntPtrTy,
-                                            Value *OffsetValue) {
+  /// and Offset. It may be sometimes useful to do some other logic
+  /// based on this alignment check, thus it can be stored into 'Info'.
+  CallInst *CreateAlignmentAssumptionHelper(
+      const DataLayout &DL, Value *PtrValue, Value *Mask, Type *IntPtrTy,
+      Value *OffsetValue, AlignmentAssumptionIntrospection *Info = nullptr) {
     Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
 
     if (OffsetValue) {
@@ -2149,6 +2154,10 @@
     Value *Zero = ConstantInt::get(IntPtrTy, 0);
     Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
     Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
+    if (Info) {
+      Info->PtrIntValue = PtrIntValue;
+      Info->InvCond = InvCond;
+    }
     return CreateAssumption(InvCond);
   }
 
@@ -2159,17 +2168,21 @@
   /// An optional offset can be provided, and if it is provided, the offset
   /// must be subtracted from the provided pointer to get the pointer with the
   /// specified alignment.
-  CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
-                                      unsigned Alignment,
-                                      Value *OffsetValue = nullptr) {
+  ///
+  /// It may be sometimes useful to do some other logic
+  /// based on this alignment check, thus it can be stored into 'Info'.
+  CallInst *
+  CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
+                            unsigned Alignment, Value *OffsetValue = nullptr,
+                            AlignmentAssumptionIntrospection *Info = nullptr) {
     assert(isa<PointerType>(PtrValue->getType()) &&
            "trying to create an alignment assumption on a non-pointer?");
     auto *PtrTy = cast<PointerType>(PtrValue->getType());
     Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
 
     Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
-                                           OffsetValue);
+                                           OffsetValue, Info);
   }
 
   /// Create an assume intrinsic call that represents an alignment
@@ -2179,11 +2192,15 @@
   /// must be subtracted from the provided pointer to get the pointer with the
   /// specified alignment.
   ///
+  /// It may be sometimes useful to do some other logic
+  /// based on this alignment check, thus it can be stored into 'Info'.
+  ///
   /// This overload handles the condition where the Alignment is dependent
   /// on an existing value rather than a static value.
-  CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
-                                      Value *Alignment,
-                                      Value *OffsetValue = nullptr) {
+  CallInst *
+  CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
+                            Value *Alignment, Value *OffsetValue = nullptr,
+                            AlignmentAssumptionIntrospection *Info = nullptr) {
     assert(isa<PointerType>(PtrValue->getType()) &&
            "trying to create an alignment assumption on a non-pointer?");
     auto *PtrTy = cast<PointerType>(PtrValue->getType());
@@ -2201,7 +2218,7 @@
                                ConstantInt::get(IntPtrTy, 0), "mask");
 
     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
-                                           OffsetValue);
+                                           OffsetValue, Info);
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54588.174236.patch
Type: text/x-patch
Size: 4305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181115/c4db9aa7/attachment.bin>


More information about the llvm-commits mailing list