[PATCH] D102367: [LowerConstantIntrinsics] only accept ConstantData other than UndefValue as constant

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 12 13:56:17 PDT 2021


nickdesaulniers created this revision.
nickdesaulniers added a reviewer: rsmith.
Herald added a subscriber: hiraditya.
nickdesaulniers requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

GlobalVariables are Constants, yet should not unconditionally be
considered true for __builtin_constant_p.

Via the LangRef
https://llvm.org/docs/LangRef.html#llvm-is-constant-intrinsic:

  This intrinsic generates no code. If its argument is known to be a
  manifest compile-time constant value, then the intrinsic will be
  converted to a constant true value. Otherwise, it will be converted
  to a constant false value.
  
  In particular, note that if the argument is a constant expression
  which refers to a global (the address of which _is_ a constant, but
  not manifest during the compile), then the intrinsic evaluates to
  false.

pr/41459


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102367

Files:
  llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
  llvm/test/Transforms/LowerConstantIntrinsics/constant-intrinsics.ll


Index: llvm/test/Transforms/LowerConstantIntrinsics/constant-intrinsics.ll
===================================================================
--- llvm/test/Transforms/LowerConstantIntrinsics/constant-intrinsics.ll
+++ llvm/test/Transforms/LowerConstantIntrinsics/constant-intrinsics.ll
@@ -112,3 +112,11 @@
 
   ret i1 %res6
 }
+
+ at real_mode_blob_end = external dso_local global [0 x i8], align 1
+define i1 @global_array() {
+; CHECK-LABEL: @global_array(
+; CHECK-NEXT: ret i1 false
+  %1 = call i1 @llvm.is.constant.i64(i64 ptrtoint ([0 x i8]* @real_mode_blob_end to i64))
+  ret i1 %1
+}
Index: llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
+++ llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp
@@ -45,10 +45,18 @@
           "Number of 'objectsize' intrinsic calls handled");
 
 static Value *lowerIsConstantIntrinsic(IntrinsicInst *II) {
-  Value *Op = II->getOperand(0);
-
-  return isa<Constant>(Op) ? ConstantInt::getTrue(II->getType())
-                           : ConstantInt::getFalse(II->getType());
+  SmallVector<Value *> WorkList;
+  WorkList.push_back(II->getOperand(0));
+  while (WorkList.size()) {
+    Value *V = WorkList.back();
+    WorkList.pop_back();
+    if (isa<ConstantData>(V) && !isa<UndefValue>(V))
+      return ConstantInt::getTrue(II->getType());
+    if (auto *CA = dyn_cast<ConstantAggregate>(V))
+      for (Value *Op : CA->operands())
+        WorkList.push_back(Op);
+  }
+  return ConstantInt::getFalse(II->getType());
 }
 
 static bool replaceConditionalBranchesOnConstant(Instruction *II,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102367.344951.patch
Type: text/x-patch
Size: 1675 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210512/956695e2/attachment.bin>


More information about the llvm-commits mailing list