[PATCH] D157432: [clang][ConstExprEmitter] handle Unary Not on integers

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 8 13:14:35 PDT 2023


nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Consider the following statement:

  int x = ~0;

With the following AST:

  `-VarDecl 0x5647bdb12d60 <x.c:1:1, col:10> col:5 x 'int' cinit
    `-UnaryOperator 0x5647bdb12e30 <col:9, col:10> 'int' prefix '~' cannot overflow
      `-IntegerLiteral 0x5647bdb12e10 <col:10> 'int' 0

Return the evaluation of the subexpression bitwise inverted.

Link: https://github.com/ClangBuiltLinux/linux/issues/1906


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157432

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1394,6 +1394,13 @@
     return nullptr;
   }
 
+  llvm::Constant *VisitUnaryNot(UnaryOperator *U, QualType T) {
+    if (llvm::Constant *C = Visit(U->getSubExpr(), T))
+      if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
+        return llvm::ConstantInt::get(CGM.getLLVMContext(), ~CI->getValue());
+    return nullptr;
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
     return CGM.getTypes().ConvertType(T);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157432.548325.patch
Type: text/x-patch
Size: 633 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230808/a54103ee/attachment.bin>


More information about the cfe-commits mailing list