[PATCH] D99248: [RFC][CodeGen] Emit range metadata for array subscript.

Clement Courbet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 24 02:13:48 PDT 2021


courbet created this revision.
courbet requested review of this revision.
Herald added a project: clang.

To allow LLVM to do better alias anaysis. See RFC on llvm-dev.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99248

Files:
  clang/lib/CodeGen/CGExpr.cpp


Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -1249,7 +1249,7 @@
 
 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
   LValue LV;
-  if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
+  if (isa<ArraySubscriptExpr>(E))
     LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
   else
     LV = EmitLValue(E);
@@ -3691,6 +3691,32 @@
   return Address(eltPtr, eltAlign);
 }
 
+// Add range metadata: It is UB to subscript outside of the array, therefore
+// array indices are known to be in range:
+//   * [0, indexing_bound) if the array is accessed.
+//   * [0, indexing_bound] if only address computation is preformed.
+static llvm::Value *addArrayIndexingRangeMetadata(CodeGenFunction &CGF,
+                                                  const Expr *Base,
+                                                  llvm::Value *Idx,
+                                                  bool Accessed) {
+  QualType IdxTy;
+  llvm::ConstantInt *Bound = dyn_cast_or_null<llvm::ConstantInt>(
+      getArrayIndexingBound(CGF, Base, IdxTy));
+  if (Bound == nullptr)
+    return Idx;
+  llvm::Instruction *AnnotatedIdx =
+      CGF.Builder.CreateIntrinsic(llvm::Intrinsic::annotation, {Idx->getType()},
+                                  {Idx, llvm::UndefValue::get(CGF.Int8PtrTy),
+                                   llvm::UndefValue::get(CGF.Int8PtrTy),
+                                   llvm::UndefValue::get(CGF.Int32Ty)});
+  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
+  AnnotatedIdx->setMetadata(
+      llvm::LLVMContext::MD_range,
+      MDHelper.createRange(llvm::APInt(Bound->getBitWidth(), 0),
+                           Bound->getValue() + (Accessed ? 0 : 1)));
+  return AnnotatedIdx;
+}
+
 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
                                                bool Accessed) {
   // The index must always be an integer, which is not an aggregate.  Emit it
@@ -3716,6 +3742,9 @@
     if (Promote && Idx->getType() != IntPtrTy)
       Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
 
+    if (CGM.getCodeGenOpts().OptimizationLevel > 0)
+      Idx = addArrayIndexingRangeMetadata(*this, E->getBase(), Idx, Accessed);
+
     return Idx;
   };
   IdxPre = nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99248.332902.patch
Type: text/x-patch
Size: 2454 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210324/6b597663/attachment.bin>


More information about the cfe-commits mailing list