<div dir="ltr">Hello, please add linebreaks to your commit messages. This commit makes `git log` look pretty ugly.</div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 21, 2018 at 9:55 AM JF Bastien via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: jfb<br>
Date: Fri Sep 21 06:54:09 2018<br>
New Revision: 342734<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=342734&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=342734&view=rev</a><br>
Log:<br>
NFC: deduplicate isRepeatedBytePattern from clang to LLVM's isBytewiseValue<br>
<br>
Summary:<br>
This code was in CGDecl.cpp and really belongs in LLVM. It happened to have isBytewiseValue which served a very similar purpose but wasn't as powerful as clang's version. Remove the clang version, and augment isBytewiseValue to be as powerful so that clang does the same thing it used to.<br>
<br>
LLVM part of this patch: D51751<br>
<br>
Subscribers: dexonsmith, cfe-commits<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D51752" rel="noreferrer" target="_blank">https://reviews.llvm.org/D51752</a><br>
<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/CGDecl.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=342734&r1=342733&r2=342734&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=342734&r1=342733&r2=342734&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Fri Sep 21 06:54:09 2018<br>
@@ -30,6 +30,7 @@<br>
 #include "clang/Basic/TargetInfo.h"<br>
 #include "clang/CodeGen/CGFunctionInfo.h"<br>
 #include "clang/Frontend/CodeGenOptions.h"<br>
+#include "llvm/Analysis/ValueTracking.h"<br>
 #include "llvm/IR/DataLayout.h"<br>
 #include "llvm/IR/GlobalVariable.h"<br>
 #include "llvm/IR/Intrinsics.h"<br>
@@ -948,111 +949,17 @@ static bool shouldUseBZeroPlusStoresToIn<br>
          canEmitInitWithFewStoresAfterBZero(Init, StoreBudget);<br>
 }<br>
