[PATCH] D142872: Honor the fwrapv option when generating the inbounds GEP .

Umesh Kalappa via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 30 05:12:00 PST 2023


umesh.kalappa0 updated this revision to Diff 493282.
umesh.kalappa0 edited the summary of this revision.

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

https://reviews.llvm.org/D142872

Files:
  clang/lib/CodeGen/CGExpr.cpp
  clang/test/CodeGen/inbounds.c
  llvm/include/llvm/IR/ConstantFold.h
  llvm/lib/IR/ConstantFold.cpp
  llvm/lib/IR/Constants.cpp


Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -2510,7 +2510,9 @@
     ArgVec.push_back(Idx);
   }
 
-  unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
+  unsigned SubClassOptionalData = ( InBounds && !llvm::getSignedWrap() ) ?
+	                           GEPOperator::IsInBounds : 0;
+
   if (InRangeIndex && *InRangeIndex < 63)
     SubClassOptionalData |= (*InRangeIndex + 1) << 1;
   const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -347,6 +347,14 @@
   }
 }
 
+bool llvm::getSignedWrap() {
+  return llvm::SignedWrap;
+}
+
+void llvm::setSignedWrap(bool SignedWrap) {
+  llvm::SignedWrap=SignedWrap;
+}
+
 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
                                             Type *DestTy) {
   if (isa<PoisonValue>(V))
Index: llvm/include/llvm/IR/ConstantFold.h
===================================================================
--- llvm/include/llvm/IR/ConstantFold.h
+++ llvm/include/llvm/IR/ConstantFold.h
@@ -26,10 +26,15 @@
 
 namespace llvm {
   template <typename T> class ArrayRef;
+  inline bool SignedWrap = false;
+
   class Value;
   class Constant;
   class Type;
 
+  bool getSignedWrap(void);
+  void setSignedWrap(bool Wrap);
+
   // Constant fold various types of instruction...
   Constant *ConstantFoldCastInstruction(
     unsigned opcode,     ///< The opcode of the cast
Index: clang/test/CodeGen/inbounds.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/inbounds.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fwrapv -emit-llvm -o - %s | FileCheck %s 
+
+extern char base[];
+
+char *test() {
+    return base + 1;
+}
+
+// CHECK: ret ptr getelementptr   (i8, ptr @base, i64 1)
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -3613,7 +3613,12 @@
   if (!E->getType()->isVariableArrayType()) {
     assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
            "Expected pointer to array");
+
+    if (getLangOpts().isSignedOverflowDefined())
+      llvm::setSignedWrap(true);
+
     Addr = Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
+
   }
 
   // The result of this decay conversion points to an array element within the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142872.493282.patch
Type: text/x-patch
Size: 2653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230130/a3e1855b/attachment-0001.bin>


More information about the cfe-commits mailing list