<div dir="ltr">On Wed, Jul 13, 2016 at 6:27 PM, Davide Italiano via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: davide<br>
Date: Wed Jul 13 20:27:29 2016<br>
New Revision: 275357<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=275357&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=275357&view=rev</a><br>
Log:<br>
[SCCP] Generalize tryToReplaceInstWithConstant to work also with arguments.<br>
<br>
Modified:<br>
    llvm/trunk/lib/Transforms/Scalar/SCCP.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=275357&r1=275356&r2=275357&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=275357&r1=275356&r2=275357&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp Wed Jul 13 20:27:29 2016<br>
@@ -1510,16 +1510,16 @@ bool SCCPSolver::ResolvedUndefsIn(Functi<br>
   return false;<br>
 }<br>
<br>
-static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst,<br>
-                                         bool shouldEraseFromParent) {<br>
+template <typename ArgOrInst><br>
+static bool tryToReplaceWithConstant(SCCPSolver Solver, ArgOrInst *AI) {<br></blockquote><div><br></div><div>Can you just use a Value* here?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
   Constant *Const = nullptr;<br>
-  if (Inst->getType()->isStructTy()) {<br>
-    std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(Inst);<br>
+  if (AI->getType()->isStructTy()) {<br>
+    std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(AI);<br>
     if (std::any_of(IVs.begin(), IVs.end(),<br>
                     [](LatticeVal &LV) { return LV.isOverdefined(); }))<br>
       return false;<br>
     std::vector<Constant *> ConstVals;<br>
-    StructType *ST = dyn_cast<StructType>(Inst->getType());<br>
+    StructType *ST = dyn_cast<StructType>(AI->getType());<br>
     for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {<br>
       LatticeVal V = IVs[i];<br>
       ConstVals.push_back(V.isConstant()<br>
@@ -1528,18 +1528,23 @@ static bool tryToReplaceInstWithConstant<br>
     }<br>
     Const = ConstantStruct::get(ST, ConstVals);<br>
   } else {<br>
-    LatticeVal IV = Solver.getLatticeValueFor(Inst);<br>
+    LatticeVal IV = Solver.getLatticeValueFor(AI);<br>
     if (IV.isOverdefined())<br>
       return false;<br>
-<br>
-    Const =<br>
-        IV.isConstant() ? IV.getConstant() : UndefValue::get(Inst->getType());<br>
+    Const = IV.isConstant() ? IV.getConstant() : UndefValue::get(AI->getType());<br>
   }<br>
   assert(Const && "Constant is nullptr here!");<br>
-  DEBUG(dbgs() << "  Constant: " << *Const << " = " << *Inst << '\n');<br>
+  DEBUG(dbgs() << "  Constant: " << *Const << " = " << *AI << '\n');<br>
<br>
   // Replaces all of the uses of a variable with uses of the constant.<br>
-  Inst->replaceAllUsesWith(Const);<br>
+  AI->replaceAllUsesWith(Const);<br>
+  return true;<br>
+}<br>
+<br>
+static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst,<br>
+                                         bool shouldEraseFromParent) {<br>
+  if (!tryToReplaceWithConstant(Solver, Inst))<br>
+    return false;<br>
<br>
   // Delete the instruction.<br>
   if (shouldEraseFromParent)<br>
@@ -1766,17 +1771,8 @@ static bool runIPSCCP(Module &M, const D<br>
         // TODO: Could use getStructLatticeValueFor to find out if the entire<br>
         // result is a constant and replace it entirely if so.<br>
<br>
-        LatticeVal IV = Solver.getLatticeValueFor(&*AI);<br>
-        if (IV.isOverdefined()) continue;<br>
-<br>
-        Constant *CST = IV.isConstant() ?<br>
-        IV.getConstant() : UndefValue::get(AI->getType());<br>
-        DEBUG(dbgs() << "***  Arg " << *AI << " = " << *CST <<"\n");<br>
-<br>
-        // Replaces all of the uses of a variable with uses of the<br>
-        // constant.<br>
-        AI->replaceAllUsesWith(CST);<br>
-        ++IPNumArgsElimed;<br>
+        if (tryToReplaceWithConstant(Solver, &*AI))<br>
+          ++IPNumArgsElimed;<br>
       }<br>
     }<br></blockquote><div><br></div><div>Testcase?<br></div><div><br></div><div>-Eli<br></div></div></div></div>