<br>
-/// A byte pattern.<br>
-///<br>
-/// Can be "any" pattern if the value was padding or known to be undef.<br>
-/// Can be "none" pattern if a sequence doesn't exist.<br>
-class BytePattern {<br>
-  uint8_t Val;<br>
-  enum class ValueType : uint8_t { Specific, Any, None } Type;<br>
-  BytePattern(ValueType Type) : Type(Type) {}<br>
-<br>
-public:<br>
-  BytePattern(uint8_t Value) : Val(Value), Type(ValueType::Specific) {}<br>
-  static BytePattern Any() { return BytePattern(ValueType::Any); }<br>
-  static BytePattern None() { return BytePattern(ValueType::None); }<br>
-  bool isAny() const { return Type == ValueType::Any; }<br>
-  bool isNone() const { return Type == ValueType::None; }<br>
-  bool isValued() const { return Type == ValueType::Specific; }<br>
-  uint8_t getValue() const {<br>
-    assert(isValued());<br>
-    return Val;<br>
-  }<br>
-  BytePattern merge(const BytePattern Other) const {<br>
-    if (isNone() || Other.isNone())<br>
-      return None();<br>
-    if (isAny())<br>
-      return Other;<br>
-    if (Other.isAny())<br>
-      return *this;<br>
-    if (getValue() == Other.getValue())<br>
-      return *this;<br>
-    return None();<br>
-  }<br>
-};<br>
-<br>
-/// Figures out whether the constant can be initialized with memset.<br>
-static BytePattern constantIsRepeatedBytePattern(llvm::Constant *C) {<br>
-  if (isa<llvm::ConstantAggregateZero>(C) || isa<llvm::ConstantPointerNull>(C))<br>
-    return BytePattern(0x00);<br>
-  if (isa<llvm::UndefValue>(C))<br>
-    return BytePattern::Any();<br>
-<br>
-  if (isa<llvm::ConstantInt>(C)) {<br>
-    auto *Int = cast<llvm::ConstantInt>(C);<br>
-    if (Int->getBitWidth() % 8 != 0)<br>
-      return BytePattern::None();<br>
-    const llvm::APInt &Value = Int->getValue();<br>
-    if (Value.isSplat(8))<br>
-      return BytePattern(Value.getLoBits(8).getLimitedValue());<br>
-    return BytePattern::None();<br>
-  }<br>
-<br>
-  if (isa<llvm::ConstantFP>(C)) {<br>
-    auto *FP = cast<llvm::ConstantFP>(C);<br>
-    llvm::APInt Bits = FP->getValueAPF().bitcastToAPInt();<br>
-    if (Bits.getBitWidth() % 8 != 0)<br>
-      return BytePattern::None();<br>
-    if (!Bits.isSplat(8))<br>
-      return BytePattern::None();<br>
-    return BytePattern(Bits.getLimitedValue() & 0xFF);<br>
-  }<br>
-<br>
-  if (isa<llvm::ConstantVector>(C)) {<br>
-    llvm::Constant *Splat = cast<llvm::ConstantVector>(C)->getSplatValue();<br>
-    if (Splat)<br>
-      return constantIsRepeatedBytePattern(Splat);<br>
-    return BytePattern::None();<br>
-  }<br>
-<br>
-  if (isa<llvm::ConstantArray>(C) || isa<llvm::ConstantStruct>(C)) {<br>
-    BytePattern Pattern(BytePattern::Any());<br>
-    for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) {<br>
-      llvm::Constant *Elt = cast<llvm::Constant>(C->getOperand(I));<br>
-      Pattern = Pattern.merge(constantIsRepeatedBytePattern(Elt));<br>
-      if (Pattern.isNone())<br>
-        return Pattern;<br>
-    }<br>
-    return Pattern;<br>
-  }<br>
-<br>
-  if (llvm::ConstantDataSequential *CDS =<br>
-          dyn_cast<llvm::ConstantDataSequential>(C)) {<br>
-    BytePattern Pattern(BytePattern::Any());<br>
-    for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {<br>
-      llvm::Constant *Elt = CDS->getElementAsConstant(I);<br>
-      Pattern = Pattern.merge(constantIsRepeatedBytePattern(Elt));<br>
-      if (Pattern.isNone())<br>
-        return Pattern;<br>
-    }<br>
-    return Pattern;<br>
-  }<br>
-<br>
-  // BlockAddress, ConstantExpr, and everything else is scary.<br>
-  return BytePattern::None();<br>
-}<br>
-<br>
 /// Decide whether we should use memset to initialize a local variable instead<br>
 /// of using a memcpy from a constant global. Assumes we've already decided to<br>
 /// not user bzero.<br>
 /// FIXME We could be more clever, as we are for bzero above, and generate<br>
 ///       memset followed by stores. It's unclear that's worth the effort.<br>
-static BytePattern shouldUseMemSetToInitialize(llvm::Constant *Init,<br>
-                                               uint64_t GlobalSize) {<br>
+static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,<br>
+                                                uint64_t GlobalSize) {<br>
   uint64_t SizeLimit = 32;<br>
   if (GlobalSize <= SizeLimit)<br>
-    return BytePattern::None();<br>
-  return constantIsRepeatedBytePattern(Init);<br>
+    return nullptr;<br>
+  return llvm::isBytewiseValue(Init);<br>
 }<br>
<br>
 static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,<br>
@@ -1081,9 +988,14 @@ static void emitStoresForConstant(CodeGe<br>
     return;<br>
   }<br>
<br>
-  BytePattern Pattern = shouldUseMemSetToInitialize(constant, ConstantSize);<br>
-  if (!Pattern.isNone()) {<br>
-    uint8_t Value = Pattern.isAny() ? 0x00 : Pattern.getValue();<br>
+  llvm::Value *Pattern = shouldUseMemSetToInitialize(constant, ConstantSize);<br>
+  if (Pattern) {<br>
+    uint64_t Value = 0x00;<br>
+    if (!isa<llvm::UndefValue>(Pattern)) {<br>
+      const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();<br>
+      assert(AP.getBitWidth() <= 8);<br>
+      Value = AP.getLimitedValue();<br>
+    }<br>
     Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, Value), SizeVal,<br>
                          isVolatile);<br>
     return;<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>