Teach the PruningFunctionCloner how to look through loads with
authorNate Begeman <natebegeman@mac.com>
Fri, 25 Apr 2008 06:37:06 +0000 (06:37 +0000)
committerNate Begeman <natebegeman@mac.com>
Fri, 25 Apr 2008 06:37:06 +0000 (06:37 +0000)
ConstantExpression GEPs pointing into constant globals.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50256 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/CloneFunction.cpp

index ba9b57d4f568b89461ab824dde4cece7393decb3..ca63399c811521d063965c476f32da098834cd86 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Instructions.h"
+#include "llvm/GlobalVariable.h"
 #include "llvm/Function.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Compiler.h"
@@ -308,13 +309,20 @@ ConstantFoldMappedInstruction(const Instruction *I) {
     else
       return 0;  // All operands not constant!
 
-  
   if (const CmpInst *CI = dyn_cast<CmpInst>(I))
     return ConstantFoldCompareInstOperands(CI->getPredicate(),
                                            &Ops[0], Ops.size(), TD);
-  else
-    return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
-                                    &Ops[0], Ops.size(), TD);
+
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
+    if (const LoadInst *LI = dyn_cast<LoadInst>(I))
+      if (!LI->isVolatile() && CE->getOpcode() == Instruction::GetElementPtr)
+        if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
+          if (GV->isConstant() && !GV->isDeclaration())
+            return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(),
+                                                          CE);
+
+  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0],
+                                  Ops.size(), TD);
 }
 
 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,